Skip to content

Commit a92937d

Browse files
matt2eclaude
andcommitted
feat(composer): style typed inline Markdown on the closing delimiter
Typing **bold**, __bold__, _italic_, *italic*, ~~strike~~ or `code` in the message composer now converts the span to its mark as the closing delimiter is typed, matching the previous Buzz client. The rule lives in the new composer-inline-input.ts as applyComposerInlineInput(tr, typed), which generalises and replaces composer-code-input.ts. It reparses the masked document text with fromMarkdown plus gfmStrikethrough, the same parser family the serialiser and timeline use, finds the strong, emphasis, delete or inlineCode node ending at the caret on a single line, and refuses anything reachable only through a link, image, HTML or fenced code. Nested spans such as ***x*** add one mark per node and delete their delimiters from the end so positions stay valid. The unmatched-opener guard from the code rule now applies to every delimiter, so **a* waits for the second asterisk instead of italicising midway. Spans carrying code, link or literal marks, or lying in a code block, stay literal, and every delimiter must sit at an editable boundary rather than inside an atom. Emphasis marks apply across mention and emoji atoms so chips and recipients survive; only the code path still expands tokens into literal text, as before. Single-tilde ~x~ is deliberately excluded (singleTilde: false): the serialiser emits ~~x~~ and a lone ~ in prose must not strike while typing. EditableInput dispatches the conversion as a second transaction with closeHistory before and separateHistory after. One undo therefore restores the literal source including the closing character, where the old code rule left `abc without its closing backtick, and the next keystroke never merges into the conversion. Near the length limit the typed character can be filtered out, in which case nothing converts. After a conversion the new marks are dropped from stored marks so continued typing is plain; code still clears them all. mentionDraft now treats *, ~ and ` as mention boundaries, like the timeline's raw binder, which continues a name only through letters, digits and underscore. Without this the first closing delimiter typed after a chip revoked its recipient before the second one arrived, and the two-transaction flow would have dropped provenance for `@Honey` too. An underscore remains a name suffix. markdownRanges drops its unused bold output. docs/channels.md gains a sentence on the typed inline conversion beside the fence behaviour. Tests cover **Hello** conversion, plain continuation, single-step undo and persistence, the closing backtick surviving undo, canonical wire forms for __x__, *x*, _x_ and ~~x~~, both marks for ***x***, ~y~ left literal, **a* waiting for **a**, snake_case staying literal while 5*3*2 italicises 3 as the renderer would, no conversion inside inline code, a code block, a linked or literal label or for pasted delimiters, the mention chip and recipient surviving **@honey**, the new mention boundaries, and a Chromium journey typing **Hello** through undo, redo and send. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 1ce9b34 commit a92937d

9 files changed

Lines changed: 467 additions & 122 deletions

File tree

‎docs/channels.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,10 @@ implemented. In the composer, a line holding only ```` ``` ```` or `~~~`, option
527527
language, becomes a code block when Enter or Shift+Enter follows it, and that Enter does not send;
528528
the language stays on the sent fence. Shift+Enter on an empty last line leaves the block. Fences
529529
inside an existing code block, and pasted fences, stay literal text and still render as code once
530-
sent. Reply counts open a bounded thread view; attachments are
530+
sent. Typing an inline span (`**bold**` or `__bold__`, `_italic_` or `*italic*`, `~~strike~~`,
531+
`` `code` ``) converts it to formatting as the closing delimiter is typed, and one undo restores
532+
the typed characters. A single `~` never strikes, and pasted or restored delimiters stay literal
533+
text that still renders once sent. Reply counts open a bounded thread view; attachments are
531534
links. Routine freshness labels are not shown; Channel Settings → Diagnostics
532535
exposes refresh, outbox inspection and timings. Packaged builds do not
533536
include the development relay broker. GitHub fetches public data only; signed-in

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

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import userEvent from "@testing-library/user-event";
1111
import { createRef, useState } from "react";
1212
import { afterEach, expect, it } from "vitest";
13+
import { fromMarkdown } from "mdast-util-from-markdown";
1314
import { EditableInput } from "./EditableInput";
1415
import type { ComposerInputElement } from "./composer-dom";
1516
import { composerDOMFixture } from "./composer-testing";
@@ -150,6 +151,150 @@ it("clearing all code and reentering a code edge leaves subsequent prose unmarke
150151
expect(h.markdown()).toBe("outside");
151152
});
152153

154+
it("converts typed **Hello** on the closing delimiter, keeps later typing plain and undoes in one step", async () => {
155+
const h = mount();
156+
await h.user.keyboard("**Hello**");
157+
expect(h.input.querySelector("strong")).toHaveTextContent("Hello");
158+
expect(h.input).toHaveValue("Hello");
159+
expect(h.markdown()).toBe("**Hello**");
160+
await h.user.keyboard(" world");
161+
expect(h.input.querySelector("strong")).toHaveTextContent("Hello");
162+
expect(h.markdown()).toBe("**Hello** world");
163+
// The prose typed next is its own step; one more undo restores the source.
164+
act(() => h.input.undo(false));
165+
expect(h.markdown()).toBe("**Hello**");
166+
act(() => h.input.undo(false));
167+
expect(h.input.querySelector("strong")).toBeNull();
168+
expect(h.input).toHaveValue("**Hello**");
169+
act(() => h.input.undo(true));
170+
expect(h.input.querySelector("strong")).toHaveTextContent("Hello");
171+
const saved = JSON.parse(JSON.stringify(h.draft()));
172+
act(() => h.input.reset(saved));
173+
expect(h.input.querySelector("strong")).toHaveTextContent("Hello");
174+
expect(h.markdown()).toBe("**Hello**");
175+
});
176+
177+
it("one undo after a typed code span restores the closing backtick too", async () => {
178+
const h = mount();
179+
await h.user.keyboard("`abc`");
180+
expect(h.markdown()).toBe("`abc`");
181+
act(() => h.input.undo(false));
182+
expect(h.input.querySelector("code")).toBeNull();
183+
expect(h.input).toHaveValue("`abc`");
184+
});
185+
186+
it.each([
187+
["__x__", "strong", "**x**"],
188+
["*x*", "em", "_x_"],
189+
["_x_", "em", "_x_"],
190+
["~~x~~", "s", "~~x~~"],
191+
])(
192+
"converts typed %s and sends the serializer's canonical delimiters",
193+
async (typed, tag, wire) => {
194+
const h = mount();
195+
await h.user.keyboard(typed);
196+
expect(h.input.querySelector(tag)).toHaveTextContent("x");
197+
expect(h.input).toHaveValue("x");
198+
expect(h.markdown()).toBe(wire);
199+
},
200+
);
201+
202+
it("adds both marks for ***x*** and leaves ~x~ literal", async () => {
203+
const h = mount();
204+
await h.user.keyboard("***x***");
205+
expect(h.input.querySelector("em strong, strong em")).toHaveTextContent("x");
206+
expect(h.input).toHaveValue("x");
207+
expect(h.markdown()).toBe("**_x_**");
208+
await h.user.keyboard(" ~y~");
209+
expect(h.input.querySelector("s")).toBeNull();
210+
expect(h.input).toHaveValue("x ~y~");
211+
expect(h.markdown()).toBe("**_x_** ~y~");
212+
});
213+
214+
it("waits for the whole closing run instead of italicising midway through **a**", async () => {
215+
const h = mount();
216+
await h.user.keyboard("**a*");
217+
expect(h.input.querySelector("em, strong")).toBeNull();
218+
expect(h.input).toHaveValue("**a*");
219+
await h.user.keyboard("*");
220+
expect(h.input.querySelector("strong")).toHaveTextContent("a");
221+
expect(h.input.querySelector("em")).toBeNull();
222+
expect(h.markdown()).toBe("**a**");
223+
});
224+
225+
it("follows the parser for intraword delimiters: snake_case stays literal, 5*3*2 italicises 3", async () => {
226+
const h = mount();
227+
await h.user.keyboard("snake_case_more 5*3*2");
228+
expect(h.input.querySelectorAll("em")).toHaveLength(1);
229+
expect(h.input.querySelector("em")).toHaveTextContent("3");
230+
expect(h.input).toHaveValue("snake_case_more 532");
231+
// The wire form encodes the neighbours so recipients render what the composer shows.
232+
expect(fromMarkdown(h.markdown()).children[0]).toMatchObject({
233+
type: "paragraph",
234+
children: [
235+
{ type: "text", value: "snake_case_more 5" },
236+
{ type: "emphasis", children: [{ type: "text", value: "3" }] },
237+
{ type: "text", value: "2" },
238+
],
239+
});
240+
});
241+
242+
it("leaves delimiters typed inside inline code or a code block literal", async () => {
243+
const h = mount();
244+
act(() => h.input.toggleFormat("code"));
245+
await h.user.keyboard("**x**");
246+
act(() => h.input.toggleFormat("code"));
247+
expect(h.input.querySelector("strong")).toBeNull();
248+
expect(h.input.querySelector("code")).toHaveTextContent("**x**");
249+
expect(h.markdown()).toBe("`**x**`");
250+
const block = mount();
251+
act(() => block.input.toggleFormat("code_block"));
252+
await block.user.keyboard("_y_ ~~z~~");
253+
expect(block.input.querySelector("em, s")).toBeNull();
254+
expect(block.markdown()).toBe("```\n_y_ ~~z~~\n```");
255+
});
256+
257+
it.each([true, false])(
258+
"leaves delimiters around a link label (linked=%s) and pasted delimiters literal",
259+
async (linked) => {
260+
const h = mount("**label");
261+
act(() => {
262+
h.input.setSelectionRange(2, 7);
263+
const edit = h.input.editLink();
264+
if (!edit) throw new Error("Link editing unavailable");
265+
if (linked) edit.save("label", "https://example.com");
266+
else edit.remove();
267+
h.input.setSelectionRange(7, 7);
268+
});
269+
await h.user.keyboard("**");
270+
expect(h.input.querySelector("strong")).toBeNull();
271+
expect(h.input).toHaveValue("**label**");
272+
expect(h.markdown()).toBe(
273+
linked ? "**[label](https://example.com/)**" : "**label**",
274+
);
275+
act(() => {
276+
h.input.setSelectionRange(0, h.input.value.length);
277+
h.input.insertText("**pasted** _source_");
278+
});
279+
expect(h.input.querySelector("strong, em")).toBeNull();
280+
expect(h.markdown()).toBe("**pasted** _source_");
281+
},
282+
);
283+
284+
it("bolds a mention typed between delimiters and keeps its chip and recipient", async () => {
285+
const h = mount();
286+
const honey = { pubkey: "a".repeat(64), name: "Honey" };
287+
await h.user.keyboard("**");
288+
act(() => h.input.insertText("", honey));
289+
expect(h.input).toHaveValue("**@Honey ");
290+
act(() => h.input.setSelectionRange(8, 8));
291+
await h.user.keyboard("**");
292+
expect(h.input).toHaveValue("@Honey ");
293+
expect(h.input.querySelector('strong [data-source="@Honey"]')).not.toBeNull();
294+
expect(h.draft().recipients).toEqual([{ ...honey, start: 0, end: 6 }]);
295+
expect(h.markdown()).toBe("**@Honey** ");
296+
});
297+
153298
it.each([
154299
["bullet_list", "ul", "-"],
155300
["ordered_list", "ol", "1."],

‎src/features/messages/EditableInput.tsx‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ import {
4747
composerCodeFence,
4848
} from "./composer-blocks";
4949
import { composerLinkLabel } from "./composer-link-label";
50-
import { applyComposerCodeInput } from "./composer-code-input";
50+
import {
51+
applyComposerInlineInput,
52+
composerInlineDelimiters,
53+
} from "./composer-inline-input";
5154
import { composerMarkdown } from "./composer-markdown";
5255
import {
5356
mentionDraft,
@@ -906,15 +909,25 @@ export function EditableInput({
906909
composerSchema.marks.code.isInSet(editor.state.storedMarks ?? [])
907910
)
908911
tr.setStoredMarks(marks);
909-
const codeInput =
910-
text === "`" &&
912+
const previous = editor.state;
913+
editor.dispatch(tr.scrollIntoView());
914+
// A closing delimiter converts its span in a second transaction: one
915+
// undo restores the typed source, closing character included, and the
916+
// next keystroke never merges into the conversion. Near maxLength the
917+
// typed character itself can be filtered out; then nothing converts.
918+
if (
911919
from === to &&
920+
composerInlineDelimiters.has(text) &&
912921
!composing.current &&
913922
!editor.composing &&
914-
applyComposerCodeInput(tr);
915-
if (codeInput) closeHistory(tr);
916-
editor.dispatch(tr.scrollIntoView());
917-
if (codeInput) separateHistory = true;
923+
editor.state !== previous
924+
) {
925+
const conversion = editor.state.tr;
926+
if (applyComposerInlineInput(conversion, text)) {
927+
editor.dispatch(closeHistory(conversion).scrollIntoView());
928+
separateHistory = true;
929+
}
930+
}
918931
return true;
919932
},
920933
handleKeyDown(_view, event) {

‎src/features/messages/composer-code-input.ts‎

Lines changed: 0 additions & 108 deletions
This file was deleted.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,12 @@ export const composerSchema = new Schema<
158158
export type SourceToken = { start: number; end: number; editAsText?: boolean };
159159
export type SourceRange = { start: number; end: number };
160160

161+
/** Source ranges the editor must not format: existing Markdown code, images,
162+
* definitions and HTML stay literal text. */
161163
export function markdownRanges(text: string) {
162164
const { tree, tooDeep } = scanMarkdown(text);
163-
const bold: SourceRange[] = [],
164-
literal: SourceRange[] = [];
165-
if (tooDeep) return { bold, literal: [{ start: 0, end: text.length }] };
165+
const literal: SourceRange[] = [];
166+
if (tooDeep) return { literal: [{ start: 0, end: text.length }] };
166167
const pending = [tree];
167168
while (pending.length) {
168169
const node = pending.pop();
@@ -183,10 +184,9 @@ export function markdownRanges(text: string) {
183184
literal.push({ start, end });
184185
continue;
185186
}
186-
if (node.type === "strong") bold.push({ start, end });
187187
pending.push(...(node.children ?? []));
188188
}
189-
return { bold, literal };
189+
return { literal };
190190
}
191191

192192
/** Draft text stays the editing/completion coordinate space. Formatting metadata

0 commit comments

Comments
 (0)