@unifast/vite

unifast 向けの Vite プラグインです。Markdown および MDX ファイルを JS モジュールへ変換します。

概要

@unifast/vite.md および .mdx ファイルを JavaScript モジュールに変換する Vite プラグインです。unifast の Markdown/MDX コンパイラを Vite のビルドパイプラインへ統合し、アプリケーション内で Markdown ファイルを直接インポートできるようにします。

インストール

@unifast/node はピア依存 (任意) です。利用可能な場合、プラグインは最大限のパフォーマンスのためにネイティブ Rust コンパイラを使用します。利用できない場合、.md ファイルは基本的な HTML レンダリングにフォールバックし、.mdx ファイルには @unifast/node が必須となります。

クイックスタート

// vite.config.ts
import { defineConfig } from "vite";
import unifast from "@unifast/vite";

export default defineConfig({
  plugins: [
    unifast({
      md: {
        // compile options for .md files
      },
      mdx: {
        // compile options for .mdx files
      },
    }),
  ],
});
// App.tsx
import doc from "./content/hello.md";

console.log(doc.html);         // compiled HTML string
console.log(doc.frontmatter);  // parsed frontmatter object
console.log(doc.toc);          // table of contents entries

モジュールのエクスポート

.md ファイル

.md ファイルは、次のエクスポートを持つモジュールに変換されます。

エクスポート説明
htmlstringコンパイル済みの HTML 文字列
frontmatterRecord<string, unknown>パース済みの frontmatter メタデータ
tocTocEntry[]抽出された目次
defaultobjecthtmlfrontmattertoc を含むオブジェクト

.mdx ファイル

.mdx ファイルは、React の jsx-runtime を使った JSX モジュールにコンパイルされます。デフォルトエクスポートとして React コンポーネントを、加えて frontmattertoc をエクスポートします。

エクスポート一覧

エクスポート種類説明
default (unifastPlugin)functionVite プラグインのファクトリ
UnifastPluginOptionstypeプラグインのオプションインターフェース