TypeScript
fix(web): copied task lists keep their nested items and code blocks (#14778)
Soroush Baraoufadfdcb67b304
diff --git a/apps/web/src/markdown-clipboard.test.ts b/apps/web/src/markdown-clipboard.test.tsindex c0535e78cec..7db29b325b2 100644--- a/apps/web/src/markdown-clipboard.test.ts+++ b/apps/web/src/markdown-clipboard.test.ts@@ -167,13 +167,13 @@ describe("serializeRenderedMarkdownFragment", () => {parentChecked: false,childChecked: true,parent: "- [ ] Parent",- child: " - [x] Child",+ child: " - [x] Child",},{parentChecked: true,childChecked: false,parent: "- [x] Parent",- child: " - [ ] Child",+ child: " - [ ] Child",},])("copies $parent with $child", ({ parentChecked, childChecked, parent, child }) => {const parentContent = parentLayout === "loose" ? new FakeElement("P") : new FakeElement("LI");@@ -206,6 +206,20 @@ describe("serializeRenderedMarkdownFragment", () => {});});+ it("indents a code block under a numbered task item by the number width only", () => {+ const checkbox = new FakeElement("INPUT", [], { type: "checkbox" });+ checkbox.checked = true;+ const item = new FakeElement("LI").append(+ new FakeElement("P").append(checkbox, new FakeText(" Run the tests")),+ renderedCodeBlock(["pnpm test"]),+ );+ const container = new FakeElement("DIV").append(new FakeElement("OL").append(item));++ expect(serializeRenderedMarkdownFragment(asNode(container))).toBe(+ "1. [x] Run the tests\n\n ```\n pnpm test\n ```",+ );+ });+it("copies the complete quote, source, and comment instead of the comment-only chip label", () => {const citation = {version: 1 as const,diff --git a/apps/web/src/markdown-clipboard.ts b/apps/web/src/markdown-clipboard.tsindex 4f941ce4912..013284bec78 100644--- a/apps/web/src/markdown-clipboard.ts+++ b/apps/web/src/markdown-clipboard.ts@@ -112,14 +112,17 @@ function serializeListItem(item: Element, ordered: boolean, index: number): stri':scope > input[type="checkbox"], :scope > p > input[type="checkbox"]',);const task = checkbox ? `[${(checkbox as HTMLInputElement).checked ? "x" : " "}] ` : "";- const marker = ordered ? `${index}. ${task}` : `- ${task}`;+ const bullet = ordered ? `${index}. ` : "- ";+ const marker = `${bullet}${task}`;let content = serializeChildren(item).replace(/\n{3,}/g, "\n\n").trim();if (!item.querySelector(":scope > p")) {content = content.replace(/\n{2,}/g, "\n");}- const continuationIndent = " ".repeat(marker.length);+ // Nested content lines up with the item text, which starts after the bullet;+ // a task box is part of that text, not of the list marker.+ const continuationIndent = " ".repeat(bullet.length);const [first = "", ...rest] = content.split("\n");return [`${marker}${first}`,