forked from bots-garden/ori
| ✨ Workspace panel, selectors, previews, desktop app, sandbox template, resizable file tree, light/dark theme | 1 | /** |
| 2 | * File-type detection for the preview and editor panels: maps a file path to | |
| 3 | * a Monaco language id and to the kind of preview it deserves. | |
| 4 | * | |
| 5 | * @example | |
| 6 | * languageForPath("main.go"); // "go" | |
| 7 | * kindForPath("README.md"); // "markdown" | |
| 8 | */ | |
| 9 | ||
| 10 | /** | |
| 11 | * How the preview pane should treat a file: a rendered document, a bitmap or | |
| 12 | * SVG shown as an image, a draw.io diagram, or code. | |
| 13 | */ | |
| 14 | export type DocumentKind = | |
| 15 | | "markdown" | |
| 16 | | "asciidoc" | |
| 17 | | "image" | |
| 18 | | "drawio" | |
| 19 | | "code"; | |
| 20 | ||
| 21 | const imageExtensions = new Set([ | |
| 22 | "avif", | |
| 23 | "bmp", | |
| 24 | "gif", | |
| 25 | "ico", | |
| 26 | "jpeg", | |
| 27 | "jpg", | |
| 28 | "png", | |
| 29 | "svg", | |
| 30 | "webp", | |
| 31 | ]); | |
| 32 | ||
| 33 | /** draw.io diagrams stored as XML (.drawio.svg/.drawio.png are plain images). */ | |
| 34 | const drawioExtensions = new Set(["drawio", "dio"]); | |
| 35 | ||
| 36 | const languageByExtension: Record<string, string> = { | |
| 37 | c: "c", | |
| 38 | cjs: "javascript", | |
| 39 | cpp: "cpp", | |
| 40 | css: "css", | |
| 41 | go: "go", | |
| 42 | h: "c", | |
| 43 | hpp: "cpp", | |
| 44 | html: "html", | |
| 45 | java: "java", | |
| 46 | js: "javascript", | |
| 47 | json: "json", | |
| 48 | jsx: "javascript", | |
| 49 | kt: "kotlin", | |
| 50 | md: "markdown", | |
| 51 | mjs: "javascript", | |
| 52 | php: "php", | |
| 53 | py: "python", | |
| 54 | rb: "ruby", | |
| 55 | rs: "rust", | |
| 56 | sh: "shell", | |
| 57 | bash: "shell", | |
| 58 | sql: "sql", | |
| 59 | svg: "xml", | |
| 60 | drawio: "xml", | |
| 61 | dio: "xml", | |
| 62 | swift: "swift", | |
| 63 | toml: "ini", | |
| 64 | ts: "typescript", | |
| 65 | tsx: "typescript", | |
| 66 | xml: "xml", | |
| 67 | yaml: "yaml", | |
| 68 | yml: "yaml", | |
| 69 | }; | |
| 70 | ||
| 71 | function extensionOf(path: string): string { | |
| 72 | const name = path.split("/").pop() ?? path; | |
| 73 | const dot = name.lastIndexOf("."); | |
| 74 | return dot > 0 ? name.slice(dot + 1).toLowerCase() : ""; | |
| 75 | } | |
| 76 | ||
| 77 | /** languageForPath returns the Monaco language id for a file (default: plaintext). */ | |
| 78 | export function languageForPath(path: string): string { | |
| 79 | if (path.endsWith("Makefile") || path.endsWith("Dockerfile")) { | |
| 80 | return path.endsWith("Makefile") ? "makefile" : "dockerfile"; | |
| 81 | } | |
| 82 | return languageByExtension[extensionOf(path)] ?? "plaintext"; | |
| 83 | } | |
| 84 | ||
| 85 | /** kindForPath tells the preview pane whether a file has a rendered form. */ | |
| 86 | export function kindForPath(path: string): DocumentKind { | |
| 87 | const extension = extensionOf(path); | |
| 88 | if (extension === "md" || extension === "markdown") { | |
| 89 | return "markdown"; | |
| 90 | } | |
| 91 | if (extension === "adoc" || extension === "asciidoc") { | |
| 92 | return "asciidoc"; | |
| 93 | } | |
| 94 | if (imageExtensions.has(extension)) { | |
| 95 | return "image"; | |
| 96 | } | |
| 97 | if (drawioExtensions.has(extension)) { | |
| 98 | return "drawio"; | |
| 99 | } | |
| 100 | return "code"; | |
| 101 | } |