mdast-util-compact is a specialized utility within the unified (syntax-tree) ecosystem designed to normalize mdast (Markdown Abstract Syntax Tree) trees by merging adjacent text nodes and collapsing blockquotes. Its primary use case is to clean up an mdast tree after programmatically modifying it, making the tree structure more consistent with how it would be generated by a parser. The current stable version is 5.0.0. The package follows semantic versioning with major releases introducing breaking changes, often related to Node.js version support or ESM migration, as well as updates to underlying `mdast` type definitions. A key differentiator is its explicit recommendation against frequent use, suggesting developers should ideally maintain clean trees themselves rather than relying on this utility post-hoc. It is an ESM-only package and fully typed with TypeScript.
npm install mdast-util-compactVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `compact` to merge adjacent text nodes within a paragraph and combine consecutive blockquote nodes in an mdast tree, modifying the tree in-place.
Ensure your project is configured for ESM, use 'import' statements, and update your 'package.json' with '"type": "module"' if necessary. Avoid 'require()'.
Upgrade your Node.js environment to version 16 or newer. For projects constrained to older Node.js versions, use `mdast-util-compact@^4`.
Refactor your code to acknowledge that `compact` modifies the tree in-place and does not return a value. Any subsequent operations should use the original `tree` reference, as it will have been mutated.
Thoroughly review your mdast tree processing logic if upgrading from versions prior to 3.0.0, especially if your application relies on precise node structures for complex or less common Markdown syntax.
Consider refactoring your tree manipulation logic to prevent the creation of adjacent text nodes or unmerged blockquotes in the first place, if possible. Use this utility primarily for final cleanup or specific normalization needs.
Ensure all `@types/mdast` dependencies across your project are updated to be compatible with the version required by `mdast-util-compact@5.0.0` to avoid TypeScript compilation errors.
Change `const { compact } = require('mdast-util-compact')` to `import { compact } from 'mdast-util-compact'`. Ensure your project's `package.json` includes `"type": "module"` or your file uses the `.mjs` extension.Use the correct named import: `import { compact } from 'mdast-util-compact'`.Rename your file to `.mjs` or add `"type": "module"` to your `package.json`. If you must use CommonJS, you will need to stick to `mdast-util-compact@^3` or earlier, as v4+ is ESM-only.
Upgrade your Node.js runtime to version 16 or newer. If upgrading is not an option, downgrade to `mdast-util-compact@^4` which supports older Node.js versions.
No dependency data recorded yet.