Skip to content

Commit 0486b93

Browse files
saphidclaude
andcommitted
fix(web): copied list items and quotes keep blank lines inside their code blocks
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5b9731c commit 0486b93

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,24 @@ describe("serializeRenderedMarkdownFragment", () => {
206206
expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("-\n\n```\npnpm test\n```");
207207
});
208208

209+
it("keeps blank lines inside a code block in a list item or quote", () => {
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+
219+
expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(list)))).toBe(
220+
"1. Create it:\n ```\n import os\n\n\n def main():\n ```",
221+
);
222+
expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(quote)))).toBe(
223+
"> Note:\n>\n> ```\n> import os\n>\n>\n> def main():\n> ```",
224+
);
225+
});
226+
209227
it("omits fences when a selection past the last line drags in the whole code block", () => {
210228
const container = new FakeElement("DIV").append(
211229
renderedCodeBlock(["printf '%s' 'TOKEN' | gh secret set CLOUDFLARE_API_TOKEN"]),

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ function codeFenceFor(code: string): string {
5757
return "`".repeat(Math.max(3, longestRun + 1));
5858
}
5959

60+
function outsideCodeFences(markdown: string, tidy: (prose: string) => string): string {
61+
return markdown
62+
.split(/(```[\s\S]*?(?:```|$))/)
63+
.map((part, index) => (index % 2 === 1 ? part : tidy(part)))
64+
.join("");
65+
}
66+
6067
function resolveCodeBlockLanguage(pre: Element): string | null {
6168
const declared =
6269
pre.closest("[data-language]")?.getAttribute("data-language") ??
@@ -120,11 +127,11 @@ function serializeListItem(
120127
const task = checkbox ? `[${(checkbox as HTMLInputElement).checked ? "x" : " "}] ` : "";
121128
const bullet = ordered ? `${index}. ` : "- ";
122129
const marker = `${bullet}${task}`;
123-
let content = serializeChildren(item, inTableCell)
124-
.replace(/\n{3,}/g, "\n\n")
125-
.trim();
130+
let content = outsideCodeFences(serializeChildren(item, inTableCell), (prose) =>
131+
prose.replace(/\n{3,}/g, "\n\n"),
132+
).trim();
126133
if (!item.querySelector(":scope > p")) {
127-
content = content.replace(/\n{2,}/g, "\n");
134+
content = outsideCodeFences(content, (prose) => prose.replace(/\n{2,}/g, "\n"));
128135
}
129136
// Continuations align after the bullet; task boxes belong to the item text.
130137
const continuationIndent = " ".repeat(bullet.length);
@@ -143,9 +150,9 @@ function serializeList(list: Element, ordered: boolean, inTableCell: boolean): s
143150
}
144151

145152
function serializeBlockquote(quote: Element, inTableCell: boolean): string {
146-
const content = serializeChildren(quote, inTableCell)
147-
.replace(/\n{3,}/g, "\n\n")
148-
.trim();
153+
const content = outsideCodeFences(serializeChildren(quote, inTableCell), (prose) =>
154+
prose.replace(/\n{3,}/g, "\n\n"),
155+
).trim();
149156
if (!content) return "";
150157
const quoted = content
151158
.split("\n")
@@ -316,13 +323,9 @@ function soleCodeBlock(container: Node): Element | null {
316323
}
317324

318325
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();
326+
return outsideCodeFences(markdown, (prose) =>
327+
prose.replace(/[ \t]+(?=\n)/g, "").replace(/\n{3,}/g, "\n\n"),
328+
).trim();
326329
}
327330

328331
export function serializeRenderedMarkdownFragment(container: Node): string {

0 commit comments

Comments
 (0)