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

markdownLinks.ts

452 lines14.3 KB

1import {
2 isNetworkPath,
3 isWindowsAbsolutePath,
4 stripSlashPrefixedWindowsDrive,
5 workspaceRelativeFilePath,
6} from "./path.ts";
7import {
8 type FilePathPosition,
9 formatFilePathPosition,
10 parseFileUrlHref,
11 resolvePathLinkTarget,
12 splitFilePathPosition,
13} from "./fileLinks.ts";
14​
15const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;
16​
17// Names may use any script (`線性代數/考試.pdf`) and routing punctuation (`posts.$id.tsx`,
18// `(group)/[id]/+page.svelte`, `@scope/pkg`). The final segment is optional so an explicit
19// folder link may end in `/` (`patches/`, `docs/My Folder/`). A bare name with no slash still fails.
20const RELATIVE_FILE_PATH_PATTERN =
21 /^(?:[\p{L}\p{M}\p{N}._$+@()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+@()[\]-]+)*\/)+(?:[\p{L}\p{M}\p{N}._$+@()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+@()[\]-]+)*(?::\d+(?::\d+|-\d+)?)?)?$/u;
22​
23// No `@` here: `me@example.com` is an address, not a file.
24const RELATIVE_FILE_NAME_PATTERN =
25 /^[\p{L}\p{M}\p{N}._$+()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+()[\]-]+)*\.[\p{L}\p{M}\p{N}_-]+(?::\d+(?::\d+|-\d+)?)?$/u;
26​
27const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/;
28​
29const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+|-\d+)?$/;
30​
31const POSITION_ONLY_PATTERN = /^\d+(?::\d+|-\d+)?$/;
32​
33const INLINE_CODE_DISQUALIFIER_PATTERN = /[\s`]/;
34​
35const PATH_SEPARATOR_PATTERN = /[\\/]/;
36// Agents shorten long paths with a `...` or `…` segment, which no file chip can open.
37const ELIDED_PATH_SEGMENT_PATTERN = /(?:^|[\\/])(?:\.{3,}|…)(?:[\\/]|$)/;
38​
39const FILE_EXTENSION_PATTERN = /\.[A-Za-z0-9_-]+$/;
40const ANY_FILE_EXTENSION_PATTERN = /\.[^./\\]+$/;
41const VERSION_SUFFIX_PATTERN = /\d\.\d[^.]*$/;
42const NUMERIC_DOTTED_PATTERN = /^\d+(?:\.\d+)+$/;
43​
44const POSIX_FILE_ROOT_PREFIXES = [
45 "/Users/",
46 "/home/",
47 "/tmp/",
48 "/var/",
49 "/etc/",
50 "/opt/",
51 "/mnt/",
52 "/Volumes/",
53 "/private/",
54 "/root/",
55 "/usr/",
56 "/bin/",
57 "/sbin/",
58 "/lib/",
59 "/lib64/",
60 "/srv/",
61 "/dev/",
62 "/proc/",
63 "/sys/",
64 "/run/",
65 "/boot/",
66 "/media/",
67 "/workspace/",
68 "/workspaces/",
69] as const;
70​
71const EXTENSIONLESS_FILE_NAMES = new Set([
72 "Makefile",
73 "makefile",
74 "GNUmakefile",
75 "Dockerfile",
76 "Containerfile",
77 "Justfile",
78 "justfile",
79 "Rakefile",
80 "Gemfile",
81 "Procfile",
82 "Brewfile",
83 "Caddyfile",
84 "Vagrantfile",
85 "Jenkinsfile",
86 "Podfile",
87 "Fastfile",
88 "BUILD",
89 "WORKSPACE",
90 "LICENSE",
91 "LICENCE",
92 "COPYING",
93 "NOTICE",
94 "AUTHORS",
95 "CONTRIBUTORS",
96 "CHANGELOG",
97 "README",
98 "CODEOWNERS",
99]);
100​
101const SINGLE_LABEL_HOSTNAMES = new Set(["localhost"]);
102​
103const GENERIC_HOSTNAME_TLDS = new Set([
104 "com",
105 "net",
106 "org",
107 "io",
108 "dev",
109 "app",
110 "ai",
111 "co",
112 "edu",
113 "gov",
114 "mil",
115 "info",
116 "biz",
117 "xyz",
118 "me",
119 "tv",
120 "gg",
121 "chat",
122 "cloud",
123 "site",
124 "online",
125 "tech",
126 "store",
127 "link",
128]);
129​
130const COUNTRY_HOSTNAME_TLDS = new Set([
131 "uk",
132 "de",
133 "fr",
134 "nl",
135 "se",
136 "no",
137 "fi",
138 "dk",
139 "pl",
140 "ch",
141 "at",
142 "be",
143 "es",
144 "it",
145 "pt",
146 "eu",
147 "us",
148 "ca",
149 "au",
150 "nz",
151 "jp",
152 "kr",
153 "cn",
154 "br",
155 "ru",
156 "mx",
157 "ie",
158 "cz",
159 "tr",
160 "sg",
161 "hk",
162 // C++ sources end in `.cc`, so `allocator.cc:217` names a file, not a host and port.
163 "cc",
164]);
165​
166function looksLikeHostname(segment: string, hasPosition: boolean): boolean {
167 if (segment.startsWith(".")) return false;
168 const lowered = segment.toLowerCase();
169 if (SINGLE_LABEL_HOSTNAMES.has(lowered)) return true;
170 if (NUMERIC_DOTTED_PATTERN.test(segment)) return true;
171 const labels = lowered.split(".");
172 const lastLabel = labels.at(-1);
173 if (labels.length < 2 || lastLabel === undefined) return false;
174 if (GENERIC_HOSTNAME_TLDS.has(lastLabel)) return true;
175 return !hasPosition && COUNTRY_HOSTNAME_TLDS.has(lastLabel);
177​
178export function inlineCodeFilePathCandidate(codeText: string): string | null {
179 const trimmed = codeText.trim();
180 if (trimmed.length === 0 || INLINE_CODE_DISQUALIFIER_PATTERN.test(trimmed)) return null;
181​
182 const candidate = isWindowsAbsolutePath(trimmed) ? trimmed : trimmed.replaceAll("\\", "/");
183 const hasPosition = POSITION_SUFFIX_PATTERN.test(candidate);
184 if (!hasPosition && !PATH_SEPARATOR_PATTERN.test(candidate)) return null;
185 if (ELIDED_PATH_SEGMENT_PATTERN.test(candidate.replace(POSITION_SUFFIX_PATTERN, ""))) return null;
186​
187 const hasExplicitPathShape =
188 RELATIVE_PATH_PREFIX_PATTERN.test(candidate) ||
189 candidate.startsWith("/") ||
190 isWindowsAbsolutePath(candidate);
191 if (!hasExplicitPathShape) {
192 const withoutPosition = candidate.replace(POSITION_SUFFIX_PATTERN, "");
193 // Markdown destinations accept `patches/`. Inline code still needs a
194 // file-shaped final segment, so a trailing slash stays plain code.
195 if (withoutPosition.endsWith("/")) return null;
196 const firstSegment = withoutPosition.split("/")[0] ?? withoutPosition;
197 if (looksLikeHostname(firstSegment, hasPosition)) return null;
198 const basename =
199 withoutPosition
200 .replace(/[/\\]+$/, "")
201 .split(/[\\/]/)
202 .at(-1) ?? "";
203 if (VERSION_SUFFIX_PATTERN.test(basename)) return null;
204 if (!FILE_EXTENSION_PATTERN.test(basename)) {
205 if (!hasPosition) return null;
206 // A `:line` suffix alone does not make `I/W217:220` a file reference.
207 if (
208 withoutPosition.includes("/") &&
209 !ANY_FILE_EXTENSION_PATTERN.test(basename) &&
210 !EXTENSIONLESS_FILE_NAMES.has(basename)
211 ) {
212 return null;
213 }
214 }
215 }
216 return candidate;
218​
219export function safeDecodeURIComponent(value: string): string {
220 try {
221 return decodeURIComponent(value);
222 } catch {
223 return value;
224 }
226​
227export function normalizeMarkdownLinkDestination(value: string): string {
228 const trimmed = value.trim();
229 return trimmed.startsWith("<") && trimmed.endsWith(">") ? trimmed.slice(1, -1) : trimmed;
231​
232export function splitMarkdownLinkSearchAndHash(value: string): {
233 readonly path: string;
234 readonly hash: string;
235} {
236 const hashIndex = value.indexOf("#");
237 const pathWithSearch = hashIndex >= 0 ? value.slice(0, hashIndex) : value;
238 const hash = hashIndex >= 0 ? value.slice(hashIndex) : "";
239 const queryIndex = pathWithSearch.indexOf("?");
240 return {
241 path: queryIndex >= 0 ? pathWithSearch.slice(0, queryIndex) : pathWithSearch,
242 hash,
243 };
245​
246/**
247 * The decoded path a markdown destination names on the host, before any workspace resolution, or
248 * null for a `file:` URL that does not parse. Clients and the guest media route both read it here.
249 */
250export function markdownDestinationHostPath(source: string): string | null {
251 if (/^file:/i.test(source)) {
252 const target = parseFileUrlHref(source);
253 return target === null
254 ? null
255 : stripSlashPrefixedWindowsDrive(safeDecodeURIComponent(target.path));
256 }
257 return stripSlashPrefixedWindowsDrive(
258 safeDecodeURIComponent(splitMarkdownLinkSearchAndHash(source).path),
259 );
261​
262export function isMarkdownFileLinkLabel(label: string, href: string): boolean {
263 const destination = parseMarkdownFileLink(href);
264 if (!destination) return false;
265 const normalize = (path: string) =>
266 path.replaceAll("\\", "/").replace(/^\.\//, "").replace(/\/+$/, "");
267 const labelPosition = splitFilePathPosition(label.trim());
268 if (
269 (labelPosition.line !== undefined && labelPosition.line !== destination.line) ||
270 (labelPosition.column !== undefined && labelPosition.column !== destination.column) ||
271 (labelPosition.endLine !== undefined && labelPosition.endLine !== destination.endLine)
272 ) {
273 return false;
274 }
275 let labelPath = normalize(labelPosition.path);
276 let destinationPath = normalize(destination.path);
277 if (labelPath.length === 0) return true;
278 if (isWindowsAbsolutePath(destination.path)) {
279 labelPath = labelPath.toLowerCase();
280 destinationPath = destinationPath.toLowerCase();
281 }
282 return destinationPath === labelPath || destinationPath.endsWith(`/${labelPath}`);
284​
285export function isRelativeFilePath(path: string): boolean {
286 return (
287 RELATIVE_PATH_PREFIX_PATTERN.test(path) ||
288 (!path.startsWith("/") && !isWindowsAbsolutePath(path))
289 );
291​
292function looksLikePosixFilesystemPath(path: string): boolean {
293 if (!path.startsWith("/")) return false;
294 if (POSIX_FILE_ROOT_PREFIXES.some((prefix) => path.startsWith(prefix))) return true;
295 if (POSITION_SUFFIX_PATTERN.test(path)) return true;
296 const basename = path.slice(path.lastIndexOf("/") + 1);
297 return EXTENSIONLESS_FILE_NAMES.has(basename) || FILE_EXTENSION_PATTERN.test(basename);
299​
300function looksLikeFilePath(path: string, authoredPath: string): boolean {
301 if (isWindowsAbsolutePath(path) || RELATIVE_PATH_PREFIX_PATTERN.test(path)) return true;
302 if (path.startsWith("/")) return looksLikePosixFilesystemPath(authoredPath);
303 if (EXTENSIONLESS_FILE_NAMES.has(path)) return true;
304 return RELATIVE_FILE_PATH_PATTERN.test(authoredPath) || RELATIVE_FILE_NAME_PATTERN.test(path);
306​
307function hasExternalScheme(path: string): boolean {
308 if (isWindowsAbsolutePath(path)) return false;
309 const match = path.match(EXTERNAL_SCHEME_PATTERN);
310 if (!match) return false;
311 const rest = match[2] ?? "";
312 if (rest.startsWith("//")) return true;
313 return !POSITION_ONLY_PATTERN.test(rest);
315​
316export function parseMarkdownFileLink(href: string): FilePathPosition | null {
317 const normalized = normalizeMarkdownLinkDestination(href);
318 if (normalized.length === 0 || normalized.startsWith("#") || normalized.startsWith("//")) {
319 return null;
320 }
321​
322 const source =
323 (normalized.toLowerCase().startsWith("file:") ? parseFileUrlHref(normalized) : null) ??
324 splitMarkdownLinkSearchAndHash(normalized);
325 const path = stripSlashPrefixedWindowsDrive(safeDecodeURIComponent(source.path.trim()));
326 const hash = safeDecodeURIComponent(source.hash.trim());
327 if (path.length === 0 || hasExternalScheme(path)) return null;
328​
329 const position = parseFilePathLink(path, hash);
330 // `src\a.ts` is Windows-relative. A link that already parses keeps its spelling, since POSIX
331 // names may contain `\`; a rooted `\\host` or `\foo` would change root if rewritten.
332 if (position || /^[\\/]/.test(path) || isWindowsAbsolutePath(path)) return position;
333 return path.includes("\\") ? parseFilePathLink(path.replaceAll("\\", "/"), hash) : null;
335​
336function parseFilePathLink(path: string, hash: string): FilePathPosition | null {
337 const position = splitFilePathPosition(path, hash);
338 return looksLikeFilePath(position.path, path) ? position : null;
340​
341const FENCED_CODE_SEGMENT_PATTERN = /(```[\s\S]*?(?:```|$))/;
342​
343const INLINE_CODE_SPAN_PATTERN = /`([^`\n]+)`/g;
344​
345export function extractInlineCodeSpans(text: string): string[] {
346 const spans: string[] = [];
347 const segments = text.split(FENCED_CODE_SEGMENT_PATTERN);
348 for (let index = 0; index < segments.length; index += 2) {
349 for (const match of (segments[index] ?? "").matchAll(INLINE_CODE_SPAN_PATTERN)) {
350 const span = match[1]?.trim();
351 if (span) spans.push(span);
352 }
353 }
354 return spans;
356​
357function markdownRunEndFinder(markdown: string, delimiter: RegExp) {
358 let runStart = -1;
359 let runEnd = -1;
360 return (start: number) => {
361 if (start >= runStart && start <= runEnd) return runEnd;
362 delimiter.lastIndex = start;
363 runStart = start;
364 runEnd = delimiter.exec(markdown)?.index ?? markdown.length;
365 return runEnd;
366 };
368​
369function markdownLinkTailScanner(markdown: string) {
370 const whitespaceEnd = markdownRunEndFinder(markdown, /\S/g);
371 const titleEnd = markdownRunEndFinder(markdown, /["']/g);
372 const titleWhitespaceEnd = markdownRunEndFinder(markdown, /\S/g);
373 return (start: number) => {
374 const end = whitespaceEnd(start);
375 if (markdown[end] === ")") return end + 1;
376 if (end === start || (markdown[end] !== '"' && markdown[end] !== "'")) return null;
377 const quoteEnd = titleEnd(end + 1);
378 if (quoteEnd === markdown.length) return null;
379 const close = titleWhitespaceEnd(quoteEnd + 1);
380 return markdown[close] === ")" ? close + 1 : null;
381 };
383​
384function markdownLinkDestinationScanner(markdown: string) {
385 const whitespaceEnd = markdownRunEndFinder(markdown, /\S/g);
386 const angleEnd = markdownRunEndFinder(markdown, /[>\n]/g);
387 const bareEnd = markdownRunEndFinder(markdown, /[\s)]/g);
388 // Separate caches keep scans moving forward when angle destinations fall back to bare ones.
389 const angleTail = markdownLinkTailScanner(markdown);
390 const bareTail = markdownLinkTailScanner(markdown);
391 return (start: number) => {
392 if (markdown[start] !== "(") return null;
393 const hrefStart = whitespaceEnd(start + 1);
394 if (markdown[hrefStart] === "<") {
395 const hrefEnd = angleEnd(hrefStart + 1);
396 if (hrefEnd > hrefStart + 1 && markdown[hrefEnd] === ">") {
397 const end = angleTail(hrefEnd + 1);
398 if (end !== null) return { hrefStart: hrefStart + 1, hrefEnd, end };
399 }
400 }
401 const hrefEnd = bareEnd(hrefStart);
402 if (hrefEnd === hrefStart) return null;
403 const end = bareTail(hrefEnd);
404 return end === null ? null : { hrefStart, hrefEnd, end };
405 };
407​
408export function extractMarkdownLinkHrefs(markdown: string): string[] {
409 const hrefs: string[] = [];
410 const scanDestination = markdownLinkDestinationScanner(markdown);
411 let cursor = 0;
412 while (cursor < markdown.length) {
413 const labelStart = markdown.indexOf("[", cursor);
414 if (labelStart === -1) break;
415 const labelEnd = markdown.indexOf("]", labelStart + 1);
416 if (labelEnd === -1) break;
417​
418 // Skip a failed label once; nested openings share its closing bracket.
419 cursor = labelEnd + 1;
420 const destination = scanDestination(cursor);
421 if (!destination) continue;
422 cursor = destination.end;
423 const href = markdown.slice(destination.hrefStart, destination.hrefEnd).trim();
424 if (href) hrefs.push(href);
425 }
426 return hrefs;
428​
429export function resolveMarkdownFileLinkTarget(
430 href: string | undefined,
431 cwd?: string,
432 baseDir: string | undefined = cwd,
433): string | null {
434 if (!href) return null;
435 const target = parseMarkdownFileLink(href);
436 if (!target) return null;
437​
438 const pathWithPosition = formatFilePathPosition(target);
439 if (!isRelativeFilePath(pathWithPosition)) {
440 // Opening a network path makes Windows sign in to its host with the user's
441 // NTLM credentials, so only the project's own share becomes a file link.
442 return isNetworkPath(target.path) && workspaceRelativeFilePath(target.path, cwd) === null
443 ? null
444 : pathWithPosition;
445 }
446 if (!baseDir) return null;
447 return resolvePathLinkTarget(pathWithPosition, baseDir);
449​
450export function isWindowsDrivePathHref(href: string): boolean {
451 return /^[A-Za-z]:[\\/]/.test(safeDecodeURIComponent(href));