Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
99 lines
3.3 KiB
TypeScript
99 lines
3.3 KiB
TypeScript
import { fileBasename, workspaceRelativeFilePath } from "@t3tools/shared/path";
|
|
import {
|
|
inlineCodeFilePathCandidate,
|
|
isRelativeFilePath,
|
|
normalizeMarkdownLinkDestination,
|
|
resolveMarkdownFileLinkTarget,
|
|
} from "@t3tools/shared/markdownLinks";
|
|
import { parseFileUrlHref, splitFilePathPosition } from "@t3tools/shared/fileLinks";
|
|
|
|
import { formatWorkspaceRelativePath } from "./filePathDisplay";
|
|
import { isTerminalLinkActivation } from "./terminal-links";
|
|
|
|
export interface MarkdownFileLinkMeta {
|
|
filePath: string;
|
|
targetPath: string;
|
|
displayPath: string;
|
|
workspaceRelativePath: string | null;
|
|
basename: string;
|
|
line?: number;
|
|
column?: number;
|
|
}
|
|
|
|
export function shouldOpenMarkdownFileLinkInEditor(
|
|
event: Pick<MouseEvent, "metaKey" | "ctrlKey">,
|
|
platform?: string,
|
|
): boolean {
|
|
return isTerminalLinkActivation(event, platform);
|
|
}
|
|
|
|
export function shouldOpenMarkdownFileLinkInBrowserByDefault(path: string): boolean {
|
|
return /\.pdf$/i.test(path.split(/[?#]/, 1)[0] ?? "");
|
|
}
|
|
|
|
export function rewriteMarkdownFileUriHref(href: string | undefined): string | null {
|
|
if (!href) return null;
|
|
const target = parseFileUrlHref(normalizeMarkdownLinkDestination(href));
|
|
return target ? `${target.path}${target.hash}` : null;
|
|
}
|
|
|
|
/**
|
|
* Inline code spans mostly hold identifiers, commands, and refs (`node.meta`,
|
|
* `origin/main`) rather than deliberate link destinations, so auto-linking
|
|
* them demands stronger path evidence than an explicit markdown link does:
|
|
* an unambiguous path prefix, a file extension, or a :line suffix.
|
|
*/
|
|
export function resolveInlineCodeFileLinkMeta(
|
|
codeText: string,
|
|
cwd?: string,
|
|
baseDir: string | undefined = cwd,
|
|
): MarkdownFileLinkMeta | null {
|
|
const candidate = inlineCodeFilePathCandidate(codeText);
|
|
if (candidate === null) return null;
|
|
|
|
return resolveMarkdownFileLinkMeta(
|
|
candidate,
|
|
cwd,
|
|
inlineCodePathNamesFromWorkspaceRoot(candidate, cwd, baseDir) ? cwd : baseDir,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Prose in a workspace file names other files from the repo root (`docs/ai/design.md`),
|
|
* unlike an explicit link. Single-segment names (`design.md:12`) and `./`, `../`
|
|
* paths still read as siblings, and files outside the workspace keep their own base.
|
|
*/
|
|
function inlineCodePathNamesFromWorkspaceRoot(
|
|
candidate: string,
|
|
cwd: string | undefined,
|
|
baseDir: string | undefined,
|
|
): boolean {
|
|
if (!cwd || !baseDir || !isRelativeFilePath(candidate)) return false;
|
|
if (/^(?:~|\.{1,2})\//.test(candidate)) return false;
|
|
if (!splitFilePathPosition(candidate).path.includes("/")) return false;
|
|
return workspaceRelativeFilePath(baseDir, cwd) !== null;
|
|
}
|
|
|
|
export function resolveMarkdownFileLinkMeta(
|
|
href: string | undefined,
|
|
cwd?: string,
|
|
baseDir: string | undefined = cwd,
|
|
): MarkdownFileLinkMeta | null {
|
|
const targetPath = resolveMarkdownFileLinkTarget(href, cwd, baseDir);
|
|
if (!targetPath) return null;
|
|
return buildFileLinkMetaFromTarget(targetPath, cwd);
|
|
}
|
|
|
|
function buildFileLinkMetaFromTarget(targetPath: string, cwd?: string): MarkdownFileLinkMeta {
|
|
const { path, line, column } = splitFilePathPosition(targetPath);
|
|
return {
|
|
filePath: path,
|
|
targetPath,
|
|
displayPath: formatWorkspaceRelativePath(targetPath, cwd),
|
|
workspaceRelativePath: workspaceRelativeFilePath(path, cwd),
|
|
basename: fileBasename(path),
|
|
...(line !== undefined ? { line } : {}),
|
|
...(column !== undefined ? { column } : {}),
|
|
};
|
|
}
|