Open History to see the last commit
1import { joinPath, resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
2import {
3 isWorkspaceAudioPreviewPath,
4 isWorkspaceVideoPreviewPath,
5} from "@t3tools/shared/filePreview";
6
7function isWindowsAbsolutePath(value: string): boolean {
8 return /^[A-Za-z]:[\\/]/.test(value) || value.startsWith("\\\\");
9}
10
11export function isAbsolutePath(value: string): boolean {
12 return value.startsWith("/") || isWindowsAbsolutePath(value);
13}
14
15export function fileRoutePathSegments(path: string): string[] {
16 const segments = path.split("/").filter((segment) => segment.length > 0);
17 return path.startsWith("/") ? ["", ...segments] : segments;
18}
19
20function isWindowsPathStyle(value: string): boolean {
21 return isWindowsAbsolutePath(value) || /^[A-Za-z]:\\/.test(value);
22}
23
24export function basename(path: string): string {
25 const parts = path.split(/[\\/]/).filter(Boolean);
26 return parts.at(-1) ?? path;
27}
28
29export function resolveWorkspaceFilePath(cwd: string, relativePath: string): string {
30 if (isAbsolutePath(relativePath)) {
31 return relativePath;
32 }
33
34 const separator: "/" | "\\" = isWindowsPathStyle(cwd) ? "\\" : "/";
35 return joinPath(cwd, relativePath, separator);
36}
37
38function normalizeRelativePath(value: string): string | null {
39 const segments: string[] = [];
40 for (const segment of value.replaceAll("\\", "/").split("/")) {
41 if (segment.length === 0 || segment === ".") {
42 continue;
43 }
44 if (segment === "..") {
45 if (segments.length === 0) {
46 return null;
47 }
48 segments.pop();
49 continue;
50 }
51 segments.push(segment);
52 }
53 return segments.length > 0 ? segments.join("/") : null;
54}
55
56export function resolveFileLinkPath(
57 workspaceRoot: string | null | undefined,
58 targetPath: string,
59): string | null {
60 if (targetPath.startsWith("~/") || targetPath.startsWith("~\\")) {
61 if (!workspaceRoot) return null;
62 const expanded = resolvePathLinkTarget(targetPath, workspaceRoot);
63 return isAbsolutePath(expanded) ? expanded : null;
64 }
65 return isAbsolutePath(targetPath) ? targetPath : normalizeRelativePath(targetPath);
66}
67
68export function resolveWorkspaceRelativeFilePath(
69 workspaceRoot: string | null | undefined,
70 targetPath: string,
71): string | null {
72 const resolved = resolveFileLinkPath(workspaceRoot, targetPath);
73 if (resolved === null) return null;
74 targetPath = resolved;
75 if (!isAbsolutePath(targetPath)) return targetPath;
76 if (!workspaceRoot) {
77 return null;
78 }
79
80 const normalizedTarget = targetPath.replaceAll("\\", "/");
81 const normalizedRoot = workspaceRoot.replaceAll("\\", "/").replace(/\/+$/, "");
82 const caseInsensitive = isWindowsAbsolutePath(targetPath) || isWindowsAbsolutePath(workspaceRoot);
83 const comparableTarget = caseInsensitive ? normalizedTarget.toLowerCase() : normalizedTarget;
84 const comparableRoot = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
85 if (!comparableTarget.startsWith(`${comparableRoot}/`)) {
86 return null;
87 }
88
89 const relativePath = normalizedTarget.slice(normalizedRoot.length + 1);
90 if (relativePath.split("/").includes("..")) {
91 return null;
92 }
93 return normalizeRelativePath(relativePath);
94}
95
96export function isVideoPreviewFile(path: string): boolean {
97 return isWorkspaceVideoPreviewPath(path);
98}
99
100export function isAudioPreviewFile(path: string): boolean {
101 return isWorkspaceAudioPreviewPath(path.split(/[?#]/, 1)[0] ?? "");
102}
103
104export function isSvgImagePreviewFile(path: string): boolean {
105 return /\.svg$/i.test(path.split(/[?#]/, 1)[0] ?? "");
106}
107
108export function isMarkdownPreviewFile(path: string): boolean {
109 return /\.(?:md|mdx)$/i.test(path.split(/[?#]/, 1)[0] ?? "");
110}
111
112export function fileHeaderSubtitle(projectName: string, relativePath: string): string {
113 const parentDir = relativePath.slice(
114 0,
115 Math.max(relativePath.lastIndexOf("/"), relativePath.lastIndexOf("\\"), 0),
116 );
117 return isAbsolutePath(relativePath)
118 ? parentDir
119 : [projectName, parentDir].filter(Boolean).join(" · ");
120}