Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { ENRICHED_INLINE_FILE_LINK_REGEX } from "./enrichedLinkPresentation";
3import { markdownWithInlineFileLinks, parseMarkdownDocument } from "./markdownDocument";
4
5const rewrite = (markdown: string) =>
6 markdownWithInlineFileLinks(markdown, ENRICHED_INLINE_FILE_LINK_REGEX);
7
8describe("inline code that names a file", () => {
9 it("becomes a link to the same path", () => {
10 expect(rewrite("See `src/math.ts:2` and `./README.md`.")).toBe(
11 "See [src/math.ts:2](<src/math.ts:2>) and [./README.md](<./README.md>).",
12 );
13 });
14
15 it("keeps commands, identifiers and URLs as code", () => {
16 const markdown = "Run `vp test run`, call `useMemo`, open `https://example.com/a.ts`.";
17 expect(rewrite(markdown)).toBe(markdown);
18 });
19
20 it("leaves fenced code blocks and code already inside a link alone", () => {
21 const markdown = "```\nsrc/math.ts:2\n```\n\n[`src/math.ts`](src/math.ts)";
22 expect(rewrite(markdown)).toBe(markdown);
23 });
24
25 it("links to the exact path when it holds backslashes or ampersands", () => {
26 for (const path of ["C:\\repo\\_private\\file.ts", "./src/a©.ts", "./src/[id].ts"]) {
27 const paragraph = parseMarkdownDocument(rewrite(`\`${path}\``)).children[0];
28 const link = paragraph?.type === "paragraph" ? paragraph.children[0] : undefined;
29 expect(link?.type === "link" && link.url).toBe(path);
30 expect(
31 link?.type === "link" && link.children[0]?.type === "text" && link.children[0].value,
32 ).toBe(path);
33 }
34 });
35});