Skip to content

Runtime API Examples

This page demonstrates usage of some of the runtime APIs provided by VitePress.

The main useData() API can be used to access site, theme, and page data for the current page. It works in both .md and .vue files:

md
<script setup>
import { useData } from 'vitepress'

const { theme, page, frontmatter } = useData()
</script>

## Results

### Theme Data
<pre>{{ theme }}</pre>

### Page Data
<pre>{{ page }}</pre>

### Page Frontmatter
<pre>{{ frontmatter }}</pre>

Results

Theme Data

{
  "logo": "/logo.png",
  "search": {
    "provider": "local"
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/jyj1202/nm-ui"
    }
  ],
  "nav": [
    {
      "text": "指南",
      "link": "/zh-CN/guide/introduction"
    },
    {
      "text": "组件",
      "activeMatch": "/zh-CN/component/",
      "items": [
        {
          "text": "element 二次封装组件",
          "link": "/zh-CN/component/form/input"
        },
        {
          "text": "web component",
          "link": "/zh-CN/component/web-component"
        }
      ]
    },
    {
      "text": "vitepress",
      "link": "/vitepress/markdown-examples"
    }
  ],
  "sidebar": {
    "/vitepress/": [
      {
        "text": "Examples",
        "items": [
          {
            "text": "Markdown Examples",
            "link": "/vitepress/markdown-examples"
          },
          {
            "text": "Runtime API Examples",
            "link": "/vitepress/api-examples"
          }
        ]
      }
    ],
    "/zh-CN/guide/": [
      {
        "text": "简介",
        "link": "/zh-CN/guide/introduction"
      },
      {
        "text": "快速开始",
        "link": "/zh-CN/guide/quick-start"
      }
    ],
    "/zh-CN/component/": [
      {
        "text": "属性文档",
        "items": [
          {
            "text": "Form",
            "link": "/zh-CN/component/form/"
          },
          {
            "text": "Input 输入框",
            "link": "/zh-CN/component/form/input"
          },
          {
            "text": "Input Number 数字输入框",
            "link": "/zh-CN/component/form/input-number"
          },
          {
            "text": "Select 选择器",
            "link": "/zh-CN/component/form/select"
          },
          {
            "text": "Checkbox 多选框",
            "link": "/zh-CN/component/form/checkbox"
          }
        ]
      }
    ]
  }
}

Page Data

{
  "title": "Runtime API Examples",
  "description": "",
  "frontmatter": {
    "outline": "deep"
  },
  "headers": [],
  "relativePath": "vitepress/api-examples.md",
  "filePath": "vitepress/api-examples.md"
}

Page Frontmatter

{
  "outline": "deep"
}

More

Check out the documentation for the full list of runtime APIs.