HAST Phrasing Content Utility
hast-util-phrasing is a niche utility within the unified (remark/rehype) ecosystem, specifically designed to determine if a given HAST (Hypertext Abstract Syntax Tree) node represents 'phrasing content' according to HTML specifications. This classification is crucial for advanced HTML manipulation and validation scenarios. The package is currently at version 3.0.1, indicating active maintenance and regular, albeit minor, updates since its major release. It adheres to the unified collective's release cadence, typically aligning major versions with Node.js LTS support lifecycles. Its key differentiator is its precise adherence to HTML phrasing content rules, making it invaluable for tools that need to strictly adhere to HTML parsing and serialization standards, particularly in content transformation pipelines.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... hast-util-phrasing.js not supported.
cause Attempting to use `require()` for this package, which is ESM-only since v2.0.0.fixUse ESM import syntax: `import { phrasing } from 'hast-util-phrasing'`. -
TypeError: Cannot read properties of undefined (reading 'type') at phrasing (hast-util-phrasing.js:...) or similar 'value is not a HAST Node' errors.
cause Passing `null`, `undefined`, or a non-object to `phrasing` after v3.0.0, which expects a valid HAST `Node`.fixEnsure the input to `phrasing` is always a valid HAST node object. Add checks like `if (value && typeof value === 'object' && 'type' in value) { phrasing(value); }` if inputs are untrusted.
Warnings
- breaking Version 3.0.0 removed support for non-nodes as input. The `phrasing` function now strictly expects a valid HAST `Node` object.
- breaking Version 3.0.0 updated `@types/hast` and other internal utilities. If you are using TypeScript, you might need to update your `@types/hast` dependency to match.
- breaking Version 3.0.0 changed the package to require Node.js 16 or higher. Older Node.js versions are no longer supported.
- breaking The package now uses `exports` in `package.json` since v3.0.0. This enforces strict import paths and removes access to private internal APIs. Direct imports to subpaths not explicitly exported will fail.
- breaking The package migrated to ESM (ES Module) only starting with version 2.0.0. CommonJS `require()` statements will no longer work.
Install
-
npm install hast-util-phrasing -
yarn add hast-util-phrasing -
pnpm add hast-util-phrasing
Imports
- phrasing
const phrasing = require('hast-util-phrasing')import { phrasing } from 'hast-util-phrasing' - phrasing
import phrasing from 'https://esm.sh/hast-util-phrasing@3'
import { phrasing } from 'https://esm.sh/hast-util-phrasing@3'
Quickstart
import { phrasing } from 'hast-util-phrasing';
// Example 1: A div element, which is typically flow content and not phrasing.
const divNode = {
type: 'element',
tagName: 'div',
children: [{ type: 'text', value: 'Hello' }]
};
console.log('Is <div> phrasing content?', phrasing(divNode));
// Expected: false
// Example 2: A meta element with 'itemProp', which can be phrasing content.
const metaItemPropNode = {
type: 'element',
tagName: 'meta',
properties: { itemProp: 'description' },
children: []
};
console.log('Is <meta itemprop> phrasing content?', phrasing(metaItemPropNode));
// Expected: true
// Example 3: A meta element with 'charset', which is not phrasing content.
const metaCharsetNode = {
type: 'element',
tagName: 'meta',
properties: { charSet: 'utf8' },
children: []
};
console.log('Is <meta charset> phrasing content?', phrasing(metaCharsetNode));
// Expected: false
// Example 4: A text node, which is always phrasing content.
const textNode = { type: 'text', value: 'Some text' };
console.log('Is text node phrasing content?', phrasing(textNode));
// Expected: true