Fumadocs rebuilt on Hamur, the original is fumadocs.dev
跳至內容
Fumadocs

本機 Markdown

在執行階段從資料夾載入 Markdown 與 MDX 檔案。

@fumadocs/local-md 會從資料夾讀取 Markdown 與 MDX 檔案並在執行階段編譯,與其他內容來源一樣支援 frontmatter、meta 檔案及搜尋資料。

與 Fumadocs MDX 不同,它不會經過打包工具,因此:

  • 圖片不會在建置時最佳化。
  • MDX 檔案無法匯入模組,請改在算繪時傳入元件。

設定

npm install @fumadocs/local-md shiki

建立來源,並透過 getSource() 取得它的載入器:

lib/source.ts
import { dynamicLoader } from 'fumadocs-core/source';
import { localMd } from '@fumadocs/local-md';

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

const docsLoader = dynamicLoader(docs.dynamicSource(), {
  baseUrl: '/docs',
});

export async function getSource() {
  return docsLoader.get();
}

算繪頁面

page.data.load() 會編譯頁面並回傳對應的算繪器。

在您的頁面中算繪:

import type { ReactNode } from 'react';
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
import { getSource } from '@/lib/source';

export default async function Layout({ children }: { children: ReactNode }) {
  const source = await getSource();

  return <DocsLayout tree={source.getPageTree()}>{children}</DocsLayout>;
}

熱重載

開發期間,監看器會在內容檔案變更時通知來源。

請加入 Vite 外掛,並將您的來源連接到該外掛:

import { defineConfig } from 'waku/config';
import { localContentPlugin } from '@fumadocs/local-md/dev/vite';

export default defineConfig({
  vite: {
    // 內容變更後會重新載入瀏覽器,可使用 `{ reload: false }` 停用
    plugins: [localContentPlugin()],
  },
});

設定

Markdown 與 MDX 檔案分別由不同的外掛編譯,因此兩種格式各有其選項:

lib/source.ts
import { localMd } from '@fumadocs/local-md';
import { pageSchema, metaSchema } from 'fumadocs-core/source/schema';
import remarkDirective from 'remark-directive';

const docs = localMd({
  dir: 'content/docs',
  // 驗證 frontmatter 與 meta 檔案
  frontmatterSchema: pageSchema.extend({
    // ...
  }),
  metaSchema: metaSchema.extend({
    // ...
  }),
  // `.md` 檔案
  mdOptions: {
    // `<include>` 僅適用於 MDX,Markdown 請改用 `::include[./file.md]`
    remarkPlugins: [remarkDirective],
  },
  // `.mdx` 檔案
  mdxOptions: {
    rehypeCodeOptions: {
      themes: { light: 'github-light', dark: 'github-dark' },
    },
  },
});

程式碼執行

MDX 檔案會透過 new Function 以 JavaScript 形式執行。請勿將不受信任的 MDX 檔案放在 dir 中。若執行環境不支援 eval() (例如 Cloudflare Workers) ,請改用 Markdown 檔案。

Markdown 檔案會直接從語法樹算繪,外掛產生的 JSX 則交由虛擬 JavaScript 引擎求值。您可以透過 rendererOptions.executor 替換此引擎。

靜態來源

staticSource() 只會讀取檔案一次,不支援熱重載,因此直接使用一般的 loader() 即可:

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

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

從 Fumadocs MDX 遷移

請將您的 lib/source.ts 替換為設定中的版本,接著:

  • 從您的 Vite 設定中移除 fumadocsMdx()。
  • 將 source 物件替換為 await getSource()。
  • 設定熱重載。

頁面資料的變更如下:

const source = await getSource();
const page = source.getPage(slugs);

// 自訂 frontmatter 屬性
page.data.full; 
page.data.frontmatter.full; 

// Markdown 原始內容
await page.data.getText('raw'); 
page.data.content; 

// 供搜尋使用的結構化資料
page.data.structuredData; 
await page.data.structuredData(); 

// 已算繪的內容
<page.data.body components={getMDXComponents()} />; 
const { body, toc } = await (await page.data.load()).render(getMDXComponents()); 

How is this guide?

最後更新於