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": "/articles/"
    }
  ],
  "sidebar": [
    {
      "text": "前端基础",
      "items": [
        {
          "text": "JavaScript 基础",
          "link": "/articles/js-basics"
        },
        {
          "text": "CSS 技巧",
          "link": "/articles/css-tips"
        }
      ]
    },
    {
      "text": "前后端技术前沿追踪与经典拆解",
      "items": [
        {
          "text": "前端技术前沿追踪",
          "link": "/articles/frontend_trends"
        },
        {
          "text": "后端技术前沿追踪",
          "link": "/articles/backend_trends"
        },
        {
          "text": "前端技术经典拆解",
          "link": "/articles/frontend_classic"
        }
      ]
    }
  ]
}

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.