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": "https://cdn.jsdelivr.net/gh/hypocritical-jiang/blog-assets@main/images/icons8-worldwide-delivery-96.png",
  "nav": [
    {
      "text": "首页",
      "link": "/"
    },
    {
      "text": "归档",
      "link": "/archives/"
    },
    {
      "text": "分类",
      "link": "/categories/"
    },
    {
      "text": "标签",
      "link": "/tags/"
    },
    {
      "text": "关于我",
      "items": [
        {
          "text": "github",
          "link": ""
        }
      ]
    }
  ],
  "sidebar": [
    {
      "text": "学习笔记",
      "collapsed": true,
      "items": [
        {
          "text": "小组设计",
          "collapsed": true,
          "items": [
            {
              "text": "GPS 的选择与比较",
              "link": "/posts/durham_study/group_project/gps"
            }
          ]
        },
        {
          "text": "光子学",
          "collapsed": true,
          "items": [
            {
              "text": "0 基础知识复习",
              "link": "/posts/durham_study/phontonics/0"
            },
            {
              "text": "1 无损传输线模型",
              "link": "/posts/durham_study/phontonics/1"
            },
            {
              "text": "2 有限传输线模型",
              "link": "/posts/durham_study/phontonics/2"
            },
            {
              "text": "3 有损传输线模型",
              "link": "/posts/durham_study/phontonics/3"
            }
          ]
        }
      ]
    },
    {
      "text": "前端",
      "collapsed": true,
      "items": []
    }
  ],
  "outline": {
    "level": [
      2,
      4
    ]
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/vuejs/vitepress"
    }
  ]
}

Page Data

{
  "title": "Runtime API Examples",
  "description": "",
  "frontmatter": {
    "outline": "deep"
  },
  "headers": [],
  "relativePath": "categories/index.md",
  "filePath": "categories/index.md"
}

Page Frontmatter

{
  "outline": "deep"
}

More

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