| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const EXTENSION_TO_LANGUAGE: Record<string, string> = { |
| |
| js: "javascript", |
| jsx: "jsx", |
| mjs: "javascript", |
| cjs: "javascript", |
| ts: "typescript", |
| tsx: "tsx", |
|
|
| |
| py: "python", |
| pyi: "python", |
| pyw: "python", |
|
|
| |
| html: "markup", |
| htm: "markup", |
| svg: "markup", |
| xml: "markup", |
| css: "css", |
| scss: "scss", |
| sass: "sass", |
| less: "less", |
|
|
| |
| json: "json", |
| json5: "json5", |
| yaml: "yaml", |
| yml: "yaml", |
| toml: "toml", |
| ini: "ini", |
| cfg: "ini", |
| properties: "properties", |
| conf: "ini", |
| env: "bash", |
|
|
| |
| md: "markdown", |
| markdown: "markdown", |
| mdx: "markdown", |
|
|
| |
| sh: "bash", |
| bash: "bash", |
| zsh: "bash", |
| fish: "bash", |
| bat: "batch", |
| cmd: "batch", |
| ps1: "powershell", |
| ps: "powershell", |
|
|
| |
| c: "c", |
| h: "c", |
| cpp: "cpp", |
| cc: "cpp", |
| cxx: "cpp", |
| hpp: "cpp", |
| hh: "cpp", |
| cs: "csharp", |
| java: "java", |
| kt: "kotlin", |
| kts: "kotlin", |
| scala: "scala", |
| rs: "rust", |
| go: "go", |
| rb: "ruby", |
| php: "php", |
| swift: "swift", |
| m: "objectivec", |
| mm: "objectivec", |
|
|
| |
| hs: "haskell", |
| ex: "elixir", |
| exs: "elixir", |
| erl: "erlang", |
| fs: "fsharp", |
| fsx: "fsharp", |
| ml: "ocaml", |
| mli: "ocaml", |
| clj: "clojure", |
| cljs: "clojure", |
| cljc: "clojure", |
|
|
| |
| lua: "lua", |
| pl: "perl", |
| pm: "perl", |
| r: "r", |
| jl: "julia", |
| dart: "dart", |
| groovy: "groovy", |
| gradle: "groovy", |
|
|
| |
| tf: "hcl", |
| hcl: "hcl", |
| dockerfile: "docker", |
|
|
| |
| sql: "sql", |
| graphql: "graphql", |
| gql: "graphql", |
| proto: "protobuf", |
| diff: "diff", |
| patch: "diff", |
| nix: "nix", |
| sol: "solidity", |
| }; |
|
|
| const FILENAME_TO_LANGUAGE: Record<string, string> = { |
| dockerfile: "docker", |
| makefile: "makefile", |
| gnumakefile: "makefile", |
| ".bashrc": "bash", |
| ".zshrc": "bash", |
| ".profile": "bash", |
| ".env": "bash", |
| ".gitignore": "bash", |
| ".dockerignore": "bash", |
| }; |
|
|
| function getExtension(path: string): string { |
| const slashIdx = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")); |
| const basename = path.slice(slashIdx + 1); |
| const dotIdx = basename.lastIndexOf("."); |
| if (dotIdx <= 0) return ""; |
| return basename.slice(dotIdx + 1).toLowerCase(); |
| } |
|
|
| function getBasename(path: string): string { |
| const slashIdx = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")); |
| return path.slice(slashIdx + 1).toLowerCase(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function getPrismLanguageForFile( |
| path: string, |
| mimeType?: string, |
| ): string | null { |
| const basename = getBasename(path); |
| const fromBasename = FILENAME_TO_LANGUAGE[basename]; |
| if (fromBasename) return fromBasename; |
|
|
| const ext = getExtension(path); |
| if (ext && EXTENSION_TO_LANGUAGE[ext]) { |
| return EXTENSION_TO_LANGUAGE[ext]; |
| } |
|
|
| |
| |
| if (mimeType) { |
| if (mimeType === "text/html") return "markup"; |
| if (mimeType === "text/css") return "css"; |
| if (mimeType === "application/json") return "json"; |
| if (mimeType === "text/markdown") return "markdown"; |
| if ( |
| mimeType === "application/javascript" || |
| mimeType === "text/javascript" |
| ) { |
| return "javascript"; |
| } |
| if (mimeType === "application/x-yaml" || mimeType === "text/yaml") { |
| return "yaml"; |
| } |
| } |
|
|
| return null; |
| } |
|
|