Skip to content
Published at:

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

{
  "siteTitle": "ShiBin's Wiki",
  "nav": [
    {
      "text": "Blogs",
      "link": "/archives"
    },
    {
      "text": "CS Primer",
      "items": [
        {
          "text": "OS",
          "link": "/csprimer/os/"
        },
        {
          "text": "Network",
          "link": "/csprimer/network/"
        },
        {
          "text": "Database",
          "link": "/csprimer/database/"
        },
        {
          "text": "Compiler",
          "link": "/csprimer/compiler/"
        }
      ]
    },
    {
      "text": "Linux",
      "items": [
        {
          "text": "Basic",
          "link": "/linux/01basic/"
        },
        {
          "text": "App",
          "link": "/linux/02app/"
        },
        {
          "text": "Kernel",
          "link": "/linux/03kernel/"
        }
      ]
    },
    {
      "text": "Language",
      "items": [
        {
          "text": "Assembly",
          "link": "/lang/assembly/"
        },
        {
          "text": "C",
          "link": "/lang/c/"
        },
        {
          "text": "C++",
          "link": "/lang/c++/"
        },
        {
          "text": "Rust",
          "link": "/lang/rust/"
        },
        {
          "text": "JS/TS",
          "link": "/lang/typescript/"
        }
      ]
    },
    {
      "text": "Misc",
      "items": [
        {
          "text": "CMake",
          "link": "/misc/cmake/index"
        },
        {
          "text": "Cheat Sheet",
          "link": "/misc/cheatsheet/index"
        },
        {
          "text": "Talks",
          "link": "/misc/talks"
        },
        {
          "text": "About Me",
          "link": "/misc/about_me"
        }
      ]
    }
  ],
  "sidebar": {
    "/csprimer/": [
      {
        "text": "CS Primer",
        "items": [
          {
            "text": "OS",
            "link": "/csprimer/os/"
          },
          {
            "text": "Network",
            "link": "/csprimer/network/"
          },
          {
            "text": "Database",
            "link": "/csprimer/database/"
          },
          {
            "text": "Compiler",
            "link": "/csprimer/compiler/"
          }
        ]
      }
    ],
    "/linux/": [
      {
        "text": "Basic",
        "link": "/linux/01basic/",
        "collapsed": true,
        "items": [
          {
            "text": "Linux系统",
            "link": "/linux/01basic/01_system"
          },
          {
            "text": "Shell及常用命令",
            "link": "/linux/01basic/02_shell"
          }
        ]
      },
      {
        "text": "App",
        "collapsed": true,
        "items": [
          {
            "text": "UNIX 基础",
            "link": "/linux/02app/01_UNIX_System_Overview"
          },
          {
            "text": "UNIX 标准及实现",
            "link": "/linux/02app/02_UNIX_Standardization_and_Implementations"
          },
          {
            "text": "文件 I/O",
            "link": "/linux/02app/03_File_I_O"
          },
          {
            "text": "文件和目录",
            "link": "/linux/02app/04_Files_and_Directories"
          },
          {
            "text": "标准 I/O 库",
            "link": "/linux/02app/05_Standard_I_O_Library"
          },
          {
            "text": "系统数据文件和信息",
            "link": "/linux/02app/06_System_Data_Files_and_Information"
          },
          {
            "text": "迸程环境",
            "link": "/linux/02app/07_Process_Environment"
          },
          {
            "text": "进程控制",
            "link": "/linux/02app/08_Process_Control"
          },
          {
            "text": "进程关系",
            "link": "/linux/02app/09_Process_Relationships"
          },
          {
            "text": "信号",
            "link": "/linux/02app/10_Signals"
          },
          {
            "text": "线程",
            "link": "/linux/02app/11_Threads"
          },
          {
            "text": "线程控制",
            "link": "/linux/02app/12_Thread_Control"
          },
          {
            "text": "守护进程",
            "link": "/linux/02app/13_Daemon_Processes"
          },
          {
            "text": "高级 I/O",
            "link": "/linux/02app/14_Advanced_I_O"
          },
          {
            "text": "迸程间通信",
            "link": "/linux/02app/15_Interprocess_Communication"
          },
          {
            "text": "网络 IPC :套接字",
            "link": "/linux/02app/16_Network_IPC:_Sockets"
          },
          {
            "text": "高级 IPC",
            "link": "/linux/02app/17_Advanced_IPC"
          },
          {
            "text": "终端 I/O",
            "link": "/linux/02app/18_Terminal_I_O"
          },
          {
            "text": "伪终端",
            "link": "/linux/02app/19_Pseudo_Terminals"
          },
          {
            "text": "数据库函数库",
            "link": "/linux/02app/20_A_Database_Library"
          },
          {
            "text": "与网络打印机通信",
            "link": "/linux/02app/21_Communicating_with_a_Network_Printer"
          }
        ]
      },
      {
        "text": "Kernel",
        "collapsed": true,
        "link": "/linux/03kernel/"
      }
    ],
    "/lang/": [
      {
        "text": "Assembly",
        "link": "/lang/assembly/"
      },
      {
        "text": "C",
        "link": "/lang/c/",
        "collapsed": true,
        "items": [
          {
            "text": "基础",
            "link": "/lang/c/00_basics"
          },
          {
            "text": "关键字",
            "link": "/lang/c/01_keywords"
          },
          {
            "text": "内存",
            "link": "/lang/c/02_memory"
          },
          {
            "text": "指针",
            "link": "/lang/c/03_pointer"
          },
          {
            "text": "标准库",
            "link": "/lang/c/04_stdlib"
          },
          {
            "text": "string及其源码",
            "link": "/lang/c/05_string"
          }
        ]
      },
      {
        "text": "C++",
        "link": "/lang/c++/"
      },
      {
        "text": "Rust",
        "link": "/lang/rust/",
        "collapsed": true,
        "items": [
          {
            "text": "简介",
            "link": "/lang/rust/0. Introduction"
          },
          {
            "text": "入门指南",
            "link": "/lang/rust/1. Getting Started"
          },
          {
            "text": "写个猜数字游戏",
            "link": "/lang/rust/2. Programming a Guessing Game"
          },
          {
            "text": "常见编程概念",
            "link": "/lang/rust/3. Common Programming Concepts"
          },
          {
            "text": "认识所有权",
            "link": "/lang/rust/4. Understanding Ownership"
          },
          {
            "text": "使用结构体组织相关联的数据",
            "link": "/lang/rust/5. Using Structs to Structure Related Data"
          },
          {
            "text": "枚举和模式匹配",
            "link": "/lang/rust/6. Enums and Pattern Matching"
          },
          {
            "text": "使用包、Crate 和模块管理",
            "link": "/lang/rust/7. Managing Growing Projects with Packages, Crates, and Modules"
          },
          {
            "text": "常见集合",
            "link": "/lang/rust/8. Common Collections"
          },
          {
            "text": "错误处理",
            "link": "/lang/rust/9. Error Handling"
          },
          {
            "text": "泛型、Trait 和生命周期",
            "link": "/lang/rust/10. Generic Types, Traits, and Lifetimes"
          },
          {
            "text": "编写自动化测试",
            "link": "/lang/rust/11. Writing Automated Tests"
          },
          {
            "text": "项目:构建命令行程序",
            "link": "/lang/rust/12. An IO Project: Building a Command Line Program"
          },
          {
            "text": "函数式语言功能:迭代器与闭包",
            "link": "/lang/rust/13. Functional Language Features: Iterators and Closures"
          },
          {
            "text": "Cargo 和 Crates.io 的内容",
            "link": "/lang/rust/14. More about Cargo and Crates.io"
          },
          {
            "text": "智能指针",
            "link": "/lang/rust/15. Smart Pointers"
          },
          {
            "text": "无畏并发",
            "link": "/lang/rust/16. Fearless Concurrency"
          },
          {
            "text": "Rust 的面向对象编程特性",
            "link": "/lang/rust/Introduction"
          },
          {
            "text": "模式与模式匹配",
            "link": "/lang/rust/Introduction"
          },
          {
            "text": "高级特征",
            "link": "/lang/rust/Introduction"
          },
          {
            "text": "项目:构建多线程 web server",
            "link": "/lang/rust/20. Final Project: Building a Multithreaded Web Server"
          }
        ]
      },
      {
        "text": "JS/TS",
        "link": "/lang/typescript/",
        "collapsed": true,
        "items": [
          {
            "text": "The Basics",
            "link": "/lang/typescript/01 The Basics"
          },
          {
            "text": "Everyday Types",
            "link": "/lang/typescript/02 Everyday Types"
          },
          {
            "text": "Narrowing",
            "link": "/lang/typescript/03 Narrowing"
          },
          {
            "text": "More on Functions",
            "link": "/lang/typescript/04 More on Functions"
          },
          {
            "text": "Object Types",
            "link": "/lang/typescript/05 Object Types"
          },
          {
            "text": "Classes",
            "link": "/lang/typescript/06 Classes"
          }
        ]
      }
    ],
    "/misc/cmake/": [
      {
        "text": "CMake",
        "items": [
          {
            "text": "Overview",
            "link": "/misc/cmake/index"
          },
          {
            "text": "Hello World",
            "link": "/misc/cmake/01_helloworld"
          },
          {
            "text": "CMake 常用脚本命令",
            "link": "/misc/cmake/02_common_script_commands"
          },
          {
            "text": "Targets",
            "link": "/misc/cmake/03_target"
          },
          {
            "text": "模块",
            "link": "/misc/cmake/04_modules"
          },
          {
            "text": "引入三方库",
            "link": "/misc/cmake/05_thirty_part_library"
          },
          {
            "text": "交叉编译",
            "link": "/misc/cmake/06_cross_compiling"
          },
          {
            "text": "安装、打包",
            "link": "/misc/cmake/07_install_pack"
          }
        ]
      }
    ],
    "/misc/cheatsheet/": [
      {
        "text": "Cheat Sheet",
        "items": [
          {
            "text": "macOS",
            "link": "/misc/cheatsheet/macos"
          },
          {
            "text": "Vim",
            "link": "/misc/cheatsheet/vim"
          },
          {
            "text": "VSCode",
            "link": "/misc/cheatsheet/vscode"
          },
          {
            "text": "Git",
            "link": "/misc/cheatsheet/git"
          },
          {
            "text": "GCC",
            "link": "/misc/cheatsheet/gcc"
          },
          {
            "text": "GDB",
            "link": "/misc/cheatsheet/gdb"
          },
          {
            "text": "Final Cut Pro",
            "link": "/misc/cheatsheet/final_cut_pro"
          }
        ]
      }
    ],
    "/misc/examples/": [
      {
        "text": "Examples",
        "items": [
          {
            "text": "Examples",
            "link": "/markdown-examples"
          },
          {
            "text": "API Examples",
            "link": "/api-examples"
          }
        ]
      }
    ]
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/WShiBin"
    }
  ],
  "footer": {
    "copyright": "Copyright © 2017-2024 Shi Bin"
  },
  "search": {
    "provider": "local"
  },
  "outline": {
    "level": [
      2,
      3
    ]
  },
  "lastUpdated": {
    "text": "Updated at",
    "formatOptions": {
      "dateStyle": "full",
      "timeStyle": "medium"
    }
  }
}

Page Data

{
  "title": "Runtime API Examples",
  "description": "",
  "frontmatter": {
    "outline": "deep",
    "layout": "doc",
    "prev": false,
    "next": false,
    "sidebar": true
  },
  "headers": [],
  "relativePath": "examples/api-examples.md",
  "filePath": "examples/api-examples.md",
  "lastUpdated": 1714231035000
}

Page Frontmatter

{
  "outline": "deep",
  "layout": "doc",
  "prev": false,
  "next": false,
  "sidebar": true
}

More

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

Updated at: