nandi/oripublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

lang.ts · 101 lines · 2.2 KBTypeScript Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
/**
 * 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<string, string> = {
	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";
}