@devries-cameron20 / t3code-1 / commits / 96a4d2f012
Fork 0

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

fix(web): pasted URLs and paths keep their underscores (#36363)

Julius Marminge committed Nov 13, 2026 at 12:33 UTC 96a4d2f012007a0bb3717ae99d026b2ea3ceeefc
5 files changed +124 -3
apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx
+15
@@ -467,6 +467,21 @@ it("keeps unknown dollar names editable when restoring and pasting a draft", asy
467 expect(serializeEditorDoc(editor.state.doc).value).toBe(value);
468 });
469
470 +// regression: #25180 Tiptap's paste rules styled the pasted text a second time
471 +it.each([
472 + "Read https://example.com/_handle_/status/1 and open src/__test__.tsx",
473 + "see `**x**` now",
474 +])("sends pasted %s as pasted", async (pasted) => {
475 + await using screen = await mountEditor("");
476 + const { editor } = screen;
477 + await act(async () => {
478 + const clipboardData = new DataTransfer();
479 + clipboardData.setData("text/plain", pasted);
480 + editor.view.dom.dispatchEvent(new ClipboardEvent("paste", { clipboardData, cancelable: true }));
481 + });
482 + expect(serializeEditorDoc(editor.state.doc).value).toBe(pasted);
483 +});
484 +
485 it.each(["x\r\n\r\ny", "x\r\ry"])(
486 "pastes %j with the same blank line as x\\n\\ny",
487 async (text) => {
apps/web/src/composer-rich-text-doc.test.ts
+1 -1
@@ -954,7 +954,7 @@ describe("pasting into a path query", () => {
954 });
955
956 it("parses markdown when the paste does not continue a path query", () => {
957 - expect(boldText(paste("Open ", "src/__test__.tsx"))).toEqual(["test"]);
957 + expect(boldText(paste("Open ", "__test__ now"))).toEqual(["test"]);
958 expect(boldText(paste("Open @a ", "__b__"))).toEqual(["b"]);
959 expect(boldText(paste("Open @", " __b__"))).toEqual(["b"]);
960 });
apps/web/src/composer-rich-text.test.ts
+52
@@ -51,6 +51,58 @@ describe("composer rich text markdown", () => {
51 ]);
52 });
53
54 + // regression: #25180 underscores in URLs and paths were read as emphasis
55 + it("keeps underscores inside URLs, paths, and filenames literal", () => {
56 + for (const text of [
57 + "Read https://example.com/_handle_/status/1 and open src/__test__.tsx",
58 + "https://example.com/~_user_/a",
59 + "_build_/file.ts",
60 + "edit __init__.py, then",
61 + "see __init__.py:42",
62 + "open src/_a.ts_ and example.com/_handle_.",
63 + "/" + "_".repeat(50_000) + "a",
64 + ]) {
65 + expect(parseInlineMarkdown(text)).toEqual([{ text, marks: [] }]);
66 + }
67 + for (const [text, styled, mark, rest] of [
68 + ["_see src/a_b.ts_ now", "see src/a_b.ts", "italic", " now"],
69 + ["_read/write access_ now", "read/write access", "italic", " now"],
70 + ["__read/write access__ now", "read/write access", "bold", " now"],
71 + ["_see src/_a.ts_(optional)", "see src/_a.ts", "italic", "(optional)"],
72 + ["__see src/__a.ts__(optional)", "see src/__a.ts", "bold", "(optional)"],
73 + ["_see src/_a.ts_.", "see src/_a.ts", "italic", "."],
74 + ["_notes.md_ here", "notes.md", "italic", " here"],
75 + ["_e.g._ this", "e.g.", "italic", " this"],
76 + ["_and_/or", "and", "italic", "/or"],
77 + ] as const) {
78 + expect(parseInlineMarkdown(text)).toEqual([
79 + { text: styled, marks: [mark] },
80 + { text: rest, marks: [] },
81 + ]);
82 + }
83 + for (const [text, first, second, mark] of [
84 + ["_yes_/_no_", "yes", "no", "italic"],
85 + ["__yes__/__no__.", "yes", "no", "bold"],
86 + ["_a_b_/_no_", "a_b", "no", "italic"],
87 + ] as const) {
88 + expect(parseInlineMarkdown(text).slice(0, 3)).toEqual([
89 + { text: first, marks: [mark] },
90 + { text: "/", marks: [] },
91 + { text: second, marks: [mark] },
92 + ]);
93 + }
94 + expect(parseInlineMarkdown("and/_or_")).toEqual([
95 + { text: "and/", marks: [] },
96 + { text: "or", marks: ["italic"] },
97 + ]);
98 + expect(parseInlineMarkdown("_build_/file.ts and _x_, __init__.py and __y__")).toEqual([
99 + { text: "_build_/file.ts and ", marks: [] },
100 + { text: "x", marks: ["italic"] },
101 + { text: ", __init__.py and ", marks: [] },
102 + { text: "y", marks: ["bold"] },
103 + ]);
104 + });
105 +
106 it("keeps code span contents literal", () => {
107 expect(parseInlineMarkdown("`**not bold**`")).toEqual([
108 { text: "**not bold**", marks: ["code"] },
apps/web/src/composer-rich-text.ts
+48
@@ -23,9 +23,52 @@ function sameMarks(left: readonly RichTextMark[], right: readonly RichTextMark[]
23 return left.length === right.length && left.every((mark, index) => mark === right[index]);
24 }
25
26 +// Brackets, quotes, commas, and Markdown delimiters end a URL or path token; a lone `~` does not.
27 +const PATH_TOKEN = /(?:[\p{L}\p{N}_/.:@%+=&?#-]|(?<!~)~(?!~))+/gu;
28 +const FILE_EXTENSION = /^\.[a-z][a-z0-9]*(?::\d+){0,2}$/i;
29 +// `_yes_/_no_`: each side of a slash is its own emphasis span, so the word is not a path.
30 +const EMPHASIS_ALTERNATIVES = /^_[^/]+_(?:\/_[^/]+_)+$/;
31 +
32 +/**
33 + * Starts of underscore runs that belong to a URL, path, or filename, not to emphasis.
34 + * A run ending the token stays a delimiter so emphasis wrapped around the token still closes.
35 + */
36 +function literalUnderscoreRuns(text: string): Set<number> {
37 + const literal = new Set<number>();
38 + if (!text.includes("_")) return literal;
39 + for (const match of text.matchAll(PATH_TOKEN)) {
40 + const token = match[0];
41 + let bodyEnd = token.length;
42 + while (bodyEnd > 0 && ".:?".includes(token[bodyEnd - 1]!)) bodyEnd -= 1;
43 + if (EMPHASIS_ALTERNATIVES.test(token.slice(0, bodyEnd))) continue;
44 + while (bodyEnd > 0 && token[bodyEnd - 1] === "_") bodyEnd -= 1;
45 + const body = token.slice(0, bodyEnd);
46 + // A slash alone is prose (`and/_or_`); a path also has a dot, a scheme or port, or a root.
47 + const isPath = body.includes("/")
48 + ? /^~?\/|[.:]/.test(body)
49 + : FILE_EXTENSION.test(body.slice(body.lastIndexOf(".")));
50 + if (!isPath) continue;
51 + let closesInside = false;
52 + let start = body.indexOf("_");
53 + while (start !== -1) {
54 + let end = start;
55 + while (body[end] === "_") end += 1;
56 + if (start > 0) {
57 + literal.add(match.index + start);
58 + closesInside ||= !/\w/.test(body[end]!);
59 + }
60 + start = body.indexOf("_", end);
61 + }
62 + // A leading run could only pair with a run inside the name, as in `__init__.py`.
63 + if (closesInside && body[0] === "_") literal.add(match.index);
64 + }
65 + return literal;
66 +}
67 +
68 // Pieces keep source offsets, so callers can map parsed text back onto the
69 // characters it came from; parsing only drops delimiters.
70 function parseInlineMarkdownPieces(text: string): SourcePiece[] {
71 + const literalRuns = literalUnderscoreRuns(text);
72 const root: SourcePiece[] = [];
73 const stack: { delimiter: string; mark: RichTextMark; start: number; pieces: SourcePiece[] }[] =
74 [];
@@ -68,6 +111,11 @@ function parseInlineMarkdownPieces(text: string): SourcePiece[] {
111 }
112 let end = index;
113 while (text[end] === char) end += 1;
114 + if (literalRuns.has(index)) {
115 + push(index, end);
116 + index = end;
117 + continue;
118 + }
119 const before = text[index - 1] ?? "";
120 const after = text[end] ?? "";
121 const canClose = before !== "" && !/\s/.test(before) && (char !== "_" || !/\w/.test(after));
apps/web/src/composer-undo-grouping.ts
+8 -2
@@ -7,15 +7,21 @@ export const COMPOSER_UNDO_GROUP_DELAY = 1000;
7
8 export type ComposerChangeKind = "insert" | "delete" | "other";
9
10 -/** Tag custom paste/cut dispatches; ProseMirror only tags its built-in paths. */
10 +const CLIPBOARD_EDIT_META = "composerClipboardEdit";
11 +
12 +/**
13 + * Tag custom paste/cut dispatches; ProseMirror only tags its built-in paths.
14 + * Not `uiEvent`: Tiptap's paste rules would style the already parsed paste a second time.
15 + */
16 export function markAsClipboardEdit(tr: Transaction, event: "paste" | "cut"): Transaction {
12 - return tr.setMeta("uiEvent", event);
17 + return tr.setMeta(CLIPBOARD_EDIT_META, event);
18 }
19
20 function changeKindOf(tr: Transaction): ComposerChangeKind | null {
21 if (!tr.docChanged) return null;
22 const uiEvent = tr.getMeta("uiEvent");
23 if (uiEvent === "paste" || uiEvent === "drop" || uiEvent === "cut") return "other";
24 + if (tr.getMeta(CLIPBOARD_EDIT_META) != null) return "other";
25 // IME input replaces its own range on each keystroke; it is still typing.
26 if (tr.getMeta("composition") != null) return "insert";
27 let kind: ComposerChangeKind | null = null;