The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 00:45 UTC
perf(shared): speed up escaped Markdown in long PR descriptions (#33438)
Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Akshar Patel committed
Nov 10, 2026 at 03:03 UTC
65ffcbf647e706dcb4c7d9834724c74ff4b8dabb
2 files changed
+90
-3
packages/shared/src/markdownWindowsPaths.test.ts
new
+77
@@ -0,0 +1,77 @@
1
+import { expect, it, vi } from "vite-plus/test";
2
+import { unified } from "unified";
3
+import remarkParse from "remark-parse";
4
+
5
+import { remarkKeepWindowsPaths, remarkKeepWindowsProsePaths } from "./markdownWindowsPaths.ts";
6
+
7
+const plugins = [
8
+ ["paths and destinations", remarkKeepWindowsPaths],
9
+ ["prose paths", remarkKeepWindowsProsePaths],
10
+] as const;
11
+
12
+it.each(plugins)("%s limits drive-path checks to the word containing each escape", (_, plugin) => {
13
+ const processor = unified().use(remarkParse).use(plugin);
14
+ const identifiers = Array.from({ length: 200 }, (_, index) => `metric_name_${index}`);
15
+ const markdown = `Supported metrics: ${identifiers.map((name) => name.replaceAll("_", "\\_")).join(", ")}.`;
16
+ const pattern = /(?:^|\W)[A-Za-z]:(?:[\\/]\S*)?$/.source;
17
+ const test = RegExp.prototype.test;
18
+ let inspectedCharacters = 0;
19
+ using _test = vi.spyOn(RegExp.prototype, "test").mockImplementation(function (
20
+ this: RegExp,
21
+ value: string,
22
+ ) {
23
+ if (this.source === pattern) inspectedCharacters += value.length;
24
+ return test.call(this, value);
25
+ });
26
+
27
+ expect(processor.parse(markdown)).toMatchObject({
28
+ children: [
29
+ {
30
+ type: "paragraph",
31
+ children: [{ type: "text", value: `Supported metrics: ${identifiers.join(", ")}.` }],
32
+ },
33
+ ],
34
+ });
35
+ expect(inspectedCharacters).toBeGreaterThan(0);
36
+ expect(inspectedCharacters).toBeLessThan(markdown.length * 2);
37
+});
38
+
39
+it.each(plugins)(
40
+ "%s preserves prose paths and ordinary escapes across whitespace boundaries",
41
+ (_, plugin) => {
42
+ const processor = unified().use(remarkParse).use(plugin);
43
+ for (const whitespace of [" ", "\t", "\u00a0", "\u2003", "\u202f", "\ufeff"]) {
44
+ const markdown = String.raw`Before${whitespace}C:\Users\me\_build${whitespace}\*literal\*`;
45
+ expect(processor.parse(markdown)).toMatchObject({
46
+ children: [
47
+ {
48
+ type: "paragraph",
49
+ children: [
50
+ {
51
+ type: "text",
52
+ value: String.raw`Before${whitespace}C:\Users\me\_build${whitespace}*literal*`,
53
+ },
54
+ ],
55
+ },
56
+ ],
57
+ });
58
+ }
59
+ },
60
+);
61
+
62
+it.each(plugins)("%s finds each escape's word after earlier escapes in the text", (_, plugin) => {
63
+ const processor = unified().use(remarkParse).use(plugin);
64
+ const markdown = String.raw`one\_two C:\a\_b \*x\* three\_four D:\c\_d \*y\* *em*E:\e\_f \*z\*`;
65
+ expect(processor.parse(markdown)).toMatchObject({
66
+ children: [
67
+ {
68
+ type: "paragraph",
69
+ children: [
70
+ { type: "text", value: String.raw`one_two C:\a\_b *x* three_four D:\c\_d *y* ` },
71
+ { type: "emphasis" },
72
+ { type: "text", value: String.raw`E:\e\_f *z*` },
73
+ ],
74
+ },
75
+ ],
76
+ });
77
+});
packages/shared/src/markdownWindowsPaths.ts
+13
-3
@@ -26,13 +26,23 @@ interface EscapeCompileContext {
26
}>;
27
}
28
29
+const WHITESPACE = /\s/g;
30
+// Text nodes only grow by appending while parsing, so each one is scanned for whitespace once.
31
+const wordScans = new WeakMap<object, { scannedTo: number; wordStart: number }>();
32
+
33
function keepWindowsPathEscape(this: EscapeCompileContext) {
34
const text = this.stack.at(-1)?.children?.at(-1);
35
if (text?.type !== "text" || text.value === undefined) return;
32
- const before = text.value.slice(0, -1);
33
- if (UNFINISHED_DRIVE_PATH.test(before)) {
34
- text.value = `${before}\\${text.value.slice(-1)}`;
36
+ let scan = wordScans.get(text);
37
+ if (!scan) wordScans.set(text, (scan = { scannedTo: 0, wordStart: 0 }));
38
+ WHITESPACE.lastIndex = scan.scannedTo;
39
+ while (WHITESPACE.exec(text.value)) scan.wordStart = WHITESPACE.lastIndex;
40
+ // A prose path cannot cross whitespace, so preceding words cannot affect this escape.
41
+ const end = text.value.length - 1;
42
+ if (UNFINISHED_DRIVE_PATH.test(text.value.slice(scan.wordStart, end))) {
43
+ text.value = `${text.value.slice(0, end)}\\${text.value.slice(end)}`;
44
}
45
+ scan.scannedTo = text.value.length;
46
}
47
48
function keepWindowsProsePathEscape(this: EscapeCompileContext) {