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