Skip to content
Source
Sign inSign up

markdownFileHostPath.ts

48 lines1.4 KB

1import { formatFilePathPosition, resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
2​
3import { workspaceRelativeFilePath } from "@t3tools/shared/path";
4import type { MarkdownFileLinkMeta } from "./markdown-links";
5​
6interface MarkdownFileHostPath {
7 absolutePath: string;
8 matched: boolean;
9}
10​
11export async function resolveMarkdownFileHostPath(input: {
12 meta: MarkdownFileLinkMeta;
13 cwd: string | undefined;
14 findWorkspaceMatch: (workspaceRelativePath: string) => Promise<string | null>;
15}) {
16 const { meta, cwd } = input;
17 const match =
18 meta.workspaceLookupPath && cwd
19 ? await input.findWorkspaceMatch(meta.workspaceLookupPath)
20 : null;
21 return match && cwd
22 ? { absolutePath: resolvePathLinkTarget(match, cwd), matched: true }
23 : { absolutePath: meta.filePath, matched: false };
25​
26export function markdownFileEditorTarget(
27 meta: MarkdownFileLinkMeta,
28 hostPath: MarkdownFileHostPath,
29) {
30 return hostPath.matched
31 ? formatFilePathPosition({ ...meta, path: hostPath.absolutePath })
32 : meta.targetPath;
34​
35export function markdownFileCopyPaths(
36 meta: MarkdownFileLinkMeta,
37 hostPath: MarkdownFileHostPath,
38 cwd: string | undefined,
39) {
40 const fullPath = markdownFileEditorTarget(meta, hostPath);
41 return {
42 fullPath,
43 relativePath: formatFilePathPosition({
44 ...meta,
45 path: workspaceRelativeFilePath(hostPath.absolutePath, cwd) ?? meta.filePath,
46 }),
47 };