Open History to see the last commit
1import { isLocalEditorFileUrl } from "@t3tools/contracts";
2import { fileBasename, isAbsolutePath, workspaceRelativeFilePath } from "@t3tools/shared/path";
3import {
4 inlineCodeFilePathCandidate,
5 isRelativeFilePath,
6 normalizeMarkdownLinkDestination,
7 parseMarkdownFileLink,
8 resolveMarkdownFileLinkTarget,
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;
25}
26
27export function shouldOpenMarkdownFileLinkInEditor(
28 event: Pick<MouseEvent, "metaKey" | "ctrlKey">,
29 platform?: string,
30): boolean {
31 return isTerminalLinkActivation(event, platform);
32}
33
34export function rewriteMarkdownFileUriHref(href: string | undefined): string | null {
35 if (!href) return null;
36 const target = parseFileUrlHref(normalizeMarkdownLinkDestination(href));
37 return target ? `${target.path}${target.hash}` : null;
38}
39
40export function isLocalEditorFileHref(href: string): boolean {
41 try {
42 return isLocalEditorFileUrl(new URL(href));
43 } catch {
44 return false;
45 }
46}
47
48export function resolveInlineCodeFileLinkMeta(
49 codeText: string,
50 cwd?: string,
51 baseDir: string | undefined = cwd,
52): MarkdownFileLinkMeta | null {
53 const candidate = inlineCodeFilePathCandidate(codeText);
54 if (candidate === null) return null;
55
56 return resolveMarkdownFileLinkMeta(
57 candidate,
58 cwd,
59 inlineCodePathNamesFromWorkspaceRoot(candidate, cwd, baseDir) ? cwd : baseDir,
60 );
61}
62
63function inlineCodePathNamesFromWorkspaceRoot(
64 candidate: string,
65 cwd: string | undefined,
66 baseDir: string | undefined,
67): boolean {
68 if (!cwd || !baseDir || !isRelativeFilePath(candidate)) return false;
69 if (/^(?:~|\.{1,2})\//.test(candidate)) return false;
70 if (!splitFilePathPosition(candidate).path.includes("/")) return false;
71 return workspaceRelativeFilePath(baseDir, cwd) !== null;
72}
73
74export function resolveMarkdownFileLinkMeta(
75 href: string | undefined,
76 cwd?: string,
77 baseDir: string | undefined = cwd,
78): MarkdownFileLinkMeta | null {
79 const targetPath = resolveMarkdownFileLinkTarget(href, cwd, baseDir);
80 if (!targetPath) return null;
81 const source = href ? parseMarkdownFileLink(href) : null;
82 return {
83 ...buildFileLinkMetaFromTarget(targetPath, cwd),
84 ...(source?.endLine !== undefined ? { endLine: source.endLine } : {}),
85 workspaceLookupPath:
86 source !== null &&
87 !isAbsolutePath(source.path) &&
88 baseDir !== undefined &&
89 workspaceRelativeFilePath(baseDir, cwd) === "."
90 ? source.path
91 : null,
92 };
93}
94
95function buildFileLinkMetaFromTarget(targetPath: string, cwd?: string) {
96 const { path, line, column } = splitFilePathPosition(targetPath);
97 return {
98 filePath: path,
99 targetPath,
100 displayPath: formatWorkspaceRelativePath(targetPath, cwd),
101 workspaceRelativePath: workspaceRelativeFilePath(path, cwd),
102 basename: fileBasename(path),
103 ...(line !== undefined ? { line } : {}),
104 ...(column !== undefined ? { column } : {}),
105 };
106}