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

filePreviewMode.ts

38 lines1.3 KB

1import { workspaceRelativeFilePath } from "@t3tools/client-runtime/markdown-links";
2import { isAbsolutePath } from "~/terminal-links";
3​
4/** Resolve workspace links before choosing between the explorer and a file preview. */
5export function resolveFilePreviewPath(path: string | null, cwd: string): string | null {
6 if (path === null) return null;
7 return path === "." || workspaceRelativeFilePath(path, cwd) === "." ? null : path;
8}
9​
10export const isMarkdownPreviewFile = (path: string): boolean => /\.(?:md|mdx)$/i.test(path);
11​
12export function shouldShowFileExplorer(input: {
13 readonly relativePath: string | null;
14 readonly explorerOpen: boolean;
15 readonly attachmentOpen: boolean;
16}): boolean {
17 if (input.attachmentOpen || (input.relativePath && isAbsolutePath(input.relativePath))) {
18 return false;
19 }
20 return input.explorerOpen || input.relativePath === null;
22​
23export function setMarkdownTaskChecked(
24 markdown: string,
25 markerOffset: number,
26 checked: boolean,
27): string {
28 if (
29 markerOffset < 0 ||
30 markdown[markerOffset] !== "[" ||
31 !/[ xX]/.test(markdown[markerOffset + 1] ?? "") ||
32 markdown[markerOffset + 2] !== "]"
33 ) {
34 return markdown;
35 }
36​
37 return `${markdown.slice(0, markerOffset + 1)}${checked ? "x" : " "}${markdown.slice(markerOffset + 2)}`;