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": "/guide/getting-started"
    },
    {
      "text": "API",
      "link": "/api/"
    }
  ],
  "sidebar": {
    "/guide/": [
      {
        "text": "入门指南",
        "items": [
          {
            "text": "快速开始",
            "link": "/guide/getting-started"
          },
          {
            "text": "高级用法",
            "link": "/guide/advanced"
          },
          {
            "text": "新文章",
            "link": "/guide/new-article"
          }
        ]
      }
    ],
    "/api/": [
      {
        "text": "API 文档",
        "items": [
          {
            "text": "概述",
            "link": "/api/"
          }
        ]
      }
    ]
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/你的用户名/my-docs"
    }
  ],
  "editLink": {
    "pattern": "https://github.com/AdnyWert/my-docs/edit/main/docs/:path",
    "text": "在 GitHub 上编辑此页"
  },
  "search": {
    "provider": "local"
  }
}

Page Data

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

Page Frontmatter

{
  "outline": "deep"
}

More

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