@devries-cameron20 / t3code-1 / commits / f39ecd3df8
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): a rule typed under a line stays a rule when sent (#29663)

Julius Marminge committed Nov 16, 2026 at 18:55 UTC f39ecd3df8f01171c190350fe93e1a9cfb3a017d
3 files changed +98 -13
apps/web/src/components/ComposerPromptEditorTiptap.tsx
+8 -4
@@ -90,6 +90,7 @@ import {
90 markdownToFlat,
91 pmToFlat,
92 serializeEditorDoc,
93 + serializedMarkdownOffset,
94 serializeSelection,
95 skillChipReplacements,
96 splitOrLiftListItem,
@@ -1643,7 +1644,7 @@ function ComposerPromptEditorTiptapInner(
1644 },
1645 );
1646 }
1646 - selectMarkdownOffset(editor, normalizedExpandedCursor);
1647 + selectMarkdownOffset(editor, value, normalizedExpandedCursor);
1648 const map = serializeEditorDoc(editor.state.doc);
1649 if (isFocused) scrollTiptapCaretIntoView(editor);
1650 // A redraw later in this commit drops the editor's scroll offset, so reveal on the next frame.
@@ -1722,7 +1723,7 @@ function ComposerPromptEditorTiptapInner(
1723 const boundedCursor = clampEditorCursor(snapshotRef.current.value, nextCursor);
1724 const expandedCursor =
1725 exactExpandedCursor ?? expandEditorCursor(snapshotRef.current.value, boundedCursor);
1725 - selectMarkdownOffset(editor, expandedCursor);
1726 + selectMarkdownOffset(editor, snapshotRef.current.value, expandedCursor);
1727 scrollTiptapCaretIntoView(editor);
1728 if (boundedCursor === snapshotRef.current.cursor) return;
1729 snapshotRef.current = {
@@ -1950,10 +1951,13 @@ function insertMarkdownParagraphs(
1951 }
1952
1953 /** Placing the caret can show Markdown markers, so place it again in the shown source. */
1953 -function selectMarkdownOffset(editor: TiptapEditor, offset: number): void {
1954 +function selectMarkdownOffset(editor: TiptapEditor, source: string, offset: number): void {
1955 for (let pass = 0; pass < 2; pass += 1) {
1956 const map = serializeEditorDoc(editor.state.doc);
1956 - const pos = flatToPm(map, markdownToFlat(map, offset));
1957 + const pos = flatToPm(
1958 + map,
1959 + markdownToFlat(map, serializedMarkdownOffset(source, map.value, offset)),
1960 + );
1961 const { selection } = editor.state;
1962 if (pass > 0 && selection.empty && selection.from === pos) return;
1963 editor.commands.setTextSelection(pos);
apps/web/src/composer-rich-text-doc.test.ts
+51 -2
@@ -30,6 +30,7 @@ import {
30 markdownToFlat,
31 pmToFlat,
32 serializeEditorDoc,
33 + serializedMarkdownOffset,
34 serializeSelection,
35 skillChipReplacements,
36 stepCaretAcrossStyledEdge,
@@ -607,6 +608,53 @@ describe("composer rich text document model", () => {
608 });
609 });
610
611 + describe("a dash rule after a paragraph", () => {
612 + const paragraph = (text: string) => ({ type: "paragraph", content: [{ type: "text", text }] });
613 + const rule = (source: string) => ({ type: "horizontalRule", attrs: { source } });
614 + const serialize = (...content: object[]) => {
615 + const doc = ProseMirrorNode.fromJSON(schema, { type: "doc", content });
616 + doc.check();
617 + return { doc, map: serializeEditorDoc(doc) };
618 + };
619 +
620 + it("is written after a blank line, or Markdown reads the paragraph as a heading", () => {
621 + const settled = "text\n\n---\nmore";
622 + expect(roundTrip("text\n---\nmore").value).toBe(settled);
623 + expect(roundTrip(settled).value).toBe(settled);
624 + });
625 +
626 + it("keeps every offset mapped through the blank line", () => {
627 + const { doc, map } = serialize(paragraph("text"), rule("---"), paragraph("more"));
628 + expect(map.value).toBe("text\n\n---\nmore");
629 + for (let flat = 0; flat <= map.docLength; flat += 1) {
630 + expect(markdownToFlat(map, flatToMarkdown(map, flat))).toBe(flat);
631 + }
632 + // The blank line is not a place to type: it clamps to the rule.
633 + expect(flatToMarkdown(map, "text".length)).toBe("text".length);
634 + expect(markdownToFlat(map, "text\n".length)).toBe("text".length + 1);
635 + expect(flatToMarkdown(map, "text".length + 1)).toBe("text\n\n---".length);
636 + expect(flatToPm(map, "text".length + 1)).toBe(doc.child(0).nodeSize + doc.child(1).nodeSize);
637 + });
638 + });
639 +
640 + it.each([
641 + ["text\n---\nmore", "text\n---\nm".length, "text\n\n---\nm".length],
642 + ["before\n> quoted\nafter", "before\n> quoted\na".length, "before\n> quoted\n\na".length],
643 + ["__x__\n---\nmore", "__x__\n---\nm".length, "**x**\n\n---\nm".length],
644 + [
645 + "*a*_b_\n> q\nx\n> q\nabcde",
646 + "*a*_b_\n> q\nx\n> q\na".length,
647 + "*a*_b_\n> q\nx\n> q\na".length,
648 + ],
649 + ["a\n---\n", "a".length, "a".length],
650 + ])("restores a caret in the glued draft %j on the same character", (value, caret, settled) => {
651 + const map = roundTrip(value);
652 + const restore = (offset: number) =>
653 + markdownToFlat(map, serializedMarkdownOffset(value, map.value, offset));
654 + expect(restore(value.length)).toBe(map.docLength);
655 + expect(restore(caret)).toBe(markdownToFlat(map, settled));
656 + });
657 +
658 it.each([
659 "---",
660 "***",
@@ -615,7 +663,8 @@ describe("composer rich text document model", () => {
663 "* * *",
664 "-----",
665 "--- ",
618 - "text\n---\nmore",
666 + "text\n***\nmore",
667 + "text\n- - -\nmore",
668 "- item\n---\n- item two",
669 "```\n---\n```",
670 "--",
@@ -661,7 +710,7 @@ describe("composer rich text document model", () => {
710
711 it.each([
712 ["---", "---"],
664 - ["a\n---", "a\n---"],
713 + ["a\n---", "a\n\n---"],
714 ["---\n", "---"],
715 ])("gives the caret a line after the final rule of %j, written %j", (value, expected) => {
716 const map = roundTrip(value);
apps/web/src/composer-rich-text-doc.ts
+39 -7
@@ -1234,14 +1234,26 @@ function pushBreakRun(acc: RichAccumulator, position?: number): void {
1234 acc.md += 1;
1235 }
1236
1237 -// Separate prose from quotes to avoid lazy continuation; an empty paragraph already separates them.
1238 -function needsBlankLineAfterQuote(previous: ProseMirrorNode, block: ProseMirrorNode): boolean {
1237 +function horizontalRuleSource(block: ProseMirrorNode): string {
1238 + const source = (block.attrs as Record<string, unknown>).source;
1239 + return typeof source === "string" && source ? source : "---";
1240 +}
1241 +
1242 +// A blank line keeps prose out of a quote (lazy continuation) and a dash rule from
1243 +// underlining the line above into a setext heading; an empty paragraph already separates them.
1244 +function needsBlankLineBetween(previous: ProseMirrorNode, block: ProseMirrorNode): boolean {
1245 + if (previous.type.name === "blockquote") {
1246 + return block.type.name === "paragraph" && block.content.size > 0;
1247 + }
1248 return (
1240 - previous.type.name === "blockquote" && block.type.name === "paragraph" && block.content.size > 0
1249 + block.type.name === "horizontalRule" &&
1250 + previous.type.name === "paragraph" &&
1251 + previous.content.size > 0 &&
1252 + /^-+[ \t]*\r?$/.test(horizontalRuleSource(block))
1253 );
1254 }
1255
1244 -// Blank-line runs own no document characters; offsets clamp to the paragraph start.
1256 +// Blank-line runs own no document characters; offsets clamp to the start of the next block.
1257 function pushBlankLineRun(acc: RichAccumulator, pmPos: number): void {
1258 acc.runs.push({
1259 kind: "prefix",
@@ -1257,6 +1269,27 @@ function pushBlankLineRun(acc: RichAccumulator, pmPos: number): void {
1269 acc.md += 1;
1270 }
1271
1272 +// Moves an offset into a parsed `value` past the blank lines its serialization inserted,
1273 +// treating other differences (`__` written as `**`) as same-length rewrites. When the two
1274 +// strings do not align that way end to end, the offset is returned unmapped.
1275 +export function serializedMarkdownOffset(
1276 + value: string,
1277 + serialized: string,
1278 + offset: number,
1279 +): number {
1280 + if (value === serialized) return offset;
1281 + let index = 0;
1282 + let mapped = 0;
1283 + let target = -1;
1284 + while (index < value.length && mapped < serialized.length) {
1285 + if (index === offset && target < 0) target = mapped;
1286 + if (value[index] === serialized[mapped] || serialized[mapped] !== "\n") index += 1;
1287 + mapped += 1;
1288 + }
1289 + if (index !== value.length || mapped !== serialized.length) return offset;
1290 + return target < 0 ? mapped + (offset - index) : target;
1291 +}
1292 +
1293 function appendInlineRuns(
1294 container: ProseMirrorNode,
1295 contentStart: number,
@@ -1685,7 +1718,7 @@ function serializeDoc(doc: ProseMirrorNode): RichDocMap {
1718 let pmBlockStart = 0;
1719 blocks.forEach((block, blockIndex) => {
1720 if (blockIndex > 0 && !isTrailingLineAfterRule(doc, blockIndex)) pushBreakRun(acc);
1688 - if (blockIndex > 0 && needsBlankLineAfterQuote(blocks[blockIndex - 1]!, block)) {
1721 + if (blockIndex > 0 && needsBlankLineBetween(blocks[blockIndex - 1]!, block)) {
1722 pushBlankLineRun(acc, pmBlockStart + 1);
1723 }
1724 if (LIST_NODE_NAMES.has(block.type.name)) {
@@ -1713,8 +1746,7 @@ function serializeDoc(doc: ProseMirrorNode): RichDocMap {
1746 acc.md += prefix.length;
1747 appendInlineRuns(block, pmBlockStart + 1, acc);
1748 } else if (block.type.name === "horizontalRule") {
1716 - const attrs = block.attrs as Record<string, unknown>;
1717 - const source = typeof attrs.source === "string" && attrs.source ? attrs.source : "---";
1749 + const source = horizontalRuleSource(block);
1750 acc.runs.push({
1751 kind: "prefix",
1752 flatStart: acc.flat,