The best way to control Claude Code, Codex and more from any computer or phone
fix(web): copied code blocks keep their blank lines in lists, quotes and longer fences (#29755)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Alex Southwell committed
Nov 3, 2026 at 16:40 UTC
b7ab02e0f6077a72cadfa5c9a40db4db67bc325f
2 files changed
+44
-14
apps/web/src/markdown-clipboard.test.ts
+26
@@ -206,6 +206,32 @@ describe("serializeRenderedMarkdownFragment", () => {
206
expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("-\n\n```\npnpm test\n```");
207
});
208
209
+ it("keeps blank lines inside a code block in a list item, quote, or longer fence", () => {
210
+ const code = ["import os", "", "", "def main():"];
211
+ const list = new FakeElement("OL").append(
212
+ new FakeElement("LI").append(new FakeText("Create it:\n"), renderedCodeBlock(code)),
213
+ );
214
+ const quote = new FakeElement("BLOCKQUOTE").append(
215
+ new FakeElement("P").append(new FakeText("Note:")),
216
+ renderedCodeBlock(code),
217
+ );
218
+ const nestedFence = new FakeElement("DIV").append(
219
+ new FakeElement("P").append(new FakeText("Example:")),
220
+ renderedCodeBlock(["```js", "a()", "", "", "b() ", "```"]),
221
+ );
222
+
223
+ expect(serializeRenderedMarkdownFragment(asNode(nestedFence))).toBe(
224
+ "Example:\n\n````\n```js\na()\n\n\nb() \n```\n````",
225
+ );
226
+
227
+ expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(list)))).toBe(
228
+ "1. Create it:\n ```\n import os\n\n\n def main():\n ```",
229
+ );
230
+ expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(quote)))).toBe(
231
+ "> Note:\n>\n> ```\n> import os\n>\n>\n> def main():\n> ```",
232
+ );
233
+ });
234
+
235
it("omits fences when a selection past the last line drags in the whole code block", () => {
236
const container = new FakeElement("DIV").append(
237
renderedCodeBlock(["printf '%s' 'TOKEN' | gh secret set CLOUDFLARE_API_TOKEN"]),
apps/web/src/markdown-clipboard.ts
+18
-14
@@ -57,6 +57,14 @@ function codeFenceFor(code: string): string {
57
return "`".repeat(Math.max(3, longestRun + 1));
58
}
59
60
+function outsideCodeFences(markdown: string, tidy: (prose: string) => string): string {
61
+ // A fence closes only at its own opening run, so split yields [prose, fence, run] triples.
62
+ return markdown
63
+ .split(/((`{3,})[\s\S]*?(?:\2|$))/)
64
+ .map((part, index) => (index % 3 === 0 ? tidy(part) : index % 3 === 1 ? part : ""))
65
+ .join("");
66
+}
67
+
68
function resolveCodeBlockLanguage(pre: Element): string | null {
69
const declared =
70
pre.closest("[data-language]")?.getAttribute("data-language") ??
@@ -120,11 +128,11 @@ function serializeListItem(
128
const task = checkbox ? `[${(checkbox as HTMLInputElement).checked ? "x" : " "}] ` : "";
129
const bullet = ordered ? `${index}. ` : "- ";
130
const marker = `${bullet}${task}`;
123
- let content = serializeChildren(item, inTableCell)
124
- .replace(/\n{3,}/g, "\n\n")
125
- .trim();
131
+ let content = outsideCodeFences(serializeChildren(item, inTableCell), (prose) =>
132
+ prose.replace(/\n{3,}/g, "\n\n"),
133
+ ).trim();
134
if (!item.querySelector(":scope > p")) {
127
- content = content.replace(/\n{2,}/g, "\n");
135
+ content = outsideCodeFences(content, (prose) => prose.replace(/\n{2,}/g, "\n"));
136
}
137
// Continuations align after the bullet; task boxes belong to the item text.
138
const continuationIndent = " ".repeat(bullet.length);
@@ -143,9 +151,9 @@ function serializeList(list: Element, ordered: boolean, inTableCell: boolean): s
151
}
152
153
function serializeBlockquote(quote: Element, inTableCell: boolean): string {
146
- const content = serializeChildren(quote, inTableCell)
147
- .replace(/\n{3,}/g, "\n\n")
148
- .trim();
154
+ const content = outsideCodeFences(serializeChildren(quote, inTableCell), (prose) =>
155
+ prose.replace(/\n{3,}/g, "\n\n"),
156
+ ).trim();
157
if (!content) return "";
158
const quoted = content
159
.split("\n")
@@ -316,13 +324,9 @@ function soleCodeBlock(container: Node): Element | null {
324
}
325
326
function tidyMarkdown(markdown: string): string {
319
- return markdown
320
- .split(/(```[\s\S]*?(?:```|$))/)
321
- .map((part, index) =>
322
- index % 2 === 1 ? part : part.replace(/[ \t]+(?=\n)/g, "").replace(/\n{3,}/g, "\n\n"),
323
- )
324
- .join("")
325
- .trim();
327
+ return outsideCodeFences(markdown, (prose) =>
328
+ prose.replace(/[ \t]+(?=\n)/g, "").replace(/\n{3,}/g, "\n\n"),
329
+ ).trim();
330
}
331
332
export function serializeRenderedMarkdownFragment(container: Node): string {