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

markdown-links.ts

138 lines4.2 KB

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