{"id":11312,"library":"micromark-util-decode-string","title":"Micromark String Decoder Utility","description":"micromark-util-decode-string is a low-level utility within the unifiedjs `micromark` ecosystem, specifically designed to process and decode character escapes and character references found in Markdown strings. It provides a single, focused function, `decodeString`, which is crucial for correctly interpreting content in areas like fenced code info strings, link destinations, labels, and titles according to the CommonMark specification. The current stable version for this utility is `2.0.1`, which is compatible with `micromark@3` and Node.js 16 and higher. While part of a larger monorepo, this package maintains its own versioning tied to its API stability. Its release cadence generally aligns with major `micromark` releases, often involving updates to drop support for unmaintained Node.js versions. As an ESM-only package, it adheres to modern JavaScript module standards. Its key differentiator is its precise adherence to Markdown specification for string decoding, making it an essential building block for custom `micromark` extensions or other Markdown processing tools.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/micromark/micromark#main","tags":["javascript","micromark","util","utility","decode","character","reference","escape","string","typescript"],"install":[{"cmd":"npm install micromark-util-decode-string","lang":"bash","label":"npm"},{"cmd":"yarn add micromark-util-decode-string","lang":"bash","label":"yarn"},{"cmd":"pnpm add micromark-util-decode-string","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is ESM-only. CommonJS `require` is not supported. Use dynamic import for compatibility if needed.","wrong":"const { decodeString } = require('micromark-util-decode-string');","symbol":"decodeString","correct":"import { decodeString } from 'micromark-util-decode-string';"},{"note":"For Deno or browser environments, use an ESM CDN like `esm.sh` and specify the version, e.g., `@2` for v2.x.","wrong":"import { decodeString } from 'micromark-util-decode-string';","symbol":"decodeString","correct":"import { decodeString } from 'https://esm.sh/micromark-util-decode-string@2'"},{"note":"When only importing the type of `decodeString`, use `import type` for better tree-shaking and clarity in TypeScript.","symbol":"decodeString (Type)","correct":"import type { decodeString } from 'micromark-util-decode-string';"}],"quickstart":{"code":"import { decodeString } from 'micromark-util-decode-string';\n\n// Example 1: Decoding HTML character references like `&amp;`\nconsole.log('Decoding HTML character references:');\nconst htmlRefString = 'This is a string with &amp; entities &copy;.';\nconsole.log(`Original: \"${htmlRefString}\"`);\nconsole.log(`Decoded:  \"${decodeString(htmlRefString)}\"`);\n// Expected output: Original: \"This is a string with &amp; entities &copy;.\"\n//                  Decoded:  \"This is a string with & entities ©.\"\n\nconsole.log('\\n---');\n\n// Example 2: Decoding numeric character references (decimal and hexadecimal)\nconsole.log('Decoding numeric character references:');\nconst numericRefString = 'Hello &#x20AC; world &#9786;!';\nconsole.log(`Original: \"${numericRefString}\"`);\nconsole.log(`Decoded:  \"${decodeString(numericRefString)}\"`);\n// Expected output: Original: \"Hello &#x20AC; world &#9786;!\"\n//                  Decoded:  \"Hello € world ☺!\"\n\nconsole.log('\\n---');\n\n// Example 3: Decoding Markdown character escapes (e.g., backslash escapes)\nconsole.log('Decoding Markdown character escapes:');\nconst escapeString = 'Special characters like \\\\*asterisks\\\\* and \\\\_underscores\\\\_ are unescaped.';\nconsole.log(`Original: \"${escapeString}\"`);\nconsole.log(`Decoded:  \"${decodeString(escapeString)}\"`);\n// Expected output: Original: \"Special characters like \\*asterisks\\* and \\_underscores\\_ are unescaped.\"\n//                  Decoded:  \"Special characters like *asterisks* and _underscores_ are unescaped.\"\n\nconsole.log('\\n---');\n\n// Example 4: Mixed string with common markdown context, simulating internal micromark use\nconsole.log('Mixed context decoding:');\nconst mixedString = 'A link title with some \\\\&amp; escaped characters &mdash; \"Example\"';\nconsole.log(`Original: \"${mixedString}\"`);\nconsole.log(`Decoded:  \"${decodeString(mixedString)}\"`);\n// Expected output: Original: \"A link title with some \\&amp; escaped characters &mdash; \"Example\"\"\n//                  Decoded:  \"A link title with some & escaped characters — \"Example\"\"\n\n// This utility is typically used internally by micromark or in custom micromark extensions\n// for precise Markdown string processing.\n","lang":"typescript","description":"This quickstart demonstrates the `decodeString` function's capability to correctly interpret and resolve various character escapes and references found within Markdown strings, including HTML entities, numeric references, and backslash escapes. It covers common use cases for this low-level utility, which is fundamental to `micromark`'s parsing process."},"warnings":[{"fix":"Ensure your Node.js environment is updated to a currently maintained LTS version (e.g., Node.js 16+, 18+, 20+). Refer to the Node.js release schedule for support timelines.","message":"Major versions of `micromark-util-decode-string` may drop support for unmaintained Node.js versions. Version `2.x` is compatible with Node.js 16 and higher.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Migrate your project to use ECMAScript Modules (`import` / `export`) or use dynamic `import()` if you need to load ESM from a CommonJS context. Ensure your `package.json` specifies `\"type\": \"module\"` if running in Node.js.","message":"This package is ESM-only and does not provide a CommonJS export. Attempting to `require()` it will result in a module loading error.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement from `const { decodeString } = require('micromark-util-decode-string');` to `import { decodeString } from 'micromark-util-decode-string';`. Ensure your `package.json` is configured for ESM, or use a bundler.","cause":"Attempting to import `micromark-util-decode-string` using CommonJS `require()` syntax in a Node.js environment.","error":"ERR_REQUIRE_ESM: Must use import to load ES Module: .../node_modules/micromark-util-decode-string/index.js"},{"fix":"Verify that you are using a named import: `import { decodeString } from 'micromark-util-decode-string';`. Avoid default imports or incorrect destructuring.","cause":"This error often indicates that the module was loaded incorrectly or the named export `decodeString` was not found, possibly due to a CommonJS-style default import `import decodeString from '...'`. The package provides only named exports.","error":"TypeError: Cannot read properties of undefined (reading 'decodeString')"}],"ecosystem":"npm"}