unifastPlugin()

建立一個 Vite 外掛,將 Markdown 與 MDX 檔案轉換為可直接匯入的 JavaScript 模組。

import unifast from "@unifast/vite";

函式簽名

function unifastPlugin(options?: UnifastPluginOptions): Plugin

參數

options?

屬性型別預設值說明
mdCompileOptions{}套用至 .md 檔案的 compile 選項
mdxCompileOptions沿用 md 的選項套用至 .mdx 檔案的 compile 選項;若未指定則沿用 md 的設定

回傳值

一個 Vite Plugin 物件,包含:

  • name"vite-plugin-unifast"

  • enforce"pre"──在其他外掛之前執行

  • transform:將 .md / .mdx 檔案編譯為 JS 模組

  • handleHotUpdate:Markdown 檔案變更時觸發 HMR

用法

// vite.config.ts
import { defineConfig } from "vite";
import unifast from "@unifast/vite";
import { gfm, frontmatter, syntect } from "@unifast/node";

export default defineConfig({
  plugins: [
    unifast({
      md: {
        plugins: [gfm(), frontmatter(), syntect()],
      },
    }),
  ],
});

範例

為 MD 與 MDX 設定不同選項

import unifast from "@unifast/vite";
import { gfm, frontmatter } from "@unifast/node";

export default defineConfig({
  plugins: [
    unifast({
      md: {
        plugins: [gfm(), frontmatter()],
      },
      mdx: {
        plugins: [gfm(), frontmatter()],
        // MDX-specific options can differ
      },
    }),
  ],
});

在元件中匯入 Markdown

import doc from "./README.md";

// doc.html — compiled HTML string
// doc.frontmatter — parsed frontmatter object
// doc.toc — table of contents entries