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