@unifast/node
Node.js binding for the unifast Markdown/MDX compiler powered by native Rust
Overview
@unifast/node is the primary entry point for using unifast in Node.js. It provides the compile() function that invokes the native Rust compiler via napi-rs for maximum performance. It also supports the plugin system, allowing HAST transforms to be applied in JavaScript after the Rust compilation step.
Installation
The native Rust addon must be pre-built. Run
cargo build -p unifast-bindings-node --releaseif the native binary is not available.
Quick Start
import { compile } from "@unifast/node";
const result = compile("# Hello, **world**!");
console.log(result.output);
// <h1 id="hello-world">Hello, <strong>world</strong>!</h1>Plugin Pipeline
When plugins are provided, compile() executes the following pipeline:
Extract plugins - Separates
pluginsfrom the rest of the optionsMerge options - Deep-merges each plugin’s
optionsinto the compile optionsForce HAST - If any plugin has a
hastTransform, the native call usesoutputKind: "hast"Native compilation - Calls the Rust compiler via napi-rs
HAST transforms - Applies each plugin’s
hastTransformin orderConvert output - If the user didn’t request HAST output, converts back to HTML via
hastToHtml
Built-in Plugins
The following plugin factory functions are included — no separate install needed:
gfm, frontmatter, sanitize, syntect, treeSitter, toc, externalLinks, autolinkHeadings, smartypants, wikiLink, codeImport, emoji, breaks, math, githubAlert, sectionize, directive, definitionList, rubyAnnotation, cjk
import { compile, gfm, frontmatter, syntect } from "@unifast/node";
const result = compile(source, {
plugins: [gfm(), frontmatter(), syntect()],
});Exports Summary
| Export | Kind | Description |
|---|---|---|
compile | function | Compile Markdown/MDX to HTML or other formats |
gfm, frontmatter, … | function | Built-in plugin factories (20 total) |
hastToHtml | function | HAST-to-HTML serializer (re-exported from @unifast/core) |
CompileOptions | type | Compilation configuration |
CompileResult | type | Compilation result |
UnifastPlugin | type | Plugin interface |
TocEntry | type | Table of contents entry |
HastRoot | type | HAST root node |
HastElement | type | HAST element node |
HastText | type | HAST text node |
HastNode | type | Union of all HAST node types |
UnifastError | class | Base error class |
ParseError | class | Parse error class |
CompileError | class | Compile error class |