commentRemoval()

Remove HTML comments from the output.

import { commentRemoval } from "@unifast/node";

Signature

function commentRemoval(): UnifastPlugin

Parameters

None.

Usage

import { compile, commentRemoval } from "@unifast/node";

const md = `# Hello

<!-- This comment will be removed -->

Some content here.`;

const result = compile(md, {
  plugins: [commentRemoval()],
});
// The HTML comment is stripped from the output

Examples

Basic comment removal

All HTML comment nodes (<!-- ... -->) are stripped from the output tree, including comments nested inside block elements like blockquotes:

import { compile, commentRemoval } from "@unifast/node";

const md = `First paragraph.

<!-- TODO: add more content -->

Second paragraph.`;

const result = compile(md, { plugins: [commentRemoval()] });
console.log(result.output);
// <p>First paragraph.</p>
// <p>Second paragraph.</p>

Non-comment HTML is preserved

Only comment nodes are removed. Other inline HTML is left untouched:

import { compile, commentRemoval } from "@unifast/node";

const md = `<!-- hidden -->

<div class="custom">Visible content</div>`;

const result = compile(md, { plugins: [commentRemoval()] });
console.log(result.output);
// <div class="custom">Visible content</div>