From 02c73e8e659d369f4baba41903be3ddde128f15c Mon Sep 17 00:00:00 2001 From: Enes Uygur <54417817+Enesuygurs@users.noreply.github.com> Date: Mon, 2 Nov 2026 17:53:31 +0300 Subject: [PATCH] fix: collapse internal whitespace when normalizing link labels (#4157) Reference definitions collapsed internal whitespace in their labels before storing them, but the reflink masking in the lexer looked labels up with normalizeLabel alone. A reference whose label held a line break or several spaces was not masked, so emphasis delimiters inside it paired with ones outside the link, and shortcut references lost the link entirely. Collapse the whitespace in normalizeLabel itself, so the definition, the reference and the masking all use the same key. --- src/Tokenizer.ts | 5 ++--- src/helpers.ts | 7 ++++--- test/specs/new/em_and_reflinks_whitespace.html | 5 +++++ test/specs/new/em_and_reflinks_whitespace.md | 13 +++++++++++++ 4 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 test/specs/new/em_and_reflinks_whitespace.html create mode 100644 test/specs/new/em_and_reflinks_whitespace.md diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index 6deb91f9..3ae62ad8 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -614,7 +614,7 @@ export class _Tokenizer { return; } - const tag = normalizeLabel(cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' '); + const tag = normalizeLabel(cap[1]); const href = cap[2] ? cap[2].replace(this.rules.other.hrefBrackets, '$1').replace(this.rules.inline.anyPunctuation, '$1') : ''; const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline.anyPunctuation, '$1') : cap[3]; return { @@ -857,8 +857,7 @@ export class _Tokenizer { return; } - const linkString = (cap[2] || cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' '); - const link = links[normalizeLabel(linkString)]; + const link = links[normalizeLabel(cap[2] || cap[1])]; if (!link) { const text = cap[0].charAt(0); return { diff --git a/src/helpers.ts b/src/helpers.ts index 49b38693..68105ca8 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -155,9 +155,10 @@ export function trimTrailingBlankLines(str: string) { */ export function normalizeLabel(label: string) { // The spec also asks for leading and trailing spaces, tabs and line endings - // to be stripped. Doing it here keeps every call site in agreement: the - // definition and the reference have to normalize to the same key. - return label.trim().toLowerCase().toUpperCase().toLowerCase(); + // to be stripped, and for internal runs of them to collapse to one space. + // Doing it here keeps every call site in agreement: the definition and the + // reference have to normalize to the same key. + return label.trim().replace(other.multipleSpaceGlobal, ' ').toLowerCase().toUpperCase().toLowerCase(); } export function findClosingBracket(str: string, b: string) { diff --git a/test/specs/new/em_and_reflinks_whitespace.html b/test/specs/new/em_and_reflinks_whitespace.html new file mode 100644 index 00000000..1969bb8d --- /dev/null +++ b/test/specs/new/em_and_reflinks_whitespace.html @@ -0,0 +1,5 @@ +

*foo*

+

*foo*

+

*my ref*

+

*my +ref*

diff --git a/test/specs/new/em_and_reflinks_whitespace.md b/test/specs/new/em_and_reflinks_whitespace.md new file mode 100644 index 00000000..d19d31e0 --- /dev/null +++ b/test/specs/new/em_and_reflinks_whitespace.md @@ -0,0 +1,13 @@ +*[foo*][my ref] + +*[foo*][my +ref] + +*[my ref*] + +*[my +ref*] + +[my ref]: /uri + +[my ref*]: /uri