Skip to content

Commit 96a4d2f

Browse files
fix(web): pasted URLs and paths keep their underscores (#36363)
1 parent 51e1346 commit 96a4d2f

5 files changed

Lines changed: 124 additions & 3 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,21 @@ it("keeps unknown dollar names editable when restoring and pasting a draft", asy
467467
expect(serializeEditorDoc(editor.state.doc).value).toBe(value);
468468
});
469469

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+
470485
it.each(["x\r\n\r\ny", "x\r\ry"])(
471486
"pastes %j with the same blank line as x\\n\\ny",
472487
async (text) => {

‎apps/web/src/composer-rich-text-doc.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ describe("pasting into a path query", () => {
954954
});
955955

956956
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"]);
958958
expect(boldText(paste("Open @a ", "__b__"))).toEqual(["b"]);
959959
expect(boldText(paste("Open @", " __b__"))).toEqual(["b"]);
960960
});

‎apps/web/src/composer-rich-text.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,58 @@ describe("composer rich text markdown", () => {
5151
]);
5252
});
5353

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+
54106
it("keeps code span contents literal", () => {
55107
expect(parseInlineMarkdown("`**not bold**`")).toEqual([
56108
{ text: "**not bold**", marks: ["code"] },

‎apps/web/src/composer-rich-text.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,52 @@ function sameMarks(left: readonly RichTextMark[], right: readonly RichTextMark[]
2323
return left.length === right.length && left.every((mark, index) => mark === right[index]);
2424
}
2525

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+
2668
// Pieces keep source offsets, so callers can map parsed text back onto the
2769
// characters it came from; parsing only drops delimiters.
2870
function parseInlineMarkdownPieces(text: string): SourcePiece[] {
71+
const literalRuns = literalUnderscoreRuns(text);
2972
const root: SourcePiece[] = [];
3073
const stack: { delimiter: string; mark: RichTextMark; start: number; pieces: SourcePiece[] }[] =
3174
[];
@@ -68,6 +111,11 @@ function parseInlineMarkdownPieces(text: string): SourcePiece[] {
68111
}
69112
let end = index;
70113
while (text[end] === char) end += 1;
114+
if (literalRuns.has(index)) {
115+
push(index, end);
116+
index = end;
117+
continue;
118+
}
71119
const before = text[index - 1] ?? "";
72120
const after = text[end] ?? "";
73121
const canClose = before !== "" && !/\s/.test(before) && (char !== "_" || !/\w/.test(after));

‎apps/web/src/composer-undo-grouping.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ export const COMPOSER_UNDO_GROUP_DELAY = 1000;
77

88
export type ComposerChangeKind = "insert" | "delete" | "other";
99

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+
*/
1116
export function markAsClipboardEdit(tr: Transaction, event: "paste" | "cut"): Transaction {
12-
return tr.setMeta("uiEvent", event);
17+
return tr.setMeta(CLIPBOARD_EDIT_META, event);
1318
}
1419

1520
function changeKindOf(tr: Transaction): ComposerChangeKind | null {
1621
if (!tr.docChanged) return null;
1722
const uiEvent = tr.getMeta("uiEvent");
1823
if (uiEvent === "paste" || uiEvent === "drop" || uiEvent === "cut") return "other";
24+
if (tr.getMeta(CLIPBOARD_EDIT_META) != null) return "other";
1925
// IME input replaces its own range on each keystroke; it is still typing.
2026
if (tr.getMeta("composition") != null) return "insert";
2127
let kind: ComposerChangeKind | null = null;

0 commit comments

Comments
 (0)