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

{
  "nav": [
    {
      "text": "首页",
      "link": "/"
    },
    {
      "text": "生活分享",
      "link": "/markdown-examples",
      "activeMatch": "/markdown-examples"
    },
    {
      "text": "成长点滴",
      "link": "/life/index"
    },
    {
      "text": "二级菜单",
      "items": [
        {
          "text": "二级菜单1",
          "link": "/life/index"
        }
      ]
    },
    {
      "text": "点击跳转",
      "link": "http://baidu.com",
      "target": "_blank"
    }
  ],
  "sidebar": {
    "/markdown-examples": [
      {
        "text": "Markdown Examples",
        "link": "/markdown-examples"
      }
    ],
    "/life/index": [
      {
        "text": "成长点滴",
        "items": [
          {
            "text": "成长点滴1",
            "link": "/life/index"
          },
          {
            "text": "成长点滴2",
            "link": "/life/index"
          }
        ]
      },
      {
        "text": "成长点滴22",
        "collapsed": true,
        "items": [
          {
            "text": "成长点滴1",
            "link": "/life/index"
          },
          {
            "text": "成长点滴2",
            "link": "/life/index"
          }
        ]
      }
    ]
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/13552569078/zhj-vitepress"
    }
  ]
}

Page Data

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

Page Frontmatter

{
  "outline": "deep"
}

More

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