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": "/posts/guide"
    },
    {
      "text": "笔记",
      "link": "/posts/notes"
    },
    {
      "text": "配置",
      "link": "/posts/config"
    },
    {
      "text": "归档",
      "link": "/pages/archives"
    }
  ],
  "logo": "logo.svg",
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/vuejs/vitepress"
    }
  ],
  "sidebar": {
    "/posts/guide/": {
      "base": "/posts/guide/",
      "items": [
        {
          "text": "default",
          "items": [
            {
              "text": "api示例",
              "link": "default/api-examples"
            },
            {
              "text": "markdown示例",
              "link": "default/markdown-examples"
            }
          ],
          "collapsed": false
        },
        {
          "text": "博客文章写作规范",
          "link": "three"
        },
        {
          "text": "博客图片优化方法",
          "link": "four"
        }
      ]
    },
    "/posts/config/": {
      "base": "/posts/config/",
      "items": [
        {
          "text": "api",
          "items": [
            {
              "text": "api-one",
              "link": "api/api-one"
            },
            {
              "text": "api-two",
              "link": "api/api-two"
            }
          ],
          "collapsed": false
        },
        {
          "text": "博客 SEO 配置要点1",
          "link": "one"
        },
        {
          "text": "博客 SEO 配置要点2",
          "link": "two"
        }
      ]
    }
  }
}

Page Data

{
  "title": "api示例",
  "description": "",
  "frontmatter": {
    "title": "api示例",
    "abstract": "api11",
    "layout": "doc",
    "tags": "blog, guide",
    "date": "2023-06-10T00:00:00.000Z",
    "order": 8
  },
  "headers": [],
  "relativePath": "posts/guide/default/api-examples.md",
  "filePath": "posts/guide/default/api-examples.md"
}

Page Frontmatter

{
  "title": "api示例",
  "abstract": "api11",
  "layout": "doc",
  "tags": "blog, guide",
  "date": "2023-06-10T00:00:00.000Z",
  "order": 8
}

More

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