Open History to see the last commit
1import { fileBasename } from "@t3tools/shared/path";
2import { fileLinkLabel } from "@t3tools/shared/fileLinks";
3import {
4 inlineCodeFilePathCandidate,
5 normalizeMarkdownLinkDestination,
6 parseMarkdownFileLink,
7 parseObsidianNavigationHref,
8} from "@t3tools/shared/markdownLinks";
9import { parseThreadLinkHref } from "@t3tools/shared/threadLinks";
10import { videoMimeType } from "@t3tools/shared/video";
11
12import type { MARKDOWN_FILE_ICON_SOURCES } from "./markdownFileIcons.generated";
13
14const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+)?$/;
15
16export type MarkdownLinkPresentation =
17 | {
18 readonly kind: "external";
19 readonly href: string;
20 readonly host: string;
21 }
22 | {
23 readonly kind: "file";
24 readonly href: string;
25 readonly icon: MarkdownFileIcon;
26 readonly label: string;
27 readonly path: string;
28 readonly line?: number;
29 readonly column?: number;
30 }
31 | {
32 readonly kind: "link";
33 readonly href: string | null;
34 };
35
36export type MarkdownFileIcon = keyof typeof MARKDOWN_FILE_ICON_SOURCES;
37
38const FILE_ICON_BY_NAME: Readonly<Record<string, MarkdownFileIcon>> = {
39 ".babelrc": "babel",
40 ".babelrc.json": "babel",
41 ".bash_profile": "bash",
42 ".bashrc": "bash",
43 ".browserslistrc": "browserslist",
44 ".dockerignore": "docker",
45 ".eslintignore": "eslint",
46 ".eslintrc": "eslint",
47 ".eslintrc.cjs": "eslint",
48 ".eslintrc.js": "eslint",
49 ".eslintrc.json": "eslint",
50 ".eslintrc.yaml": "eslint",
51 ".eslintrc.yml": "eslint",
52 ".gitattributes": "git",
53 ".gitignore": "git",
54 ".gitkeep": "git",
55 ".gitmodules": "git",
56 ".oxlintrc.json": "oxc",
57 ".postcssrc": "postcss",
58 ".postcssrc.json": "postcss",
59 ".postcssrc.yaml": "postcss",
60 ".postcssrc.yml": "postcss",
61 ".prettierignore": "prettier",
62 ".prettierrc": "prettier",
63 ".prettierrc.json": "prettier",
64 ".prettierrc.cjs": "prettier",
65 ".prettierrc.js": "prettier",
66 ".prettierrc.mjs": "prettier",
67 ".prettierrc.toml": "prettier",
68 ".prettierrc.yaml": "prettier",
69 ".prettierrc.yml": "prettier",
70 ".stylelintignore": "stylelint",
71 ".stylelintrc": "stylelint",
72 ".stylelintrc.cjs": "stylelint",
73 ".stylelintrc.js": "stylelint",
74 ".stylelintrc.json": "stylelint",
75 ".stylelintrc.mjs": "stylelint",
76 ".stylelintrc.yaml": "stylelint",
77 ".stylelintrc.yml": "stylelint",
78 ".terraform.lock.hcl": "terraform",
79 ".zprofile": "bash",
80 ".zshenv": "bash",
81 ".zshrc": "bash",
82 "agents.md": "agents",
83 "babel.config.js": "babel",
84 "babel.config.cjs": "babel",
85 "babel.config.json": "babel",
86 "babel.config.mjs": "babel",
87 "biome.json": "biome",
88 "biome.jsonc": "biome",
89 "bun.lock": "bun",
90 "bun.lockb": "bun",
91 "bunfig.toml": "bun",
92 "claude.md": "claude",
93 "compose.yaml": "docker",
94 "compose.yml": "docker",
95 "docker-compose.yaml": "docker",
96 "docker-compose.yml": "docker",
97 "docker-compose.override.yml": "docker",
98 dockerfile: "docker",
99 "eslint.config.js": "eslint",
100 "eslint.config.cjs": "eslint",
101 "eslint.config.mjs": "eslint",
102 "eslint.config.mts": "eslint",
103 "eslint.config.ts": "eslint",
104 gemfile: "ruby",
105 "next.config.js": "nextjs",
106 "next.config.mjs": "nextjs",
107 "next.config.mts": "nextjs",
108 "next.config.ts": "nextjs",
109 "package.json": "npm",
110 "pnpm-lock.yaml": "pnpm",
111 "pnpm-workspace.yaml": "pnpm",
112 "pnpm-lock.yml": "pnpm",
113 "pnpm-workspace.yml": "pnpm",
114 "pnpmfile.js": "pnpm",
115 "pnpmfile.cjs": "pnpm",
116 "mix.lock": "elixir",
117 "vitest.config.js": "vitest",
118 "vitest.config.ts": "vitest",
119 "vitest.config.mjs": "vitest",
120 "vitest.config.mts": "vitest",
121 "vitest.config.cjs": "vitest",
122 "vitest.config.cts": "vitest",
123 "vitest.workspace.js": "vitest",
124 "vitest.workspace.ts": "vitest",
125 "vitest.workspace.mjs": "vitest",
126 "vitest.workspace.mts": "vitest",
127 "rolldown.config.js": "rolldown",
128 "rolldown.config.ts": "rolldown",
129 "rolldown.config.mjs": "rolldown",
130 "rolldown.config.mts": "rolldown",
131 "rolldown.config.cjs": "rolldown",
132 "rolldown.config.cts": "rolldown",
133 ".oxlintrc.jsonc": "oxc",
134 "oxlint.config.ts": "oxc",
135 "oxlint.config.mts": "oxc",
136 ".oxfmtrc.json": "oxc",
137 ".oxfmtrc.jsonc": "oxc",
138 "oxfmt.config.ts": "oxc",
139 "oxfmt.config.mts": "oxc",
140 "postcss.config.js": "postcss",
141 "postcss.config.cjs": "postcss",
142 "postcss.config.mjs": "postcss",
143 "postcss.config.ts": "postcss",
144 "prettier.config.js": "prettier",
145 "prettier.config.cjs": "prettier",
146 "prettier.config.mjs": "prettier",
147 rakefile: "ruby",
148 "readme.md": "markdown",
149 "stylelint.config.js": "stylelint",
150 "stylelint.config.cjs": "stylelint",
151 "stylelint.config.mjs": "stylelint",
152 "svgo.config.js": "svgo",
153 "svgo.config.cjs": "svgo",
154 "svgo.config.mjs": "svgo",
155 "svgo.config.ts": "svgo",
156 "tailwind.config.js": "tailwind",
157 "tailwind.config.cjs": "tailwind",
158 "tailwind.config.mjs": "tailwind",
159 "tailwind.config.ts": "tailwind",
160 "tsconfig.json": "typescript",
161 "vite.config.js": "vite",
162 "vite.config.mjs": "vite",
163 "vite.config.mts": "vite",
164 "vite.config.ts": "vite",
165 "webpack.config.js": "webpack",
166 "webpack.config.babel.js": "webpack",
167 "webpack.config.cjs": "webpack",
168 "webpack.config.mjs": "webpack",
169 "webpack.config.ts": "webpack",
170};
171
172const FILE_ICON_BY_EXTENSION: Readonly<Record<string, MarkdownFileIcon>> = {
173 "test.js": "javascript_test",
174 "spec.js": "javascript_test",
175 "test.jsx": "javascript_test",
176 "spec.jsx": "javascript_test",
177 "test.ts": "typescript_test",
178 "spec.ts": "typescript_test",
179 "test.tsx": "typescript_test",
180 "spec.tsx": "typescript_test",
181 cs: "csharp",
182 csx: "csharp",
183 ex: "elixir",
184 exs: "elixir",
185 kt: "kotlin",
186 kts: "kotlin",
187 m: "objc",
188 mm: "objc",
189 "7z": "zip",
190 astro: "astro",
191 avif: "image",
192 "code-workspace": "vscode",
193 bash: "bash",
194 bmp: "image",
195 bz2: "zip",
196 c: "c",
197 cc: "cpp",
198 cjs: "javascript",
199 cpp: "cpp",
200 cxx: "cpp",
201 css: "css",
202 csv: "table",
203 cts: "typescript",
204 db: "database",
205 env: "text",
206 "env.development": "text",
207 "env.local": "text",
208 "env.production": "text",
209 eot: "font",
210 erb: "ruby",
211 fish: "bash",
212 gif: "image",
213 go: "go",
214 gql: "graphql",
215 graphql: "graphql",
216 gz: "zip",
217 h: "c",
218 hh: "cpp",
219 hpp: "cpp",
220 hxx: "cpp",
221 htm: "html",
222 html: "html",
223 ico: "image",
224 icns: "image",
225 ini: "text",
226 inl: "cpp",
227 jar: "zip",
228 jpeg: "image",
229 jpg: "image",
230 js: "javascript",
231 jsx: "react",
232 json: "json",
233 jsonc: "json",
234 less: "css",
235 md: "markdown",
236 mdx: "markdown",
237 "mdx.tsx": "markdown",
238 mjs: "javascript",
239 mts: "typescript",
240 png: "image",
241 postcss: "css",
242 py: "python",
243 pyi: "python",
244 pyw: "python",
245 pyx: "python",
246 rake: "ruby",
247 rar: "zip",
248 rb: "ruby",
249 rs: "rust",
250 sass: "sass",
251 scss: "sass",
252 sh: "bash",
253 sql: "database",
254 sqlite: "database",
255 sqlite3: "database",
256 svelte: "svelte",
257 svg: "svg",
258 swift: "swift",
259 tar: "zip",
260 tf: "terraform",
261 tfstate: "terraform",
262 tfvars: "terraform",
263 tgz: "zip",
264 ts: "typescript",
265 tsv: "table",
266 tsx: "react",
267 txt: "text",
268 woff: "font",
269 woff2: "font",
270 vue: "vue",
271 wasm: "wasm",
272 webp: "image",
273 yml: "yml",
274 yaml: "yml",
275 zig: "zig",
276 zip: "zip",
277 zsh: "bash",
278};
279
280/** Native link and media APIs have no document scheme to inherit from protocol-relative URLs. */
281export function normalizeNativeMarkdownUrl(value: string): string {
282 return value.startsWith("//") ? `https:${value}` : value;
283}
284
285export function resolveMarkdownFileIcon(value: string): MarkdownFileIcon {
286 const basename = fileBasename(value).replace(POSITION_SUFFIX_PATTERN, "").toLowerCase();
287 if (videoMimeType({ name: basename, mimeType: "" }) !== null) return "video";
288 if (Object.hasOwn(FILE_ICON_BY_NAME, basename)) return FILE_ICON_BY_NAME[basename]!;
289 if (basename.startsWith("tsconfig.") && basename.endsWith(".json")) {
290 return "typescript";
291 }
292 const segments = basename.split(".");
293 for (let index = 1; index < segments.length; index += 1) {
294 const extension = segments.slice(index).join(".");
295 if (Object.hasOwn(FILE_ICON_BY_EXTENSION, extension)) return FILE_ICON_BY_EXTENSION[extension]!;
296 }
297 return "default";
298}
299
300export function resolveMarkdownLinkPresentation(href: string): MarkdownLinkPresentation {
301 const normalized = normalizeMarkdownLinkDestination(href);
302 try {
303 const parsed = new URL(normalizeNativeMarkdownUrl(normalized));
304 if (parsed.protocol === "http:" || parsed.protocol === "https:") {
305 return {
306 kind: "external",
307 href: parsed.toString(),
308 host: parsed.hostname,
309 };
310 }
311 } catch {}
312
313 const target = parseMarkdownFileLink(normalized);
314 if (target) {
315 return {
316 kind: "file",
317 href: normalized,
318 icon: resolveMarkdownFileIcon(target.path),
319 label: fileLinkLabel(target, new Map()),
320 path: target.path,
321 ...(target.line ? { line: target.line } : {}),
322 ...(target.column ? { column: target.column } : {}),
323 };
324 }
325
326 return {
327 kind: "link",
328 href:
329 parseObsidianNavigationHref(normalized) ??
330 (/^(?:mailto|tel):/i.test(normalized) || parseThreadLinkHref(normalized) !== null
331 ? normalized
332 : null),
333 };
334}
335
336export function resolveMarkdownInlineCodePresentation(
337 content: string,
338): Extract<MarkdownLinkPresentation, { readonly kind: "file" }> | null {
339 const candidate = inlineCodeFilePathCandidate(content);
340 if (candidate === null) return null;
341 const presentation = resolveMarkdownLinkPresentation(candidate);
342 return presentation.kind === "file" ? presentation : null;
343}