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 API reference

This document covers the Node.js API in the peisar namespace exposed by the peisar package. Its main entry point parses and renders a single Markdown document.

peisar API

ExportKindPurpose
PeisarclassParse Markdown and access its AST, HTML, and frontmatter
PeisarAstclassParse Markdown and access its AST and frontmatter
PeisarOptions, AstOptions, RenderOptionsinterfacesConfigure parsing and rendering
PeisarDocument, Block, Inline, and supporting AST typestypesDescribe AST nodes, spans, attributes, and table/list data
PeisarPlugin and plugin context/result/control typesinterfaces and typesImplement parser hooks and AST visitors
Frontmatter, FrontmatterType, FrontmatterResultinterface and typeDescribe extracted frontmatter

Peisar

Constructor

new Peisar(markdown: string, options?: PeisarOptions)

Getters

MemberTypeDescription
astPeisarDocumentParsed AST as a JavaScript object
htmlstringRendered HTML
frontmatterFrontmatterDetected format and raw frontmatter content; not deserialized

Methods

use(plugin) registers parser hooks, AST visitors, or both on the document. Registering a plugin invalidates any cached HTML.

setHtmlCacheEnabled(enabled) enables or disables an instance-local cache of the most recently rendered HTML. Caching is disabled by default and is cleared when disabled or when a plugin is registered.

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

const document = new Peisar("# Hello", { fragment: true });
document.use({
  visitBlock([block]) {
    if (block.type === "Heading") return { recurse: true };
  },
});

Visitor callbacks receive a one-element tuple containing the node, so visitBlock([block]) and visitInline([inline]) are the callback forms. Visitors run when the AST or HTML is accessed. Parser hooks run before the built-in matchers; registering them reparses the original source.

PeisarAst

PeisarAst provides lower-level parsing without rendering:

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

const parsed = new PeisarAst("# Hello");
const ast = parsed.getAst();
const frontmatter = parsed.getFrontmatter();

It also provides addPlugin(plugin) and clearPlugins(). Its optional AstOptions supports gfm, kramdown, and fileName.

PeisarOptions

All properties are optional.

PropertyTypeDefaultDescription
gfmbooleantrueEnable GitHub Flavored Markdown
kramdownbooleantrueEnable Kramdown block attributes
fileNamestringundefinedAttach a file name to the parsed document
fragmentbooleanfalseRender a fragment instead of a full HTML document
charsetbooleantrueInclude <meta charset> in full-document mode
viewportbooleantrueInclude <meta name="viewport"> in full-document mode
titlestringundefinedHTML <title> in full-document mode
bodyClassstringundefinedExtra classes for <body> in full-document mode
stylestringundefinedInline CSS added to <head>

Plugin object

Each property is optional:

type Plugin = {
  visitBlock?: (args: [block: object]) => VisitorControl | void;
  visitInline?: (args: [inline: object]) => InlineVisitorControl | void;
  parseBlock?: (args: [ctx: BlockParserContext]) => BlockParseResult | void;
  parseInline?: (args: [ctx: InlineParserContext]) => InlineParseResult | void;
};

Visitor controls include insertBefore, insertAfter, replaceWith, remove, and recurse. Block parser hooks return a block and the number of lines consumed; inline hooks return an inline and the number of characters consumed. A parser hook can return undefined to decline the current input.

The generated TypeScript declarations place these classes and types under peisar; JavaScript examples can import the classes from the package root as shown above.

Frontmatter is recognized at the start of the Markdown source:

FormatOpeningClosing
YAML------
TOML++++++
JSON{ or optional ;;; followed by {Matching }; with ;;;, a closing ;;; is required

The raw property contains the text between YAML/TOML delimiters or the complete JSON object without optional ;;; delimiters. frontmatterType is one of "Yaml", "Toml", "Json", or "Empty". No YAML, TOML, or JSON parsing or validation is done. When no complete frontmatter is found, the type is "Empty" and raw is an empty string.