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.
This commit is contained in:
Enes Uygur
2026-11-02 07:53:31 -07:00
committed by GitHub
parent c38704c2fa
commit 02c73e8e65
4 changed files with 24 additions and 6 deletions
+2 -3
View File
@@ -614,7 +614,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
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<ParserOutput = string, RendererOutput = string> {
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 {
+4 -3
View File
@@ -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) {
@@ -0,0 +1,5 @@
<p>*<a href="/uri">foo*</a></p>
<p>*<a href="/uri">foo*</a></p>
<p>*<a href="/uri">my ref*</a></p>
<p>*<a href="/uri">my
ref*</a></p>
@@ -0,0 +1,13 @@
*[foo*][my ref]
*[foo*][my
ref]
*[my ref*]
*[my
ref*]
[my ref]: /uri
[my ref*]: /uri