Skip to content

Commit 03bc160

Browse files
fixfonclaude
andcommitted
fix(web): a comment under a composer quote no longer joins the quote
The composer wrote a paragraph on the line right after a quote, which Markdown reads as a lazy continuation of the quote, so a comment typed below a quote was sent quoted. It now writes a blank line between them. An empty paragraph the user left is already that blank line, so it does not get a second one. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 8414d16 commit 03bc160

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
@@ -475,13 +475,74 @@ describe("composer rich text document model", () => {
475475
"> **bold** and @README.md inside",
476476
"> - looks like a list but stays quote text",
477477
"> > nested stays literal inside the quote",
478-
"before\n> quoted\nafter",
478+
"before\n> quoted",
479479
"> quote\n\n> another",
480480
"- item\n> quote after list",
481+
"> quoted\n\ncomment",
482+
"> line one\n> line two\n\ncomment",
481483
])("round-trips the quote %s through a real ProseMirror document", (value) => {
482484
expect(roundTrip(value).value).toBe(value);
483485
});
484486

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

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

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

863+
/**
864+
* A paragraph on the line right after a quote reads as part of it in Markdown
865+
* (a lazy continuation), so the draft writes a blank line between them. An empty
866+
* paragraph is that blank line already.
867+
*/
868+
function needsBlankLineAfterQuote(previous: ProseMirrorNode, block: ProseMirrorNode): boolean {
869+
return (
870+
previous.type.name === "blockquote" && block.type.name === "paragraph" && block.content.size > 0
871+
);
872+
}
873+
874+
/**
875+
* That blank line, as a run that owns no document characters, like a marker:
876+
* offsets inside it clamp to the start of the paragraph.
877+
*/
878+
function pushBlankLineRun(acc: RichAccumulator, pmPos: number): void {
879+
acc.runs.push({
880+
kind: "prefix",
881+
flatStart: acc.flat,
882+
docLen: 0,
883+
collapsedLen: 1,
884+
mdLen: 1,
885+
openLen: 0,
886+
closeLen: 0,
887+
pmPos,
888+
mdStart: acc.md,
889+
collapsedStart: acc.collapsed,
890+
});
891+
acc.value += "\n";
892+
acc.collapsed += 1;
893+
acc.md += 1;
894+
}
895+
863896
function appendInlineRuns(
864897
container: ProseMirrorNode,
865898
contentStart: number,
@@ -1367,6 +1400,9 @@ export function serializeEditorDoc(doc: ProseMirrorNode): RichDocMap {
13671400
let pmBlockStart = 0;
13681401
blocks.forEach((block, blockIndex) => {
13691402
if (blockIndex > 0 && !isTrailingLineAfterRule(doc, blockIndex)) pushBreakRun(acc);
1403+
if (blockIndex > 0 && needsBlankLineAfterQuote(blocks[blockIndex - 1]!, block)) {
1404+
pushBlankLineRun(acc, pmBlockStart + 1);
1405+
}
13701406
if (LIST_NODE_NAMES.has(block.type.name)) {
13711407
walkList(block, pmBlockStart, acc);
13721408
} else if (block.type.name === "codeBlock") {

0 commit comments

Comments
 (0)