Skip to content

Commit b62e80e

Browse files
fix(web): copying an alert callout keeps its alert Markdown (pingdotgg#35932)
1 parent bedc865 commit b62e80e

4 files changed

Lines changed: 94 additions & 6 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ vi.mock(import("../hooks/useSettings"), async (original) => {
120120
});
121121

122122
import ChatMarkdown from "./ChatMarkdown";
123+
import { chatMarkdownClipboardPayload } from "../markdown-clipboard";
123124
import { AudioPreview } from "./files/AudioPreview";
124125

125126
it("keeps compact audio playback loaded across signed URL renewals", async () => {
@@ -617,6 +618,30 @@ describe("ChatMarkdown inline code selection", () => {
617618
});
618619
});
619620

621+
describe("ChatMarkdown alert copy", () => {
622+
it("copies a fully selected callout as alert Markdown and a partial one as text", async () => {
623+
setup();
624+
await using screen = await renderText("> [!WARNING]\n> Back up first.");
625+
const [label, body] = screen.container.querySelectorAll('[role="note"] > p');
626+
const select = (startNode: Node, endNode: Node, endOffset: number) => {
627+
const range = document.createRange();
628+
range.setStart(startNode, 0);
629+
range.setEnd(endNode, endOffset);
630+
const selection = window.getSelection()!;
631+
selection.removeAllRanges();
632+
selection.addRange(range);
633+
return chatMarkdownClipboardPayload(selection)?.text;
634+
};
635+
const labelText = label!.lastChild!;
636+
const bodyText = body!.firstChild!;
637+
638+
expect(select(labelText, bodyText, bodyText.textContent!.length)).toBe(
639+
"> [!WARNING]\n> Back up first.",
640+
);
641+
expect(select(labelText, bodyText, "Back up".length)).toBe("Warning\n\nBack up");
642+
});
643+
});
644+
620645
describe("ChatMarkdown file chip tooltips", () => {
621646
it("wraps a long path so its file name stays inside the tooltip", async () => {
622647
setup();

‎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-alert-label=""
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,38 @@ describe("serializeRenderedMarkdownFragment", () => {
198198
expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("first line\nsecond line");
199199
});
200200

201+
it("copies alert and insight callouts as their Markdown instead of the rendered label", () => {
202+
// The shape ChatMarkdown renders for `> [!WARNING]\n> Back up first.`.
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-alert-label": "",
210+
}).append(new FakeElement("SVG", [], { "aria-hidden": "true" }), new FakeText("Warning")),
211+
new FakeElement("P").append(new FakeText("Back up first.")),
212+
);
213+
const container = new FakeElement("DIV").append(callout);
214+
215+
expect(serializeRenderedMarkdownFragment(asNode(container))).toBe(
216+
"> [!WARNING]\n> Back up first.",
217+
);
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-alert-label": "",
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+
);
231+
});
232+
201233
it("preserves explicit ordered-list values and continues from resets when copying", () => {
202234
const list = new FakeElement("OL", [], { start: "0" }).append(
203235
new FakeElement("LI").append(new FakeText("A")),

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const SANITIZED_HTML_SELECTOR = [
1010
...SKIPPED_CLASS_NAMES.map((className) => `.${className}`),
1111
].join(", ");
1212

13+
const ALERT_ATTRIBUTE = "data-markdown-alert";
14+
const ALERT_LABEL = "data-markdown-alert-label";
15+
1316
export interface MarkdownClipboardPayload {
1417
text: string;
1518
html: string;
@@ -159,10 +162,21 @@ function serializeList(list: Element, ordered: boolean, inTableCell: boolean): s
159162
.join("\n")}\n\n`;
160163
}
161164

162-
function serializeBlockquote(quote: Element, inTableCell: boolean): string {
163-
const content = outsideCode(serializeChildren(quote, inTableCell), (prose) =>
164-
prose.replace(/\n{3,}/g, "\n\n"),
165-
).trim();
165+
function serializeQuoteBody(quote: Element, inTableCell: boolean): string {
166+
let content = "";
167+
for (const child of quote.childNodes) {
168+
// A callout's rendered label stands for the marker its serializer writes.
169+
if (child.nodeType === Node.ELEMENT_NODE && (child as Element).hasAttribute(ALERT_LABEL)) {
170+
continue;
171+
}
172+
content += serializeNode(child, inTableCell);
173+
}
174+
return outsideCode(content, (prose) => prose.replace(/\n{3,}/g, "\n\n")).trim();
175+
}
176+
177+
function serializeBlockquote(quote: Element, inTableCell: boolean, marker = ""): string {
178+
const body = serializeQuoteBody(quote, inTableCell);
179+
const content = marker && body ? `${marker}\n${body}` : marker || body;
166180
if (!content) return "";
167181
const quoted = content
168182
.split("\n")
@@ -171,6 +185,11 @@ function serializeBlockquote(quote: Element, inTableCell: boolean): string {
171185
return `${quoted}\n\n`;
172186
}
173187

188+
function serializeInsight(insight: Element, inTableCell: boolean): string {
189+
const body = serializeQuoteBody(insight, inTableCell);
190+
return `★ Insight ${"─".repeat(37)}\n${body ? `${body}\n` : ""}${"─".repeat(49)}\n\n`;
191+
}
192+
174193
function serializeDetails(details: Element): string {
175194
const summary =
176195
details.querySelector(":scope > [data-markdown-details-summary]")?.textContent?.trim() ??
@@ -231,6 +250,9 @@ function serializeNode(node: Node, inTableCell = false): string {
231250
: escapeTablePipes(markdownCopy, inTableCell);
232251
}
233252
if (isSkippedElement(element)) return "";
253+
const alert = element.getAttribute(ALERT_ATTRIBUTE);
254+
if (alert === "insight") return serializeInsight(element, inTableCell);
255+
if (alert !== null) return serializeBlockquote(element, inTableCell, `[!${alert.toUpperCase()}]`);
234256

235257
const headingLevel = /^H([1-6])$/.exec(element.tagName)?.[1];
236258
if (headingLevel) {
@@ -478,6 +500,13 @@ export function chatMarkdownClipboardPayload(
478500
}
479501
continue;
480502
}
503+
// A range spanning exactly one callout's text clones its contents but not the callout.
504+
const callout = ancestorElement?.closest(`[${ALERT_ATTRIBUTE}]`);
505+
if (callout && range.toString().trim() === (callout.textContent ?? "").trim()) {
506+
const wrapper = callout.cloneNode(false) as Element;
507+
wrapper.append(...container.childNodes);
508+
container.replaceChildren(wrapper);
509+
}
481510
const text = serializeRenderedMarkdownFragment(container);
482511
if (!text) continue;
483512
texts.push(text);

0 commit comments

Comments
 (0)