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