mdast Phrasing Content Utility
mdast-util-phrasing is a specialized utility within the unified (syntax-tree) ecosystem designed to accurately identify if a given mdast (Markdown Abstract Syntax Tree) node constitutes "phrasing content." This includes elements like text, emphasis, strong, links, and code spans, but notably excludes `html` nodes due to their dual nature as both phrasing and flow content. The package is currently stable at version 4.1.0 and is actively maintained by the unified collective, with major releases typically aligning with Node.js LTS version updates or significant architectural shifts, such as the transition to ESM-only in version 3.0.0. Its core functionality relies on `unist-util-is` for robust node testing, making it a foundational tool for building other mdast utilities that need to differentiate between inline and block-level content in Markdown.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` to import `mdast-util-phrasing` after version 3.0.0, which is an ESM-only package.fixChange your import statement to `import { phrasing } from 'mdast-util-phrasing'` and ensure your environment supports ESM (e.g., `type: "module"` in `package.json` for Node.js). -
TypeError: mdast_util_phrasing_1.phrasing is not a function
cause Incorrectly importing `phrasing` as a default export, when it is a named export.fixUse a named import: `import { phrasing } from 'mdast-util-phrasing';` -
Module '"mdast-util-phrasing"' has no exported member 'phrasing'.
cause This TypeScript error often occurs due to mismatched `@types/mdast` versions or using an unsupported Node.js version (pre-16) with `mdast-util-phrasing@4+`.fixEnsure your Node.js version is 16 or newer. Update `@types/mdast` and `mdast-util-phrasing` to their latest compatible versions.
Warnings
- breaking Version 4.0.0 changed to require Node.js 16 or newer and utilizes an `export` map. Older Node.js versions are no longer supported.
- breaking Version 3.0.0 switched the package to be ESM-only. CommonJS `require()` statements will no longer work.
- breaking Version 2.0.0 updated its dependency on `unist-util-is` which could be a breaking change, particularly for TypeScript users relying on specific type definitions from `unist-util-is`.
- gotcha The `phrasing` function explicitly excludes `html` nodes from its check, returning `false` for them. This is because HTML can represent both phrasing and flow content, and the utility takes a conservative approach.
Install
-
npm install mdast-util-phrasing -
yarn add mdast-util-phrasing -
pnpm add mdast-util-phrasing
Imports
- phrasing
const phrasing = require('mdast-util-phrasing')import { phrasing } from 'mdast-util-phrasing' - phrasing
import phrasing from 'mdast-util-phrasing'
import { phrasing } from 'mdast-util-phrasing' - Node
import type { Node } from 'mdast'
Quickstart
import { phrasing } from 'mdast-util-phrasing';
import { paragraph, strong, text } from 'mdast-builder';
// Example 1: A paragraph is not phrasing content
const paragraphNode = paragraph([text('Alpha')]);
console.log(`Is paragraph node phrasing content? ${phrasing(paragraphNode)}`);
// Example 2: A strong node (inline) is phrasing content
const strongNode = strong([text('Delta')]);
console.log(`Is strong node phrasing content? ${phrasing(strongNode)}`);
// Example 3: An HTML node is explicitly excluded and returns false
const htmlNode = { type: 'html', value: '<span>Hello</span>' };
console.log(`Is HTML node phrasing content? ${phrasing(htmlNode)}`);
// Output:
// Is paragraph node phrasing content? false
// Is strong node phrasing content? true
// Is HTML node phrasing content? false