compile()

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

input

ÖzellikTürVarsayılanAçıklama
inputstringMarkdown veya MDX kaynak string’i

options?

ÖzellikTürVarsayılanAçıklama
options?CompileOptionsPlugin’ler dahil derleme yapılandırması

Dönüş Değeri

ÖzellikTürAçıklama
outputstring | objectDerlenmiş çıktı. Varsayılan olarak HTML string; HAST/MDAST için JSON string.
frontmatterRecord<string, unknown>Ayrıştırılmış frontmatter üst verisi (yoksa boş {})
diagnosticsDiagnostic[]{ level, message, start?, end? } dizisi
stats{ parseMs, transformMs, emitMs }Zamanlama dökümü (ms)
tocTocEntry[]Çı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",       // "md" | "mdx"
  outputKind: "html",    // "html" | "hast" | "mdast" | "mdxJs"
  plugins: [gfm(), frontmatter(), toc({ maxDepth: 3 }), sanitize()],
});

console.log(result.output);
// <h1 id="hello-world">Hello, <strong>world</strong>!</h1>
// <table>...</table>

console.log(result.frontmatter);
// { title: "My Document" }

console.log(result.toc);
// [{ depth: 1, text: "Hello, world!", slug: "hello-world" }]

console.log(result.stats);
// { parseMs: 0.12, transformMs: 0.08, emitMs: 0.05 }

Örnekler

Temel Markdown’dan HTML’e

import { compile } from "@unifast/node";

const result = compile("# Hello, **world**!");

console.log(result.output);
// <h1 id="hello-world">Hello, <strong>world</strong>!</h1>

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);
// { type: "root", children: [{ type: "element", tagName: "h1", ... }] }

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);
// { type: "root", children: [{ type: "heading", depth: 1, ... }] }

MDX JS çıktısı

import { compile } from "@unifast/node";

const result = compile(mdxSource, {
  inputKind: "mdx",
  outputKind: "mdxJs",
});

console.log(result.output);
// MDX'in JSX'e derlenmiş JavaScript modül string'i