Skip to content

Commit caefe8a

Browse files
fix(shared): keep footnotes in GFM tables after unmatched backticks (#48263)
Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent ab07a09 commit caefe8a

2 files changed

Lines changed: 512 additions & 21 deletions

File tree

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

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test";
22

33
import { toHtml } from "hast-util-to-html";
44
import remarkGfm from "remark-gfm";
5+
import remarkMath from "remark-math";
56
import remarkParse from "remark-parse";
67
import remarkRehype from "remark-rehype";
78
import { unified } from "unified";
@@ -1048,6 +1049,144 @@ describe("chunked Markdown footnotes", () => {
10481049
expect(section.startsWith("[^a] [^b]\n\n")).toBe(true);
10491050
});
10501051

1052+
it.each([
1053+
["across rows", "| a | b |\n| --- | --- |\n| `unclosed | x |\n| [^1] | ` |"],
1054+
["across header cells", "| ` | [^1] | ` |\n| --- | --- | --- |"],
1055+
["with an escaped pipe inside code", "| a | b |\n| --- | --- |\n| `a\\|[^2]` | [^1] |"],
1056+
])("matches GFM table footnotes %s", (_name, table) => {
1057+
const filler = "filler paragraph text.\n\n".repeat(10);
1058+
const text = `${filler}${table}\n\n${filler}[^1]: note\n[^2]: hidden\n`;
1059+
const parser = unified().use(remarkParse).use(remarkGfm).use(remarkRehype);
1060+
const render = (source: string) => toHtml(parser.runSync(parser.parse(source)));
1061+
const reference = 'href="#user-content-fn-1"';
1062+
expect(render(text)).toContain(reference);
1063+
expect(render(text)).not.toContain('href="#user-content-fn-2"');
1064+
1065+
const { chunks, definitions, footnotes } = splitMarkdown(text, 100);
1066+
expect(chunks.length).toBeGreaterThan(1);
1067+
expect(footnotes?.chunkRefs.flat()).toEqual(["1"]);
1068+
const tableIndex = chunks.findIndex((chunk) => chunk.text.includes(table));
1069+
const prepared = prepareMarkdownChunk(
1070+
chunks[tableIndex]!.text,
1071+
tableIndex,
1072+
definitions,
1073+
footnotes,
1074+
);
1075+
expect(render(prepared.text)).toContain(reference);
1076+
expect(render(buildMarkdownFootnotesSection(definitions, footnotes)!.text)).toContain(
1077+
"<p>note ",
1078+
);
1079+
});
1080+
1081+
it.each([
1082+
["ordered list starting at two", "| a |\n| --- |\n| x |\n2. item | [^1]", ["1"]],
1083+
["list item with code", "| a | b |\n| --- | --- |\n| x | y |\n- `unclosed | [^1] | `", []],
1084+
["lazy quote continuation", "> quote\n`unclosed\n| --- |\n[^1] | `", []],
1085+
["nonbreaking space in the header", "\u00a0| a |\n| --- |\nx | [^1]", ["1"]],
1086+
["indented code after a table", "| a |\n| --- |\n code\nprose | [^1]", ["1"]],
1087+
["delimiter outside a list item", "- intro\n | a |\n| --- |\nx | [^1]", ["1"]],
1088+
["delimiter inside a list item", "- intro\n | a |\n | --- |\n x | [^1]", []],
1089+
["lazy header inside a list item", "- intro\n| a |\n | --- |\n x | [^1]", []],
1090+
["prose outside a list table", "- intro\n | a |\n | --- |\nx | [^1]", ["1"]],
1091+
["overindented list table header", "- intro\n | a |\n | --- |\n x | [^1]", ["1"]],
1092+
])("matches GFM footnotes around a table boundary: %s", (_name, block, refs) => {
1093+
const filler = "filler paragraph text.\n\n".repeat(10);
1094+
const text = `${filler}${block}\n\n${filler}[^1]: note\n`;
1095+
const parser = unified().use(remarkParse).use(remarkGfm).use(remarkRehype);
1096+
const html = toHtml(parser.runSync(parser.parse(text)));
1097+
expect(html.includes('href="#user-content-fn-1"')).toBe(refs.length > 0);
1098+
expect(splitMarkdown(text, 100).footnotes?.chunkRefs.flat() ?? []).toEqual(refs);
1099+
});
1100+
1101+
it.each([
1102+
["inline math pipes in body", "| a | b |\n| --- | --- |\n| $|x|$ | [^1] |", ["1"]],
1103+
[
1104+
"math after a short HTML comment",
1105+
"| a | b |\n| --- | --- |\n| <!--> $x|y$ --> | [^1] |",
1106+
["1"],
1107+
],
1108+
[
1109+
"footnote text inside TeX parens",
1110+
"| a | b |\n| --- | --- |\n| `open | \\(x[^1]\\) | ` |",
1111+
[],
1112+
],
1113+
[
1114+
"footnote after TeX brackets",
1115+
"| a | b |\n| --- | --- |\n| `open | \\[x[^1]\\] [^1] | ` |",
1116+
["1"],
1117+
],
1118+
["double-dollar math pipes in body", "| a | b |\n| --- | --- |\n| $$|x|$$ | [^1] |", ["1"]],
1119+
["unclosed dollar before pipes", "| a | b |\n| --- | --- |\n| $|x | [^1] |", []],
1120+
["math pipes inside code span", "| `$|$` | b |\n| --- | --- |\n| y | z | [^1] |", ["1"]],
1121+
[
1122+
"math pipes inside link destination",
1123+
"| [a](https://example.com/$|$) | b |\n| --- | --- |\n| y | z | [^1] |",
1124+
["1"],
1125+
],
1126+
[
1127+
"math pipes inside autolink",
1128+
"| <https://example.com/$|$> | b |\n| --- | --- |\n| y | z | [^1] |",
1129+
["1"],
1130+
],
1131+
[
1132+
"math pipes inside HTML text",
1133+
'| <span data-x="$|$">a</span> | b |\n| --- | --- |\n| y | z | [^1] |',
1134+
["1"],
1135+
],
1136+
[
1137+
"numeric closer after single dollar",
1138+
"| $x|y$2 | b |\n| --- | --- |\n| y | z | [^1] |",
1139+
["1"],
1140+
],
1141+
["URL in single-dollar math", "| $https://x|y$ | b |\n| --- | --- |\n| y | z | [^1] |", ["1"]],
1142+
[
1143+
"comma before single-dollar closer followed by text",
1144+
"| $x|y,$z | b |\n| --- | --- |\n| y | z | [^1] |",
1145+
["1"],
1146+
],
1147+
[
1148+
"comma before closer followed by allowed punctuation",
1149+
"| a | b |\n| --- | --- |\n| $x|y,$. | [^1] |",
1150+
["1"],
1151+
],
1152+
[
1153+
"math consuming HTML tag start before second math span",
1154+
'| a | b |\n| --- | --- |\n| $<span title="$ $x|y$"> | [^1] |',
1155+
["1"],
1156+
],
1157+
["triple-dollar math pipes in body", "| a | b |\n| --- | --- |\n| $$$x|y$$$ | [^1] |", ["1"]],
1158+
[
1159+
"triple-dollar math with matching spaces",
1160+
"| a | b |\n| --- | --- |\n| $$$ x|y $$$ | [^1] |",
1161+
["1"],
1162+
],
1163+
[
1164+
"triple-dollar math with mismatched spaces",
1165+
"| a | b |\n| --- | --- |\n| $$$ x|y$$$ | [^1] |",
1166+
[],
1167+
],
1168+
])("matches table-math footnote boundaries: %s", (_name, table, refs) => {
1169+
const filler = "filler paragraph text.\n\n".repeat(10);
1170+
const text = `${filler}${table}\n\n${filler}[^1]: note\n`;
1171+
const parser = unified()
1172+
.use(remarkParse)
1173+
.use(remarkGfm)
1174+
.use(remarkMath, { singleDollarTextMath: true })
1175+
.use(remarkRehype);
1176+
const html = toHtml(parser.runSync(parser.parse(text)));
1177+
expect(html.includes('href="#user-content-fn-1"')).toBe(refs.length > 0);
1178+
expect(splitMarkdown(text, 100).footnotes?.chunkRefs.flat() ?? []).toEqual(refs);
1179+
});
1180+
1181+
it.each([
1182+
["TeX paren math pipes", "| a | b |\n| --- | --- |\n| \\(|x|\\) | [^1] |"],
1183+
["TeX bracket math pipes", "| a | b |\n| --- | --- |\n| \\[|x|\\] | [^1] |"],
1184+
])("keeps table footnotes after %s", (_name, table) => {
1185+
const filler = "filler paragraph text.\n\n".repeat(10);
1186+
const text = `${filler}${table}\n\n${filler}[^1]: note\n`;
1187+
expect(splitMarkdown(text, 100).footnotes?.chunkRefs.flat()).toEqual(["1"]);
1188+
});
1189+
10511190
it.each([
10521191
["a list item", "- Item[^a]`"],
10531192
["a quote", "> Quote[^a]`"],

0 commit comments

Comments
 (0)