Skip to content
TypeScript

fix(web): a comment under a composer quote no longer joins the quote (#19450)

fixfon02e9d2417a46
diff --git a/apps/web/src/composer-rich-text-doc.test.ts b/apps/web/src/composer-rich-text-doc.test.ts
index 3fa9eb6a0be..d60590cbbb0 100644
--- a/apps/web/src/composer-rich-text-doc.test.ts
+++ b/apps/web/src/composer-rich-text-doc.test.ts
@@ -472,13 +472,74 @@ describe("composer rich text document model", () => {
"> **bold** and @README.md inside",
"> - looks like a list but stays quote text",
"> > nested stays literal inside the quote",
- "before\n> quoted\nafter",
+ "before\n> quoted",
"> quote\n\n> another",
"- item\n> quote after list",
+ "> quoted\n\ncomment",
+ "> line one\n> line two\n\ncomment",
])("round-trips the quote %s through a real ProseMirror document", (value) => {
expect(roundTrip(value).value).toBe(value);
});
+ describe("a paragraph after a quote", () => {
+ const paragraph = (text?: string) => ({
+ type: "paragraph",
+ ...(text ? { content: [{ type: "text", text }] } : {}),
+ });
+ const quote = (...lines: string[]) => ({
+ type: "blockquote",
+ attrs: { prefix: "> " },
+ content: lines.map((line) => paragraph(line)),
+ });
+ const serialize = (...content: object[]) => {
+ const doc = ProseMirrorNode.fromJSON(schema, { type: "doc", content });
+ doc.check();
+ return { doc, map: serializeEditorDoc(doc) };
+ };
+
+ it("is written after a blank line, or Markdown folds it into the quote", () => {
+ expect(serialize(quote("quoted"), paragraph("Comment to it")).map.value).toBe(
+ "> quoted\n\nComment to it",
+ );
+ expect(serialize(quote("a", "b"), paragraph("comment")).map.value).toBe(
+ "> a\n> b\n\ncomment",
+ );
+ });
+
+ it("does not double up on the empty line the user left themselves", () => {
+ expect(serialize(quote("quoted"), paragraph(), paragraph("Comment to it")).map.value).toBe(
+ "> quoted\n\nComment to it",
+ );
+ });
+
+ it("settles on the blank line when a draft glued it to the quote", () => {
+ const settled = "before\n> quoted\n\nafter";
+ expect(roundTrip("before\n> quoted\nafter").value).toBe(settled);
+ expect(roundTrip(settled).value).toBe(settled);
+ });
+
+ it.each(["# Heading", "- item", "3. item", "---", "```\ncode\n```"])(
+ "leaves %s on the next line, since it opens its own block",
+ (next) => {
+ const value = `> quoted\n${next}`;
+ expect(roundTrip(value).value).toBe(value);
+ },
+ );
+
+ it("keeps every offset mapped through the blank line", () => {
+ const { doc, map } = serialize(quote("quoted"), paragraph("Comment"));
+ for (let flat = 0; flat <= map.docLength; flat += 1) {
+ expect(collapsedToFlat(map, flatToCollapsed(map, flat))).toBe(flat);
+ }
+ // The caret on the end of the quote stays on it, and the blank line is
+ // not a place to type: it clamps to the start of the comment.
+ expect(flatToCollapsed(map, "quoted".length)).toBe("> quoted".length);
+ expect(collapsedToFlat(map, "> quoted\n".length)).toBe("quoted".length + 1);
+ expect(flatToCollapsed(map, "quoted".length + 1)).toBe("> quoted\n\n".length);
+ expect(flatToPm(map, "quoted".length + 1)).toBe(doc.child(0).nodeSize + 1);
+ });
+ });
+
it.each([
"---",
"***",
diff --git a/apps/web/src/composer-rich-text-doc.ts b/apps/web/src/composer-rich-text-doc.ts
index 2f778cf6059..4c365c3afcd 100644
--- a/apps/web/src/composer-rich-text-doc.ts
+++ b/apps/web/src/composer-rich-text-doc.ts
@@ -741,6 +741,39 @@ function pushBreakRun(acc: RichAccumulator, position?: number): void {
acc.md += 1;
}
+/**
+ * A paragraph on the line right after a quote reads as part of it in Markdown
+ * (a lazy continuation), so the draft writes a blank line between them. An empty
+ * paragraph is that blank line already.
+ */
+function needsBlankLineAfterQuote(previous: ProseMirrorNode, block: ProseMirrorNode): boolean {
+ return (
+ previous.type.name === "blockquote" && block.type.name === "paragraph" && block.content.size > 0
+ );
+}
+
+/**
+ * That blank line, as a run that owns no document characters, like a marker:
+ * offsets inside it clamp to the start of the paragraph.
+ */
+function pushBlankLineRun(acc: RichAccumulator, pmPos: number): void {
+ acc.runs.push({
+ kind: "prefix",
+ flatStart: acc.flat,
+ docLen: 0,
+ collapsedLen: 1,
+ mdLen: 1,
+ openLen: 0,
+ closeLen: 0,
+ pmPos,
+ mdStart: acc.md,
+ collapsedStart: acc.collapsed,
+ });
+ acc.value += "\n";
+ acc.collapsed += 1;
+ acc.md += 1;
+}
+
function appendInlineRuns(
container: ProseMirrorNode,
contentStart: number,
@@ -1197,6 +1230,9 @@ export function serializeEditorDoc(doc: ProseMirrorNode): RichDocMap {
let pmBlockStart = 0;
blocks.forEach((block, blockIndex) => {
if (blockIndex > 0 && !isTrailingLineAfterRule(doc, blockIndex)) pushBreakRun(acc);
+ if (blockIndex > 0 && needsBlankLineAfterQuote(blocks[blockIndex - 1]!, block)) {
+ pushBlankLineRun(acc, pmBlockStart + 1);
+ }
if (LIST_NODE_NAMES.has(block.type.name)) {
walkList(block, pmBlockStart, acc);
} else if (block.type.name === "codeBlock") {