The best way to control Claude Code, Codex and more from any computer or phone
fix(web): wrapping a selection in backticks formats it as code (#14870)
Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: maria-rcks <maria@kuuro.net>
Sepehr Rajabi committed
Nov 11, 2026 at 22:24 UTC
21a9aa1d79684b22adb3cd9331deacd547762eff
3 files changed
+126
apps/web/src/components/ComposerPromptEditorTiptap.tsx
+6
@@ -92,6 +92,7 @@ import {
92
serializeSelection,
93
skillChipReplacements,
94
splitOrLiftListItem,
95
+ surroundSelectionWithMark,
96
stepCaretAcrossStyledEdge,
97
type IsKnownModel,
98
type SkillMeta,
@@ -1355,6 +1356,11 @@ function ComposerPromptEditorTiptapInner(
1356
const startMd = flatToMarkdown(map, pmToFlat(map, from));
1357
const endMd = flatToMarkdown(map, pmToFlat(map, to));
1358
if (selectionTouchesMentionBoundary(map.value, startMd, endMd)) return false;
1359
+ const markTr = surroundSelectionWithMark(view.state, from, to, text);
1360
+ if (markTr) {
1361
+ view.dispatch(markTr);
1362
+ return true;
1363
+ }
1364
const tr = view.state.tr.insertText(closer, to).insertText(text, from);
1365
tr.setSelection(TextSelection.create(tr.doc, from + text.length, to + text.length));
1366
view.dispatch(tr);
apps/web/src/composer-rich-text-doc.test.ts
+81
@@ -33,6 +33,7 @@ import {
33
serializeSelection,
34
skillChipReplacements,
35
stepCaretAcrossStyledEdge,
36
+ surroundSelectionWithMark,
37
} from "./composer-rich-text-doc";
38
39
function stubAtom(name: string, attrs: Record<string, { default: unknown }>) {
@@ -1359,3 +1360,83 @@ describe("inline code entry", () => {
1360
expect(marked(editor, mark)).toEqual(["foo"]);
1361
});
1362
});
1363
+
1364
+describe("surroundSelectionWithMark", () => {
1365
+ it("keeps markers inside math literal across draft restoration", () => {
1366
+ const doc = schema.node("doc", null, [schema.node("paragraph", null, schema.text("$x$"))]);
1367
+ const state = EditorState.create({ doc, selection: TextSelection.create(doc, 2, 3) });
1368
+ expect(surroundSelectionWithMark(state, 2, 3, "*")).toBeNull();
1369
+ });
1370
+
1371
+ it.each([
1372
+ ["`", "hello", "say `hello` now"],
1373
+ ["*", "hello", "say *hello* now"],
1374
+ ["_", "hello", "say *hello* now"],
1375
+ ["`", " ", "say ` ` now"],
1376
+ ])("formats a selection with %s like typed markers (%j)", (marker, selected, expected) => {
1377
+ const doc = schema.node("doc", null, [
1378
+ schema.node("paragraph", null, schema.text(`say ${selected} now`)),
1379
+ ]);
1380
+ const to = 5 + selected.length;
1381
+ const state = EditorState.create({ doc, selection: TextSelection.create(doc, 5, to) });
1382
+ const tr = surroundSelectionWithMark(state, 5, to, marker);
1383
+ expect(tr).not.toBeNull();
1384
+ const formatted = state.apply(tr!);
1385
+ const typed = ProseMirrorNode.fromJSON(
1386
+ schema,
1387
+ buildDocJson(`say ${marker}${selected}${marker} now`, (name) => ({
1388
+ label: name,
1389
+ description: null,
1390
+ })),
1391
+ );
1392
+ expect(formatted.doc.eq(typed)).toBe(true);
1393
+ expect(serializeEditorDoc(formatted.doc).value).toBe(expected);
1394
+ expect(formatted.doc.textBetween(formatted.selection.from, formatted.selection.to)).toBe(
1395
+ selected,
1396
+ );
1397
+ });
1398
+
1399
+ it.each([
1400
+ ["*", " "],
1401
+ ["_", " "],
1402
+ ["*", "a*b"],
1403
+ ["`", "foo`bar"],
1404
+ ])("leaves %s around %j to literal wrapping when the mark cannot round-trip", (marker, text) => {
1405
+ const doc = schema.node("doc", null, [schema.node("paragraph", null, schema.text(text))]);
1406
+ const to = 1 + text.length;
1407
+ const state = EditorState.create({ doc, selection: TextSelection.create(doc, 1, to) });
1408
+ expect(surroundSelectionWithMark(state, 1, to, marker)).toBeNull();
1409
+ });
1410
+
1411
+ it.each([
1412
+ [
1413
+ "plain mode",
1414
+ plainSchema.node("doc", null, [plainSchema.node("paragraph", null, plainSchema.text("one"))]),
1415
+ ],
1416
+ [
1417
+ "paragraphs",
1418
+ schema.node("doc", null, [
1419
+ schema.node("paragraph", null, schema.text("one")),
1420
+ schema.node("paragraph", null, schema.text("two")),
1421
+ ]),
1422
+ ],
1423
+ [
1424
+ "a hard break",
1425
+ schema.node("doc", null, [
1426
+ schema.node("paragraph", null, [
1427
+ schema.text("one"),
1428
+ schema.node("hardBreak"),
1429
+ schema.text("two"),
1430
+ ]),
1431
+ ]),
1432
+ ],
1433
+ [
1434
+ "a code block",
1435
+ schema.node("doc", null, [schema.node("codeBlock", null, schema.text("one"))]),
1436
+ ],
1437
+ ])("leaves selections in %s to literal wrapping", (_, doc) => {
1438
+ doc.check();
1439
+ const state = EditorState.create({ doc });
1440
+ expect(surroundSelectionWithMark(state, 1, doc.content.size - 1, "`")).toBeNull();
1441
+ });
1442
+});
apps/web/src/composer-rich-text-doc.ts
+39
@@ -304,6 +304,45 @@ function markInputRuleOutsideCode(find: RegExp, type: MarkType) {
304
});
305
}
306
307
+const SURROUND_MARKS: Record<string, string> = { "`": "code", "*": "italic", _: "italic" };
308
+
309
+export function surroundSelectionWithMark(
310
+ state: EditorState,
311
+ from: number,
312
+ to: number,
313
+ marker: string,
314
+) {
315
+ const markType = state.schema.marks[SURROUND_MARKS[marker] ?? ""];
316
+ if (!markType || touchesMarkdownSource(state, from, to)) return null;
317
+ const $from = state.doc.resolve(from);
318
+ if (!$from.sameParent(state.doc.resolve(to)) || !$from.parent.type.allowsMarkType(markType)) {
319
+ return null;
320
+ }
321
+ // Inline markdown cannot span lines, so keep literal wrapping across hard breaks.
322
+ let crossesLine = false;
323
+ state.doc.nodesBetween(from, to, (node) => {
324
+ if (node.type.name === "hardBreak") crossesLine = true;
325
+ return !crossesLine;
326
+ });
327
+ if (crossesLine) return null;
328
+ const tr = state.tr.addMark(from, to, markType.create());
329
+ // Nearby delimiters can change how the saved span parses. Atoms are opaque
330
+ // to inline markdown, so use a placeholder instead of parsing their sources.
331
+ const children: ProseMirrorNode[] = [];
332
+ tr.doc.resolve(from).parent.forEach((node) => {
333
+ children.push(
334
+ node.isAtom && !node.isText && node.type.name !== "hardBreak"
335
+ ? state.schema.text("\uFFFC", node.marks)
336
+ : node,
337
+ );
338
+ });
339
+ const paragraph = state.schema.nodes.paragraph!.create(null, children);
340
+ const markdown = serializeEditorDoc(state.doc.type.create(null, paragraph)).value;
341
+ const rebuilt = buildTiptapContent(markdown, () => null, { blocks: false });
342
+ const restored = state.schema.nodeFromJSON({ type: "doc", content: rebuilt });
343
+ return paragraph.content.eq(restored.firstChild!.content) ? tr : null;
344
+}
345
+
346
/**
347
* The composer writes italic as `*text*`, so it keeps only the `*` shortcuts.
348
* Tiptap's `_text_` rules fire on the `_` in names like `node_modules` and