Skip to content

Commit 37b28d8

Browse files
authored
fix: allow one more level of nested brackets in a link label (#4064)
1 parent 8f432f0 commit 37b28d8

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

‎src/rules.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,14 @@ const tag = edit(
441441
.replace('attribute', /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/)
442442
.getRegex();
443443

444-
const _inlineLabel = /(?:\[(?:\\[\s\S]|[^\[\]\\])*\]|\\[\s\S]|`+(?!`)[^`]*?`+(?!`)|``+(?=\])|[^\[\]\\`])*?/;
444+
// One matched pair of brackets, holding no bracket of its own.
445+
const _inlineLabelBrackets = /\[(?:\\[\s\S]|[^\[\]\\])*\]/;
446+
// CommonMark lets the brackets in a link label nest to any depth, which a regex
447+
// cannot follow, so it stops at a fixed one. Two levels is what the spec suite
448+
// asks for: a third and a fourth flip no further example.
449+
const _inlineLabel = edit(/(?:\[(?:brackets|\\[\s\S]|[^\[\]\\])*\]|\\[\s\S]|`+(?!`)[^`]*?`+(?!`)|``+(?=\])|[^\[\]\\`])*?/)
450+
.replace('brackets', _inlineLabelBrackets)
451+
.getRegex();
445452

446453
const link = edit(/^!?\[(label)\]\(\s*(href)(?:(?:[ \t]+(?:\n[ \t]*)?|\n[ \t]*)(title))?\s*\)/)
447454
.replace('label', _inlineLabel)

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4096,8 +4096,7 @@
40964096
"example": 512,
40974097
"start_line": 7840,
40984098
"end_line": 7844,
4099-
"section": "Links",
4100-
"shouldFail": true
4099+
"section": "Links"
41014100
},
41024101
{
41034102
"markdown": "[link] bar](/uri)\n",
@@ -4161,8 +4160,7 @@
41614160
"example": 520,
41624161
"start_line": 7900,
41634162
"end_line": 7904,
4164-
"section": "Links",
4165-
"shouldFail": true
4163+
"section": "Links"
41664164
},
41674165
{
41684166
"markdown": "*[foo*](/uri)\n",
@@ -4228,8 +4226,7 @@
42284226
"example": 528,
42294227
"start_line": 8004,
42304228
"end_line": 8010,
4231-
"section": "Links",
4232-
"shouldFail": true
4229+
"section": "Links"
42334230
},
42344231
{
42354232
"markdown": "[link \\[bar][ref]\n\n[ref]: /uri\n",

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4096,8 +4096,7 @@
40964096
"example": 512,
40974097
"start_line": 7840,
40984098
"end_line": 7844,
4099-
"section": "Links",
4100-
"shouldFail": true
4099+
"section": "Links"
41014100
},
41024101
{
41034102
"markdown": "[link] bar](/uri)\n",
@@ -4161,8 +4160,7 @@
41614160
"example": 520,
41624161
"start_line": 7900,
41634162
"end_line": 7904,
4164-
"section": "Links",
4165-
"shouldFail": true
4163+
"section": "Links"
41664164
},
41674165
{
41684166
"markdown": "*[foo*](/uri)\n",
@@ -4228,8 +4226,7 @@
42284226
"example": 528,
42294227
"start_line": 8004,
42304228
"end_line": 8010,
4231-
"section": "Links",
4232-
"shouldFail": true
4229+
"section": "Links"
42334230
},
42344231
{
42354232
"markdown": "[link \\[bar][ref]\n\n[ref]: /uri\n",

0 commit comments

Comments
 (0)