@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 --release if 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:

  1. Extract plugins - Separates plugins from the rest of the options

  2. Merge options - Deep-merges each plugin’s options into the compile options

  3. Force HAST - If any plugin has a hastTransform, the native call uses outputKind: "hast"

  4. Native compilation - Calls the Rust compiler via napi-rs

  5. HAST transforms - Applies each plugin’s hastTransform in order

  6. Convert 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

ExportKindDescription
compilefunctionCompile Markdown/MDX to HTML or other formats
gfm, frontmatter, …functionBuilt-in plugin factories (20 total)
hastToHtmlfunctionHAST-to-HTML serializer (re-exported from @unifast/core)
CompileOptionstypeCompilation configuration
CompileResulttypeCompilation result
UnifastPlugintypePlugin interface
TocEntrytypeTable of contents entry
HastRoottypeHAST root node
HastElementtypeHAST element node
HastTexttypeHAST text node
HastNodetypeUnion of all HAST node types
UnifastErrorclassBase error class
ParseErrorclassParse error class
CompileErrorclassCompile error class