mdast HTML Comment Zone Utility
mdast-zone is a utility within the `syntax-tree` ecosystem designed to manipulate Markdown Abstract Syntax Trees (mdast) by identifying and modifying content sections defined by HTML comments. It provides a `zone` function that takes an mdast tree, a comment name (e.g., 'foo'), and a handler function. This handler receives the `start` comment, the `nodes` between the comments, and the `end` comment, allowing developers to replace or modify the content within these defined zones. The current stable version is 6.1.0, and new releases, including major versions, appear roughly annually, with minor and patch updates in between, demonstrating active maintenance. It differentiates itself from similar utilities like `mdast-util-heading-range` by using hidden HTML comments as markers for sections rather than visible headings, making it suitable for programmatic content generation or manipulation where markers should not be visible in the rendered output. This package is ESM-only and requires Node.js 16+ since v6.0.0.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to import `mdast-zone` using CommonJS `require()` syntax in an environment that enforces ESM-only.fixChange `const { zone } = require('mdast-zone');` to `import { zone } from 'mdast-zone';` and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`). -
TypeError: Cannot read properties of undefined (reading 'Root')
cause Often occurs when `unified` types are missing for plugin annotations, or when `remark` and `unified` versions are incompatible.fixEnsure `unified` is installed (`npm install unified`) and imported if you're using TypeScript annotations like `import('unified').Plugin`. -
Cannot find name 'ZoneInfo'. Did you mean 'Info'?
cause Using the old type name `ZoneInfo` after upgrading to `mdast-zone` v6.0.0 or later.fixReplace `ZoneInfo` with `Info` in your TypeScript code.
Warnings
- breaking mdast-zone migrated to being an ESM-only package. Attempting to `require` it will result in an `ERR_REQUIRE_ESM` error.
- breaking Version 6.0.0 increased the minimum required Node.js version to 16. Running on older Node.js versions will cause compatibility issues.
- breaking The `ZoneInfo` TypeScript type was renamed to `Info` in version 6.0.0. Code referencing `ZoneInfo` will no longer compile.
- breaking Version 6.0.0 changed to use an `export` map, which might affect some build systems or specific module resolution strategies if relying on undocumented or private APIs.
- breaking Version 4.0.0 updated its dependency on `unist-util-visit`, which could potentially introduce breaking changes for TypeScript users if their types were tightly coupled or if they relied on specific `unist-util-visit` type definitions.
Install
-
npm install mdast-zone -
yarn add mdast-zone -
pnpm add mdast-zone
Imports
- zone
const { zone } = require('mdast-zone');import { zone } from 'mdast-zone'; - Handler
import { Handler } from 'mdast-zone';import type { Handler } from 'mdast-zone'; - Info
import type { ZoneInfo } from 'mdast-zone';import type { Info } from 'mdast-zone';
Quickstart
import {zone} from 'mdast-zone';
import {remark} from 'remark';
import {unified} from 'unified'; // Required for unified.Plugin types
async function processMarkdown() {
const markdownInput = `<!--foo start-->\n\nFoo\n\n<!--foo end-->`;
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
function myPluginThatReplacesFoo() {
return function (tree) {
zone(tree, 'foo', function (start, nodes, end) {
console.log(`Found zone 'foo' with content: '${nodes.map(n => 'value' in n ? n.value : '').join('')}'`);
return [
start,
{type: 'paragraph', children: [{type: 'text', value: 'Bar.'}]},
end
];
});
};
}
const file = await remark()
.use(myPluginThatReplacesFoo)
.process(markdownInput);
console.log(String(file));
}
processMarkdown();