The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { formatFilePathPosition, resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; |
| 2 | |
| 3 | import { workspaceRelativeFilePath } from "@t3tools/shared/path"; |
| 4 | import type { MarkdownFileLinkMeta } from "./markdown-links"; |
| 5 | |
| 6 | interface MarkdownFileHostPath { |
| 7 | absolutePath: string; |
| 8 | matched: boolean; |
| 9 | } |
| 10 | |
| 11 | export 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 }; |
| 24 | } |
| 25 | |
| 26 | export function markdownFileEditorTarget( |
| 27 | meta: MarkdownFileLinkMeta, |
| 28 | hostPath: MarkdownFileHostPath, |
| 29 | ) { |
| 30 | return hostPath.matched |
| 31 | ? formatFilePathPosition({ ...meta, path: hostPath.absolutePath }) |
| 32 | : meta.targetPath; |
| 33 | } |
| 34 | |
| 35 | export 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 | }; |
| 48 | } |