@unifast/core

TypeScript type definitions, HAST utilities, and error classes shared across unifast runtime packages

Overview

@unifast/core provides shared TypeScript type definitions for compile options, results, HAST (HTML Abstract Syntax Tree) nodes, the plugin interface, and error classes. It is a dependency of runtime packages like @unifast/node — you typically do not need to install it directly.

For the compiler and built-in plugins, use @unifast/node.

Installation

Type Definitions

CompileOptions

Configuration for the compile() function.

PropertyTypeDefaultDescription
inputKind"md" | "mdx""md"Input format
outputKind"html" | "hast" | "mdast" | "mdxJs""html"Output format
gfmobject-GitHub Flavored Markdown settings
gfm.tablesboolean-Enable GFM tables
gfm.taskListboolean-Enable task list checkboxes
gfm.strikethroughboolean-Enable ~~strikethrough~~
gfm.footnotesboolean-Enable footnotes
gfm.autolinkboolean-Enable URL auto-linking
frontmatterobject-Frontmatter parsing settings
frontmatter.yamlboolean-Parse YAML frontmatter
frontmatter.tomlboolean-Parse TOML frontmatter
frontmatter.jsonboolean-Parse JSON frontmatter
rawHtml"disallow" | "allowDangerous" | "parseAndSanitize"-How to handle raw HTML in source
sanitizeobject-HTML sanitization settings
sanitize.enabledboolean-Enable sanitization
sanitize.schemaSanitizeSchema-Custom sanitization rules
highlightobject-Syntax highlighting settings
highlight.enabledboolean-Enable syntax highlighting
highlight.engine"none" | "syntect" | "treeSitter"-Highlighting engine
slugobject-Heading slug generation settings
slug.mode"github" | "unicode"-Slug generation algorithm
tocobject-Table of contents settings
toc.enabledboolean-Enable TOC extraction
toc.maxDepthnumber-Maximum heading depth to include
diagnosticsobject-Diagnostic output settings
diagnostics.format"compact" | "verbose"-Diagnostic format
cacheobject-Caching settings
cache.enabledboolean-Enable result caching
cache.dirstring-Cache directory path
pluginsUnifastPlugin[][]Array of plugins to apply

CompileResult

Returned by the compile() function.

PropertyTypeDescription
outputstring | objectCompiled output (HTML string, HAST JSON, or MDAST JSON depending on outputKind)
frontmatterRecord<string, unknown>Parsed frontmatter metadata
diagnosticsDiagnostic[]Array of warnings and errors
stats{ parseMs, transformMs, emitMs }Timing breakdown in milliseconds
tocTocEntry[]Extracted table of contents entries

Diagnostic

PropertyTypeDescription
level"error" | "warn"Severity level
messagestringHuman-readable message
startnumber | undefinedStart byte offset in source
endnumber | undefinedEnd byte offset in source
linenumber | undefinedLine number (1-based)
columnnumber | undefinedColumn number (1-based)

TocEntry

A single heading extracted for the table of contents.

PropertyTypeDescription
depthnumberHeading level (1-6)
textstringPlain text content of the heading
slugstringGenerated slug for anchor linking

SanitizeSchema

Custom sanitization rules.

PropertyTypeDescription
allowedTagsstring[]HTML tags to allow
allowedAttributesRecord<string, string[]>Allowed attributes per tag
allowedProtocolsRecord<string, string[]>Allowed URL protocols per attribute

UnifastPlugin

The plugin interface for extending unifast.

PropertyTypeDescription
namestringUnique plugin name
options?Partial<CompileOptions>Options to merge into compile options
hastTransform?(hast: HastRoot) => HastRootTransform the HAST tree after compilation

HAST Node Types

Typetype fieldPropertiesDescription
HastRoot"root"children: HastNode[]Root node of the tree
HastElement"element"tagName, properties, childrenAn HTML element
HastText"text"value: stringA text node
HastRaw"raw"value: stringRaw HTML passthrough
HastComment"comment"value: stringAn HTML comment
HastDoctype"doctype"-A <!DOCTYPE html> node

HastNode is the union type: HastRoot | HastElement | HastText | HastRaw | HastComment | HastDoctype

Error Classes

ClassExtendsPropertiesDescription
UnifastErrorErrorcode?: string, span?: { start, end }Base error class for all unifast errors
ParseErrorUnifastErrorcode: "PARSE_ERROR"Thrown when Markdown/MDX input cannot be parsed
CompileErrorUnifastErrorcode: "COMPILE_ERROR"Thrown when compilation fails after parsing
import { UnifastError, ParseError, CompileError } from "@unifast/core";

try {
  // ... compile something
} catch (err) {
  if (err instanceof ParseError) {
    console.error(`Parse error at ${err.span?.start}: ${err.message}`);
  } else if (err instanceof CompileError) {
    console.error(`Compile error: ${err.message}`);
  }
}

Exports Summary

ExportKindDescription
CompileOptionstypeCompilation configuration
CompileResulttypeCompilation result
TocEntrytypeTable of contents entry
SanitizeSchematypeSanitization rules
UnifastPlugintypePlugin interface
HastNodetypeUnion of all HAST node types
HastRoottypeHAST root node
HastElementtypeHAST element node
HastTexttypeHAST text node
HastRawtypeHAST raw HTML node
HastCommenttypeHAST comment node
HastDoctypetypeHAST doctype node
hastToHtmlfunctionHAST-to-HTML serializer
escapeHtmlfunctionEscape HTML special characters (&, <, >, ")
extractLangfunctionExtract language identifier from a HAST <code> element
extractTextfunctionExtract plain text content from a HAST node
findCodeChildfunctionFind the <code> child element inside a <pre> element
visitHastfunctionVisitor pattern utility for walking HAST trees
UnifastErrorclassBase error class
ParseErrorclassParse error class
CompileErrorclassCompile error class