Open History to see the last commit
1import {
2 fileBasename,
3 formatFilePathPosition,
4 inlineCodeFilePathCandidate,
5 isRelativeFilePath,
6 normalizeMarkdownLinkDestination,
7 parseFileUrlHref,
8 parseMarkdownFileLink,
9 safeDecodeURIComponent,
10 splitFilePathPosition,
11 workspaceRelativeFilePath,
12} from "@t3tools/client-runtime/markdown-links";
13
14import { formatWorkspaceRelativePath } from "./filePathDisplay";
15import { isTerminalLinkActivation, resolvePathLinkTarget } from "./terminal-links";
16
17export { normalizeMarkdownLinkDestination };
18
19const MARKDOWN_LINK_HREF_PATTERN =
20 /\[[^\]]*]\(\s*(?:<([^>\n]+)>|([^\s)]+))(?:\s+["'][^"']*["'])?\s*\)/g;
21
22export interface MarkdownFileLinkMeta {
23 filePath: string;
24 targetPath: string;
25 displayPath: string;
26 workspaceRelativePath: string | null;
27 basename: string;
28 line?: number;
29 column?: number;
30}
31
32export function extractMarkdownLinkHrefs(markdown: string): string[] {
33 const hrefs: string[] = [];
34 for (const match of markdown.matchAll(MARKDOWN_LINK_HREF_PATTERN)) {
35 const href = (match[1] ?? match[2])?.trim();
36 if (href) hrefs.push(href);
37 }
38 return hrefs;
39}
40
41export function shouldOpenMarkdownFileLinkInEditor(
42 event: Pick<MouseEvent, "metaKey" | "ctrlKey">,
43 platform?: string,
44): boolean {
45 return isTerminalLinkActivation(event, platform);
46}
47
48export function shouldOpenMarkdownFileLinkInBrowserByDefault(path: string): boolean {
49 return /\.pdf$/i.test(path.split(/[?#]/, 1)[0] ?? "");
50}
51
52export function isWindowsDrivePathHref(href: string): boolean {
53 return /^[A-Za-z]:[\\/]/.test(safeDecodeURIComponent(href));
54}
55
56export function rewriteMarkdownFileUriHref(href: string | undefined): string | null {
57 if (!href) return null;
58 const target = parseFileUrlHref(normalizeMarkdownLinkDestination(href));
59 return target ? `${target.path}${target.hash}` : null;
60}
61
62/**
63 * `baseDir` anchors relative links; it defaults to the workspace root and is the
64 * file's own directory when rendering a markdown file. `cwd` stays the workspace
65 * root so the result still knows whether the target is inside it.
66 */
67export function resolveMarkdownFileLinkTarget(
68 href: string | undefined,
69 cwd?: string,
70 baseDir: string | undefined = cwd,
71): string | null {
72 if (!href) return null;
73 const target = parseMarkdownFileLink(href);
74 if (!target) return null;
75
76 const pathWithPosition = formatFilePathPosition(target);
77 if (!isRelativeFilePath(pathWithPosition)) return pathWithPosition;
78 if (!baseDir) return null;
79 return resolvePathLinkTarget(pathWithPosition, baseDir);
80}
81
82/**
83 * Inline code spans mostly hold identifiers, commands, and refs (`node.meta`,
84 * `origin/main`) rather than deliberate link destinations, so auto-linking
85 * them demands stronger path evidence than an explicit markdown link does:
86 * an unambiguous path prefix, a file extension, or a :line suffix.
87 */
88export function resolveInlineCodeFileLinkMeta(
89 codeText: string,
90 cwd?: string,
91 baseDir: string | undefined = cwd,
92): MarkdownFileLinkMeta | null {
93 const candidate = inlineCodeFilePathCandidate(codeText);
94 if (candidate === null) return null;
95
96 return resolveMarkdownFileLinkMeta(candidate, cwd, baseDir);
97}
98
99export function resolveMarkdownFileLinkMeta(
100 href: string | undefined,
101 cwd?: string,
102 baseDir: string | undefined = cwd,
103): MarkdownFileLinkMeta | null {
104 const targetPath = resolveMarkdownFileLinkTarget(href, cwd, baseDir);
105 if (!targetPath) return null;
106 return buildFileLinkMetaFromTarget(targetPath, cwd);
107}
108
109function buildFileLinkMetaFromTarget(targetPath: string, cwd?: string): MarkdownFileLinkMeta {
110 const { path, line, column } = splitFilePathPosition(targetPath);
111 return {
112 filePath: path,
113 targetPath,
114 displayPath: formatWorkspaceRelativePath(targetPath, cwd),
115 workspaceRelativePath: workspaceRelativeFilePath(path, cwd),
116 basename: fileBasename(path),
117 ...(line !== undefined ? { line } : {}),
118 ...(column !== undefined ? { column } : {}),
119 };
120}