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

markdown-links.ts

145 lines4.4 KB

1import { isLocalEditorFileUrl } from "@t3tools/contracts";
2import { fileBasename, isAbsolutePath, workspaceRelativeFilePath } from "@t3tools/shared/path";
3import {
4 inlineCodeFilePathCandidate,
5 isRelativeFilePath,
6 normalizeMarkdownLinkDestination,
7 parseMarkdownFileLink,
8 resolveMarkdownFileLink,
9} from "@t3tools/shared/markdownLinks";
10import { parseFileUrlHref, splitFilePathPosition } from "@t3tools/shared/fileLinks";
11​
12import { formatWorkspaceRelativePath } from "./filePathDisplay";
13import { isTerminalLinkActivation } from "./terminal-links";
14​
15export interface MarkdownFileLinkMeta {
16 filePath: string;
17 workspaceLookupPath: string | null;
18 targetPath: string;
19 displayPath: string;
20 workspaceRelativePath: string | null;
21 basename: string;
22 line?: number;
23 column?: number;
24 endLine?: number;
26​
27export function shouldOpenMarkdownFileLinkInEditor(
28 event: Pick<MouseEvent, "metaKey" | "ctrlKey">,
29 platform?: string,
30): boolean {
31 return isTerminalLinkActivation(event, platform);
33​
34export function rewriteMarkdownFileUriHref(href: string | undefined): string | null {
35 if (!href) return null;
36 const normalized = normalizeMarkdownLinkDestination(href);
37 const target = parseFileUrlHref(normalized);
38 if (!target) return null;
39 const rewritten = `${target.path}${target.hash}`;
40 // Only the scheme marks some host paths, like `/nix/store/x/bin/tool`, as files.
41 return parseMarkdownFileLink(rewritten) || !parseMarkdownFileLink(normalized)
42 ? rewritten
43 : normalized;
45​
46export function isLocalEditorFileHref(href: string): boolean {
47 try {
48 return isLocalEditorFileUrl(new URL(href));
49 } catch {
50 return false;
51 }
53​
54// Every message resolves its links and code spans more than once while rendering.
55const MAX_MEMOIZED = 4_000;
56const inlineCodeMetas = new Map<string, MarkdownFileLinkMeta | null>();
57const linkMetas = new Map<string, MarkdownFileLinkMeta | null>();
58​
59function memoized(
60 cache: Map<string, MarkdownFileLinkMeta | null>,
61 key: string,
62 resolve: () => MarkdownFileLinkMeta | null,
63): MarkdownFileLinkMeta | null {
64 const cached = cache.get(key);
65 if (cached !== undefined) return cached;
66 if (cache.size >= MAX_MEMOIZED) cache.clear();
67 const meta = resolve();
68 cache.set(key, meta);
69 return meta;
71​
72export function resolveInlineCodeFileLinkMeta(
73 codeText: string,
74 cwd?: string,
75 baseDir: string | undefined = cwd,
76): MarkdownFileLinkMeta | null {
77 return memoized(inlineCodeMetas, `${cwd ?? ""}\u0000${baseDir ?? ""}\u0000${codeText}`, () =>
78 resolveInlineCodeFileLinkMetaUncached(codeText, cwd, baseDir),
79 );
81​
82function resolveInlineCodeFileLinkMetaUncached(
83 codeText: string,
84 cwd: string | undefined,
85 baseDir: string | undefined,
86): MarkdownFileLinkMeta | null {
87 const candidate = inlineCodeFilePathCandidate(codeText);
88 if (candidate === null) return null;
89​
90 return resolveMarkdownFileLinkMeta(
91 candidate,
92 cwd,
93 inlineCodePathNamesFromWorkspaceRoot(candidate, cwd, baseDir) ? cwd : baseDir,
94 );
96​
97function inlineCodePathNamesFromWorkspaceRoot(
98 candidate: string,
99 cwd: string | undefined,
100 baseDir: string | undefined,
101): boolean {
102 if (!cwd || !baseDir || !isRelativeFilePath(candidate)) return false;
103 if (/^(?:~|\.{1,2})\//.test(candidate)) return false;
104 if (!splitFilePathPosition(candidate).path.includes("/")) return false;
105 return workspaceRelativeFilePath(baseDir, cwd) !== null;
107​
108export function resolveMarkdownFileLinkMeta(
109 href: string | undefined,
110 cwd?: string,
111 baseDir: string | undefined = cwd,
112): MarkdownFileLinkMeta | null {
113 if (href === undefined) return null;
114 return memoized(linkMetas, `${cwd ?? ""}\u0000${baseDir ?? ""}\u0000${href}`, () =>
115 resolveMarkdownFileLinkMetaUncached(href, cwd, baseDir),
116 );
118​
119function resolveMarkdownFileLinkMetaUncached(
120 href: string | undefined,
121 cwd: string | undefined,
122 baseDir: string | undefined,
123): MarkdownFileLinkMeta | null {
124 const resolved = resolveMarkdownFileLink(href, cwd, baseDir);
125 if (!resolved) return null;
126 const {
127 sourcePath,
128 targetPath,
129 position: { path, ...lines },
130 } = resolved;
131 return {
132 filePath: path,
133 targetPath,
134 displayPath: formatWorkspaceRelativePath(targetPath, cwd),
135 workspaceRelativePath: workspaceRelativeFilePath(path, cwd),
136 basename: fileBasename(path),
137 ...lines,
138 workspaceLookupPath:
139 !isAbsolutePath(sourcePath) &&
140 baseDir !== undefined &&
141 workspaceRelativeFilePath(baseDir, cwd) === "."
142 ? sourcePath
143 : null,
144 };