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

{
  "logo": "/avatar.png",
  "nav": [
    {
      "text": "首页",
      "link": "/"
    },
    {
      "text": "前端技术",
      "link": "/frontend-tech/package/pnpm"
    },
    {
      "text": "编程技术",
      "link": "/backend-tech/design-pattern/6principles23patterns"
    }
  ],
  "sidebar": {
    "/frontend-tech/": {
      "text": "前端技术",
      "items": [
        {
          "text": "前端包管理工具",
          "link": "/frontend-tech/package/pnpm"
        },
        {
          "text": "ES6",
          "link": "/frontend-tech/javascript/es6/es6-api"
        },
        {
          "text": "防抖和节流",
          "link": "/frontend-tech/javascript/js-utils/debounce-throttle"
        },
        {
          "text": "如何编写高质量CSS",
          "link": "/frontend-tech/css/good-css"
        }
      ]
    },
    "/backend-tech/": {
      "text": "编程技术",
      "items": [
        {
          "text": "6原则和23模式",
          "link": "/backend-tech/design-pattern/6principles23patterns"
        },
        {
          "text": "TCP协议",
          "link": "/backend-tech/network/TCP-IP/TCP"
        },
        {
          "text": "TCP/IP基础",
          "link": "/backend-tech/network/TCP-IP/introduction"
        },
        {
          "text": "计算机网络简介",
          "link": "/backend-tech/network/base/introduction"
        },
        {
          "text": "数据结构与算法",
          "link": "/backend-tech/data-structure/introduction"
        },
        {
          "text": "线性表",
          "link": "/backend-tech/data-structure/linear-list"
        }
      ]
    }
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/FBmm"
    }
  ]
}

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.