From 37b28d8f640e39c9680e24b83fe7828dd111073d Mon Sep 17 00:00:00 2001 From: Luan Taraschi <130802253+luantaraschi@users.noreply.github.com> Date: Fri, 4 Sep 2026 02:59:25 -0300 Subject: [PATCH] fix: allow one more level of nested brackets in a link label (#4064) --- src/rules.ts | 9 ++++++++- test/specs/commonmark/commonmark.0.31.2.json | 9 +++------ test/specs/gfm/commonmark.0.31.2.json | 9 +++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/rules.ts b/src/rules.ts index 72016895..d51d84bc 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -441,7 +441,14 @@ const tag = edit( .replace('attribute', /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/) .getRegex(); -const _inlineLabel = /(?:\[(?:\\[\s\S]|[^\[\]\\])*\]|\\[\s\S]|`+(?!`)[^`]*?`+(?!`)|``+(?=\])|[^\[\]\\`])*?/; +// One matched pair of brackets, holding no bracket of its own. +const _inlineLabelBrackets = /\[(?:\\[\s\S]|[^\[\]\\])*\]/; +// CommonMark lets the brackets in a link label nest to any depth, which a regex +// cannot follow, so it stops at a fixed one. Two levels is what the spec suite +// asks for: a third and a fourth flip no further example. +const _inlineLabel = edit(/(?:\[(?:brackets|\\[\s\S]|[^\[\]\\])*\]|\\[\s\S]|`+(?!`)[^`]*?`+(?!`)|``+(?=\])|[^\[\]\\`])*?/) + .replace('brackets', _inlineLabelBrackets) + .getRegex(); const link = edit(/^!?\[(label)\]\(\s*(href)(?:(?:[ \t]+(?:\n[ \t]*)?|\n[ \t]*)(title))?\s*\)/) .replace('label', _inlineLabel) diff --git a/test/specs/commonmark/commonmark.0.31.2.json b/test/specs/commonmark/commonmark.0.31.2.json index 92b13ab0..1619f1fe 100644 --- a/test/specs/commonmark/commonmark.0.31.2.json +++ b/test/specs/commonmark/commonmark.0.31.2.json @@ -4096,8 +4096,7 @@ "example": 512, "start_line": 7840, "end_line": 7844, - "section": "Links", - "shouldFail": true + "section": "Links" }, { "markdown": "[link] bar](/uri)\n", @@ -4161,8 +4160,7 @@ "example": 520, "start_line": 7900, "end_line": 7904, - "section": "Links", - "shouldFail": true + "section": "Links" }, { "markdown": "*[foo*](/uri)\n", @@ -4228,8 +4226,7 @@ "example": 528, "start_line": 8004, "end_line": 8010, - "section": "Links", - "shouldFail": true + "section": "Links" }, { "markdown": "[link \\[bar][ref]\n\n[ref]: /uri\n", diff --git a/test/specs/gfm/commonmark.0.31.2.json b/test/specs/gfm/commonmark.0.31.2.json index a2bf8caf..5e8d7475 100644 --- a/test/specs/gfm/commonmark.0.31.2.json +++ b/test/specs/gfm/commonmark.0.31.2.json @@ -4096,8 +4096,7 @@ "example": 512, "start_line": 7840, "end_line": 7844, - "section": "Links", - "shouldFail": true + "section": "Links" }, { "markdown": "[link] bar](/uri)\n", @@ -4161,8 +4160,7 @@ "example": 520, "start_line": 7900, "end_line": 7904, - "section": "Links", - "shouldFail": true + "section": "Links" }, { "markdown": "*[foo*](/uri)\n", @@ -4228,8 +4226,7 @@ "example": 528, "start_line": 8004, "end_line": 8010, - "section": "Links", - "shouldFail": true + "section": "Links" }, { "markdown": "[link \\[bar][ref]\n\n[ref]: /uri\n",