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"
  },
  "nav": [
    {
      "text": "Home",
      "link": "/"
    },
    {
      "text": "Pages",
      "activeMatch": "/pages/",
      "items": [
        {
          "items": [
            {
              "text": "Linux",
              "link": "/pages/linux/"
            },
            {
              "text": "Docker",
              "link": "/pages/docker/"
            }
          ]
        },
        {
          "items": [
            {
              "text": "Rust",
              "link": "/pages/rust/"
            },
            {
              "text": "Python",
              "link": "/pages/python/"
            },
            {
              "text": "Golang",
              "link": "/pages/golang/"
            }
          ]
        },
        {
          "items": [
            {
              "text": "Misc",
              "link": "/pages/misc/"
            }
          ]
        }
      ]
    },
    {
      "component": "StatusBadge"
    }
  ],
  "sidebar": {
    "/pages/linux/": [
      {
        "text": "Linux",
        "collapsed": false,
        "items": [
          {
            "text": "VPS 安全指北",
            "link": "/pages/linux/vps_reload"
          },
          {
            "text": "H-UI 面板",
            "link": "/pages/linux/hui"
          },
          {
            "text": "Git",
            "link": "/pages/linux/git"
          },
          {
            "text": "How To",
            "link": "/pages/linux/how_to"
          },
          {
            "text": "ssh",
            "link": "/pages/linux/ssh"
          }
        ]
      },
      {
        "text": "Tools",
        "collapsed": false,
        "items": [
          {
            "text": "oh-my-zsh",
            "link": "/pages/linux/tools/ohmyzsh"
          },
          {
            "text": "starship",
            "link": "/pages/linux/tools/starship"
          },
          {
            "text": "shortkeys",
            "link": "/pages/linux/tools/shortkeys"
          },
          {
            "text": "tmux",
            "link": "/pages/linux/tools/tmux"
          }
        ]
      }
    ],
    "/pages/docker/": [
      {
        "text": "Docker",
        "items": [
          {
            "text": "Pass",
            "link": "/pages/docker/pass"
          }
        ]
      }
    ],
    "/pages/rust/": [
      {
        "text": "Rust",
        "items": [
          {
            "text": "cargo",
            "link": "/pages/rust/cargo"
          },
          {
            "text": "所有权",
            "link": "/pages/rust/ownership"
          },
          {
            "text": "集合",
            "link": "/pages/rust/collections"
          },
          {
            "text": "错误处理",
            "link": "/pages/rust/error"
          },
          {
            "text": "生命周期",
            "link": "/pages/rust/lifetime"
          },
          {
            "text": "特征",
            "link": "/pages/rust/trait"
          },
          {
            "text": "闭包和迭代器",
            "link": "/pages/rust/closures_Iterators"
          },
          {
            "text": "智能指针",
            "link": "/pages/rust/smart_pointer"
          },
          {
            "text": "无畏并发",
            "link": "/pages/rust/fearless_concurrency"
          },
          {
            "text": "模式与匹配",
            "link": "/pages/rust/match"
          },
          {
            "text": "枚举与结构体",
            "link": "/pages/rust/struct_enum"
          },
          {
            "text": "泛型",
            "link": "/pages/rust/generic"
          }
        ]
      }
    ],
    "/pages/golang/": [
      {
        "text": "Golang",
        "items": [
          {
            "text": "defer",
            "link": "/pages/golang/defer"
          }
        ]
      }
    ],
    "/pages/python/": [
      {
        "text": "Python",
        "items": [
          {
            "text": "test1",
            "link": "/pages/python/test1"
          }
        ]
      }
    ],
    "/pages/misc/": [
      {
        "text": "Examples",
        "collapsed": false,
        "items": [
          {
            "text": "Markdown Examples",
            "link": "/pages/misc/markdown-examples"
          },
          {
            "text": "Runtime API Examples",
            "link": "/pages/misc/api-examples"
          }
        ]
      },
      {
        "text": "Tips",
        "collapsed": false,
        "items": [
          {
            "text": "Did You Know",
            "link": "/pages/misc/tips/did_you_know"
          }
        ]
      }
    ]
  }
}

Page Data

{
  "title": "Runtime API Examples",
  "description": "",
  "frontmatter": {},
  "headers": [],
  "relativePath": "pages/misc/api-examples.md",
  "filePath": "pages/misc/api-examples.md",
  "lastUpdated": 1752524702000
}

Page Frontmatter

{}

More

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