Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

markdownInlineFileLinks.test.ts

223 lines8.9 KB

1import { describe, expect, it } from "vite-plus/test";
2import { unified } from "unified";
3import remarkParse from "remark-parse";
4import remarkGfm from "remark-gfm";
5import type { Root, RootContent } from "mdast";
6import { ENRICHED_INLINE_FILE_LINK_REGEX } from "./enrichedLinkPresentation";
7import {
8 markdownForNativeRendering,
9 markdownWithInlineFileLinks,
10 markdownWithHtml,
11} from "./markdownDocument";
12​
13const rewrite = (markdown: string) =>
14 markdownWithInlineFileLinks(markdown, ENRICHED_INLINE_FILE_LINK_REGEX);
15​
16describe("absolute paths in prose", () => {
17 it("become links without corrupting the source around them", () => {
18 const path =
19 "/tmp/claude-1000/-home-user--t3-worktrees-app-t3-abc123/session/scratchpad/notes.md";
20 expect(rewrite(`Saved to ${path}.`)).toBe(`Saved to [${path}](<${path}>).`);
21 expect(
22 markdownForNativeRendering(
23 "Intro /tmp/notes.md\n| a | c |\n|---|---|\n| `./a\\|b.ts` | b |",
24 ENRICHED_INLINE_FILE_LINK_REGEX,
25 ),
26 ).toContain("[/tmp/notes.md](</tmp/notes.md>)");
27 expect(rewrite(String.raw`See \* and /tmp/notes.md.`)).toBe(
28 String.raw`See \* and [/tmp/notes.md](</tmp/notes.md>).`,
29 );
30 for (const decoded of [String.raw`See /tmp/a\_b.md.`, "See /tmp/a&copy;.md."])
31 expect(rewrite(decoded)).toBe(decoded);
32 });
33});
34​
35describe("inline code that names a file", () => {
36 it.each([
37 [
38 "file links",
39 "See `src/math.ts:2`, `C:\\repo\\a.ts`, `./src/a&copy;.ts`, and [`./README.md`](readme).",
40 ],
41 [
42 "tables",
43 "Intro `src/math.ts:2`\n| `./README.md` | Fit |\n|---|---|\n| `./a.ts` | A |\n\nMore\n| a |\n|---|\n| b |",
44 ],
45 ["escaped pipe in file header", "Intro text\n| `./a\\|b.ts` | c |\n|---|---|\n| a | b |"],
46 ["escaped pipe in file body", "Intro text\n| a | c |\n|---|---|\n| `./a\\|b.ts` | b |"],
47 [
48 "escaped pipe after a normal file span",
49 "Intro `./normal.ts`\n| `./a\\|b.ts` | c |\n|---|---|\n| a | b |",
50 ],
51 [
52 "unmatched delimiter before escaped pipe in header",
53 "Intro `./normal.ts`\n| `x``./a\\|b.ts`` | c |\n|---|---|\n|a|b|",
54 ],
55 [
56 "unmatched delimiter before escaped pipe in body",
57 "Intro `./normal.ts`\n| a | c |\n|---|---|\n| `x``./a\\|b.ts`` | b |",
58 ],
59 ["tabbed quote header", ">\tIntro `./a.ts`\n>\t| `./b.ts` | c |\n>\t|---|---|\n>\t| a | b |"],
60 ["lazy quote header", "> Intro\n`./README.md` | file\n> | - | - |\n> | ok | yes |"],
61 ["lazy list header", "- Intro\n`./README.md` | file\n | - | - |\n | ok | yes |"],
62 [
63 "HTML",
64 '<details>\n<summary>Files</summary>\n\nSee `src/math.ts:2`\n| `./README.md` |\n|---|\n| ok |\n<img alt="A &amp; B" src="https://example.com/a.png?a=1&amp;b=2">\n</details>',
65 ],
66 [
67 "nested fences",
68 '> - Example\n> ````md\n> ```ts\n> `src/math.ts:2`\n> <img src="literal.png">\n> | a |\n> |---|\n> ```\n> ````\n\nSee `./README.md`.',
69 ],
70 [
71 "HTML fences",
72 '<details>\n<summary>Example</summary>\n\n````md\n```html\n<img src="literal.png">\n```\n`src/math.ts`\n````\n\nSee `./README.md`\n</details>',
73 ],
74 ])("preserves the two-pass output for every streamed prefix: %s", (_name, fixture) => {
75 for (const ending of ["\n", "\r\n", "\r"]) {
76 const document = fixture.replaceAll("\n", ending);
77 for (let length = 0; length <= document.length; length++) {
78 const prefix = document.slice(0, length);
79 const expected = previousTableBreaks(
80 previousFileLinks(markdownWithHtml(prefix), ENRICHED_INLINE_FILE_LINK_REGEX),
81 );
82 expect(
83 markdownForNativeRendering(prefix, ENRICHED_INLINE_FILE_LINK_REGEX),
84 JSON.stringify(prefix),
85 ).toBe(expected);
86 }
87 }
88 });
89​
90 it("becomes a link to the same path", () => {
91 expect(rewrite("See `/tmp/claude-1000/...`.")).toBe("See `/tmp/claude-1000/...`.");
92 expect(rewrite("See `bin/python3.12`.")).toBe("See [bin/python3.12](<bin/python3.12>).");
93 expect(rewrite("See `src/math.ts:2` and `./README.md`.")).toBe(
94 "See [src/math.ts:2](<src/math.ts:2>) and [./README.md](<./README.md>).",
95 );
96 for (const path of [
97 "src/數學.ts:2",
98 "數學/math.ts:2",
99 "數學.ts:2",
100 "src/數學.ts:2-4",
101 "docker/Dockerfile",
102 ]) {
103 expect(rewrite(`See \`${path}\`.`), path).toBe(`See [${path}](<${path}>).`);
104 }
105 });
106​
107 it("keeps commands, identifiers and URLs as code", () => {
108 const markdown = "Run `vp test run`, call `useMemo`, open `https://example.com/a.ts`.";
109 expect(rewrite(markdown)).toBe(markdown);
110 });
111​
112 it("leaves fenced code blocks and code already inside a link alone", () => {
113 const markdown = "```\nsrc/math.ts:2\n```\n\n[`src/math.ts`](src/math.ts)";
114 expect(rewrite(markdown)).toBe(markdown);
115 });
116​
117 it("links to the exact path when it holds backslashes or ampersands", () => {
118 for (const path of ["C:\\repo\\_private\\file.ts", "./src/a&copy;.ts", "./src/[id].ts"]) {
119 const paragraph = parser.parse(rewrite(`\`${path}\``)).children[0];
120 const link = paragraph?.type === "paragraph" ? paragraph.children[0] : undefined;
121 expect(link?.type === "link" && link.url).toBe(path);
122 expect(
123 link?.type === "link" && link.children[0]?.type === "text" && link.children[0].value,
124 ).toBe(path);
125 }
126 });
127});
128​
129// Freeze the pre-optimization passes so shared transform regressions cannot change the oracle.
130const parser = unified().use(remarkParse).use(remarkGfm);
131type MarkdownNode = Root | RootContent;
132const INLINE_CODE_SPAN = /`+([^`\r\n]+)`+/g;
133​
134function previousFileLinks(markdown: string, fileLink: RegExp): string {
135 if (![...markdown.matchAll(INLINE_CODE_SPAN)].some(([, code]) => fileLink.test(code!.trim())))
136 return markdown;
137 const edits: Array<{ start: number; end: number; content: string }> = [];
138 const visit = (node: MarkdownNode) => {
139 if (node.type === "inlineCode") {
140 const start = node.position?.start.offset;
141 const end = node.position?.end.offset;
142 const escaped = node.value.replace(/[\\[\]&]/g, "\\$&");
143 if (
144 start !== undefined &&
145 end !== undefined &&
146 fileLink.test(node.value) &&
147 !/[<>]/.test(node.value)
148 )
149 edits.push({
150 start,
151 end,
152 content: `[${escaped}](<${escaped}>)`,
153 });
154 } else if (node.type !== "link" && node.type !== "linkReference" && "children" in node)
155 node.children.forEach(visit);
156 };
157 visit(parser.parse(markdown));
158 for (const edit of edits.toReversed())
159 markdown = markdown.slice(0, edit.start) + edit.content + markdown.slice(edit.end);
160 return markdown;
162​
163// A delimiter row such as `|---|:--:|`, possibly inside a blockquote or list item.
164function isTableDelimiterRow(line: string): boolean {
165 if (!line.includes("-")) return false;
166 const row = line.replace(/^[ \t>]+/, "").trim();
167 return row
168 .replace(/^\||\|$/g, "")
169 .split("|")
170 .every((cell) => /^:?-+:?$/.test(cell.trim()));
172​
173// Cheap pre-check for a delimiter row two lines below text, so most messages skip the parse.
174function mayHaveTableUnderText(markdown: string): boolean {
175 if (!markdown.includes("|")) return false;
176 const lines = markdown.split(/\r\n|\r|\n/);
177 for (let index = 2; index < lines.length; index += 1) {
178 if (isTableDelimiterRow(lines[index]!) && /[^\s>]/.test(lines[index - 2]!)) return true;
179 }
180 return false;
182​
183/**
184 * GFM lets a table interrupt a paragraph, but the native parser (md4c) only starts a table
185 * on a paragraph's first line, so a table right under a line of text renders as one
186 * run-on paragraph. Insert the blank line md4c needs before each such table.
187 */
188function previousTableBreaks(markdown: string): string {
189 if (!mayHaveTableUnderText(markdown)) return markdown;
190 const lineStarts: number[] = [];
191 const visit = (node: MarkdownNode) => {
192 if (!("children" in node)) return;
193 node.children.forEach((child, index) => {
194 const previous = node.children[index - 1];
195 const start = child.position?.start;
196 if (
197 child.type === "table" &&
198 previous?.type === "paragraph" &&
199 start?.offset !== undefined &&
200 previous.position?.end.line === start.line - 1
201 )
202 lineStarts.push(
203 Math.max(
204 markdown.lastIndexOf("\n", start.offset - 1),
205 markdown.lastIndexOf("\r", start.offset - 1),
206 ) + 1,
207 );
208 visit(child);
209 });
210 };
211 visit(parser.parse(markdown));
212 for (const lineStart of lineStarts.toReversed()) {
213 // The header row may be a lazy line without its container markers (`>` or list indent),
214 // so copy the delimiter row's markers onto the blank line and the header row.
215 const [, header = "", cells = "", ending = "\n", markers = ""] =
216 /^([ \t>]*)([^\r\n]*)(\r\n|\r|\n)([ \t>]*)/.exec(markdown.slice(lineStart)) ?? [];
217 markdown =
218 markdown.slice(0, lineStart) +
219 `${markers.trimEnd()}${ending}${markers}${cells}` +
220 markdown.slice(lineStart + header.length + cells.length);
221 }
222 return markdown;