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": "Examples",
      "link": "/markdown-examples"
    }
  ],
  "sidebar": [
    {
      "text": "Examples",
      "items": [
        {
          "text": "Markdown 左侧",
          "link": "/markdown-examples"
        },
        {
          "text": "Runtime API 左侧",
          "link": "/api-examples"
        }
      ]
    }
  ],
  "lastUpdated": {
    "text": "最后更改时间",
    "formatOptions": {
      "dateStyle": "full",
      "timeStyle": "short"
    }
  },
  "search": {
    "provider": "local"
  },
  "docFooter": {
    "prev": "上一页",
    "next": "下一页"
  },
  "socialLinks": [
    {
      "icon": "npm",
      "link": "https://www.npmjs.com/package/vitepress"
    },
    {
      "icon": "github",
      "link": "https://github.com/vuejs/vitepress"
    }
  ]
}

Page Data

{
  "title": "Runtime API Examples",
  "description": "Runtime API Examples",
  "frontmatter": {
    "outline": "deep",
    "prev": {
      "text": "上一页: 首页1111ss",
      "link": "/markdown-examples"
    },
    "head": [
      [
        "meta",
        {
          "name": "title",
          "content": "Runtime API Examples"
        }
      ],
      [
        "meta",
        {
          "name": "description",
          "content": "Runtime API Examples"
        }
      ]
    ]
  },
  "headers": [],
  "relativePath": "api-examples.md",
  "filePath": "api-examples.md",
  "lastUpdated": 1725952163000
}

Page Frontmatter

{
  "outline": "deep",
  "prev": {
    "text": "上一页: 首页1111ss",
    "link": "/markdown-examples"
  },
  "head": [
    [
      "meta",
      {
        "name": "title",
        "content": "Runtime API Examples"
      }
    ],
    [
      "meta",
      {
        "name": "description",
        "content": "Runtime API Examples"
      }
    ]
  ]
}

More

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