Skip to content

Commit f372994

Browse files
committed
fix(web): copied Markdown tables keep formula pipes
Table copy escaped every pipe after serializing a cell, including TeX source from rendered formulas. Math-aware table parsing keeps a formula's pipes in its cell, so |x| came back as \|x\| and \|x\| gained another backslash. Escape pipes only in prose, code, links and other copied Markdown, and keep formula source as authored.
1 parent 448b8a9 commit f372994

2 files changed

Lines changed: 166 additions & 34 deletions

File tree

‎apps/web/src/markdown-clipboard.test.ts‎

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
22

3-
import { serializeRenderedMarkdownFragment } from "./markdown-clipboard";
3+
import {
4+
serializeRenderedMarkdownFragment,
5+
serializeTableElementToMarkdown,
6+
} from "./markdown-clipboard";
47
import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
58
import {
69
collectAssistantCitations,
710
serializeAssistantCitation,
811
} from "@t3tools/shared/assistantCitations";
12+
import { markdownMathRanges, parseMarkdownWithMath } from "@t3tools/shared/markdownMath";
913

1014
const TEXT_NODE = 3;
1115
const ELEMENT_NODE = 1;
@@ -95,6 +99,22 @@ class FakeElement {
9599
};
96100
return search(this);
97101
}
102+
103+
/** Supports the `:scope > a > b` child paths the table serializer queries. */
104+
querySelectorAll(selector: string): FakeElement[] {
105+
return selector.split(", ").flatMap((part) =>
106+
part
107+
.replace(/^:scope > /, "")
108+
.split(" > ")
109+
.reduce<FakeElement[]>(
110+
(level, tag) =>
111+
level.flatMap((element) =>
112+
element.children.filter((child) => child.tagName === tag.toUpperCase()),
113+
),
114+
[this],
115+
),
116+
);
117+
}
98118
}
99119

100120
function asNode(element: FakeElement): Node {
@@ -198,3 +218,98 @@ describe("serializeRenderedMarkdownFragment", () => {
198218
);
199219
});
200220
});
221+
222+
function renderedFormula(tex: string): FakeElement {
223+
// MarkdownMath's wrapper; its children stand in for KaTeX's glyphs.
224+
return new FakeElement("SPAN", [], {
225+
"data-markdown-math": "inline",
226+
"data-markdown-copy": `\\(${tex}\\)`,
227+
}).append(new FakeText(tex));
228+
}
229+
230+
function renderedTable(...rows: Array<Array<FakeElement | FakeText>>): FakeElement {
231+
const row = (tag: string, cells: Array<FakeElement | FakeText>) =>
232+
new FakeElement("TR").append(...cells.map((cell) => new FakeElement(tag).append(cell)));
233+
const [head = [], ...body] = rows;
234+
return new FakeElement("TABLE").append(
235+
new FakeElement("THEAD").append(row("TH", head)),
236+
new FakeElement("TBODY").append(...body.map((cells) => row("TD", cells))),
237+
);
238+
}
239+
240+
describe("copying rendered Markdown tables", () => {
241+
beforeEach(() => {
242+
vi.stubGlobal("Node", { TEXT_NODE, ELEMENT_NODE });
243+
});
244+
245+
afterEach(() => {
246+
vi.unstubAllGlobals();
247+
});
248+
249+
it("keeps formula pipes as authored while escaping prose pipes", () => {
250+
const table = renderedTable(
251+
[new FakeText("Formula"), new FakeText("Note")],
252+
[renderedFormula("|x|"), new FakeText("a | b")],
253+
[renderedFormula("\\|x\\|"), new FakeElement("CODE").append(new FakeText("c|d"))],
254+
[
255+
new FakeElement("A", [], { href: "https://example.com/?q=a|b" }).append(
256+
new FakeText("norm "),
257+
renderedFormula("|y|"),
258+
),
259+
new FakeText("e"),
260+
],
261+
);
262+
const markdown = [
263+
"| Formula | Note |",
264+
"| --- | --- |",
265+
"| \\(|x|\\) | a \\| b |",
266+
"| \\(\\|x\\|\\) | `c\\|d` |",
267+
"| [norm \\(|y|\\)](https://example.com/?q=a\\|b) | e |",
268+
].join("\n");
269+
270+
expect(serializeTableElementToMarkdown(table as unknown as Element)).toBe(markdown);
271+
expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(table)))).toBe(
272+
markdown,
273+
);
274+
275+
const tree = parseMarkdownWithMath(markdown);
276+
const parsed = tree.children[0];
277+
if (parsed?.type !== "table") throw new Error("Expected a table");
278+
expect(parsed.children.map((row) => row.children.length)).toEqual([2, 2, 2, 2]);
279+
expect(markdownMathRanges(markdown, tree).map((formula) => formula.tex)).toEqual([
280+
"|x|",
281+
"\\|x\\|",
282+
"|y|",
283+
]);
284+
const [, , codeRow, linkRow] = parsed.children;
285+
expect(codeRow?.children[1]?.children[0]).toMatchObject({ type: "inlineCode", value: "c|d" });
286+
expect(linkRow?.children[0]?.children[0]).toMatchObject({
287+
type: "link",
288+
url: "https://example.com/?q=a|b",
289+
});
290+
});
291+
292+
it("escapes prose pipes in lists and quotes inside raw HTML table cells", () => {
293+
const table = renderedTable(
294+
[new FakeText("List"), new FakeText("Quote")],
295+
[
296+
new FakeElement("UL").append(
297+
new FakeElement("LI").append(renderedFormula("|x|"), new FakeText(" a | b")),
298+
),
299+
new FakeElement("BLOCKQUOTE").append(new FakeElement("P").append(new FakeText("c | d"))),
300+
],
301+
);
302+
const markdown = [
303+
"| List | Quote |",
304+
"| --- | --- |",
305+
"| - \\(|x|\\) a \\| b | > c \\| d |",
306+
].join("\n");
307+
308+
expect(serializeTableElementToMarkdown(table as unknown as Element)).toBe(markdown);
309+
const tree = parseMarkdownWithMath(markdown);
310+
const parsed = tree.children[0];
311+
if (parsed?.type !== "table") throw new Error("Expected a table");
312+
expect(parsed.children.map((row) => row.children.length)).toEqual([2, 2]);
313+
expect(markdownMathRanges(markdown, tree).map((formula) => formula.tex)).toEqual(["|x|"]);
314+
});
315+
});

‎apps/web/src/markdown-clipboard.ts‎

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function serializeCodeBlock(pre: Element): string {
7979
}
8080

8181
function serializeTableCell(cell: Element): string {
82-
return serializeChildren(cell).replace(/\n+/g, " ").trim().replaceAll("|", "\\|");
82+
return serializeChildren(cell, true).replace(/\n+/g, " ").trim();
8383
}
8484

8585
function tableSeparatorFor(headerCells: Element[]): string {
@@ -111,14 +111,19 @@ function serializeTable(table: Element): string {
111111
return `${lines.join("\n")}\n\n`;
112112
}
113113

114-
function serializeListItem(item: Element, ordered: boolean, index: number): string {
114+
function serializeListItem(
115+
item: Element,
116+
ordered: boolean,
117+
index: number,
118+
inTableCell: boolean,
119+
): string {
115120
const checkbox = item.querySelector(
116121
':scope > input[type="checkbox"], :scope > p > input[type="checkbox"]',
117122
);
118123
const task = checkbox ? `[${(checkbox as HTMLInputElement).checked ? "x" : " "}] ` : "";
119124
const bullet = ordered ? `${index}. ` : "- ";
120125
const marker = `${bullet}${task}`;
121-
let content = serializeChildren(item)
126+
let content = serializeChildren(item, inTableCell)
122127
.replace(/\n{3,}/g, "\n\n")
123128
.trim();
124129
if (!item.querySelector(":scope > p")) {
@@ -134,15 +139,15 @@ function serializeListItem(item: Element, ordered: boolean, index: number): stri
134139
].join("\n");
135140
}
136141

137-
function serializeList(list: Element, ordered: boolean): string {
142+
function serializeList(list: Element, ordered: boolean, inTableCell: boolean): string {
138143
const start = Number.parseInt(list.getAttribute("start") ?? "1", 10) || 1;
139144
const items = [...list.children].filter((child) => child.tagName === "LI");
140145
if (items.length === 0) return "";
141-
return `${items.map((item, index) => serializeListItem(item, ordered, start + index)).join("\n")}\n\n`;
146+
return `${items.map((item, index) => serializeListItem(item, ordered, start + index, inTableCell)).join("\n")}\n\n`;
142147
}
143148

144-
function serializeBlockquote(quote: Element): string {
145-
const content = serializeChildren(quote)
149+
function serializeBlockquote(quote: Element, inTableCell: boolean): string {
150+
const content = serializeChildren(quote, inTableCell)
146151
.replace(/\n{3,}/g, "\n\n")
147152
.trim();
148153
if (!content) return "";
@@ -163,44 +168,56 @@ function serializeDetails(details: Element): string {
163168
return `<details${open}>\n<summary>${summary}</summary>${content ? `\n\n${content}` : ""}\n</details>\n\n`;
164169
}
165170

166-
function serializeAnchor(anchor: Element): string {
171+
function serializeAnchor(anchor: Element, inTableCell: boolean): string {
167172
const markdownCopy = anchor.getAttribute("data-markdown-copy");
168-
if (markdownCopy !== null) return markdownCopy;
169-
const content = serializeChildren(anchor);
170-
const href = anchor.getAttribute("href") ?? "";
173+
if (markdownCopy !== null) return escapeTablePipes(markdownCopy, inTableCell);
174+
const content = serializeChildren(anchor, inTableCell);
175+
const href = escapeTablePipes(anchor.getAttribute("href") ?? "", inTableCell);
171176
if (!/^https?:\/\//i.test(href)) return content;
172177
const label = content.trim();
173178
if (!label) return "";
174179
if (label === href) return href;
175180
return `[${label}](${href})`;
176181
}
177182

178-
function serializeChildren(node: Node): string {
183+
/**
184+
* GFM splits a table row at every unescaped pipe, but its math-aware parsing
185+
* keeps a formula's pipes in the cell, so formula source is copied unescaped.
186+
*/
187+
function escapeTablePipes(text: string, inTableCell: boolean): string {
188+
return inTableCell ? text.replaceAll("|", "\\|") : text;
189+
}
190+
191+
function serializeChildren(node: Node, inTableCell = false): string {
179192
let out = "";
180193
for (const child of node.childNodes) {
181-
out += serializeNode(child);
194+
out += serializeNode(child, inTableCell);
182195
}
183196
return out;
184197
}
185198

186-
function serializeNode(node: Node): string {
199+
function serializeNode(node: Node, inTableCell = false): string {
187200
if (node.nodeType === Node.TEXT_NODE) {
188201
const text = node.textContent ?? "";
189202
if (text.includes("\n") && text.trim().length === 0) return "\n";
190-
return text;
203+
return escapeTablePipes(text, inTableCell);
191204
}
192205
if (node.nodeType !== Node.ELEMENT_NODE) return "";
193206
const element = node as Element;
194207
if (element.hasAttribute("data-markdown-details")) {
195-
return serializeDetails(element);
208+
return escapeTablePipes(serializeDetails(element), inTableCell);
196209
}
197210
const markdownCopy = element.getAttribute("data-markdown-copy");
198-
if (markdownCopy !== null) return markdownCopy;
211+
if (markdownCopy !== null) {
212+
return element.hasAttribute("data-markdown-math")
213+
? markdownCopy
214+
: escapeTablePipes(markdownCopy, inTableCell);
215+
}
199216
if (isSkippedElement(element)) return "";
200217

201218
const headingLevel = /^H([1-6])$/.exec(element.tagName)?.[1];
202219
if (headingLevel) {
203-
return `${"#".repeat(Number(headingLevel))} ${serializeChildren(element).trim()}\n\n`;
220+
return `${"#".repeat(Number(headingLevel))} ${serializeChildren(element, inTableCell).trim()}\n\n`;
204221
}
205222

206223
switch (element.tagName) {
@@ -209,45 +226,45 @@ function serializeNode(node: Node): string {
209226
case "HR":
210227
return "---\n\n";
211228
case "P":
212-
return `${serializeChildren(element).trim()}\n\n`;
229+
return `${serializeChildren(element, inTableCell).trim()}\n\n`;
213230
case "PRE":
214-
return serializeCodeBlock(element);
231+
return escapeTablePipes(serializeCodeBlock(element), inTableCell);
215232
case "CODE": {
216-
const content = element.textContent ?? "";
233+
const content = escapeTablePipes(element.textContent ?? "", inTableCell);
217234
return isBlockCodeElement(element, content) ? content : wrapInlineCode(content);
218235
}
219236
case "STRONG":
220237
case "B":
221-
return wrapInlineMarker(serializeChildren(element), "**");
238+
return wrapInlineMarker(serializeChildren(element, inTableCell), "**");
222239
case "EM":
223240
case "I":
224-
return wrapInlineMarker(serializeChildren(element), "*");
241+
return wrapInlineMarker(serializeChildren(element, inTableCell), "*");
225242
case "DEL":
226243
case "S":
227-
return wrapInlineMarker(serializeChildren(element), "~~");
244+
return wrapInlineMarker(serializeChildren(element, inTableCell), "~~");
228245
case "A":
229-
return serializeAnchor(element);
246+
return serializeAnchor(element, inTableCell);
230247
case "IMG": {
231-
const alt = element.getAttribute("alt") ?? "";
232-
const src = element.getAttribute("src") ?? "";
248+
const alt = escapeTablePipes(element.getAttribute("alt") ?? "", inTableCell);
249+
const src = escapeTablePipes(element.getAttribute("src") ?? "", inTableCell);
233250
return alt && src ? `![${alt}](${src})` : "";
234251
}
235252
case "UL":
236-
return serializeList(element, false);
253+
return serializeList(element, false, inTableCell);
237254
case "OL":
238-
return serializeList(element, true);
255+
return serializeList(element, true, inTableCell);
239256
case "BLOCKQUOTE":
240-
return serializeBlockquote(element);
257+
return serializeBlockquote(element, inTableCell);
241258
case "TABLE":
242-
return serializeTable(element);
259+
return escapeTablePipes(serializeTable(element), inTableCell);
243260
case "DIV":
244261
case "SECTION":
245262
case "ARTICLE": {
246-
const content = serializeChildren(element);
263+
const content = serializeChildren(element, inTableCell);
247264
return content && !content.endsWith("\n") ? `${content}\n` : content;
248265
}
249266
default:
250-
return serializeChildren(element);
267+
return serializeChildren(element, inTableCell);
251268
}
252269
}
253270

0 commit comments

Comments
 (0)