lang-detect.ts
typescript
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2
feat: add repair-commit wire endpoint (API parity with repa…
Opus 4.8
minor
⚠ breaking
1 day ago
| 1 | /** |
| 2 | * lang-detect.ts — Map file paths to highlight.js language identifiers. |
| 3 | * |
| 4 | * Pure function, no DOM or hljs dependency — fully unit-testable. |
| 5 | * Used by blob.ts and diff.ts for client-side syntax highlighting. |
| 6 | */ |
| 7 | |
| 8 | const EXT_MAP: Record<string, string> = { |
| 9 | py: 'python', pyw: 'python', |
| 10 | ts: 'typescript', tsx: 'typescript', |
| 11 | js: 'javascript', jsx: 'javascript', mjs: 'javascript', cjs: 'javascript', |
| 12 | rs: 'rust', |
| 13 | go: 'go', |
| 14 | swift: 'swift', |
| 15 | kt: 'kotlin', kts: 'kotlin', |
| 16 | java: 'java', |
| 17 | rb: 'ruby', rake: 'ruby', |
| 18 | cpp: 'cpp', cc: 'cpp', cxx: 'cpp', c: 'cpp', h: 'cpp', hpp: 'cpp', |
| 19 | hs: 'haskell', |
| 20 | json: 'json', jsonc: 'json', |
| 21 | yaml: 'yaml', yml: 'yaml', |
| 22 | toml: 'toml', |
| 23 | bats: 'bash', |
| 24 | sh: 'bash', bash: 'bash', zsh: 'bash', |
| 25 | html: 'xml', htm: 'xml', xml: 'xml', svg: 'xml', |
| 26 | css: 'css', scss: 'css', sass: 'css', |
| 27 | sql: 'sql', |
| 28 | md: 'markdown', mdx: 'markdown', |
| 29 | txt: 'plaintext', |
| 30 | }; |
| 31 | |
| 32 | /** Dotfiles with no extension, keyed by exact filename. */ |
| 33 | const FILENAME_MAP: Record<string, string> = { |
| 34 | '.museattributes': 'toml', |
| 35 | '.museignore': 'toml', |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * Return the highlight.js language identifier for a file path. |
| 40 | * Falls back to `'plaintext'` for unknown extensions. |
| 41 | */ |
| 42 | export function extToLang(path: string): string { |
| 43 | const filename = path.split('/').pop() ?? path; |
| 44 | if (filename in FILENAME_MAP) return FILENAME_MAP[filename]!; |
| 45 | // Zsh completion files: _<command> with no extension (e.g. completions/_muse) |
| 46 | if (filename.startsWith('_') && !filename.includes('.')) return 'bash'; |
| 47 | const ext = filename.split('.').pop()?.toLowerCase() ?? ''; |
| 48 | return EXT_MAP[ext] ?? 'plaintext'; |
| 49 | } |
File History
1 commit
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2
feat: add repair-commit wire endpoint (API parity with repa…
Opus 4.8
minor
⚠
1 day ago