From aed933680dc146d868cb4573f393367abde04aae Mon Sep 17 00:00:00 2001 From: Gyanu Mayank Date: Tue, 8 Sep 2026 21:00:29 +0530 Subject: [PATCH] fix: case fold reference link labels (#4077) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Labels were matched with toLowerCase(), which is not the Unicode case fold CommonMark asks for, so labels that only agree once folded never matched: [ẞ] [Straße] [SS]: /url [STRASSE]: /url 'ẞ'.toLowerCase() is 'ß', never 'ss'. Round-tripping through upper case reaches the fold, the way commonmark.js normalizeReference() does; lowercasing afterwards keeps def.tag and the tokens.links keys in the lower case form they have always had. The two link-map lookups in Lexer used the label verbatim, so they went through the same helper: without it a folded key would stop the reflink masking from finding definitions it used to find. This turns on CommonMark example 540 (Links 81/90 -> 82/90). --- src/Lexer.ts | 5 +++-- src/Tokenizer.ts | 5 +++-- src/helpers.ts | 11 +++++++++++ test/specs/commonmark/commonmark.0.31.2.json | 3 +-- test/specs/gfm/commonmark.0.31.2.json | 3 +-- test/specs/new/reflink_case_folding.html | 1 + test/specs/new/reflink_case_folding.md | 6 ++++++ 7 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 test/specs/new/reflink_case_folding.html create mode 100644 test/specs/new/reflink_case_folding.md diff --git a/src/Lexer.ts b/src/Lexer.ts index bc12e9f3..b7f20aaf 100644 --- a/src/Lexer.ts +++ b/src/Lexer.ts @@ -1,6 +1,7 @@ import { _Tokenizer } from './Tokenizer.ts'; import { _defaults } from './defaults.ts'; import { other, block, inline } from './rules.ts'; +import { normalizeLabel } from './helpers.ts'; import type { Token, TokensList, Tokens } from './Tokens.ts'; import type { MarkedOptions } from './MarkedOptions.ts'; @@ -321,7 +322,7 @@ export class _Lexer { for (const match of text.matchAll(this.tokenizer.rules.inline.reflinkSearch)) { const match0 = match[0]; const refStart = match0.lastIndexOf('['); - if (match0.charAt(0) === '!' || !Object.hasOwn(this.tokens.links, match0.slice(refStart + 1, -1))) { + if (match0.charAt(0) === '!' || !Object.hasOwn(this.tokens.links, normalizeLabel(match0.slice(refStart + 1, -1)))) { continue; } // a candidate holding a link is not a link either, so it does not count @@ -347,7 +348,7 @@ export class _Lexer { const reflinkSearch = this.tokenizer.rules.inline.reflinkSearch; const maskReflink = (match0: string): string => { const refStart = match0.lastIndexOf('['); - if (!Object.hasOwn(this.tokens.links, match0.slice(refStart + 1, -1))) { + if (!Object.hasOwn(this.tokens.links, normalizeLabel(match0.slice(refStart + 1, -1)))) { return match0; } // CommonMark: "Links may not contain other links, at any level of diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index 369bb15f..3877aa9a 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -4,6 +4,7 @@ import { splitCells, findClosingBracket, expandTabs, + normalizeLabel, trimTrailingBlankLines, } from './helpers.ts'; import type { Rules } from './rules.ts'; @@ -527,7 +528,7 @@ export class _Tokenizer { def(src: string): Tokens.Def | undefined { const cap = this.rules.block.def.exec(src); if (cap) { - const tag = cap[1].toLowerCase().replace(this.rules.other.multipleSpaceGlobal, ' '); + const tag = normalizeLabel(cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' '); const href = cap[2] ? cap[2].replace(this.rules.other.hrefBrackets, '$1').replace(this.rules.inline.anyPunctuation, '$1') : ''; const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline.anyPunctuation, '$1') : cap[3]; return { @@ -746,7 +747,7 @@ export class _Tokenizer { if ((cap = this.rules.inline.reflink.exec(src)) || (cap = this.rules.inline.nolink.exec(src))) { const linkString = (cap[2] || cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' '); - const link = links[linkString.toLowerCase()]; + const link = links[normalizeLabel(linkString)]; if (!link) { const text = cap[0].charAt(0); return { diff --git a/src/helpers.ts b/src/helpers.ts index ae8c380b..2f9c3408 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -123,6 +123,17 @@ export function trimTrailingBlankLines(str: string) { return lines.slice(0, end + 1).join('\n'); } +/** + * Normalizes a link label so definitions and references can be matched. + * CommonMark asks for a Unicode case fold, which `toLowerCase()` does not + * reach: `ẞ` lowercases to `ß` and so never meets `SS`. Round-tripping + * through upper case does, as in commonmark.js; the final `toLowerCase()` + * keeps the folded label lower case, the form `def.tag` has always used. + */ +export function normalizeLabel(label: string) { + return label.toLowerCase().toUpperCase().toLowerCase(); +} + export function findClosingBracket(str: string, b: string) { if (str.indexOf(b[1]) === -1) { return -1; diff --git a/test/specs/commonmark/commonmark.0.31.2.json b/test/specs/commonmark/commonmark.0.31.2.json index 1619f1fe..9ca5fc57 100644 --- a/test/specs/commonmark/commonmark.0.31.2.json +++ b/test/specs/commonmark/commonmark.0.31.2.json @@ -4324,8 +4324,7 @@ "example": 540, "start_line": 8129, "end_line": 8135, - "section": "Links", - "shouldFail": true + "section": "Links" }, { "markdown": "[Foo\n bar]: /url\n\n[Baz][Foo bar]\n", diff --git a/test/specs/gfm/commonmark.0.31.2.json b/test/specs/gfm/commonmark.0.31.2.json index 5e8d7475..389b5908 100644 --- a/test/specs/gfm/commonmark.0.31.2.json +++ b/test/specs/gfm/commonmark.0.31.2.json @@ -4324,8 +4324,7 @@ "example": 540, "start_line": 8129, "end_line": 8135, - "section": "Links", - "shouldFail": true + "section": "Links" }, { "markdown": "[Foo\n bar]: /url\n\n[Baz][Foo bar]\n", diff --git a/test/specs/new/reflink_case_folding.html b/test/specs/new/reflink_case_folding.html new file mode 100644 index 00000000..9b12fef7 --- /dev/null +++ b/test/specs/new/reflink_case_folding.html @@ -0,0 +1 @@ +

ẞ and Straße and Dž and fi.

diff --git a/test/specs/new/reflink_case_folding.md b/test/specs/new/reflink_case_folding.md new file mode 100644 index 00000000..f7f61bcc --- /dev/null +++ b/test/specs/new/reflink_case_folding.md @@ -0,0 +1,6 @@ +[ẞ] and [Straße] and [Dž] and [fi]. + +[SS]: /sharp-s +[STRASSE]: /street +[DŽ]: /digraph +[FI]: /ligature