Open History to see the last commit
1import { isWorkspaceVideoPreviewPath } from "@t3tools/shared/filePreview";
2
3export interface FileBreadcrumb {
4 readonly label: string;
5 readonly path: string;
6 readonly kind: "project" | "directory" | "file";
7}
8
9function isWindowsAbsolutePath(value: string): boolean {
10 return /^[A-Za-z]:[\\/]/.test(value) || value.startsWith("\\\\");
11}
12
13/** A file route holding an absolute path shows a host file outside the workspace. */
14export function isAbsolutePath(value: string): boolean {
15 return value.startsWith("/") || isWindowsAbsolutePath(value);
16}
17
18/** Route segments that `normalizeRoutePath` joins back into the same path, root included. */
19export function fileRoutePathSegments(path: string): string[] {
20 const segments = path.split("/").filter((segment) => segment.length > 0);
21 return path.startsWith("/") ? ["", ...segments] : segments;
22}
23
24function isWindowsPathStyle(value: string): boolean {
25 return isWindowsAbsolutePath(value) || /^[A-Za-z]:\\/.test(value);
26}
27
28function joinPath(base: string, next: string, separator: "/" | "\\"): string {
29 const cleanBase = base.replace(/[\\/]+$/, "");
30 if (separator === "\\") {
31 return `${cleanBase}\\${next.replaceAll("/", "\\")}`;
32 }
33 return `${cleanBase}/${next.replace(/^\/+/, "")}`;
34}
35
36export function basename(path: string): string {
37 const parts = path.split(/[\\/]/).filter(Boolean);
38 return parts.at(-1) ?? path;
39}
40
41export function resolveWorkspaceFilePath(cwd: string, relativePath: string): string {
42 if (isAbsolutePath(relativePath)) {
43 return relativePath;
44 }
45
46 const separator: "/" | "\\" = isWindowsPathStyle(cwd) ? "\\" : "/";
47 return joinPath(cwd, relativePath, separator);
48}
49
50function normalizeRelativePath(value: string): string | null {
51 const segments: string[] = [];
52 for (const segment of value.replaceAll("\\", "/").split("/")) {
53 if (segment.length === 0 || segment === ".") {
54 continue;
55 }
56 if (segment === "..") {
57 if (segments.length === 0) {
58 return null;
59 }
60 segments.pop();
61 continue;
62 }
63 segments.push(segment);
64 }
65 return segments.length > 0 ? segments.join("/") : null;
66}
67
68export function resolveWorkspaceRelativeFilePath(
69 workspaceRoot: string | null | undefined,
70 targetPath: string,
71): string | null {
72 if (!isAbsolutePath(targetPath)) {
73 if (targetPath.startsWith("~/") || targetPath.startsWith("~\\")) {
74 return null;
75 }
76 return normalizeRelativePath(targetPath);
77 }
78 if (!workspaceRoot) {
79 return null;
80 }
81
82 const normalizedTarget = targetPath.replaceAll("\\", "/");
83 const normalizedRoot = workspaceRoot.replaceAll("\\", "/").replace(/\/+$/, "");
84 const caseInsensitive = isWindowsAbsolutePath(targetPath) || isWindowsAbsolutePath(workspaceRoot);
85 const comparableTarget = caseInsensitive ? normalizedTarget.toLowerCase() : normalizedTarget;
86 const comparableRoot = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
87 if (!comparableTarget.startsWith(`${comparableRoot}/`)) {
88 return null;
89 }
90
91 const relativePath = normalizedTarget.slice(normalizedRoot.length + 1);
92 // `/repo/../x` starts with the root but escapes it.
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 isSvgImagePreviewFile(path: string): boolean {
104 return /\.svg$/i.test(path.split(/[?#]/, 1)[0] ?? "");
105}
106
107export function isMarkdownPreviewFile(path: string): boolean {
108 return /\.(?:md|mdx)$/i.test(path.split(/[?#]/, 1)[0] ?? "");
109}
110
111export function fileBreadcrumbs(projectName: string, relativePath: string): FileBreadcrumb[] {
112 const parts = relativePath.split("/").filter(Boolean);
113 return [
114 { label: projectName, path: "", kind: "project" },
115 ...parts.map((part, index) => ({
116 label: part,
117 path: parts.slice(0, index + 1).join("/"),
118 kind: index === parts.length - 1 ? ("file" as const) : ("directory" as const),
119 })),
120 ];
121}