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

markdown-links.ts

99 lines3.1 KB

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