Open History to see the last commit
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
71export const EXTENSIONLESS_FILE_NAMES: ReadonlySet<string> = 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);
176}
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 EXTENSIONLESS_FILE_NAMES.has(basename) ? candidate : 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;
217}
218
219export function safeDecodeURIComponent(value: string): string {
220 try {
221 return decodeURIComponent(value);
222 } catch {
223 return value;
224 }
225}
226
227export function normalizeMarkdownLinkDestination(value: string): string {
228 const trimmed = value.trim();
229 return trimmed.startsWith("<") && trimmed.endsWith(">") ? trimmed.slice(1, -1) : trimmed;
230}
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 };
244}
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 );
260}
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}`);
283}
284
285export function isRelativeFilePath(path: string): boolean {
286 return (
287 RELATIVE_PATH_PREFIX_PATTERN.test(path) ||
288 (!path.startsWith("/") && !isWindowsAbsolutePath(path))
289 );
290}
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);
298}
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);
305}
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);
314}
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;
334}
335
336function parseFilePathLink(path: string, hash: string): FilePathPosition | null {
337 const position = splitFilePathPosition(path, hash);
338 return !ELIDED_PATH_SEGMENT_PATTERN.test(position.path) && looksLikeFilePath(position.path, path)
339 ? position
340 : null;
341}
342
343const FENCED_CODE_SEGMENT_PATTERN = /(```[\s\S]*?(?:```|$))/;
344
345const INLINE_CODE_SPAN_PATTERN = /`([^`\n]+)`/g;
346
347export function extractInlineCodeSpans(text: string): string[] {
348 const spans: string[] = [];
349 const segments = text.split(FENCED_CODE_SEGMENT_PATTERN);
350 for (let index = 0; index < segments.length; index += 2) {
351 for (const match of (segments[index] ?? "").matchAll(INLINE_CODE_SPAN_PATTERN)) {
352 const span = match[1]?.trim();
353 if (span) spans.push(span);
354 }
355 }
356 return spans;
357}
358
359function markdownRunEndFinder(markdown: string, delimiter: RegExp) {
360 let runStart = -1;
361 let runEnd = -1;
362 return (start: number) => {
363 if (start >= runStart && start <= runEnd) return runEnd;
364 delimiter.lastIndex = start;
365 runStart = start;
366 runEnd = delimiter.exec(markdown)?.index ?? markdown.length;
367 return runEnd;
368 };
369}
370
371function markdownLinkTailScanner(markdown: string) {
372 const whitespaceEnd = markdownRunEndFinder(markdown, /\S/g);
373 const titleEnd = markdownRunEndFinder(markdown, /["']/g);
374 const titleWhitespaceEnd = markdownRunEndFinder(markdown, /\S/g);
375 return (start: number) => {
376 const end = whitespaceEnd(start);
377 if (markdown[end] === ")") return end + 1;
378 if (end === start || (markdown[end] !== '"' && markdown[end] !== "'")) return null;
379 const quoteEnd = titleEnd(end + 1);
380 if (quoteEnd === markdown.length) return null;
381 const close = titleWhitespaceEnd(quoteEnd + 1);
382 return markdown[close] === ")" ? close + 1 : null;
383 };
384}
385
386// micromark's own nesting limit. It also keeps link scanning linear: a scan only runs past another
387// link's opening parenthesis while that parenthesis counts toward this depth.
388const MAX_BARE_DESTINATION_PAREN_DEPTH = 32;
389
390function bareMarkdownLinkDestinationEnd(markdown: string, start: number): number | null {
391 const delimiter = /[\s()\\]/g;
392 delimiter.lastIndex = start;
393 let depth = 0;
394 while (true) {
395 const index = delimiter.exec(markdown)?.index ?? markdown.length;
396 const char = markdown[index];
397 if (char === "\\") {
398 if (/[()\\]/.test(markdown[index + 1] ?? "")) delimiter.lastIndex = index + 2;
399 } else if (char === "(") {
400 if (depth === MAX_BARE_DESTINATION_PAREN_DEPTH) return null;
401 depth += 1;
402 } else if (char === ")" && depth > 0) {
403 depth -= 1;
404 } else {
405 return depth === 0 ? index : null;
406 }
407 }
408}
409
410function markdownLinkDestinationScanner(markdown: string) {
411 const whitespaceEnd = markdownRunEndFinder(markdown, /\S/g);
412 const angleEnd = markdownRunEndFinder(markdown, /[>\n]/g);
413 // Separate caches keep scans moving forward when angle destinations fall back to bare ones.
414 const angleTail = markdownLinkTailScanner(markdown);
415 const bareTail = markdownLinkTailScanner(markdown);
416 return (start: number) => {
417 if (markdown[start] !== "(") return null;
418 const hrefStart = whitespaceEnd(start + 1);
419 if (markdown[hrefStart] === "<") {
420 const hrefEnd = angleEnd(hrefStart + 1);
421 if (hrefEnd > hrefStart + 1 && markdown[hrefEnd] === ">") {
422 const end = angleTail(hrefEnd + 1);
423 if (end !== null) return { hrefStart: hrefStart + 1, hrefEnd, end };
424 }
425 }
426 const hrefEnd = bareMarkdownLinkDestinationEnd(markdown, hrefStart);
427 if (hrefEnd === null || hrefEnd === hrefStart) return null;
428 const end = bareTail(hrefEnd);
429 return end === null ? null : { hrefStart, hrefEnd, end };
430 };
431}
432
433// The composer escapes brackets in labels (`[\[id\].tsx](...)`), and `\]` does not close one.
434function unescapedLabelEnd(markdown: string, start: number): number {
435 let end = markdown.indexOf("]", start);
436 while (end !== -1) {
437 let backslashes = 0;
438 while (end - backslashes > start && markdown[end - backslashes - 1] === "\\") backslashes += 1;
439 if (backslashes % 2 === 0) return end;
440 end = markdown.indexOf("]", end + 1);
441 }
442 return -1;
443}
444
445export function extractMarkdownLinkHrefs(markdown: string): string[] {
446 const hrefs: string[] = [];
447 const scanDestination = markdownLinkDestinationScanner(markdown);
448 let cursor = 0;
449 while (cursor < markdown.length) {
450 const labelStart = markdown.indexOf("[", cursor);
451 if (labelStart === -1) break;
452 const labelEnd = unescapedLabelEnd(markdown, labelStart + 1);
453 if (labelEnd === -1) break;
454
455 // Skip a failed label once; nested openings share its closing bracket.
456 cursor = labelEnd + 1;
457 const destination = scanDestination(cursor);
458 if (!destination) continue;
459 cursor = destination.end;
460 const href = markdown.slice(destination.hrefStart, destination.hrefEnd).trim();
461 if (href) hrefs.push(href);
462 }
463 return hrefs;
464}
465
466export function resolveMarkdownFileLinkTarget(
467 href: string | undefined,
468 cwd?: string,
469 baseDir: string | undefined = cwd,
470): string | null {
471 if (!href) return null;
472 const target = parseMarkdownFileLink(href);
473 if (!target) return null;
474
475 const pathWithPosition = formatFilePathPosition(target);
476 if (!isRelativeFilePath(pathWithPosition)) {
477 // Opening a network path makes Windows sign in to its host with the user's
478 // NTLM credentials, so only the project's own share becomes a file link.
479 return isNetworkPath(target.path) && workspaceRelativeFilePath(target.path, cwd) === null
480 ? null
481 : pathWithPosition;
482 }
483 if (!baseDir) return null;
484 return resolvePathLinkTarget(pathWithPosition, baseDir);
485}
486
487export function isWindowsDrivePathHref(href: string): boolean {
488 return /^[A-Za-z]:[\\/]/.test(safeDecodeURIComponent(href));
489}
490
491// Bare prose paths need a filesystem root or a position, so `/api/users.json` stays a route,
492// and a file-shaped last segment.
493export function absoluteFilePathMatches(text: string): Array<{ index: number; path: string }> {
494 const paths: Array<{ index: number; path: string }> = [];
495 for (const match of text.matchAll(/(^|[\s(["'])(\/[^\s<>"'`]+)/g)) {
496 const source = match[2]!;
497 let parentheses = 0;
498 let brackets = 0;
499 for (const character of source) {
500 if (character === "(") parentheses += 1;
501 if (character === ")") parentheses -= 1;
502 if (character === "[") brackets += 1;
503 if (character === "]") brackets -= 1;
504 }
505 let end = source.length;
506 while (end > 0) {
507 const character = source[end - 1]!;
508 if (character === ")" && parentheses < 0) parentheses += 1;
509 else if (character === "]" && brackets < 0) brackets += 1;
510 else if (!".,;!?:".includes(character)) break;
511 end -= 1;
512 }
513 const path = source.slice(0, end);
514 // Trimming `/tmp/a/....` to `/tmp/a/` would hide its elided segment.
515 if (path.endsWith("/") && source.startsWith("...", end)) continue;
516 if (
517 !POSIX_FILE_ROOT_PREFIXES.some((prefix) => path.startsWith(prefix)) &&
518 !POSITION_SUFFIX_PATTERN.test(path)
519 )
520 continue;
521 // Prose ends a path at whitespace, so `/Users/me/Library/Application Support/...` must not link.
522 const basename = path.slice(path.lastIndexOf("/") + 1);
523 if (
524 !POSITION_SUFFIX_PATTERN.test(path) &&
525 !FILE_EXTENSION_PATTERN.test(basename) &&
526 !EXTENSIONLESS_FILE_NAMES.has(basename)
527 )
528 continue;
529 if (!parseMarkdownFileLink(path)) continue;
530 paths.push({ index: match.index + match[1]!.length, path });
531 }
532 return paths;
533}