跳至內容
Fumadocs

快速入門

認識 Fumadocs 的官方內容來源 Fumadocs MDX。

簡介

Fumadocs MDX 是一個將內容轉換為型別安全資料的工具,類似於 Content Collections。

它並非完整的 CMS,而是專為 React 框架設計的內容處理層,可用來處理部落格文章及其他各類內容。

什麼是集合?

集合 (Collection) 是指由特定類型檔案所組成的集合。您可以在應用程式中使用 Macro API 定義集合:

lib/source.ts
import { defineDocs } from 'fumadocs-mdx/macro';
import { loader } from 'fumadocs-core/source';

const docs = defineDocs({
  dir: 'content/docs',
});

export const source = loader({
  baseUrl: '/docs',
  source: docs.toFumadocsSource(),
});

可用的集合:

將 Markdown & MDX 檔案編譯為 React 元件,並提供目錄等實用屬性。

import { defineCollections } from 'fumadocs-mdx/macro';

export const test = defineCollections({
  type: 'doc',
  dir: 'content/docs',
});

例如,doc 集合會轉換 .md 與 .mdx 檔案:

ui.md
hello.md
index.mdx
meta.json

您也可以透過 Config API 在 source.config.ts 中定義集合,這種方式會在 .source 底下產生進入點檔案。

安裝

在以下環境中設定 Fumadocs MDX:

常見問題

內建屬性

MDX 檔案預設會匯出以下屬性。

屬性說明
frontmatterfrontmatter
toc目錄
structuredData結構化資料,可用於實作搜尋功能
extractedReferences用於分析 hrefs 參照

自訂 Frontmatter

使用 schema 選項傳入驗證用的 schema,藉此驗證 frontmatter 並定義其輸出屬性。

MDX 頁面也可以在程式碼中匯出 frontmatter。匯出的內容會合併至 YAML frontmatter 並覆蓋其值,再由集合的 schema 驗證合併後的結果:

content/docs/index.mdx
---
title: Introduction
---

import { version } from '../../package.json';

export const frontmatter = {
  description: `The docs of v${version}.`,
};

這個 export 會在建置時執行。它可以使用頁面的匯入,以及同一區塊中位於它之前的 export,但無法使用頁面的內容或元件。staticParams 仍須保留在 YAML frontmatter 中,因為載入器會在 export 執行之前就讀取它。

自訂編譯器

Fumadocs MDX 透過 MDX Compiler,以 unified 將 MDX 檔案編譯為 JavaScript 檔案。

集合也可以改用 Sätteri 進行編譯。兩者都內建一套由外掛與設定組成的預設組合。

您可以在全域設定或集合設定中自訂編譯器。

How is this guide?

最後更新於