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

markdownLinks.ts

298 lines7.9 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 "postcss.config.js": "postcss",
112 "postcss.config.cjs": "postcss",
113 "postcss.config.mjs": "postcss",
114 "postcss.config.ts": "postcss",
115 "prettier.config.js": "prettier",
116 "prettier.config.cjs": "prettier",
117 "prettier.config.mjs": "prettier",
118 rakefile: "ruby",
119 "readme.md": "markdown",
120 "stylelint.config.js": "stylelint",
121 "stylelint.config.cjs": "stylelint",
122 "stylelint.config.mjs": "stylelint",
123 "svgo.config.js": "svgo",
124 "svgo.config.cjs": "svgo",
125 "svgo.config.mjs": "svgo",
126 "svgo.config.ts": "svgo",
127 "tailwind.config.js": "tailwind",
128 "tailwind.config.cjs": "tailwind",
129 "tailwind.config.mjs": "tailwind",
130 "tailwind.config.ts": "tailwind",
131 "tsconfig.json": "typescript",
132 "vite.config.js": "vite",
133 "vite.config.mjs": "vite",
134 "vite.config.mts": "vite",
135 "vite.config.ts": "vite",
136 "webpack.config.js": "webpack",
137 "webpack.config.babel.js": "webpack",
138 "webpack.config.cjs": "webpack",
139 "webpack.config.mjs": "webpack",
140 "webpack.config.ts": "webpack",
141};
142​
143const FILE_ICON_BY_EXTENSION: Readonly<Record<string, MarkdownFileIcon>> = {
144 "7z": "zip",
145 astro: "astro",
146 avif: "image",
147 "code-workspace": "vscode",
148 bash: "bash",
149 bmp: "image",
150 bz2: "zip",
151 c: "c",
152 cc: "cpp",
153 cjs: "javascript",
154 cpp: "cpp",
155 cxx: "cpp",
156 css: "css",
157 csv: "table",
158 cts: "typescript",
159 db: "database",
160 env: "text",
161 "env.development": "text",
162 "env.local": "text",
163 "env.production": "text",
164 eot: "font",
165 erb: "ruby",
166 fish: "bash",
167 gif: "image",
168 go: "go",
169 gql: "graphql",
170 graphql: "graphql",
171 gz: "zip",
172 h: "c",
173 hh: "cpp",
174 hpp: "cpp",
175 hxx: "cpp",
176 htm: "html",
177 html: "html",
178 ico: "image",
179 icns: "image",
180 ini: "text",
181 inl: "cpp",
182 jar: "zip",
183 jpeg: "image",
184 jpg: "image",
185 js: "javascript",
186 jsx: "react",
187 json: "json",
188 jsonc: "json",
189 less: "css",
190 md: "markdown",
191 mdx: "markdown",
192 "mdx.tsx": "markdown",
193 mjs: "javascript",
194 mts: "typescript",
195 png: "image",
196 postcss: "css",
197 py: "python",
198 pyi: "python",
199 pyw: "python",
200 pyx: "python",
201 rake: "ruby",
202 rar: "zip",
203 rb: "ruby",
204 rs: "rust",
205 sass: "sass",
206 scss: "sass",
207 sh: "bash",
208 sql: "database",
209 sqlite: "database",
210 sqlite3: "database",
211 svelte: "svelte",
212 svg: "svg",
213 swift: "swift",
214 tar: "zip",
215 tf: "terraform",
216 tfstate: "terraform",
217 tfvars: "terraform",
218 tgz: "zip",
219 ts: "typescript",
220 tsv: "table",
221 tsx: "react",
222 txt: "text",
223 woff: "font",
224 woff2: "font",
225 vue: "vue",
226 wasm: "wasm",
227 webp: "image",
228 yml: "yml",
229 yaml: "yml",
230 zig: "zig",
231 zip: "zip",
232 zsh: "bash",
233};
234​
235/** Native link and media APIs have no document scheme to inherit from protocol-relative URLs. */
236export function normalizeNativeMarkdownUrl(value: string): string {
237 return value.startsWith("//") ? `https:${value}` : value;
239​
240export function resolveMarkdownFileIcon(value: string): MarkdownFileIcon {
241 const basename = fileBasename(value).replace(POSITION_SUFFIX_PATTERN, "").toLowerCase();
242 if (videoMimeType({ name: basename, mimeType: "" }) !== null) return "video";
243 const exactIcon = FILE_ICON_BY_NAME[basename];
244 if (exactIcon) return exactIcon;
245 if (basename.startsWith("tsconfig.") && basename.endsWith(".json")) {
246 return "typescript";
247 }
248 const segments = basename.split(".");
249 for (let index = 1; index < segments.length; index += 1) {
250 const icon = FILE_ICON_BY_EXTENSION[segments.slice(index).join(".")];
251 if (icon) return icon;
252 }
253 return "default";
255​
256export function resolveMarkdownLinkPresentation(href: string): MarkdownLinkPresentation {
257 const normalized = normalizeMarkdownLinkDestination(href);
258 try {
259 const parsed = new URL(normalizeNativeMarkdownUrl(normalized));
260 if (parsed.protocol === "http:" || parsed.protocol === "https:") {
261 return {
262 kind: "external",
263 href: parsed.toString(),
264 host: parsed.hostname,
265 };
266 }
267 } catch {}
268​
269 const target = parseMarkdownFileLink(normalized);
270 if (target) {
271 return {
272 kind: "file",
273 href: normalized,
274 icon: resolveMarkdownFileIcon(target.path),
275 label: fileLinkLabel(target, new Map()),
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:
285 /^(?:mailto|tel):/i.test(normalized) || parseThreadLinkHref(normalized) !== null
286 ? normalized
287 : null,
288 };
290​
291export function resolveMarkdownInlineCodePresentation(
292 content: string,
293): Extract<MarkdownLinkPresentation, { readonly kind: "file" }> | null {
294 const candidate = inlineCodeFilePathCandidate(content);
295 if (candidate === null) return null;
296 const presentation = resolveMarkdownLinkPresentation(candidate);
297 return presentation.kind === "file" ? presentation : null;