Skip to content

Commit 540e0ed

Browse files
fix(web): copying an alert callout keeps its alert Markdown
ChatMarkdown renders GitHub alerts and Claude insights as a div with a label paragraph, which the clipboard serializer copied as plain paragraphs ("Warning\n\nBack up first."). The callout now carries its alert kind and its label is excluded from copy, so alerts copy as "> [!WARNING]\n> ..." and insights as their ★ Insight rule block. Fixes #35894 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 946bf93 commit 540e0ed

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,19 +1055,21 @@ const CHAT_MARKDOWN_COMPONENTS = {
10551055
);
10561056
},
10571057
blockquote: function MarkdownBlockquote({ node: _node, children, ...props }) {
1058-
const alert =
1059-
GITHUB_ALERT_PRESENTATIONS[String((props as Record<string, unknown>)["data-alert"] ?? "")];
1058+
const alertKind = String((props as Record<string, unknown>)["data-alert"] ?? "");
1059+
const alert = GITHUB_ALERT_PRESENTATIONS[alertKind];
10601060
if (!alert) {
10611061
return <blockquote {...props}>{children}</blockquote>;
10621062
}
10631063
return (
10641064
<div
10651065
role="note"
1066+
data-markdown-alert={alertKind}
10661067
dir={props.dir}
10671068
className={cn("my-1 border-s-2 ps-3", alert.borderClassName)}
10681069
>
10691070
<p
10701071
data-thread-find-ignore="true"
1072+
data-markdown-copy=""
10711073
className={cn("flex items-center gap-1.5 font-medium", alert.titleClassName)}
10721074
>
10731075
<alert.Icon aria-hidden className="size-3.5 shrink-0" />

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,36 @@ describe("serializeRenderedMarkdownFragment", () => {
198198
expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("first line\nsecond line");
199199
});
200200

201-
it("copies a GitHub alert callout as alert Markdown instead of its rendered label", () => {
201+
it("copies alert and insight callouts as their Markdown instead of the rendered label", () => {
202202
// The shape ChatMarkdown renders for `> [!WARNING]\n> Back up first.`.
203-
const callout = new FakeElement("DIV", [], { role: "note" }).append(
204-
new FakeElement("P", [], { "data-thread-find-ignore": "true" }).append(
205-
new FakeElement("SVG", [], { "aria-hidden": "true" }),
206-
new FakeText("Warning"),
207-
),
203+
const callout = new FakeElement("DIV", [], {
204+
role: "note",
205+
"data-markdown-alert": "warning",
206+
}).append(
207+
new FakeElement("P", [], {
208+
"data-thread-find-ignore": "true",
209+
"data-markdown-copy": "",
210+
}).append(new FakeElement("SVG", [], { "aria-hidden": "true" }), new FakeText("Warning")),
208211
new FakeElement("P").append(new FakeText("Back up first.")),
209212
);
210213
const container = new FakeElement("DIV").append(callout);
211214

212215
expect(serializeRenderedMarkdownFragment(asNode(container))).toBe(
213216
"> [!WARNING]\n> Back up first.",
214217
);
218+
const insight = new FakeElement("DIV", [], {
219+
role: "note",
220+
"data-markdown-alert": "insight",
221+
}).append(
222+
new FakeElement("P", [], {
223+
"data-thread-find-ignore": "true",
224+
"data-markdown-copy": "",
225+
}).append(new FakeText("Insight")),
226+
new FakeElement("P").append(new FakeText("Caches are warm.")),
227+
);
228+
expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(insight)))).toBe(
229+
`★ Insight ${"─".repeat(37)}\nCaches are warm.\n${"─".repeat(49)}`,
230+
);
215231
});
216232

217233
it("preserves explicit ordered-list values and continues from resets when copying", () => {

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ function serializeList(list: Element, ordered: boolean, inTableCell: boolean): s
159159
.join("\n")}\n\n`;
160160
}
161161

162-
function serializeBlockquote(quote: Element, inTableCell: boolean): string {
163-
const content = outsideCode(serializeChildren(quote, inTableCell), (prose) =>
162+
function serializeBlockquote(quote: Element, inTableCell: boolean, marker = ""): string {
163+
const body = outsideCode(serializeChildren(quote, inTableCell), (prose) =>
164164
prose.replace(/\n{3,}/g, "\n\n"),
165165
).trim();
166+
const content = marker && body ? `${marker}\n${body}` : marker || body;
166167
if (!content) return "";
167168
const quoted = content
168169
.split("\n")
@@ -171,6 +172,13 @@ function serializeBlockquote(quote: Element, inTableCell: boolean): string {
171172
return `${quoted}\n\n`;
172173
}
173174

175+
function serializeInsight(insight: Element, inTableCell: boolean): string {
176+
const body = outsideCode(serializeChildren(insight, inTableCell), (prose) =>
177+
prose.replace(/\n{3,}/g, "\n\n"),
178+
).trim();
179+
return `★ Insight ${"─".repeat(37)}\n${body ? `${body}\n` : ""}${"─".repeat(49)}\n\n`;
180+
}
181+
174182
function serializeDetails(details: Element): string {
175183
const summary =
176184
details.querySelector(":scope > [data-markdown-details-summary]")?.textContent?.trim() ??
@@ -231,6 +239,9 @@ function serializeNode(node: Node, inTableCell = false): string {
231239
: escapeTablePipes(markdownCopy, inTableCell);
232240
}
233241
if (isSkippedElement(element)) return "";
242+
const alert = element.getAttribute("data-markdown-alert");
243+
if (alert === "insight") return serializeInsight(element, inTableCell);
244+
if (alert !== null) return serializeBlockquote(element, inTableCell, `[!${alert.toUpperCase()}]`);
234245

235246
const headingLevel = /^H([1-6])$/.exec(element.tagName)?.[1];
236247
if (headingLevel) {

0 commit comments

Comments
 (0)