Skip to content

Commit 0d083be

Browse files
matt2eclaude
andcommitted
fix(composer): carry list parity and fence state across split paragraphs, write intraword emphasis with asterisks
Addresses the three review threads on PR #455 (kalvinnchau, 2026-09-30). blocksMarkdown reset the marker parity on every non-list block while it kept the last list kind across whitespace-only output, so a third list separated from the second by an empty paragraph repeated the second marker: `- a`, Shift+Enter twice, `- b`, Shift+Enter three times, `- c` sent `- a\n\n* b\n\n\n\n* c`, which remark reads as two lists with one and two items where the composer shows three. The parity now changes only when a list is written and resets, with the list kind, only after a block with visible output, so both values carry across empty paragraphs together and the same input sends `- a\n\n* b\n\n\n\n- c`; the ordered form sends `1. a\n\n1) b\n\n\n\n1. c`. Every existing expected string is byte-for-byte unchanged. Emphasis was written with _ unless the span held a recipient, and _ cannot open or close intraword, so the stock handler encoded both neighbours as character references: typed foo*bar*baz sent fo&#x6F;_&#x62;a&#x72;_&#x62;az and 2*3*4 sent &#x32;_&#x33;_&#x34;, readable on the timeline but not to agents or the CLI reading the raw event. The emphasis handler now also chooses * when the span touches a word character, read from the wire neighbours the serializer hands it in info.before and info.after rather than from the paragraph text, so a delimiter another span writes beside the emphasis still counts as punctuation: **_a_**_b_ and _a_**b** keep their underscores while foo*bar*baz, 2*3*4, a*b* and **a*b*c** are sent as typed. The peek keeps its recipient-only rule, since it only tells the preceding node which character follows and both markers are punctuation to every rule that reads it. The recipient case is unchanged. composerCodeFence and composerBlockPrefix scanned fence state within the caret's paragraph only, although sibling paragraphs serialise joined by single newlines and the timeline reads them as one source. Typing ```, undoing, Shift+Enter, `code`, Shift+Enter, then toggling the quote on and off to split the empty line into its own paragraph and typing ``` turned the closing fence into a second code block and sent an empty block after the first. A new paragraphRun helper walks the caret's siblings in both directions while they are paragraphs and maps the run's bounds to draft offsets; the fence rule reads the earlier lines of the whole run for an open block and the later lines for a closing fence, and the prefix rule reads the earlier lines, so a marker typed under an open fence in a previous paragraph stays literal too. A fence typed in a paragraph below a closed block still opens a new one. No document change: docs/channels.md already says only an opening fence converts and that lists separated by empty lines alternate their markers. Tests: the serializer case adds bullet and ordered three-list documents with empty paragraphs between them, parsed as three single-item lists, and a new case asserts foo*bar*baz, 2*3*4, a*b*, *b*c, a _b_ c, (_b_), _b_, _a_**b** and **a*b*c** with the parsed emphasis. EditableInput adds the exact wire snake_case_more 5*3*2 to the intraword case, types both three-list sequences and asserts three lists on the wire and in the parse, replays the reviewer's paragraph-split fence sequence and then opens a block from a fence typed below the closed one, types the third backtick of an opening fence whose closing line sits in a later split paragraph, and types `- ` under a fence left open in an earlier paragraph. All eight new or changed cases fail at cb486cfd and pass with the fix. The messages folder passes (58 files, 1040 tests), tsc --noEmit is clean and biome check --error-on-warnings passes for src/features/messages. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 721189f commit 0d083be

4 files changed

Lines changed: 264 additions & 23 deletions

File tree

‎src/features/messages/EditableInput.test.tsx‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ it("follows the parser for intraword delimiters: snake_case stays literal, 5*3*2
282282
expect(h.input.querySelectorAll("em")).toHaveLength(1);
283283
expect(h.input.querySelector("em")).toHaveTextContent("3");
284284
expect(h.input).toHaveValue("snake_case_more 532");
285-
// The wire form encodes the neighbours so recipients render what the composer shows.
285+
// The wire keeps the typed asterisks, which form intraword where _ would
286+
// need its neighbours encoded, so raw readers see the source as typed.
287+
expect(h.markdown()).toBe("snake_case_more 5*3*2");
286288
expect(fromMarkdown(h.markdown()).children[0]).toMatchObject({
287289
type: "paragraph",
288290
children: [
@@ -652,6 +654,64 @@ it("keeps a typed opening fence literal when a later line already closes it", as
652654
expect(h.markdown()).toBe("```\ncode\n```");
653655
});
654656

657+
/** Split the caret's empty last line into its own paragraph with the toolbar's
658+
* quote toggle and its lift, which leaves the split in place. Sibling paragraphs
659+
* send joined by one newline, so the wire is unchanged. */
660+
function splitLastLine(h: ReturnType<typeof mount>, paragraphs: number) {
661+
act(() => h.input.toggleFormat("blockquote"));
662+
act(() => h.input.toggleFormat("blockquote"));
663+
expect(h.input.querySelector("blockquote")).toBeNull();
664+
expect(h.input.querySelectorAll(":scope > p")).toHaveLength(paragraphs);
665+
}
666+
667+
it("keeps a fence literal that closes a block an earlier sibling paragraph opened, then converts below the closed block", async () => {
668+
const h = mount();
669+
await h.user.keyboard("```");
670+
act(() => h.input.undo(false));
671+
expect(h.input).toHaveValue("```");
672+
await h.user.keyboard("{Shift>}{Enter}{/Shift}code{Shift>}{Enter}{/Shift}");
673+
splitLastLine(h, 2);
674+
expect(h.markdown()).toBe("```\ncode\n");
675+
// The line closes the fence the first paragraph opened; on the wire the two
676+
// paragraphs are one fenced block, so the third backtick stays literal.
677+
await h.user.keyboard("```");
678+
expect(h.input.querySelector("pre")).toBeNull();
679+
expect(h.input).toHaveValue("```\ncode\n```");
680+
expect(h.markdown()).toBe("```\ncode\n```");
681+
expect(fromMarkdown(h.markdown()).children).toMatchObject([
682+
{ type: "code", value: "code" },
683+
]);
684+
// Below the closed block, a fence in a further split paragraph opens one.
685+
await h.user.keyboard("{Shift>}{Enter}{/Shift}");
686+
splitLastLine(h, 3);
687+
await h.user.keyboard("```more");
688+
expect(h.input.querySelectorAll("pre")).toHaveLength(1);
689+
expect(h.input.querySelector("pre > code")).toHaveTextContent("more");
690+
expect(h.input).toHaveValue("```\ncode\n```\nmore");
691+
expect(h.markdown()).toBe("```\ncode\n```\n\n```\nmore\n```");
692+
expect(fromMarkdown(h.markdown()).children).toMatchObject([
693+
{ type: "code", value: "code" },
694+
{ type: "code", value: "more" },
695+
]);
696+
});
697+
698+
it("keeps a typed opening fence literal when a later sibling paragraph closes it", async () => {
699+
const h = mount("``");
700+
await h.user.keyboard("{Shift>}{Enter}{/Shift}");
701+
splitLastLine(h, 2);
702+
act(() => h.input.insertText("code\n```"));
703+
expect(h.input).toHaveValue("``\ncode\n```");
704+
act(() => h.input.setSelectionRange(2, 2));
705+
await h.user.keyboard("`");
706+
expect(h.input.querySelector("pre")).toBeNull();
707+
expect(h.input.querySelectorAll(":scope > p")).toHaveLength(2);
708+
expect(h.input).toHaveValue("```\ncode\n```");
709+
expect(h.markdown()).toBe("```\ncode\n```");
710+
expect(fromMarkdown(h.markdown()).children).toMatchObject([
711+
{ type: "code", value: "code" },
712+
]);
713+
});
714+
655715
it.each([
656716
["- ", "ul", "- "],
657717
["* ", "ul", "- "],
@@ -1099,6 +1159,29 @@ it("alternates the marker between adjacent lists of one kind so they stay separa
10991159
).toEqual(["list", "list", "list", "paragraph", "list"]);
11001160
});
11011161

1162+
it.each([
1163+
["- ", "ul", "- a\n\n* b\n\n\n\n- c"],
1164+
["1. ", "ol", "1. a\n\n1) b\n\n\n\n1. c"],
1165+
] as const)(
1166+
"keeps alternating %j list markers across an empty paragraph so three lists stay three",
1167+
async (marker, tag, wire) => {
1168+
const h = mount();
1169+
// Two Shift+Enters leave the list; a third on the empty paragraph adds a
1170+
// blank line, which the marker's conversion splits into its own paragraph.
1171+
await h.user.keyboard(
1172+
`${marker}a{Shift>}{Enter}{Enter}{/Shift}${marker}b{Shift>}{Enter}{Enter}{Enter}{/Shift}${marker}c`,
1173+
);
1174+
expect(h.input.querySelectorAll(`:scope > ${tag}`)).toHaveLength(3);
1175+
expect(h.input.querySelectorAll(":scope > p")).toHaveLength(1);
1176+
expect(h.markdown()).toBe(wire);
1177+
expect(fromMarkdown(h.markdown()).children).toMatchObject([
1178+
{ type: "list", children: [{ type: "listItem" }] },
1179+
{ type: "list", children: [{ type: "listItem" }] },
1180+
{ type: "list", children: [{ type: "listItem" }] },
1181+
]);
1182+
},
1183+
);
1184+
11021185
it("leaves a marker typed after prose, and ordinary spaces, as paragraph text", async () => {
11031186
const h = mount();
11041187
await h.user.keyboard("note - one 1. two > three");
@@ -1147,6 +1230,16 @@ it("does not convert an inserted or pasted marker, or one typed inside pasted fe
11471230
expect(fenced.input.querySelector("ul, pre")).toBeNull();
11481231
expect(fenced.input).toHaveValue("```\n- code\n```");
11491232
expect(fenced.markdown()).toBe("```\n- code\n```");
1233+
// A fence left open by an earlier sibling paragraph still covers the line.
1234+
const split = mount();
1235+
await split.user.keyboard("```");
1236+
act(() => split.input.undo(false));
1237+
await split.user.keyboard("{Shift>}{Enter}{/Shift}");
1238+
splitLastLine(split, 2);
1239+
await split.user.keyboard("- item");
1240+
expect(split.input.querySelector("ul, pre")).toBeNull();
1241+
expect(split.input).toHaveValue("```\n- item");
1242+
expect(split.markdown()).toBe("```\n- item");
11501243
});
11511244

11521245
it("keeps a marker literal on a line holding a mention chip", async () => {

‎src/features/messages/composer-blocks.ts‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
exitCode,
2020
} from "prosemirror-commands";
2121
import type { Command } from "prosemirror-state";
22+
import type { ResolvedPos } from "prosemirror-model";
2223
import {
2324
composerSchema as schema,
2425
projectComposerDocument,
@@ -289,14 +290,42 @@ function closedBelow(fence: Fence, lines: readonly string[]): boolean {
289290
return lines.some((line) => !readFence(line, fence));
290291
}
291292

293+
/** Draft offsets of the run of sibling paragraphs holding the caret's. Sibling
294+
* paragraphs serialise joined by single newlines, exactly as the draft text
295+
* holds them, so the timeline reads the run as one source context: a fence
296+
* opened or closed in one paragraph counts for the others, and a toolbar
297+
* toggle that splits a paragraph changes nothing on the wire. Any other block
298+
* between two paragraphs ends the run. */
299+
function paragraphRun(
300+
$from: ResolvedPos,
301+
source: ReturnType<typeof projectComposerDocument>,
302+
): { start: number; end: number } {
303+
const depth = $from.depth,
304+
parent = $from.node(depth - 1);
305+
let first = $from.index(depth - 1),
306+
last = first,
307+
from = $from.before(depth),
308+
to = $from.after(depth);
309+
while (first > 0 && parent.child(first - 1).type === schema.nodes.paragraph)
310+
from -= parent.child(--first).nodeSize;
311+
while (
312+
last + 1 < parent.childCount &&
313+
parent.child(last + 1).type === schema.nodes.paragraph
314+
)
315+
to += parent.child(++last).nodeSize;
316+
return { start: source.source(from), end: source.source(to - 1) };
317+
}
318+
292319
/** Typing the third character of a line holding only ``` or ~~~ turns that line
293320
* into a code block at once, without waiting for Enter. Only a fence the typed
294321
* character completes at the end of its line qualifies: a fence that closes or
295-
* sits inside a block the paragraph's other lines open (pasted source, or a
296-
* message opened for editing), fences inside code/link/literal ranges or
297-
* tokens, and any line inside an existing code block stay literal source. The
298-
* transaction carries the deletion and the block; the caller closes history
299-
* around it, so one undo restores the typed fence. */
322+
* sits inside a block the other lines of its paragraph, or of the sibling
323+
* paragraphs joined to it on the wire, open (pasted source, a message opened
324+
* for editing, or a paragraph a toolbar toggle split), fences inside
325+
* code/link/literal ranges or tokens, and any line inside an existing code
326+
* block stay literal source. The transaction carries the deletion and the
327+
* block; the caller closes history around it, so one undo restores the typed
328+
* fence. */
300329
export function composerCodeFence(
301330
state: EditorState,
302331
typed: string,
@@ -318,9 +347,11 @@ export function composerCodeFence(
318347
if (!match || match[1]?.[0] !== typed) return;
319348
// A closing fence, a fence line inside an open block, or an opening fence a
320349
// later line already closes is authored source the timeline renders as code:
321-
// it never opens a second block.
322-
if (insideFence(text.slice(block.start, start).split("\n"))) return;
323-
const below = text.slice(caret, source.source(block.to));
350+
// it never opens a second block. The lines read are those of the whole run
351+
// of sibling paragraphs, since the wire joins them with single newlines.
352+
const run = paragraphRun($from, source);
353+
if (insideFence(text.slice(run.start, start).split("\n"))) return;
354+
const below = text.slice(caret, run.end);
324355
if (
325356
below &&
326357
closedBelow({ marker: typed, length: 3 }, below.slice(1).split("\n"))
@@ -419,8 +450,14 @@ export function composerBlockPrefix(
419450
if (!block) return;
420451
const start = Math.max(block.start, text.lastIndexOf("\n", caret - 1) + 1);
421452
if (text.slice(start, caret) !== match[0]) return;
422-
// A marker inside authored fenced source is a code line on the timeline.
423-
if (insideFence(text.slice(block.start, start).split("\n"))) return;
453+
// A marker inside authored fenced source is a code line on the timeline,
454+
// whichever paragraph of the run the fence was opened in.
455+
if (
456+
insideFence(
457+
text.slice(paragraphRun($from, source).start, start).split("\n"),
458+
)
459+
)
460+
return;
424461
const from = source.position(start),
425462
to = $from.pos;
426463
if (source.source(from) !== start) return;

‎src/features/messages/composer-document.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ describe("composer Markdown boundary", () => {
185185
markdown(numbers(1, "a"), paragraph(), numbers(1, "b")),
186186
["list", "list"],
187187
],
188+
// The parity carries across the empty paragraph as the list kind does,
189+
// so a third list separated the same way takes the first marker again
190+
// rather than repeating the second and merging with it.
191+
[
192+
markdown(
193+
bullets("a"),
194+
paragraph(),
195+
bullets("b"),
196+
paragraph(),
197+
paragraph(),
198+
bullets("c"),
199+
),
200+
["list", "list", "list"],
201+
],
202+
[
203+
markdown(
204+
numbers(1, "a"),
205+
paragraph(),
206+
numbers(1, "b"),
207+
paragraph(),
208+
numbers(1, "c"),
209+
),
210+
["list", "list", "list"],
211+
],
188212
[
189213
markdown(bullets("a"), paragraph(schema.text("text")), bullets("b")),
190214
["list", "paragraph", "list"],
@@ -217,6 +241,8 @@ describe("composer Markdown boundary", () => {
217241
"999999999. item\n999999999. next",
218242
"- a\n\n\n\n* b",
219243
"1. a\n\n\n\n1) b",
244+
"- a\n\n\n\n* b\n\n\n\n\n- c",
245+
"1. a\n\n\n\n1) b\n\n\n\n1. c",
220246
"- a\n\ntext\n\n- b",
221247
"- a\n\n```\ncode\n```\n\n- b",
222248
"- a\n\n> q\n\n- b",
@@ -248,6 +274,69 @@ describe("composer Markdown boundary", () => {
248274
{ type: "list", ordered: true, start: 1, children: [{}] },
249275
{ type: "list", ordered: true, start: 1, children: [{}] },
250276
]);
277+
for (const index of [6, 7])
278+
expect(fromMarkdown(cases[index]?.[0] ?? "").children).toMatchObject([
279+
{ type: "list", children: [{}] },
280+
{ type: "list", children: [{}] },
281+
{ type: "list", children: [{}] },
282+
]);
283+
});
284+
it("writes intraword emphasis with asterisks so the source stays readable", () => {
285+
const italic = (text: string) =>
286+
schema.text(text, [schema.marks.italic.create()]);
287+
const cases: [string, string, string][] = [
288+
["foo", "bar", "baz"],
289+
["2", "3", "4"],
290+
["a", "b", ""],
291+
["", "b", "c"],
292+
];
293+
for (const [before, inner, after] of cases) {
294+
const output = serialize(
295+
...(before ? [schema.text(before)] : []),
296+
italic(inner),
297+
...(after ? [schema.text(after)] : []),
298+
);
299+
expect(output).toBe(`${before}*${inner}*${after}`);
300+
expect(fromMarkdown(output).children[0]).toMatchObject({
301+
type: "paragraph",
302+
children: [
303+
...(before ? [{ type: "text", value: before }] : []),
304+
{ type: "emphasis", children: [{ type: "text", value: inner }] },
305+
...(after ? [{ type: "text", value: after }] : []),
306+
],
307+
});
308+
}
309+
// Whitespace or punctuation beside the span keeps the underscore, and so
310+
// does another span's delimiter: the neighbour is read from the wire, not
311+
// from the paragraph text.
312+
expect(serialize(schema.text("a "), italic("b"), schema.text(" c"))).toBe(
313+
"a _b_ c",
314+
);
315+
expect(serialize(schema.text("("), italic("b"), schema.text(")"))).toBe(
316+
"(_b_)",
317+
);
318+
expect(serialize(italic("b"))).toBe("_b_");
319+
expect(serialize(italic("a"), bold("b"))).toBe("_a_**b**");
320+
// Inside a bold run the neighbours are the bold prose, so * forms intraword
321+
// where the stock handler would encode them.
322+
const both = schema.text("b", [
323+
schema.marks.bold.create(),
324+
schema.marks.italic.create(),
325+
]);
326+
expect(serialize(bold("a"), both, bold("c"))).toBe("**a*b*c**");
327+
expect(fromMarkdown("**a*b*c**").children[0]).toMatchObject({
328+
type: "paragraph",
329+
children: [
330+
{
331+
type: "strong",
332+
children: [
333+
{ type: "text", value: "a" },
334+
{ type: "emphasis", children: [{ type: "text", value: "b" }] },
335+
{ type: "text", value: "c" },
336+
],
337+
},
338+
],
339+
});
251340
});
252341
it("uses syntax-aware escaping and delimiter flanking", () => {
253342
expect(serialize(bold("*"))).toBe("**\\***");

0 commit comments

Comments
 (0)