fix: case fold reference link labels (#4077)

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).
This commit is contained in:
Gyanu Mayank
2026-09-08 09:30:29 -06:00
committed by GitHub
parent 123ce04b81
commit aed933680d
7 changed files with 26 additions and 8 deletions
+3 -2
View File
@@ -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<ParserOutput = string, RendererOutput = string> {
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<ParserOutput = string, RendererOutput = string> {
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
+3 -2
View File
@@ -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<ParserOutput = string, RendererOutput = string> {
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<ParserOutput = string, RendererOutput = string> {
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 {
+11
View File
@@ -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;
+1 -2
View File
@@ -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",
+1 -2
View File
@@ -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",
+1
View File
@@ -0,0 +1 @@
<p><a href="/sharp-s">ẞ</a> and <a href="/street">Straße</a> and <a href="/digraph">Dž</a> and <a href="/ligature">fi</a>.</p>
+6
View File
@@ -0,0 +1,6 @@
[ẞ] and [Straße] and [Dž] and [fi].
[SS]: /sharp-s
[STRASSE]: /street
[DŽ]: /digraph
[FI]: /ligature