import { compile } from "@unifast/node";
函式簽名
function compile(input: string, options?: CompileOptions): CompileResult
參數
| 屬性 | 型別 | 預設值 | 說明 |
|---|
input | string | — | Markdown 或 MDX 原始碼字串 |
options?
| 屬性 | 型別 | 預設值 | 說明 |
|---|
options? | CompileOptions | — | 包含外掛在內的編譯設定 |
回傳值
| 屬性 | 型別 | 說明 |
|---|
output | string | object | 編譯後的輸出。預設為 HTML 字串;HAST/MDAST 時則為 JSON 字串。 |
frontmatter | Record<string, unknown> | 已解析的 frontmatter 後設資料(若無則為空的 {}) |
diagnostics | Diagnostic[] | { level, message, start?, end? } 陣列 |
stats | { parseMs, transformMs, emitMs } | 各階段耗時(毫秒) |
toc | TocEntry[] | 擷取而得的目錄(停用 TOC 時為空的 []) |
用法
import { compile, gfm, frontmatter, toc, sanitize } from "@unifast/node";
const md = `---
title: My Document
---
# Hello, **world**!
| Column A | Column B |
|----------|----------|
| Cell 1 | Cell 2 |
`;
const result = compile(md, {
inputKind: "md",
outputKind: "html",
plugins: [gfm(), frontmatter(), toc({ maxDepth: 3 }), sanitize()],
});
console.log(result.output);
console.log(result.frontmatter);
console.log(result.toc);
console.log(result.stats);
範例
基本的 Markdown 轉 HTML
import { compile } from "@unifast/node";
const result = compile("# Hello, **world**!");
console.log(result.output);
MDX 輸入
import { compile } from "@unifast/node";
const mdx = `
import { Alert } from "./components";
# Hello
<Alert type="info">This is MDX!</Alert>
`;
const result = compile(mdx, { inputKind: "mdx" });
HAST 輸出
import { compile } from "@unifast/node";
const result = compile("# Hello", { outputKind: "hast" });
const hast = JSON.parse(result.output as string);
console.log(hast);
MDAST 輸出
import { compile } from "@unifast/node";
const result = compile("# Hello", { outputKind: "mdast" });
const mdast = JSON.parse(result.output as string);
console.log(mdast);
MDX JS 輸出
import { compile } from "@unifast/node";
const result = compile(mdxSource, {
inputKind: "mdx",
outputKind: "mdxJs",
});
console.log(result.output);