Registry / serialization / micromark-util-encode

micromark-util-encode

JSON →
library2.0.1jsnpmunverified

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.

npm install micromark-util-encode
INSTALL
IMPORT
SIG · MICROMARK-UTIL-ENC
M
micromark-util-encode
serializationjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

encode
import { encode } from 'micromark-util-encode'
const { encode } = require('micromark-util-encode')
This package is ESM-only. CommonJS `require()` is not supported.
encode (default)
import { encode } from 'micromark-util-encode'
import encode from 'micromark-util-encode'
There is no default export; the `encode` function must be imported as a named export.
Types
import type { Value } from 'micromark-util-types'
While this package is fully typed, it exports no additional types directly. Core types for `micromark` are often found in `micromark-util-types` or inferred from usage.

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;
Debug
Known issues
breakingVersion 2.x of `micromark-util-encode` is ESM-only. Direct `require()` calls for this package will result in an error.
fix
Migrate your project to use ES modules (`import`) or ensure you're using a bundler/transpiler that handles ESM correctly. For Node.js, ensure your package.json specifies `"type": "module"` or use `.mjs` files.
affects: >=2.0.0
breakingVersion 2.x of `micromark-util-encode` officially supports Node.js 16 and higher. Older Node.js versions are not guaranteed to work and may encounter runtime errors.
fix
Upgrade your Node.js environment to version 16 or newer. For projects requiring older Node.js compatibility, consider pinning to `micromark-util-encode@1.x` if available and compatible with your `micromark` version.
affects: >=2.0.0
gotchaThe `encode` function specifically targets HTML characters (<, >, &, ", '). It does not perform full URL encoding or sanitize other potentially unsafe content like CSS injections or script execution from attributes (e.g., `onerror`).
fix
Always understand the specific scope of encoding. For broader security, combine with other sanitization libraries or consider a full templating engine that handles auto-escaping.
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to `require()` an ESM-only package in a CommonJS context.
fix
Change `const { encode } = require('micromark-util-encode')` to `import { encode } from 'micromark-util-encode'` and ensure your project is configured for ES modules.
TypeError: encode is not a function
Trying to import `encode` as a default import when it is a named export.
fix
Ensure you are using named imports: `import { encode } from 'micromark-util-encode'`.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
micromark-util-encode — npm install micromark-util-encode · libregistry