Skip to content

Commit e5417c2

Browse files
fix(web): keep image alt text in delimited table cells
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9a99740 commit e5417c2

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,21 @@ describe("chatMarkdownClipboardPayload", () => {
3737
);
3838
});
3939

40-
it("keeps empty edge cells and line breaks inside cells", () => {
40+
it("keeps empty edge cells, line breaks, and image alt text inside cells", () => {
4141
document.body.innerHTML = `
4242
<p>Status</p>
4343
<table>
4444
<thead><tr><th>Question</th><th>Owner</th></tr></thead>
4545
<tbody>
4646
<tr><td></td><td>Platform</td></tr>
4747
<tr><td>first<br>second</td><td></td></tr>
48+
<tr><td><img alt="Build badge" src="b.svg"></td><td>ok</td></tr>
4849
</tbody>
4950
</table>`;
5051
const selection = selectBetween(document.body, document.body);
5152

5253
expect(chatMarkdownClipboardPayload(selection, "tsv")?.text).toBe(
53-
"Status\n\nQuestion\tOwner\n\tPlatform\nfirst second\t",
54+
"Status\n\nQuestion\tOwner\n\tPlatform\nfirst second\t\nBuild badge\tok",
5455
);
5556
});
5657

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ describe("markdownWithTableCopyFormat", () => {
355355
"| --- | --- |",
356356
"| **Loader** URL? | See [docs](https://example.com) and `v2\\|v3` |",
357357
"| Owner, team | Platform |",
358+
"| ![Build badge](https://example.com/b.svg) | ok |",
358359
"",
359360
"```md",
360361
"| a | b |",
@@ -370,6 +371,7 @@ describe("markdownWithTableCopyFormat", () => {
370371
"Question\tAnswer",
371372
"Loader URL?\tSee docs and v2|v3",
372373
"Owner, team\tPlatform",
374+
"Build badge\tok",
373375
"",
374376
"```md",
375377
"| a | b |",

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,16 @@ function delimitedRows(rows: ReadonlyArray<ReadonlyArray<string>>, format: "tsv"
391391
.join("\n");
392392
}
393393

394-
/** Cell text with `<br>` kept as a word boundary, which `textContent` drops. */
394+
/**
395+
* Cell text with `<br>` kept as a word boundary and images as their alt text,
396+
* both of which `textContent` drops.
397+
*/
395398
function tableCellText(node: Node): string {
396399
if (node.nodeType === Node.TEXT_NODE) return node.textContent ?? "";
397400
if (node.nodeType !== Node.ELEMENT_NODE) return "";
398-
if ((node as Element).tagName === "BR") return " ";
401+
const element = node as Element;
402+
if (element.tagName === "BR") return " ";
403+
if (element.tagName === "IMG") return element.getAttribute("alt") ?? "";
399404
return [...node.childNodes].map(tableCellText).join("");
400405
}
401406

@@ -418,6 +423,7 @@ const markdownTableParser = unified().use(remarkParse).use(remarkGfm);
418423

419424
function markdownPlainText(node: Nodes): string {
420425
if (node.type === "html" || node.type === "break") return " ";
426+
if (node.type === "image" || node.type === "imageReference") return node.alt ?? "";
421427
if ("value" in node) return node.value;
422428
if ("children" in node) return node.children.map(markdownPlainText).join("");
423429
return "";

0 commit comments

Comments
 (0)