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

markdownLinks.ts

308 lines8.3 KB

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