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