Skip to content

Commit 65ffcbf

Browse files
AksharP5juliusmarmingeclaude
authored
perf(shared): speed up escaped Markdown in long PR descriptions (pingdotgg#33438)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b37c7f6 commit 65ffcbf

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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&#x20;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‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,23 @@ interface EscapeCompileContext {
2626
}>;
2727
}
2828

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+
2933
function keepWindowsPathEscape(this: EscapeCompileContext) {
3034
const text = this.stack.at(-1)?.children?.at(-1);
3135
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)}`;
3544
}
45+
scan.scannedTo = text.value.length;
3646
}
3747

3848
function keepWindowsProsePathEscape(this: EscapeCompileContext) {

0 commit comments

Comments
 (0)