Skip to content

Commit 8a6c612

Browse files
committed
perf(shared): avoid rescanning paragraphs for Markdown escapes
1 parent b8c2cfa commit 8a6c612

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
);

‎packages/shared/src/markdownWindowsPaths.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ interface EscapeCompileContext {
2929
function keepWindowsPathEscape(this: EscapeCompileContext) {
3030
const text = this.stack.at(-1)?.children?.at(-1);
3131
if (text?.type !== "text" || text.value === undefined) return;
32-
const before = text.value.slice(0, -1);
32+
// A prose path cannot cross whitespace, so preceding words cannot affect this escape.
33+
const end = text.value.length - 1;
34+
let start = end;
35+
while (start > 0 && !/\s/.test(text.value[start - 1]!)) start -= 1;
36+
const before = text.value.slice(start, end);
3337
if (UNFINISHED_DRIVE_PATH.test(before)) {
34-
text.value = `${before}\\${text.value.slice(-1)}`;
38+
text.value = `${text.value.slice(0, end)}\\${text.value.slice(end)}`;
3539
}
3640
}
3741

0 commit comments

Comments
 (0)