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": {
    "src": "/favicon.ico",
    "width": 24,
    "height": 24
  },
  "nav": [
    {
      "text": "Home",
      "link": "/"
    },
    {
      "text": "Category",
      "link": "/category/markdown-examples",
      "activeMatch": "/category/"
    }
  ],
  "sidebar": {
    "/category/": {
      "base": "/category/",
      "items": [
        {
          "text": "Category",
          "items": [
            {
              "text": "Markdown Examples",
              "link": "markdown-examples"
            },
            {
              "text": "API Examples",
              "link": "api-examples"
            },
            {
              "text": "CSS",
              "link": "CSS"
            },
            {
              "text": "Html",
              "link": "Html"
            },
            {
              "text": "JavaScript",
              "link": "JavaScript"
            },
            {
              "text": "CSS",
              "items": [
                {
                  "text": "Box model",
                  "link": "box_model"
                },
                {
                  "text": "BFC",
                  "link": "BFC"
                },
                {
                  "text": "CSS selectors",
                  "link": "css_selectors"
                },
                {
                  "text": "Responsive Design",
                  "link": "responsive_design"
                },
                {
                  "text": "Center element",
                  "link": "center"
                },
                {
                  "text": "Hidden elememt",
                  "link": "hidden_element"
                },
                {
                  "text": "multi-column layout",
                  "link": "column_layout"
                },
                {
                  "text": "Text overflew style",
                  "link": "overflow_style"
                }
              ]
            },
            {
              "text": "ES6",
              "items": [
                {
                  "text": "Versions",
                  "link": "versions"
                },
                {
                  "text": "Set and Map",
                  "link": "set_map"
                },
                {
                  "text": "Promise",
                  "link": "promise"
                }
              ]
            },
            {
              "text": "Vue",
              "items": [
                {
                  "text": "Vuex vs Pinia",
                  "link": "vuex_vs_pinia"
                }
              ]
            },
            {
              "text": "Node.js",
              "items": [
                {
                  "text": "Node.js",
                  "link": "nodejs"
                },
                {
                  "text": "Buffer",
                  "link": "Buffer"
                },
                {
                  "text": "event loop",
                  "link": "event_loop"
                },
                {
                  "text": "EventEmitter",
                  "link": "EventEmitter"
                },
                {
                  "text": "fs",
                  "link": "fs"
                },
                {
                  "text": "global",
                  "link": "global"
                },
                {
                  "text": "process",
                  "link": "process"
                },
                {
                  "text": "Stream",
                  "link": "Stream"
                },
                {
                  "text": "Middleware",
                  "link": "Middleware"
                },
                {
                  "text": "require order",
                  "link": "require_order"
                },
                {
                  "text": "file upload",
                  "link": "file_upload"
                },
                {
                  "text": "JSON Web Token",
                  "link": "jwt"
                },
                {
                  "text": "Performance optimization",
                  "link": "performance"
                }
              ]
            },
            {
              "text": "Java",
              "items": [
                {
                  "text": "Installation",
                  "link": "installation"
                }
              ]
            },
            {
              "text": "Git",
              "link": "Git"
            },
            {
              "text": "test",
              "link": "test"
            },
            {
              "text": "category1",
              "base": "/category/examples-",
              "items": [
                {
                  "text": "examples1",
                  "link": "1"
                },
                {
                  "text": "examples2",
                  "link": "2"
                }
              ]
            }
          ]
        },
        {
          "text": "category2",
          "base": "/category/",
          "collapsed": false,
          "items": [
            {
              "text": "example1",
              "link": "example1"
            },
            {
              "text": "example2",
              "link": "example2"
            },
            {
              "text": "examples",
              "base": "/category/examples-",
              "items": [
                {
                  "text": "examples1",
                  "link": "1"
                },
                {
                  "text": "examples2",
                  "link": "2"
                }
              ]
            }
          ]
        }
      ]
    }
  },
  "editLink": {
    "pattern": "https://github.com/Jokerzhzh/facial-sutra/blob/main/:path",
    "text": "Edit this page on GitHub"
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/Jokerzhzh/facial-sutra"
    }
  ]
}

Page Data

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

Page Frontmatter

{
  "outline": "deep"
}

More

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