Birincil derleme fonksiyonu. Native Rust derleyicisini kullanarak Markdown veya MDX'i HTML'e veya diğer çıktı biçimlerine dönüştürür.
import { compile } from "@unifast/node";
İmza
function compile(input: string, options?: CompileOptions): CompileResult
Parametreler
| Özellik | Tür | Varsayılan | Açıklama |
|---|
input | string | — | Markdown veya MDX kaynak string’i |
options?
| Özellik | Tür | Varsayılan | Açıklama |
|---|
options? | CompileOptions | — | Plugin’ler dahil derleme yapılandırması |
Dönüş Değeri
| Özellik | Tür | Açıklama |
|---|
output | string | object | Derlenmiş çıktı. Varsayılan olarak HTML string; HAST/MDAST için JSON string. |
frontmatter | Record<string, unknown> | Ayrıştırılmış frontmatter üst verisi (yoksa boş {}) |
diagnostics | Diagnostic[] | { level, message, start?, end? } dizisi |
stats | { parseMs, transformMs, emitMs } | Zamanlama dökümü (ms) |
toc | TocEntry[] | Çıkarılan içindekiler tablosu (TOC devre dışıysa boş []) |
Kullanım
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);
Örnekler
Temel Markdown’dan HTML’e
import { compile } from "@unifast/node";
const result = compile("# Hello, **world**!");
console.log(result.output);
MDX girdisi
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 çıktısı
import { compile } from "@unifast/node";
const result = compile("# Hello", { outputKind: "hast" });
const hast = JSON.parse(result.output as string);
console.log(hast);
MDAST çıktısı
import { compile } from "@unifast/node";
const result = compile("# Hello", { outputKind: "mdast" });
const mdast = JSON.parse(result.output as string);
console.log(mdast);
MDX JS çıktısı
import { compile } from "@unifast/node";
const result = compile(mdxSource, {
inputKind: "mdx",
outputKind: "mdxJs",
});
console.log(result.output);