Skip to content

Commit 9a99740

Browse files
fix(web): harden delimited table copy edge cases
Quoted or indented tables no longer keep a stray container prefix, a non-element fragment no longer throws, and table placeholders use a per-copy marker so copied text can't collide with them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ef24c7b commit 9a99740

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ describe("markdownWithTableCopyFormat", () => {
385385
expect(markdownWithTableCopyFormat(ragged, "tsv")).toBe("a\tb\n1\t2\nx\t");
386386
});
387387

388+
it("drops the quote prefix from every row of a quoted table", () => {
389+
const quoted = ["> Note", ">", "> | a | b |", "> | - | - |", "> | 1 | 2 |"].join("\n");
390+
391+
expect(markdownWithTableCopyFormat(quoted, "tsv")).toBe("> Note\n>\na\tb\n1\t2");
392+
});
393+
388394
it("quotes CSV cells that contain commas", () => {
389395
expect(markdownWithTableCopyFormat(message, "csv")).toContain('"Owner, team",Platform');
390396
});

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,6 @@ function tidyMarkdown(markdown: string): string {
343343
.trim();
344344
}
345345

346-
const DELIMITED_TABLE_MARKER = "\uE000";
347-
const DELIMITED_TABLE_PLACEHOLDER = /\uE000(\d+)\uE000/g;
348-
349346
/**
350347
* Serializes a rendered fragment back to markdown. Tables follow `tableFormat`;
351348
* delimited formats replace each table with its TSV or CSV rows.
@@ -356,22 +353,23 @@ export function serializeRenderedMarkdownFragment(
356353
): string {
357354
const codeBlock = soleCodeBlock(container);
358355
if (codeBlock) return (codeBlock.textContent ?? "").replace(/\n$/, "");
359-
if (tableFormat === "markdown") return tidyMarkdown(serializeChildren(container));
356+
if (tableFormat === "markdown" || container.nodeType !== Node.ELEMENT_NODE) {
357+
return tidyMarkdown(serializeChildren(container));
358+
}
360359
// Tag a copy so the caller's fragment still produces untouched HTML. Tables
361360
// stay placeholders through tidyMarkdown, which would strip the tabs of empty
362-
// edge cells and shift the columns after them.
361+
// edge cells and shift the columns after them. The per-copy marker cannot
362+
// collide with copied text.
363363
const tagged = container.cloneNode(true) as Element;
364+
const marker = `\uE000${Math.random().toString(36).slice(2)}\uE000`;
364365
const delimitedTables: string[] = [];
365366
for (const table of tagged.querySelectorAll("table")) {
366-
table.setAttribute(
367-
"data-markdown-copy",
368-
`${DELIMITED_TABLE_MARKER}${delimitedTables.length}${DELIMITED_TABLE_MARKER}\n\n`,
369-
);
367+
table.setAttribute("data-markdown-copy", `${marker}${delimitedTables.length}${marker}\n\n`);
370368
delimitedTables.push(serializeTableElementToDelimited(table, tableFormat));
371369
}
372370
return tidyMarkdown(serializeChildren(tagged)).replace(
373-
DELIMITED_TABLE_PLACEHOLDER,
374-
(_, index: string) => delimitedTables[Number(index)] ?? "",
371+
new RegExp(`${marker}(\\d+)${marker}`, "g"),
372+
(match, index: string) => delimitedTables[Number(index)] ?? match,
375373
);
376374
}
377375

@@ -457,7 +455,12 @@ export function markdownWithTableCopyFormat(markdown: string, format: TableCopyF
457455
return cell ? markdownPlainText(cell) : "";
458456
}),
459457
);
460-
result = `${result.slice(0, start)}${delimitedRows(rows, format)}${result.slice(end)}`;
458+
// A table in a blockquote or list starts after its line's container prefix
459+
// ("> ", indentation) and swallows that prefix on later lines. Replace from
460+
// the line start so no row keeps a stray marker.
461+
const lineStart = result.lastIndexOf("\n", start - 1) + 1;
462+
const replaceFrom = /^[\s>]*$/.test(result.slice(lineStart, start)) ? lineStart : start;
463+
result = `${result.slice(0, replaceFrom)}${delimitedRows(rows, format)}${result.slice(end)}`;
461464
}
462465
return result;
463466
}

0 commit comments

Comments
 (0)