Skip to content

Commit 02c73e8

Browse files
authored
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.
1 parent c38704c commit 02c73e8

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

‎src/Tokenizer.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
614614
return;
615615
}
616616

617-
const tag = normalizeLabel(cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' ');
617+
const tag = normalizeLabel(cap[1]);
618618
const href = cap[2] ? cap[2].replace(this.rules.other.hrefBrackets, '$1').replace(this.rules.inline.anyPunctuation, '$1') : '';
619619
const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline.anyPunctuation, '$1') : cap[3];
620620
return {
@@ -857,8 +857,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
857857
return;
858858
}
859859

860-
const linkString = (cap[2] || cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' ');
861-
const link = links[normalizeLabel(linkString)];
860+
const link = links[normalizeLabel(cap[2] || cap[1])];
862861
if (!link) {
863862
const text = cap[0].charAt(0);
864863
return {

‎src/helpers.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ export function trimTrailingBlankLines(str: string) {
155155
*/
156156
export function normalizeLabel(label: string) {
157157
// The spec also asks for leading and trailing spaces, tabs and line endings
158-
// to be stripped. Doing it here keeps every call site in agreement: the
159-
// definition and the reference have to normalize to the same key.
160-
return label.trim().toLowerCase().toUpperCase().toLowerCase();
158+
// to be stripped, and for internal runs of them to collapse to one space.
159+
// Doing it here keeps every call site in agreement: the definition and the
160+
// reference have to normalize to the same key.
161+
return label.trim().replace(other.multipleSpaceGlobal, ' ').toLowerCase().toUpperCase().toLowerCase();
161162
}
162163

163164
export function findClosingBracket(str: string, b: string) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>*<a href="/uri">foo*</a></p>
2+
<p>*<a href="/uri">foo*</a></p>
3+
<p>*<a href="/uri">my ref*</a></p>
4+
<p>*<a href="/uri">my
5+
ref*</a></p>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*[foo*][my ref]
2+
3+
*[foo*][my
4+
ref]
5+
6+
*[my ref*]
7+
8+
*[my
9+
ref*]
10+
11+
[my ref]: /uri
12+
13+
[my ref*]: /uri

0 commit comments

Comments
 (0)