micromark utility normalize identifiers (as found in references, definitions)
npx @tessl/cli install tessl/npm-micromark-util-normalize-identifier@2.0.0micromark-util-normalize-identifier is a utility package that provides identifier normalization functionality for markdown parsers. It normalizes identifiers found in references and definitions by collapsing whitespace, trimming, and performing case normalization to create canonical forms for reliable identifier matching.
npm install micromark-util-normalize-identifierimport { normalizeIdentifier } from "micromark-util-normalize-identifier";import { normalizeIdentifier } from "micromark-util-normalize-identifier";
// Basic whitespace normalization and trimming
normalizeIdentifier(' a '); // → 'A'
normalizeIdentifier('a\t\r\nb'); // → 'A B'
// Unicode case normalization
normalizeIdentifier('ТОЛПОЙ'); // → 'ТОЛПОЙ'
normalizeIdentifier('Толпой'); // → 'ТОЛПОЙ'
// Complex identifiers with mixed whitespace
normalizeIdentifier(' My Reference \n ID '); // → 'MY REFERENCE ID'Normalizes markdown identifiers to create canonical forms for reference matching. The normalization process ensures consistent identifier handling across markdown parsers by collapsing whitespace, trimming, and performing proper case conversion.
/**
* Normalize an identifier (as found in references, definitions).
*
* Collapses markdown whitespace, trim, and then lower- and uppercase.
* Some characters are considered "uppercase", such as U+03F4 (ϴ), but if their
* lowercase counterpart (U+03B8 (θ)) is uppercased will result in a different
* uppercase character (U+0398 (Θ)). So, to get a canonical form, we perform
* both lower- and uppercase. Using uppercase last makes sure keys will never
* interact with default prototypal values (such as constructor): nothing in
* the prototype of Object is uppercase.
*
* @param {string} value - Identifier to normalize
* @returns {string} Normalized identifier
*/
function normalizeIdentifier(value);Algorithm Details:
[\t\n\r ]+) with single space characters/^ | $/gUsage Examples:
import { normalizeIdentifier } from "micromark-util-normalize-identifier";
// Markdown reference normalization
const ref1 = normalizeIdentifier('[my link]'); // → '[MY LINK]'
const ref2 = normalizeIdentifier('[My Link]'); // → '[MY LINK]'
const ref3 = normalizeIdentifier('[ my link ]'); // → '[MY LINK]'
// Definition normalization
const def = normalizeIdentifier('My Definition Label'); // → 'MY DEFINITION LABEL'
// Unicode handling
const unicode1 = normalizeIdentifier('θεός'); // → 'ΘΕΟΣ'
const unicode2 = normalizeIdentifier('ΘΕΟΣ'); // → 'ΘΕΟΣ'Error Handling:
The function expects a string input. Non-string inputs will cause runtime errors during string method calls. Always ensure the input is a string:
// Safe usage
const normalized = normalizeIdentifier(String(input));