Micromark HTML Character Encoder

2.0.1 · active · verified Sun Apr 19

micromark-util-encode is a focused utility package within the micromark ecosystem, designed specifically for encoding dangerous HTML characters to ensure text is safe for embedding in HTML documents. Its current stable version is 2.0.1, part of the 2.x release line which maintains compatibility with Node.js 16 and higher. As a component of the unified collective, it follows a pragmatic release cadence often aligned with major micromark and Node.js version updates. This package differentiates itself by providing a robust, performant, and standards-compliant algorithm for HTML escaping, specifically tailored for the needs of markdown processors and related text transformation tools, where correctness and minimal overhead are critical. It is fully typed with TypeScript, ensuring good developer experience for static analysis and type safety.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `encode` function to escape common HTML special characters for safe embedding.

import { encode } from 'micromark-util-encode';

const unsafeHtml = '<script>alert("XSS")</script> & <img src="x" onerror="alert(\'Error\')">';
const safeHtml = encode(unsafeHtml);

console.log('Original:', unsafeHtml);
console.log('Encoded:', safeHtml);
// Expected output: Original: <script>alert("XSS")</script> & <img src="x" onerror="alert(\'Error\')">
// Expected output: Encoded: &lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt; &amp; &lt;img src=&quot;x&quot; onerror=&quot;alert(&#x27;Error&#x27;)&quot;&gt;

view raw JSON →