跳至內容
Fumadocs

Remark LLMs

匯出文件的 Markdown,供 LLM 使用。

remarkLLMs 外掛會將文件的 Markdown 匯出為 _markdown,供 LLM 使用,並保留您原本撰寫的 Markdown。產生方式請參閱字串化。

import { Callout } from 'fumadocs-ui/components/callout';

## Getting Started

<Callout>Hello **world**.</Callout>

ESM 會被移除,標題會加上各自的 ID (請參閱 headingIds) ,而透過 Fumadocs MDX 引入的內容也會展開。

使用方式

remarkLLMs 是一個 remark 外掛。您可以在 Fumadocs MDX 中透過 includeProcessedMarkdown 啟用它,unified 與 Sätteri 皆適用:

source.config.ts
import { defineDocs } from 'fumadocs-mdx/config';

export const docs = defineDocs({
  docs: {
    postprocess: {
      // `true`,或 `remarkLLMs` 的選項
      includeProcessedMarkdown: true,
    },
  },
});

在頁面中使用 getText('processed') 讀取內容,它會搭配頁面的 props 算繪 Markdown:

const markdown = await page.data.getText('processed');

圖片

與頁面放在一起、由 Remark Image 匯入的圖片,會使用打包工具提供這些圖片時的 URL:

![How a calculation flows](./flow.png)

public 目錄中的圖片會保留原本的位址。小於 build.assetsInlineLimit 的圖片會由 Vite 內嵌為 data: URL。

輸出

output 選項用於設定 _markdown 的形式:

  • function:_markdown 是一個元件,請使用 renderToMarkdown() 加以算繪。Markdown 會依據頁面 props 產生:<show> 中未算繪的內容會被省略,表達式會替換為其值,JSX 元素則會替換為對應的 Markdown 形式。
  • string:_markdown 是一個字串。表達式與 JSX 元素會以原始文字保留,而包含 <show> 的文件則會編譯失敗。

在 Fumadocs MDX 與 @fumadocs/astro 中,預設值為 function,其他情況則為 string。自訂內容來源若使用執行階段輸出,則會得到 function。

JSX 元素

使用 output: 'function' 時,元件的查找方式與 MDX 相同:先查找檔案中的匯入與宣告,再查找頁面 props 中的 MDX 元件。請透過 getPage() 將 MDX 元件傳入頁面:

const page = source.getPage(slugs, { props: { components } });

元件可透過 asMarkdown() 定義其 Markdown 形式,未定義的元件則會保留為 JSX。若要移除元素,或改以其子元素取代,請使用 filterElement (Sätteri 不支援) 。

預留位置

若要自行算繪 JSX 元素,請將它們列為預留位置:

import type { LLMsOptions } from 'fumadocs-core/mdx-plugins/remark-llms';

const llmsOptions: LLMsOptions = {
  mdxAsPlaceholder: ['Callout'],
};

接著將它們算繪出來,必要時可使用執行階段的資料:

import { renderPlaceholder } from 'fumadocs-core/mdx-plugins/remark-llms.runtime';

const markdown = await renderPlaceholder(await page.data.getText('processed'), {
  // `children` 為元素內容的 Markdown
  Callout: ({ attributes, children }) => `> **${attributes.title}**\n>\n> ${children}`,
});

使用 Sätteri 時,無法使用 mdxAsPlaceholder。

選項

屬性

型別

How is this guide?

最後更新於