fix: respect raw tokens when closing link labels (#4066)

This commit is contained in:
Evgeny Ponomarev
2026-09-12 14:18:27 -06:00
committed by GitHub
parent c6a25bb2a9
commit ef394f71df
3 changed files with 55 additions and 16 deletions
+47
View File
@@ -76,6 +76,43 @@ function indentCodeCompensation(raw: string, text: string, rules: Rules) {
.join('\n');
}
function isLabelEndInsideToken(src: string, label: string, labelStart: number, rules: Rules) {
if (!label.includes('<')) {
return false;
}
for (let i = 0; i < label.length; i++) {
if (label[i] === '\\') {
i++;
continue;
}
if (label[i] === '`') {
const code = rules.inline.code.exec(label.slice(i));
if (code) {
i += code[0].length - 1;
continue;
}
}
if (label[i] !== '<') {
continue;
}
const tokenSrc = src.slice(labelStart + i);
const token = rules.inline.tag.exec(tokenSrc) || rules.inline.autolink.exec(tokenSrc);
if (!token) {
continue;
}
if (token[0].length > label.length - i) {
return true;
}
i += token[0].length - 1;
}
return false;
}
/**
* Tokenizer
*/
@@ -684,6 +721,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
link(src: string): Tokens.Link | Tokens.Image | undefined {
const cap = this.rules.inline.link.exec(src);
if (cap) {
const labelStart = cap[0].charAt(0) === '!' ? 2 : 1;
if (!this.options.pedantic && isLabelEndInsideToken(src, cap[1], labelStart, this.rules)) {
return;
}
const trimmedUrl = cap[2].trim();
if (!this.options.pedantic && this.rules.other.startAngleBracket.test(trimmedUrl)) {
// commonmark requires matching angle brackets
@@ -746,6 +788,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
let cap;
if ((cap = this.rules.inline.reflink.exec(src))
|| (cap = this.rules.inline.nolink.exec(src))) {
const labelStart = cap[0].charAt(0) === '!' ? 2 : 1;
if (!this.options.pedantic && isLabelEndInsideToken(src, cap[1], labelStart, this.rules)) {
return;
}
const linkString = (cap[2] || cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' ');
const link = links[normalizeLabel(linkString)];
if (!link) {
+4 -8
View File
@@ -4192,8 +4192,7 @@
"example": 524,
"start_line": 7937,
"end_line": 7941,
"section": "Links",
"shouldFail": true
"section": "Links"
},
{
"markdown": "[foo`](/uri)`\n",
@@ -4209,8 +4208,7 @@
"example": 526,
"start_line": 7951,
"end_line": 7955,
"section": "Links",
"shouldFail": true
"section": "Links"
},
{
"markdown": "[foo][bar]\n\n[bar]: /url \"title\"\n",
@@ -4290,8 +4288,7 @@
"example": 536,
"start_line": 8089,
"end_line": 8095,
"section": "Links",
"shouldFail": true
"section": "Links"
},
{
"markdown": "[foo`][ref]`\n\n[ref]: /uri\n",
@@ -4307,8 +4304,7 @@
"example": 538,
"start_line": 8107,
"end_line": 8113,
"section": "Links",
"shouldFail": true
"section": "Links"
},
{
"markdown": "[foo][BaR]\n\n[bar]: /url \"title\"\n",
+4 -8
View File
@@ -4192,8 +4192,7 @@
"example": 524,
"start_line": 7937,
"end_line": 7941,
"section": "Links",
"shouldFail": true
"section": "Links"
},
{
"markdown": "[foo`](/uri)`\n",
@@ -4209,8 +4208,7 @@
"example": 526,
"start_line": 7951,
"end_line": 7955,
"section": "Links",
"shouldFail": true
"section": "Links"
},
{
"markdown": "[foo][bar]\n\n[bar]: /url \"title\"\n",
@@ -4290,8 +4288,7 @@
"example": 536,
"start_line": 8089,
"end_line": 8095,
"section": "Links",
"shouldFail": true
"section": "Links"
},
{
"markdown": "[foo`][ref]`\n\n[ref]: /uri\n",
@@ -4307,8 +4304,7 @@
"example": 538,
"start_line": 8107,
"end_line": 8113,
"section": "Links",
"shouldFail": true
"section": "Links"
},
{
"markdown": "[foo][BaR]\n\n[bar]: /url \"title\"\n",