hast-util-minify-whitespace: HTML Whitespace Minification

1.0.1 · active · verified Sun Apr 19

hast-util-minify-whitespace is a focused utility within the unified ecosystem, specifically designed to optimize HTML by minifying whitespace between elements in a `hast` (HTML Abstract Syntax Tree). It reduces multiple consecutive whitespace characters to a single space, or to a newline if configured, thereby improving the size of HTML fragments. The package is currently at version 1.0.1, with releases coordinated within the larger `rehypejs` collective, which emphasizes modern JavaScript practices. Key differentiators include its robust integration into the pluggable unified processing pipeline, full TypeScript support, and its commitment to WHATWG HTML parsing standards. It operates as an ESM-only module and requires Node.js 16 or newer, aligning with contemporary Node.js environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use `minifyWhitespace` to process a `hast` tree created with `hastscript`, showing the transformation of internal whitespace.

import { h } from 'hastscript';
import { minifyWhitespace } from 'hast-util-minify-whitespace';

const tree = h('p', [
  '  ',
  h('strong', 'foo'),
  '  ',
  h('em', 'bar'),
  '  ',
  h('meta', { itemProp: true }),
  '  '
]);

console.log('Original tree:\n', JSON.stringify(tree, null, 2));

minifyWhitespace(tree);

console.log('\nMinified tree:\n', JSON.stringify(tree, null, 2));

view raw JSON →