mdast Paragraph Squeezer
mdast-squeeze-paragraphs is a focused utility within the unified ecosystem, specifically designed to remove empty paragraphs from an mdast (Markdown Abstract Syntax Tree) tree. It identifies and eliminates paragraphs that contain no non-whitespace characters, often a result of previous tree transformations. The current stable version is 6.0.0. Releases are typically tied to Node.js LTS cycles, with new major versions dropping support for unmaintained Node.js versions. A key differentiator and notable recommendation from its maintainers is that developers should 'probably never!' use this package directly, encouraging users to clean their trees themselves or use the higher-level `remark-squeeze-paragraphs` plugin instead. This package is primarily intended for authors of other mdast utilities or plugins who need fine-grained control over tree sanitation at a low level.
Common errors
-
TypeError: require is not a function
cause Attempting to use CommonJS `require()` with an ESM-only package.fixConvert your import statements to ESM `import { squeezeParagraphs } from 'mdast-squeeze-paragraphs';` and ensure your project is configured for ESM. -
Error: 'mdast-squeeze-paragraphs' requires Node.js 16 or later.
cause Running the package on an unsupported Node.js version (older than 16).fixUpgrade your Node.js environment to version 16 or higher to meet the package's minimum requirement. -
TypeError: (0 , mdast_squeeze_paragraphs_1.squeezeParagraphs) is not a function
cause Incorrect module resolution or transpilation in TypeScript projects targeting CommonJS that try to import an ESM package.fixSet your TypeScript `tsconfig.json` `compilerOptions.module` to `ESNext` or `NodeNext` and `compilerOptions.moduleResolution` to `NodeNext` for better ESM interoperability. Alternatively, use dynamic `import()` for ESM packages in CJS projects. -
TypeError: squeezeParagraphs is not a function (when using a wildcard or default import)
cause Attempting to call `mod.squeezeParagraphs` after `import mod from 'mdast-squeeze-paragraphs'` or when `squeezeParagraphs` is not correctly destructured.fixEnsure you are using a named import correctly: `import { squeezeParagraphs } from 'mdast-squeeze-paragraphs';`.
Warnings
- breaking Version 6.0.0 changes Node.js compatibility, now requiring Node.js 16 or later.
- breaking The package transitioned to ESM-only starting from version 5.0.0, removing CommonJS support.
- breaking Version 6.0.0 introduced the use of an `export` map, which might affect module resolution in some environments or older tooling.
- breaking The `squeezeParagraphs` function in version 6.0.0 now explicitly returns `undefined`.
- breaking Version 6.0.0 updates its dependency on `@types/mdast`, which might require you to update your own `@types/mdast` version if you encounter type conflicts.
- breaking Version 4.0.0 updated `unist-util-remove`, which could potentially introduce breaking changes if your project or its dependents rely on specific internal types or behaviors of `unist-util-remove`.
- gotcha The maintainers advise against direct use of `mdast-squeeze-paragraphs` in most application code, recommending higher-level plugins like `remark-squeeze-paragraphs` or manual tree cleaning instead.
Install
-
npm install mdast-squeeze-paragraphs -
yarn add mdast-squeeze-paragraphs -
pnpm add mdast-squeeze-paragraphs
Imports
- squeezeParagraphs
const squeezeParagraphs = require('mdast-squeeze-paragraphs')import { squeezeParagraphs } from 'mdast-squeeze-paragraphs' - squeezeParagraphs
import { squeezeParagraphs } from 'https://esm.sh/mdast-squeeze-paragraphs@6' - Wildcard import
import mod from 'mdast-squeeze-paragraphs'
import * as mod from 'mdast-squeeze-paragraphs'
Quickstart
import {squeezeParagraphs} from 'mdast-squeeze-paragraphs';
import {u} from 'unist-builder';
const tree = u('root', [
u('paragraph', []),
u('paragraph', [u('text', 'Alpha')]),
u('paragraph', [u('text', ' ')])
]);
squeezeParagraphs(tree);
console.dir(tree, {depth: null});
// Expected output:
// { type: 'root',
// children:
// [ { type: 'paragraph',
// children: [ { type: 'text', value: 'Alpha' } ] } ] }