Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { resolveMarkdownFileIcon, resolveMarkdownLinkPresentation } from "./markdownLinks";
4
5describe("resolveMarkdownFileIcon", () => {
6 it.each(["constructor", "/tmp/main.constructor"])("uses the generic file icon for %s", (path) => {
7 expect(resolveMarkdownFileIcon(path)).toBe("default");
8 });
9});
10
11describe("resolveMarkdownLinkPresentation", () => {
12 it("opens relative directory links as workspace paths", () => {
13 expect(resolveMarkdownLinkPresentation("src/")).toMatchObject({
14 kind: "file",
15 path: "src/",
16 });
17 expect(resolveMarkdownLinkPresentation("docs/My Folder/")).toMatchObject({
18 kind: "file",
19 path: "docs/My Folder/",
20 });
21 });
22
23 it("treats protocol-relative media as an external URL, not a filesystem path", () => {
24 expect(resolveMarkdownLinkPresentation("//cdn.example.com/clip.mp4?sig=a%2fb#t=2")).toEqual({
25 kind: "external",
26 href: "https://cdn.example.com/clip.mp4?sig=a%2fb#t=2",
27 host: "cdn.example.com",
28 });
29 });
30
31 it("separates encoded filename characters from a video playback fragment", () => {
32 expect(resolveMarkdownLinkPresentation("/tmp/clip%23one.mp4#t=2")).toMatchObject({
33 path: "/tmp/clip#one.mp4",
34 label: "clip#one.mp4",
35 icon: "video",
36 });
37 });
38
39 it.each([
40 ["file:///Users/julius/project/src/main.ts#L42C7", "/Users/julius/project/src/main.ts"],
41 ["file://server/share/src/main.ts#L42C7", "\\\\server\\share\\src\\main.ts"],
42 ])("preserves the file URL path and position for %s", (href, path) => {
43 expect(resolveMarkdownLinkPresentation(href)).toEqual({
44 kind: "file",
45 href,
46 icon: "typescript",
47 label: "main.ts · L42:C7",
48 path,
49 line: 42,
50 column: 7,
51 });
52 });
53
54 it("recognizes relative source paths and bare filenames", () => {
55 expect(resolveMarkdownLinkPresentation("apps/mobile/src/index.ts:10")).toEqual({
56 kind: "file",
57 href: "apps/mobile/src/index.ts:10",
58 icon: "typescript",
59 label: "index.ts · L10",
60 path: "apps/mobile/src/index.ts",
61 line: 10,
62 });
63 expect(resolveMarkdownLinkPresentation("AGENTS.md")).toEqual({
64 kind: "file",
65 href: "AGENTS.md",
66 icon: "agents",
67 label: "AGENTS.md",
68 path: "AGENTS.md",
69 });
70 expect(resolveMarkdownLinkPresentation("package.json")).toEqual({
71 kind: "file",
72 href: "package.json",
73 icon: "npm",
74 label: "package.json",
75 path: "package.json",
76 });
77 });
78
79 it("shows CommonJS sources with the JavaScript icon", () => {
80 expect(resolveMarkdownLinkPresentation("scripts/build.cjs")).toMatchObject({
81 kind: "file",
82 icon: "javascript",
83 });
84 });
85
86 // regression: #8098 open agent file links in the file viewer
87 it("recognizes spaced relative paths", () => {
88 expect(resolveMarkdownLinkPresentation("docs/My%20Folder/checklist.xml")).toMatchObject({
89 kind: "file",
90 path: "docs/My Folder/checklist.xml",
91 label: "checklist.xml",
92 });
93 });
94
95 it("preserves Obsidian note destinations but not command or write links", () => {
96 const href = "obsidian://adv-uri?vault=Example%20Vault&filepath=notes%2FExample.md";
97 expect(resolveMarkdownLinkPresentation(href)).toEqual({ kind: "link", href });
98 expect(
99 resolveMarkdownLinkPresentation("obsidian://adv-uri?commandid=app%3Adelete-file"),
100 ).toEqual({ kind: "link", href: null });
101 });
102
103 it("does not style app routes as file links", () => {
104 expect(resolveMarkdownLinkPresentation("/chat/settings")).toEqual({
105 kind: "link",
106 href: null,
107 });
108 });
109});