Skip to content

Commit 02e9d24

Browse files
fixfonclaude
andauthored
fix(web): a comment under a composer quote no longer joins the quote (pingdotgg#19450)
Co-authored-by: Claude Sonnet 5.5 <[email protected]>
1 parent 7506aed commit 02e9d24

2 files changed

Lines changed: 98 additions & 1 deletion

File tree

‎apps/web/src/composer-rich-text-doc.test.ts‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,74 @@ describe("composer rich text document model", () => {
472472
"> **bold** and @README.md inside",
473473
"> - looks like a list but stays quote text",
474474
"> > nested stays literal inside the quote",
475-
"before\n> quoted\nafter",
475+
"before\n> quoted",
476476
"> quote\n\n> another",
477477
"- item\n> quote after list",
478+
"> quoted\n\ncomment",
479+
"> line one\n> line two\n\ncomment",
478480
])("round-trips the quote %s through a real ProseMirror document", (value) => {
479481
expect(roundTrip(value).value).toBe(value);
480482
});
481483

484+
describe("a paragraph after a quote", () => {
485+
const paragraph = (text?: string) => ({
486+
type: "paragraph",
487+
...(text ? { content: [{ type: "text", text }] } : {}),
488+
});
489+
const quote = (...lines: string[]) => ({
490+
type: "blockquote",
491+
attrs: { prefix: "> " },
492+
content: lines.map((line) => paragraph(line)),
493+
});
494+
const serialize = (...content: object[]) => {
495+
const doc = ProseMirrorNode.fromJSON(schema, { type: "doc", content });
496+
doc.check();
497+
return { doc, map: serializeEditorDoc(doc) };
498+
};
499+
500+
it("is written after a blank line, or Markdown folds it into the quote", () => {
501+
expect(serialize(quote("quoted"), paragraph("Comment to it")).map.value).toBe(
502+
"> quoted\n\nComment to it",
503+
);
504+
expect(serialize(quote("a", "b"), paragraph("comment")).map.value).toBe(
505+
"> a\n> b\n\ncomment",
506+
);
507+
});
508+
509+
it("does not double up on the empty line the user left themselves", () => {
510+
expect(serialize(quote("quoted"), paragraph(), paragraph("Comment to it")).map.value).toBe(
511+
"> quoted\n\nComment to it",
512+
);
513+
});
514+
515+
it("settles on the blank line when a draft glued it to the quote", () => {
516+
const settled = "before\n> quoted\n\nafter";
517+
expect(roundTrip("before\n> quoted\nafter").value).toBe(settled);
518+
expect(roundTrip(settled).value).toBe(settled);
519+
});
520+
521+
it.each(["# Heading", "- item", "3. item", "---", "```\ncode\n```"])(
522+
"leaves %s on the next line, since it opens its own block",
523+
(next) => {
524+
const value = `> quoted\n${next}`;
525+
expect(roundTrip(value).value).toBe(value);
526+
},
527+
);
528+
529+
it("keeps every offset mapped through the blank line", () => {
530+
const { doc, map } = serialize(quote("quoted"), paragraph("Comment"));
531+
for (let flat = 0; flat <= map.docLength; flat += 1) {
532+
expect(collapsedToFlat(map, flatToCollapsed(map, flat))).toBe(flat);
533+
}
534+
// The caret on the end of the quote stays on it, and the blank line is
535+
// not a place to type: it clamps to the start of the comment.
536+
expect(flatToCollapsed(map, "quoted".length)).toBe("> quoted".length);
537+
expect(collapsedToFlat(map, "> quoted\n".length)).toBe("quoted".length + 1);
538+
expect(flatToCollapsed(map, "quoted".length + 1)).toBe("> quoted\n\n".length);
539+
expect(flatToPm(map, "quoted".length + 1)).toBe(doc.child(0).nodeSize + 1);
540+
});
541+
});
542+
482543
it.each([
483544
"---",
484545
"***",

‎apps/web/src/composer-rich-text-doc.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,39 @@ function pushBreakRun(acc: RichAccumulator, position?: number): void {
741741
acc.md += 1;
742742
}
743743

744+
/**
745+
* A paragraph on the line right after a quote reads as part of it in Markdown
746+
* (a lazy continuation), so the draft writes a blank line between them. An empty
747+
* paragraph is that blank line already.
748+
*/
749+
function needsBlankLineAfterQuote(previous: ProseMirrorNode, block: ProseMirrorNode): boolean {
750+
return (
751+
previous.type.name === "blockquote" && block.type.name === "paragraph" && block.content.size > 0
752+
);
753+
}
754+
755+
/**
756+
* That blank line, as a run that owns no document characters, like a marker:
757+
* offsets inside it clamp to the start of the paragraph.
758+
*/
759+
function pushBlankLineRun(acc: RichAccumulator, pmPos: number): void {
760+
acc.runs.push({
761+
kind: "prefix",
762+
flatStart: acc.flat,
763+
docLen: 0,
764+
collapsedLen: 1,
765+
mdLen: 1,
766+
openLen: 0,
767+
closeLen: 0,
768+
pmPos,
769+
mdStart: acc.md,
770+
collapsedStart: acc.collapsed,
771+
});
772+
acc.value += "\n";
773+
acc.collapsed += 1;
774+
acc.md += 1;
775+
}
776+
744777
function appendInlineRuns(
745778
container: ProseMirrorNode,
746779
contentStart: number,
@@ -1197,6 +1230,9 @@ export function serializeEditorDoc(doc: ProseMirrorNode): RichDocMap {
11971230
let pmBlockStart = 0;
11981231
blocks.forEach((block, blockIndex) => {
11991232
if (blockIndex > 0 && !isTrailingLineAfterRule(doc, blockIndex)) pushBreakRun(acc);
1233+
if (blockIndex > 0 && needsBlankLineAfterQuote(blocks[blockIndex - 1]!, block)) {
1234+
pushBlankLineRun(acc, pmBlockStart + 1);
1235+
}
12001236
if (LIST_NODE_NAMES.has(block.type.name)) {
12011237
walkList(block, pmBlockStart, acc);
12021238
} else if (block.type.name === "codeBlock") {

0 commit comments

Comments
 (0)