Registry / devops / unist-util-add

unist-util-add

JSON →
library1.2.0jsnpmunverified

unist-util-add is a utility for the unified ecosystem that simplifies adding and managing extensions for toMarkdown, micromark, and fromMarkdown to data objects. Version 1.2.0, released in 2023, is the current stable version. It provides a single add() function that handles appending extensions to arrays, and it ships with TypeScript types. Unlike manually pushing to arrays, it handles initialization and validation. It requires Node.js >=18 and works with unified, mdast-util-to-markdown, micromark-util-types, and mdast-util-from-markdown.

npm install unist-util-add
INSTALL
IMPORT
SIG · UNIST-UTIL-ADD
U
unist-util-add
devopsjavascriptv1.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

add
import { add } from 'unist-util-add'
const add = require('unist-util-add').add
Package is ESM-only since Node >=18; use import syntax.
add (default import)
import add from 'unist-util-add'
import { add } from 'unist-util-add'
This package exports both named and default exports; the default export is also the add function.
Types
import { add, type AddFunction } from 'unist-util-add'
import { AddFunction } from 'unist-util-add' (when using at runtime)
TypeScript type imports should use import type to avoid runtime errors.

Demonstrates adding toMarkdown and micromark extensions to a unified data object using the add function.

import { add } from 'unist-util-add'; import type { Data } from 'unified'; import type { Options as ToMarkdownExtension } from 'mdast-util-to-markdown'; import type { Extension as MicromarkExtension } from 'micromark-util-types'; const data: Data = {}; const toMarkdownExt: ToMarkdownExtension = { handlers: { emphasis(node, _, state) { state.write('*' + state.all(node).join('') + '*'); } } }; const micromarkExt: MicromarkExtension = { text: { '123': { tokenize(effects, ok, nok) { const self = this; return start; function start(code) { effects.enter('number'); effects.consume(code); return more; } function more(code) { if (code !== null && code >= 48 && code <= 57) { effects.consume(code); return more; } effects.exit('number'); return ok(code); } } } } }; add(data, 'toMarkdownExtensions', toMarkdownExt); add(data, 'micromarkExtensions', micromarkExt); console.log(JSON.stringify(data));
Debug
Known issues
breakingThe package requires Node.js >=18.
fix
Upgrade Node.js to version 18 or later.
affects: >=1.0.0
gotchaThe add function mutates the data object; it does not return a new object.
fix
Ensure you are not relying on immutability; pass a new object if needed.
affects: >=1.0.0
gotchaFor `fromMarkdownExtensions`, the parameter can be a single extension or an array of extensions; other fields only accept a single extension per call.
fix
Use add(data, 'fromMarkdownExtensions', [ext1, ext2]) to add multiple at once for fromMarkdownExtensions.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: add is not a function
Trying to import using CommonJS require on an ESM-only package.
fix
Use import { add } from 'unist-util-add' or ensure your project is configured for ESM.
Cannot find module 'unist-util-add' or its corresponding type declarations.
TypeScript cannot find types because the package is not installed or types are not recognized.
fix
Run npm install unist-util-add and ensure tsconfig.json includes 'node_modules/unist-util-add' or has 'moduleResolution': 'node'.
Argument of type 'X' is not assignable to parameter of type 'ToMarkdownExtension | MicromarkExtension | FromMarkdownExtension | FromMarkdownExtension[]'.
Passing an extension of the wrong type to a field that expects a different type.
fix
Ensure the extension object matches the expected type for the field: toMarkdownExtensions, micromarkExtensions, or fromMarkdownExtensions.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
unist-util-add — npm install unist-util-add · libregistry