Open History to see the last commit
1import type { AssetResource, ProjectReadFileError, ScopedThreadRef } from "@t3tools/contracts";
2import { isAbsolutePath, workspaceRelativeFilePath } from "@t3tools/shared/path";
3
4export function resolveFilePreviewPath(path: string | null, cwd: string): string | null {
5 if (path === null) return null;
6 return path === "." || workspaceRelativeFilePath(path, cwd) === "." ? null : path;
7}
8
9export const isMarkdownPreviewFile = (path: string): boolean => /\.(?:md|mdx)$/i.test(path);
10
11export function filePreviewReadErrorMessage(error: ProjectReadFileError): string {
12 switch (error.failure) {
13 case "path_not_file":
14 return "The path is a directory or special file, not a regular file.";
15 case "binary_file":
16 return "The file is binary and cannot be displayed as text.";
17 case "workspace_path_outside_root":
18 return "The requested path is outside the workspace.";
19 case "resolved_path_outside_root":
20 return "The path resolves to a location outside the workspace.";
21 case "operation_failed":
22 return error.operation === "realpath-workspace-root"
23 ? "The workspace folder could not be accessed."
24 : "The file could not be accessed or read. It may be missing or inaccessible.";
25 default:
26 return error.message;
27 }
28}
29
30export function shouldShowFileExplorer(input: {
31 readonly relativePath: string | null;
32 readonly explorerOpen: boolean;
33 readonly attachmentOpen: boolean;
34}): boolean {
35 if (input.attachmentOpen || (input.relativePath && isAbsolutePath(input.relativePath))) {
36 return false;
37 }
38 return input.explorerOpen || input.relativePath === null;
39}
40
41export function setMarkdownTaskChecked(
42 markdown: string,
43 markerOffset: number,
44 checked: boolean,
45): string {
46 if (
47 markerOffset < 0 ||
48 markdown[markerOffset] !== "[" ||
49 !/[ xX]/.test(markdown[markerOffset + 1] ?? "") ||
50 markdown[markerOffset + 2] !== "]"
51 ) {
52 return markdown;
53 }
54
55 return `${markdown.slice(0, markerOffset + 1)}${checked ? "x" : " "}${markdown.slice(markerOffset + 2)}`;
56}
57
58export function workspaceAssetResource(input: {
59 readonly kind: "workspace-file" | "media-file";
60 readonly threadRef: ScopedThreadRef;
61 readonly draft: boolean;
62 readonly workspaceRoot: string;
63 readonly absolutePath: string;
64}): AssetResource {
65 if (input.draft) {
66 return { _tag: "draft-workspace-file", cwd: input.workspaceRoot, path: input.absolutePath };
67 }
68 return { _tag: input.kind, threadId: input.threadRef.threadId, path: input.absolutePath };
69}