Hàm biên dịch chính. Biến đổi Markdown hoặc MDX thành HTML hoặc các định dạng đầu ra khác bằng trình biên dịch Rust native.
import { compile } from "@unifast/node";
Chữ ký
function compile(input: string, options?: CompileOptions): CompileResult
Tham số
| Thuộc tính | Kiểu | Mặc định | Mô tả |
|---|
input | string | — | Chuỗi mã nguồn Markdown hoặc MDX |
options?
| Thuộc tính | Kiểu | Mặc định | Mô tả |
|---|
options? | CompileOptions | — | Cấu hình biên dịch bao gồm cả các plugin |
Giá trị trả về
| Thuộc tính | Kiểu | Mô tả |
|---|
output | string | object | Đầu ra đã biên dịch. Mặc định là chuỗi HTML; JSON string cho HAST/MDAST. |
frontmatter | Record<string, unknown> | Metadata frontmatter đã được parse (rỗng {} nếu không có) |
diagnostics | Diagnostic[] | Mảng { level, message, start?, end? } |
stats | { parseMs, transformMs, emitMs } | Thống kê thời gian (ms) |
toc | TocEntry[] | Bảng mục lục đã được trích xuất (rỗng [] nếu TOC bị tắt) |
Cách dùng
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);
Ví dụ
Markdown cơ bản sang HTML
import { compile } from "@unifast/node";
const result = compile("# Hello, **world**!");
console.log(result.output);
Đầu vào 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" });
Đầu ra HAST
import { compile } from "@unifast/node";
const result = compile("# Hello", { outputKind: "hast" });
const hast = JSON.parse(result.output as string);
console.log(hast);
Đầu ra MDAST
import { compile } from "@unifast/node";
const result = compile("# Hello", { outputKind: "mdast" });
const mdast = JSON.parse(result.output as string);
console.log(mdast);
Đầu ra MDX JS
import { compile } from "@unifast/node";
const result = compile(mdxSource, {
inputKind: "mdx",
outputKind: "mdxJs",
});
console.log(result.output);