Skip to content

Commit 6d94471

Browse files
committed
fix(markdown): preserve table boundaries around multiline math
1 parent 43f55a0 commit 6d94471

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,37 @@ describe("chat math parsing", () => {
155155
});
156156

157157
describe("math in GFM tables", () => {
158+
it.each(["\\(", "\\["])("keeps table rows intact when %s closes in a later row", (opener) => {
159+
const closer = opener === "\\(" ? "\\)" : "\\]";
160+
const source = `| Name | Value |\n| --- | --- |\n| first | ${opener}x |\n| second | y${closer} |\n| valid | $|z|$ |`;
161+
const tree = parseMarkdownWithMath(source);
162+
const table = tree.children[0];
163+
expect(table?.type).toBe("table");
164+
if (table?.type !== "table") throw new Error("Missing table");
165+
expect(table.children.map((row) => row.children.length)).toEqual([2, 2, 2, 2]);
166+
expect(
167+
collect(tree)
168+
.filter((node) => node.type === "text")
169+
.map((node) => node.value),
170+
).toEqual([
171+
"Name",
172+
"Value",
173+
"first",
174+
opener.slice(1) + "x",
175+
"second",
176+
"y" + closer.slice(1),
177+
"valid",
178+
]);
179+
expect(markdownMathRanges(source)).toEqual([
180+
expect.objectContaining({ source: "$|z|$", tex: "|z|" }),
181+
]);
182+
expect(JSON.stringify(tree)).not.toMatch(/[\uE000-\uF8FF]/);
183+
});
184+
185+
it("retains multiline math pipes outside tables", () => {
186+
const source = "Outside \\(x|y\n+z\\) remains math.\n\n\\[\n|a|\n+|b|\n\\]";
187+
expect(markdownMathRanges(source).map((range) => range.tex)).toEqual(["x|y\n+z", "|a|\n+|b|"]);
188+
});
158189
it.each(["\\(\\frac{1}{|x|}\\)", "$$|x|$$", "$|x|$", "\\[|x|\\]"])(
159190
"keeps pipes inside %s in one cell and preserves source offsets",
160191
(formula) => {

‎packages/shared/src/markdownMath.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,12 @@ function protectMathPipes(source: string) {
411411
if (node.type === "math" || node.type === "inlineMath") {
412412
const start = node.position?.start.offset;
413413
const end = node.position?.end.offset;
414-
if (start !== undefined && end !== undefined && source.slice(start, end).includes("|")) {
414+
if (start === undefined || end === undefined) return;
415+
const authored = source.slice(start, end);
416+
// A GFM cell cannot span rows. A prepass formula crossing a line ending
417+
// must not hide the table's separators, even if it closes in a later row.
418+
if (node.type === "inlineMath" && /[\r\n]/.test(authored)) return;
419+
if (authored.includes("|")) {
415420
replacements.push({ start, end });
416421
}
417422
} else if ("children" in node) node.children.forEach(visit);
@@ -431,8 +436,9 @@ function protectMathPipes(source: string) {
431436
function restoreMathPipes(tree: Root, pipe: string | null) {
432437
const visit = (node: Root | Root["children"][number], inTable: boolean) => {
433438
const table = inTable || node.type === "tableCell";
439+
// GFM may reinterpret a protected range. Never expose its placeholder as text.
440+
if (pipe && "value" in node) node.value = node.value.replaceAll(pipe, "|");
434441
if (node.type === "math" || node.type === "inlineMath") {
435-
if (pipe) node.value = node.value.replaceAll(pipe, "|");
436442
// GFM consumes one escaping backslash before a table pipe, including
437443
// inside code spans. TeX row separators before that escape stay intact.
438444
if (table) {

0 commit comments

Comments
 (0)