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

markdown-links.ts

87 lines2.8 KB

1import { fileBasename, workspaceRelativeFilePath } from "@t3tools/shared/path";
2import {
3 inlineCodeFilePathCandidate,
4 isRelativeFilePath,
5 normalizeMarkdownLinkDestination,
6 resolveMarkdownFileLinkTarget,
7} from "@t3tools/shared/markdownLinks";
8import { parseFileUrlHref, splitFilePathPosition } from "@t3tools/shared/fileLinks";
9​
10import { formatWorkspaceRelativePath } from "./filePathDisplay";
11import { isTerminalLinkActivation } from "./terminal-links";
12​
13export interface MarkdownFileLinkMeta {
14 filePath: string;
15 targetPath: string;
16 displayPath: string;
17 workspaceRelativePath: string | null;
18 basename: string;
19 line?: number;
20 column?: number;
22​
23export function shouldOpenMarkdownFileLinkInEditor(
24 event: Pick<MouseEvent, "metaKey" | "ctrlKey">,
25 platform?: string,
26): boolean {
27 return isTerminalLinkActivation(event, platform);
29​
30export function shouldOpenMarkdownFileLinkInBrowserByDefault(path: string): boolean {
31 return /\.pdf$/i.test(path.split(/[?#]/, 1)[0] ?? "");
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;
39​
40export function resolveInlineCodeFileLinkMeta(
41 codeText: string,
42 cwd?: string,
43 baseDir: string | undefined = cwd,
44): MarkdownFileLinkMeta | null {
45 const candidate = inlineCodeFilePathCandidate(codeText);
46 if (candidate === null) return null;
47​
48 return resolveMarkdownFileLinkMeta(
49 candidate,
50 cwd,
51 inlineCodePathNamesFromWorkspaceRoot(candidate, cwd, baseDir) ? cwd : baseDir,
52 );
54​
55function inlineCodePathNamesFromWorkspaceRoot(
56 candidate: string,
57 cwd: string | undefined,
58 baseDir: string | undefined,
59): boolean {
60 if (!cwd || !baseDir || !isRelativeFilePath(candidate)) return false;
61 if (/^(?:~|\.{1,2})\//.test(candidate)) return false;
62 if (!splitFilePathPosition(candidate).path.includes("/")) return false;
63 return workspaceRelativeFilePath(baseDir, cwd) !== null;
65​
66export function resolveMarkdownFileLinkMeta(
67 href: string | undefined,
68 cwd?: string,
69 baseDir: string | undefined = cwd,
70): MarkdownFileLinkMeta | null {
71 const targetPath = resolveMarkdownFileLinkTarget(href, cwd, baseDir);
72 if (!targetPath) return null;
73 return buildFileLinkMetaFromTarget(targetPath, cwd);
75​
76function buildFileLinkMetaFromTarget(targetPath: string, cwd?: string): MarkdownFileLinkMeta {
77 const { path, line, column } = splitFilePathPosition(targetPath);
78 return {
79 filePath: path,
80 targetPath,
81 displayPath: formatWorkspaceRelativePath(targetPath, cwd),
82 workspaceRelativePath: workspaceRelativeFilePath(path, cwd),
83 basename: fileBasename(path),
84 ...(line !== undefined ? { line } : {}),
85 ...(column !== undefined ? { column } : {}),
86 };