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

markdownLinks.ts

158 lines3.9 KB

1const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
2const WINDOWS_UNC_PATH_PATTERN = /^\\\\/;
3const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;
4const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+)?$/;
5const INLINE_CODE_DISQUALIFIER_PATTERN = /[\s`]/;
6const PATH_SEPARATOR_PATTERN = /[\\/]/;
7const FILE_EXTENSION_PATTERN = /\.[A-Za-z0-9_-]+$/;
8const NUMERIC_DOTTED_PATTERN = /^\d+(?:\.\d+)+$/;
9const BARE_EXTENSIONLESS_POSITION_PATTERN = /^[A-Za-z0-9_-]+(?::\d+){1,2}$/;
10// `Name:digits` also matches `error:1`, `port:3000`, and `TODO:12`.
11const EXTENSIONLESS_FILE_NAMES = new Set([
12 "Makefile",
13 "makefile",
14 "GNUmakefile",
15 "Dockerfile",
16 "Containerfile",
17 "Justfile",
18 "justfile",
19 "Rakefile",
20 "Gemfile",
21 "Procfile",
22 "Brewfile",
23 "Caddyfile",
24 "Vagrantfile",
25 "Jenkinsfile",
26 "Podfile",
27 "Fastfile",
28 "BUILD",
29 "WORKSPACE",
30 "LICENSE",
31 "LICENCE",
32 "COPYING",
33 "NOTICE",
34 "AUTHORS",
35 "CONTRIBUTORS",
36 "CHANGELOG",
37 "README",
38 "CODEOWNERS",
39]);
40const SINGLE_LABEL_HOSTNAMES = new Set(["localhost"]);
41// These allowlists avoid classifying dotted directories such as `conf.d/`
42// or filenames such as `Makefile.in:12` as hosts.
43const GENERIC_HOSTNAME_TLDS = new Set([
44 "com",
45 "net",
46 "org",
47 "io",
48 "dev",
49 "app",
50 "ai",
51 "co",
52 "edu",
53 "gov",
54 "mil",
55 "info",
56 "biz",
57 "xyz",
58 "me",
59 "tv",
60 "cc",
61 "gg",
62 "chat",
63 "cloud",
64 "site",
65 "online",
66 "tech",
67 "store",
68 "link",
69]);
70// Country codes also name file extensions. A :line suffix makes `.pl`
71// and `.pt` files more likely than hostnames.
72const COUNTRY_HOSTNAME_TLDS = new Set([
73 "uk",
74 "de",
75 "fr",
76 "nl",
77 "se",
78 "no",
79 "fi",
80 "dk",
81 "pl",
82 "ch",
83 "at",
84 "be",
85 "es",
86 "it",
87 "pt",
88 "eu",
89 "us",
90 "ca",
91 "au",
92 "nz",
93 "jp",
94 "kr",
95 "cn",
96 "br",
97 "ru",
98 "mx",
99 "ie",
100 "cz",
101 "tr",
102 "sg",
103 "hk",
104]);
105​
106function looksLikeHostname(segment: string, hasPosition: boolean): boolean {
107 if (segment.startsWith(".")) return false;
108 const lowered = segment.toLowerCase();
109 if (SINGLE_LABEL_HOSTNAMES.has(lowered)) return true;
110 if (NUMERIC_DOTTED_PATTERN.test(segment)) return true;
111 const labels = lowered.split(".");
112 const lastLabel = labels.at(-1);
113 if (labels.length < 2 || lastLabel === undefined) return false;
114 if (GENERIC_HOSTNAME_TLDS.has(lastLabel)) return true;
115 return !hasPosition && COUNTRY_HOSTNAME_TLDS.has(lastLabel);
117​
118/** Recognizes conventional extensionless filenames with an explicit line position. */
119export function isConventionalFilePosition(path: string): boolean {
120 return (
121 BARE_EXTENSIONLESS_POSITION_PATTERN.test(path) &&
122 EXTENSIONLESS_FILE_NAMES.has(path.replace(POSITION_SUFFIX_PATTERN, ""))
123 );
125​
126/**
127 * Picks path-shaped inline code for the client's markdown file-link resolver.
128 * It does not resolve paths or turn plain prose and fenced code into links.
129 */
130export function inlineCodeFilePathCandidate(codeText: string): string | null {
131 const trimmed = codeText.trim();
132 if (trimmed.length === 0 || INLINE_CODE_DISQUALIFIER_PATTERN.test(trimmed)) return null;
133​
134 const candidate =
135 WINDOWS_DRIVE_PATH_PATTERN.test(trimmed) || WINDOWS_UNC_PATH_PATTERN.test(trimmed)
136 ? trimmed
137 : trimmed.replaceAll("\\", "/");
138 const hasPosition = POSITION_SUFFIX_PATTERN.test(candidate);
139 if (!hasPosition && !PATH_SEPARATOR_PATTERN.test(candidate)) return null;
140​
141 const hasExplicitPathShape =
142 RELATIVE_PATH_PREFIX_PATTERN.test(candidate) ||
143 candidate.startsWith("/") ||
144 WINDOWS_DRIVE_PATH_PATTERN.test(candidate) ||
145 WINDOWS_UNC_PATH_PATTERN.test(candidate);
146 if (!hasExplicitPathShape) {
147 const withoutPosition = candidate.replace(POSITION_SUFFIX_PATTERN, "");
148 const firstSegment = withoutPosition.split("/")[0] ?? withoutPosition;
149 if (looksLikeHostname(firstSegment, hasPosition)) return null;
150 const basename =
151 withoutPosition
152 .replace(/[/\\]+$/, "")
153 .split(/[\\/]/)
154 .at(-1) ?? "";
155 if (!hasPosition && !FILE_EXTENSION_PATTERN.test(basename)) return null;
156 }
157 return candidate;