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

{
  "search": {
    "provider": "local",
    "options": {
      "locales": {
        "zh": {
          "translations": {
            "button": {
              "buttonText": "搜索文档",
              "buttonAriaLabel": "搜索文档"
            },
            "modal": {
              "noResultsText": "无法找到相关结果",
              "resetButtonTitle": "清除查询条件",
              "footer": {
                "selectText": "选择",
                "navigateText": "切换"
              }
            }
          }
        }
      }
    }
  },
  "nav": [
    {
      "text": "首页",
      "link": "/"
    },
    {
      "text": "生活",
      "link": "/life"
    },
    {
      "text": "代码",
      "link": "/code"
    },
    {
      "text": "种草",
      "link": "/recommended"
    }
  ],
  "sidebar": {
    "/life/": [
      {
        "text": "生活",
        "link": "/life",
        "items": [
          {
            "text": "报福小镇",
            "link": "/life/baofu"
          },
          {
            "text": "arc浏览器推荐",
            "link": "/life/arc"
          }
        ]
      }
    ],
    "recommended": [
      {
        "text": "种草",
        "link": "/recommended",
        "items": [
          {
            "text": "handraw漂亮的手绘工具",
            "link": "/recommended/handraw"
          },
          {
            "text": "arc浏览器推荐",
            "link": "/recommended/arc"
          }
        ]
      }
    ],
    "/code/": [
      {
        "text": "代码",
        "link": "/code",
        "items": [
          {
            "text": "handraw漂亮的手绘工具",
            "link": "/recommended/handraw"
          },
          {
            "text": "arc浏览器推荐",
            "link": "/recommended/arc"
          }
        ]
      }
    ]
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/bobowiki"
    }
  ]
}

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.