Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
findClosingBracket,
expandTabs,
trimTrailingBlankLines,
normalizeLabel,
} from './helpers.ts';
import type { Rules } from './rules.ts';
import type { _Lexer } from './Lexer.ts';
Expand Down Expand Up @@ -499,7 +500,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 {
Expand Down Expand Up @@ -717,8 +718,8 @@ 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 linkString = (cap[2] || cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' ');
const link = links[linkString.toLowerCase()];
const linkString = normalizeLabel(cap[2] || cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' ');
const link = links[linkString];
if (!link) {
const text = cap[0].charAt(0);
return {
Expand Down
22 changes: 22 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,25 @@ export function expandTabs(line: string, indent = 0) {

return expanded;
}

/**
* Normalize a link reference label per CommonMark spec:
* perform Unicode full case fold (covering multi-char expansions that
* String.prototype.toLowerCase() misses, e.g. ẞ→ss, ligatures→letters),
* then collapse runs of whitespace to a single space.
*
* @see https://spec.commonmark.org/0.31.2/#matches
*/
export function normalizeLabel(label: string): string {
return label
// Apply Unicode full case fold before toLowerCase so that
// ẞ (U+1E9E) → ss and ß (U+00DF) → ss (simple fold maps ẞ to ß first).
.toLowerCase()
.replace(/ß/g, 'ss') // U+00DF + folded U+1E9E

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the test for this first one, but do we have tests for the others?

.replace(/ff/g, 'ff') // U+FB00
.replace(/fi/g, 'fi') // U+FB01
.replace(/fl/g, 'fl') // U+FB02
.replace(/ffi/g, 'ffi') // U+FB03
.replace(/ffl/g, 'ffl') // U+FB04
.replace(/ſt|st/g, 'st'); // U+FB05, U+FB06

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this list exhaustive?

I'm wondering if we can use a built-in API like Intl or perhaps label.normalize('NFKC') instead?

}
3 changes: 1 addition & 2 deletions test/specs/commonmark/commonmark.0.31.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4331,8 +4331,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",
Expand Down
3 changes: 1 addition & 2 deletions test/specs/gfm/commonmark.0.31.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4331,8 +4331,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",
Expand Down