Skip to content

Commit ef394f7

Browse files
fix: respect raw tokens when closing link labels (#4066)
1 parent c6a25bb commit ef394f7

3 files changed

Lines changed: 55 additions & 16 deletions

File tree

‎src/Tokenizer.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,43 @@ function indentCodeCompensation(raw: string, text: string, rules: Rules) {
7676
.join('\n');
7777
}
7878

79+
function isLabelEndInsideToken(src: string, label: string, labelStart: number, rules: Rules) {
80+
if (!label.includes('<')) {
81+
return false;
82+
}
83+
84+
for (let i = 0; i < label.length; i++) {
85+
if (label[i] === '\\') {
86+
i++;
87+
continue;
88+
}
89+
90+
if (label[i] === '`') {
91+
const code = rules.inline.code.exec(label.slice(i));
92+
if (code) {
93+
i += code[0].length - 1;
94+
continue;
95+
}
96+
}
97+
98+
if (label[i] !== '<') {
99+
continue;
100+
}
101+
102+
const tokenSrc = src.slice(labelStart + i);
103+
const token = rules.inline.tag.exec(tokenSrc) || rules.inline.autolink.exec(tokenSrc);
104+
if (!token) {
105+
continue;
106+
}
107+
108+
if (token[0].length > label.length - i) {
109+
return true;
110+
}
111+
i += token[0].length - 1;
112+
}
113+
return false;
114+
}
115+
79116
/**
80117
* Tokenizer
81118
*/
@@ -684,6 +721,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
684721
link(src: string): Tokens.Link | Tokens.Image | undefined {
685722
const cap = this.rules.inline.link.exec(src);
686723
if (cap) {
724+
const labelStart = cap[0].charAt(0) === '!' ? 2 : 1;
725+
if (!this.options.pedantic && isLabelEndInsideToken(src, cap[1], labelStart, this.rules)) {
726+
return;
727+
}
728+
687729
const trimmedUrl = cap[2].trim();
688730
if (!this.options.pedantic && this.rules.other.startAngleBracket.test(trimmedUrl)) {
689731
// commonmark requires matching angle brackets
@@ -746,6 +788,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
746788
let cap;
747789
if ((cap = this.rules.inline.reflink.exec(src))
748790
|| (cap = this.rules.inline.nolink.exec(src))) {
791+
const labelStart = cap[0].charAt(0) === '!' ? 2 : 1;
792+
if (!this.options.pedantic && isLabelEndInsideToken(src, cap[1], labelStart, this.rules)) {
793+
return;
794+
}
795+
749796
const linkString = (cap[2] || cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' ');
750797
const link = links[normalizeLabel(linkString)];
751798
if (!link) {

‎test/specs/commonmark/commonmark.0.31.2.json‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4192,8 +4192,7 @@
41924192
"example": 524,
41934193
"start_line": 7937,
41944194
"end_line": 7941,
4195-
"section": "Links",
4196-
"shouldFail": true
4195+
"section": "Links"
41974196
},
41984197
{
41994198
"markdown": "[foo`](/uri)`\n",
@@ -4209,8 +4208,7 @@
42094208
"example": 526,
42104209
"start_line": 7951,
42114210
"end_line": 7955,
4212-
"section": "Links",
4213-
"shouldFail": true
4211+
"section": "Links"
42144212
},
42154213
{
42164214
"markdown": "[foo][bar]\n\n[bar]: /url \"title\"\n",
@@ -4290,8 +4288,7 @@
42904288
"example": 536,
42914289
"start_line": 8089,
42924290
"end_line": 8095,
4293-
"section": "Links",
4294-
"shouldFail": true
4291+
"section": "Links"
42954292
},
42964293
{
42974294
"markdown": "[foo`][ref]`\n\n[ref]: /uri\n",
@@ -4307,8 +4304,7 @@
43074304
"example": 538,
43084305
"start_line": 8107,
43094306
"end_line": 8113,
4310-
"section": "Links",
4311-
"shouldFail": true
4307+
"section": "Links"
43124308
},
43134309
{
43144310
"markdown": "[foo][BaR]\n\n[bar]: /url \"title\"\n",

‎test/specs/gfm/commonmark.0.31.2.json‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4192,8 +4192,7 @@
41924192
"example": 524,
41934193
"start_line": 7937,
41944194
"end_line": 7941,
4195-
"section": "Links",
4196-
"shouldFail": true
4195+
"section": "Links"
41974196
},
41984197
{
41994198
"markdown": "[foo`](/uri)`\n",
@@ -4209,8 +4208,7 @@
42094208
"example": 526,
42104209
"start_line": 7951,
42114210
"end_line": 7955,
4212-
"section": "Links",
4213-
"shouldFail": true
4211+
"section": "Links"
42144212
},
42154213
{
42164214
"markdown": "[foo][bar]\n\n[bar]: /url \"title\"\n",
@@ -4290,8 +4288,7 @@
42904288
"example": 536,
42914289
"start_line": 8089,
42924290
"end_line": 8095,
4293-
"section": "Links",
4294-
"shouldFail": true
4291+
"section": "Links"
42954292
},
42964293
{
42974294
"markdown": "[foo`][ref]`\n\n[ref]: /uri\n",
@@ -4307,8 +4304,7 @@
43074304
"example": 538,
43084305
"start_line": 8107,
43094306
"end_line": 8113,
4310-
"section": "Links",
4311-
"shouldFail": true
4307+
"section": "Links"
43124308
},
43134309
{
43144310
"markdown": "[foo][BaR]\n\n[bar]: /url \"title\"\n",

0 commit comments

Comments
 (0)