跳至內容

Frontmatter

用法

VitePress 支援所有 Markdown 檔案中的 YAML frontmatter,並使用 gray-matter 來剖析。frontmatter 必須在 Markdown 檔案的最上方(在任何元素,包含 <script> 標籤之前),且必須採用在三條破折號之間的有效 YAML 格式。範例

md
---
title: Docs with VitePress
editLink: true
---

許多網站或預設佈景主題設定選項都有對應的 frontmatter 選項。你可以使用 frontmatter 來覆寫目前頁面專屬的特定行為。詳細資訊請參閱 Frontmatter 設定參考

你也可以定義自己的自訂 frontmatter 資料,以在頁面上的動態 Vue 表達式中使用。

存取 Frontmatter 資料

frontmatter 資料可透過特殊 $frontmatter 全域變數存取

以下是範例,說明如何在 Markdown 檔案中使用它

md
---
title: Docs with VitePress
editLink: true
---

# {{ $frontmatter.title }}

Guide content

您也可以在 <script setup> 中使用 useData() 輔助函式存取目前頁面的 frontmatter 資料。

其他 Frontmatter 格式

VitePress 也支援 JSON frontmatter 語法,以大括弧開始和結束

json
---
{
  "title": "Blogging Like a Hacker",
  "editLink": true
}
---

在 MIT 授權條款下發布。