mdast-util-assert: Markdown Abstract Syntax Tree Assertion Utility

5.0.0 · active · verified Sun Apr 19

mdast-util-assert is a focused utility from the unified collective designed to validate the structure and properties of mdast (Markdown Abstract Syntax Tree) nodes. It provides functions to assert that a given object conforms to the expected shape of mdast nodes, including parent, literal, and void nodes. This package is particularly useful within API contexts where specific node types are expected, ensuring data integrity and preventing unexpected runtime errors. The current stable version is 5.0.0, which requires Node.js 16+ and is exclusively an ES Module (ESM). Release cadence generally follows semver, with major versions introducing breaking changes related to Node.js compatibility or module systems, while minor versions address bug fixes and documentation. Its key differentiator is its specialization for mdast nodes, building upon and re-exporting parts of the more general `unist-util-assert` for universal syntax trees.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `assert` to validate mdast nodes, showing successful and failing assertions with error handling.

import { assert } from 'mdast-util-assert'

// Assert valid mdast nodes
assert({type: 'root', children: []})
assert({type: 'break'})
assert({type: 'listItem', checked: true, children: []})

// This will throw an AssertionError due to missing 'type' property
try {
  assert({children: []})
} catch (error) {
  console.error(error.message) // AssertionError: node should have a type: `{ children: [] }`
}

// This will throw an AssertionError due to incorrect property for a 'paragraph' node
try {
  assert({type: 'paragraph', value: 'foo'})
} catch (error) {
  console.error(error.message) // AssertionError: parent should have children: `{ type: 'paragraph', value: 'foo' }`
}

view raw JSON →