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 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;
24}
25
26export function shouldOpenMarkdownFileLinkInEditor(
27 event: Pick<MouseEvent, "metaKey" | "ctrlKey">,
28 platform?: string,
29): boolean {
30 return isTerminalLinkActivation(event, platform);
31}
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;
37}
38
39export function isLocalEditorFileHref(href: string): boolean {
40 try {
41 return isLocalEditorFileUrl(new URL(href));
42 } catch {
43 return false;
44 }
45}
46
47export function resolveInlineCodeFileLinkMeta(
48 codeText: string,
49 cwd?: string,
50 baseDir: string | undefined = cwd,
51): MarkdownFileLinkMeta | null {
52 const candidate = inlineCodeFilePathCandidate(codeText);
53 if (candidate === null) return null;
54
55 return resolveMarkdownFileLinkMeta(
56 candidate,
57 cwd,
58 inlineCodePathNamesFromWorkspaceRoot(candidate, cwd, baseDir) ? cwd : baseDir,
59 );
60}
61
62function inlineCodePathNamesFromWorkspaceRoot(
63 candidate: string,
64 cwd: string | undefined,
65 baseDir: string | undefined,
66): boolean {
67 if (!cwd || !baseDir || !isRelativeFilePath(candidate)) return false;
68 if (/^(?:~|\.{1,2})\//.test(candidate)) return false;
69 if (!splitFilePathPosition(candidate).path.includes("/")) return false;
70 return workspaceRelativeFilePath(baseDir, cwd) !== null;
71}
72
73export function resolveMarkdownFileLinkMeta(
74 href: string | undefined,
75 cwd?: string,
76 baseDir: string | undefined = cwd,
77): MarkdownFileLinkMeta | null {
78 const resolved = resolveMarkdownFileLink(href, cwd, baseDir);
79 if (!resolved) return null;
80 const {
81 sourcePath,
82 targetPath,
83 position: { path, ...lines },
84 } = resolved;
85 return {
86 filePath: path,
87 targetPath,
88 displayPath: formatWorkspaceRelativePath(targetPath, cwd),
89 workspaceRelativePath: workspaceRelativeFilePath(path, cwd),
90 basename: fileBasename(path),
91 ...lines,
92 workspaceLookupPath:
93 !isAbsolutePath(sourcePath) &&
94 baseDir !== undefined &&
95 workspaceRelativeFilePath(baseDir, cwd) === "."
96 ? sourcePath
97 : null,
98 };
99}