Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

markdownLinks.ts

342 lines9.1 KB

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