unifastPlugin()

Markdown および MDX ファイルをインポート可能な JavaScript モジュールに変換する Vite プラグインを生成します。

import unifast from "@unifast/vite";

シグネチャ

function unifastPlugin(options?: UnifastPluginOptions): Plugin

パラメータ

options?

プロパティデフォルト説明
mdCompileOptions{}.md ファイルに適用される compile オプション
mdxCompileOptionsmd オプションを使用.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