/** * File-type detection for the preview and editor panels: maps a file path to * a Monaco language id and to the kind of preview it deserves. * * @example * languageForPath("main.go"); // "go" * kindForPath("README.md"); // "markdown" */ /** * How the preview pane should treat a file: a rendered document, a bitmap or * SVG shown as an image, a draw.io diagram, or code. */ export type DocumentKind = | "markdown" | "asciidoc" | "image" | "drawio" | "code"; const imageExtensions = new Set([ "avif", "bmp", "gif", "ico", "jpeg", "jpg", "png", "svg", "webp", ]); /** draw.io diagrams stored as XML (.drawio.svg/.drawio.png are plain images). */ const drawioExtensions = new Set(["drawio", "dio"]); const languageByExtension: Record = { c: "c", cjs: "javascript", cpp: "cpp", css: "css", go: "go", h: "c", hpp: "cpp", html: "html", java: "java", js: "javascript", json: "json", jsx: "javascript", kt: "kotlin", md: "markdown", mjs: "javascript", php: "php", py: "python", rb: "ruby", rs: "rust", sh: "shell", bash: "shell", sql: "sql", svg: "xml", drawio: "xml", dio: "xml", swift: "swift", toml: "ini", ts: "typescript", tsx: "typescript", xml: "xml", yaml: "yaml", yml: "yaml", }; function extensionOf(path: string): string { const name = path.split("/").pop() ?? path; const dot = name.lastIndexOf("."); return dot > 0 ? name.slice(dot + 1).toLowerCase() : ""; } /** languageForPath returns the Monaco language id for a file (default: plaintext). */ export function languageForPath(path: string): string { if (path.endsWith("Makefile") || path.endsWith("Dockerfile")) { return path.endsWith("Makefile") ? "makefile" : "dockerfile"; } return languageByExtension[extensionOf(path)] ?? "plaintext"; } /** kindForPath tells the preview pane whether a file has a rendered form. */ export function kindForPath(path: string): DocumentKind { const extension = extensionOf(path); if (extension === "md" || extension === "markdown") { return "markdown"; } if (extension === "adoc" || extension === "asciidoc") { return "asciidoc"; } if (imageExtensions.has(extension)) { return "image"; } if (drawioExtensions.has(extension)) { return "drawio"; } return "code"; }