Skip to content

Commit e00ef1f

Browse files
fix(web): preserve Unicode footnotes across Markdown file chunks (#48250)
Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 58fdc24 commit e00ef1f

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

‎packages/shared/src/markdownChunks.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,26 @@ describe("chunked Markdown footnotes", () => {
869869
expect(findFootnoteRefChunkIndex(footnotes!, 1, 3)).toBeNull();
870870
});
871871

872+
it.each([
873+
{ firstLabel: "ς", secondLabel: "Σ", definitionLabel: "σ" },
874+
{ firstLabel: "ß", secondLabel: "ẞ", definitionLabel: "ss" },
875+
{ firstLabel: "ı", secondLabel: "I", definitionLabel: "i" },
876+
])(
877+
"matches footnote citations $firstLabel and $secondLabel to $definitionLabel across chunks",
878+
({ firstLabel, secondLabel, definitionLabel }) => {
879+
const definition = `[^${definitionLabel}]: Shared note`;
880+
const source = `First[^${firstLabel}] ${"lead ".repeat(900).trimEnd()}\n\n# Second\n\nAgain[^${secondLabel}].\n\n${definition}\n\n[^${secondLabel}]: Duplicate ignored.`;
881+
const { chunks, definitions, footnotes } = splitMarkdown(source);
882+
expect(chunks).toHaveLength(2);
883+
expect(footnotes?.stubs).toBe(`[^${definitionLabel}]: .`);
884+
expect(buildMarkdownFootnotesSection(definitions, footnotes)?.text).toBe(
885+
`[^${firstLabel}] [^${secondLabel}]\n\n${definition}`,
886+
);
887+
expect(findFootnoteRefChunkIndex(footnotes!, 1, 1)).toBe(0);
888+
expect(findFootnoteRefChunkIndex(footnotes!, 1, 2)).toBe(1);
889+
},
890+
);
891+
872892
it.each([" \t", " \t", " \t"])(
873893
"keeps a footnote paragraph indented with %j as a continuation",
874894
(indent) => {

‎packages/shared/src/markdownChunks.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ function linkDestinationEnd(text: string, start: number): number {
363363
return -1;
364364
}
365365

366+
// Mirrors micromark's normalizeIdentifier so chunks match footnotes exactly as one render would.
367+
function footnoteLabelKey(label: string): string {
368+
return label.toLowerCase().toUpperCase();
369+
}
370+
366371
const TABLE_URI_AUTOLINK = /<[A-Za-z][A-Za-z0-9+.-]{1,31}:[^\x00-\x20<>]*>/y;
367372
const TABLE_EMAIL_AUTOLINK =
368373
/<[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>/y;
@@ -804,7 +809,7 @@ function extractFootnoteRefs(
804809
const isDefAtLineStart = !tableCell && text[j + 1] === ":" && isAtLineStart(i);
805810
if (!isDefAtLineStart) {
806811
const label = text.slice(i + 2, j);
807-
if (definedKeys.has(label.toLowerCase())) {
812+
if (definedKeys.has(footnoteLabelKey(label))) {
808813
refs.push(label);
809814
}
810815
}
@@ -895,7 +900,7 @@ function splitMarkdownBlocks(
895900
if (activeFootnoteLabel !== null && activeFootnoteLines !== null) {
896901
const block = activeFootnoteLines.join("\n").trimEnd();
897902
if (footnoteChars + block.length < SHARED_FOOTNOTES_MAX_CHARS) {
898-
capturedFootnoteKeys.add(activeFootnoteLabel.toLowerCase());
903+
capturedFootnoteKeys.add(footnoteLabelKey(activeFootnoteLabel));
899904
capturedFootnoteLabels.push(activeFootnoteLabel);
900905
footnoteDefs.push(block);
901906
footnoteDefSpans.push([activeFootnoteOffset, block.length]);
@@ -1057,7 +1062,7 @@ function splitMarkdownBlocks(
10571062
mayHaveFootnotes && line.includes("[^") ? FOOTNOTE_DEFINITION_START.exec(line) : null;
10581063
if (fnMatch) {
10591064
const label = fnMatch[1]!;
1060-
const key = label.toLowerCase();
1065+
const key = footnoteLabelKey(label);
10611066
inFootnoteDef = true;
10621067
if (!seenFootnoteKeys.has(key)) {
10631068
seenFootnoteKeys.add(key);
@@ -1548,7 +1553,7 @@ export function findFootnoteRefChunkIndex(
15481553
const refs = footnotes.chunkRefs[chunkIndex];
15491554
if (!refs) continue;
15501555
for (const label of refs) {
1551-
const key = label.toLowerCase();
1556+
const key = footnoteLabelKey(label);
15521557
let num = numberByKey.get(key);
15531558
if (num === undefined) {
15541559
num = nextNumber++;

0 commit comments

Comments
 (0)