Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

filePath.ts

150 lines4.8 KB

1import type { FilesystemEntryMetadata } from "@t3tools/contracts";
2import { filePreviewDelimiter } from "@t3tools/shared/delimitedPreview";
3import { joinPath, resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
4import {
5 isWorkspaceAudioPreviewPath,
6 isWorkspaceVideoPreviewPath,
7} from "@t3tools/shared/filePreview";
8​
9function isWindowsAbsolutePath(value: string): boolean {
10 return /^[A-Za-z]:[\\/]/.test(value) || value.startsWith("\\\\");
12​
13export function isAbsolutePath(value: string): boolean {
14 return value.startsWith("/") || isWindowsAbsolutePath(value);
16​
17export function fileRoutePathSegments(path: string): string[] {
18 const segments = path.split("/").filter((segment) => segment.length > 0);
19 return path.startsWith("/") ? ["", ...segments] : segments;
21​
22function isWindowsPathStyle(value: string): boolean {
23 return isWindowsAbsolutePath(value) || /^[A-Za-z]:\\/.test(value);
25​
26export function basename(path: string): string {
27 const parts = path.split(/[\\/]/).filter(Boolean);
28 return parts.at(-1) ?? path;
30​
31export function resolveWorkspaceFilePath(cwd: string, relativePath: string): string {
32 if (isAbsolutePath(relativePath)) {
33 return relativePath;
34 }
35​
36 const separator: "/" | "\\" = isWindowsPathStyle(cwd) ? "\\" : "/";
37 return joinPath(cwd, relativePath, separator);
39​
40function normalizeRelativePath(value: string): string | null {
41 const segments: string[] = [];
42 for (const segment of value.replaceAll("\\", "/").split("/")) {
43 if (segment.length === 0 || segment === ".") {
44 continue;
45 }
46 if (segment === "..") {
47 if (segments.length === 0) {
48 return null;
49 }
50 segments.pop();
51 continue;
52 }
53 segments.push(segment);
54 }
55 return segments.length > 0 ? segments.join("/") : null;
57​
58export function resolveFileLinkPath(
59 workspaceRoot: string | null | undefined,
60 targetPath: string,
61): string | null {
62 if (targetPath.startsWith("~/") || targetPath.startsWith("~\\")) {
63 if (!workspaceRoot) return null;
64 const expanded = resolvePathLinkTarget(targetPath, workspaceRoot);
65 return isAbsolutePath(expanded) ? expanded : null;
66 }
67 return isAbsolutePath(targetPath) ? targetPath : normalizeRelativePath(targetPath);
69​
70export function resolveMarkdownFileLinkPath(
71 workspaceRoot: string,
72 markdownDirectory: string,
73 targetPath: string,
74): string | null {
75 if (targetPath.startsWith("~/") || targetPath.startsWith("~\\")) {
76 return resolveFileLinkPath(workspaceRoot, targetPath);
77 }
78 return resolveWorkspaceFilePath(markdownDirectory, targetPath);
80​
81export function resolveWorkspaceRelativeFilePath(
82 workspaceRoot: string | null | undefined,
83 targetPath: string,
84): string | null {
85 const resolved = resolveFileLinkPath(workspaceRoot, targetPath);
86 if (resolved === null) return null;
87 targetPath = resolved;
88 if (!isAbsolutePath(targetPath)) return targetPath;
89 if (!workspaceRoot) {
90 return null;
91 }
92​
93 const normalizedTarget = targetPath.replaceAll("\\", "/");
94 const normalizedRoot = workspaceRoot.replaceAll("\\", "/").replace(/\/+$/, "");
95 const caseInsensitive = isWindowsAbsolutePath(targetPath) || isWindowsAbsolutePath(workspaceRoot);
96 const comparableTarget = caseInsensitive ? normalizedTarget.toLowerCase() : normalizedTarget;
97 const comparableRoot = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
98 if (!comparableTarget.startsWith(`${comparableRoot}/`)) {
99 return null;
100 }
101​
102 const relativePath = normalizedTarget.slice(normalizedRoot.length + 1);
103 if (relativePath.split("/").includes("..")) {
104 return null;
105 }
106 return normalizeRelativePath(relativePath);
108​
109export function isVideoPreviewFile(path: string): boolean {
110 return isWorkspaceVideoPreviewPath(path);
112​
113export function isAudioPreviewFile(path: string): boolean {
114 return isWorkspaceAudioPreviewPath(path);
116​
117export function isSvgImagePreviewFile(path: string): boolean {
118 return /\.svg$/i.test(path);
120​
121export function isMarkdownPreviewFile(path: string): boolean {
122 return /\.(?:md|mdx|markdown|mdown|mkd)$/i.test(path);
124​
125export function needsWorkspaceFileContents(input: {
126 readonly relativePath: string | null;
127 readonly metadataKind: FilesystemEntryMetadata["kind"] | undefined;
128 readonly activeMode: "preview" | "source";
129}): boolean {
130 const path = input.relativePath;
131 return (
132 input.metadataKind !== "directory" &&
133 path !== null &&
134 !isVideoPreviewFile(path) &&
135 !isAudioPreviewFile(path) &&
136 (input.activeMode === "source" ||
137 isMarkdownPreviewFile(path) ||
138 filePreviewDelimiter({ name: path }) !== null)
139 );
141​
142export function fileHeaderSubtitle(projectName: string, relativePath: string): string {
143 const parentDir = relativePath.slice(
144 0,
145 Math.max(relativePath.lastIndexOf("/"), relativePath.lastIndexOf("\\"), 0),
146 );
147 return isAbsolutePath(relativePath)
148 ? parentDir
149 : [projectName, parentDir].filter(Boolean).join(" · ");