Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Node.js usage

Peisar’s main Node.js package is the native addon published as peisar. Use it in Node.js servers, scripts, build tools, and frameworks that can load native .node binaries.

This page is the Node.js overview. Use it as the entry point for framework, runtime, and API-specific guides.

At a glance

  • Package: peisar
  • Module surface: require("peisar") or import ... from "peisar"
  • Runtime requirement: see the engines.node field in the root package.json
  • Browser and WASI runtimes: see WASM
  • Full API details: see Node.js API reference

For browser-only runtimes or environments that cannot load native addons, use the WebAssembly package documented in WASM.

Install

npm i peisar

Basic usage

const { Peisar } = require("peisar");

const document = new Peisar(
  `---
title: Hello
---

# Hello **world**`,
  { fragment: true },
);

console.log(document.html);
console.log(document.frontmatter);

Current API structure

The Node.js API in the peisar namespace currently centers around these classes and properties:

ExportPurpose
PeisarParse one Markdown document and read ast, html, or frontmatter
PeisarAstParse a document and read its AST or frontmatter without rendering
document.frontmatterRead the detected frontmatter type and raw contents

Frontmatter is returned as { frontmatterType, raw }. The type is "Yaml", "Toml", "Json", or "Empty"; Peisar does not deserialize the raw content. See the API reference for the formats and shape.

Detailed signatures and option shapes are in the Node.js API reference.

Preferred extension API

Use Peisar.use() for Node.js integrations. A plugin may provide any combination of:

  • visitBlock
  • visitInline
  • parseBlock
  • parseInline
const { Peisar } = require("peisar");

const document = new Peisar("# Hello", { fragment: true });

document.use({
  visitBlock([block]) {
    if (block.type === "Heading") {
      return { recurse: true };
    }
  },
});