Skip to content

Commit bc04cff

Browse files
SoroushRFclaude
andcommitted
fix(web): copied task lists keep their nested items and code blocks
Copying a rendered task item indented its nested lines past the "[ ] " box, 6 spaces under "- [ ]" and 7 under "1. [x]". Markdown measures list continuation from the end of the bullet, so the nested list pasted as plain text inside the parent line and a nested code block pasted as an indented code block with its fences showing. Indent nested content by the bullet width only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c2fa9fc commit bc04cff

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ describe("serializeRenderedMarkdownFragment", () => {
169169
parentChecked: false,
170170
childChecked: true,
171171
parent: "- [ ] Parent",
172-
child: " - [x] Child",
172+
child: " - [x] Child",
173173
},
174174
{
175175
parentChecked: true,
176176
childChecked: false,
177177
parent: "- [x] Parent",
178-
child: " - [ ] Child",
178+
child: " - [ ] Child",
179179
},
180180
])("copies $parent with $child", ({ parentChecked, childChecked, parent, child }) => {
181181
const parentContent = parentLayout === "loose" ? new FakeElement("P") : new FakeElement("LI");
@@ -208,6 +208,20 @@ describe("serializeRenderedMarkdownFragment", () => {
208208
});
209209
});
210210

211+
it("indents a code block under a numbered task item by the number width only", () => {
212+
const checkbox = new FakeElement("INPUT", [], { type: "checkbox" });
213+
checkbox.checked = true;
214+
const item = new FakeElement("LI").append(
215+
new FakeElement("P").append(checkbox, new FakeText(" Run the tests")),
216+
renderedCodeBlock(["pnpm test"]),
217+
);
218+
const container = new FakeElement("DIV").append(new FakeElement("OL").append(item));
219+
220+
expect(serializeRenderedMarkdownFragment(asNode(container))).toBe(
221+
"1. [x] Run the tests\n\n ```\n pnpm test\n ```",
222+
);
223+
});
224+
211225
it("copies the complete quote, source, and comment instead of the comment-only chip label", () => {
212226
const citation = {
213227
version: 1 as const,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,18 @@ function serializeListItem(item: Element, ordered: boolean, index: number): stri
123123
':scope > input[type="checkbox"], :scope > p > input[type="checkbox"]',
124124
);
125125
const task = checkbox ? `[${(checkbox as HTMLInputElement).checked ? "x" : " "}] ` : "";
126-
const marker = ordered ? `${index}. ${task}` : `- ${task}`;
126+
const bullet = ordered ? `${index}. ` : "- ";
127+
const marker = `${bullet}${task}`;
127128
let content = serializeChildren(item)
128129
.replace(/\n{3,}/g, "\n\n")
129130
.trim();
130131
// Tight list items (no paragraph children) keep nested lists on adjacent lines.
131132
if (!item.querySelector(":scope > p")) {
132133
content = content.replace(/\n{2,}/g, "\n");
133134
}
134-
const continuationIndent = " ".repeat(marker.length);
135+
// Nested content lines up with the item text, which starts after the bullet;
136+
// a task box is part of that text, not of the list marker.
137+
const continuationIndent = " ".repeat(bullet.length);
135138
const [first = "", ...rest] = content.split("\n");
136139
return [
137140
`${marker}${first}`,

0 commit comments

Comments
 (0)