Files
rhema-together/web/app/utils/highlight.ts
T
vhaudiquet acc545ede7
ci / checks (push) Successful in 50s
ci / test (push) Successful in 55s
ci / chart (push) Successful in 6s
ci / web (push) Successful in 19s
ci / docker (push) Successful in 14s
ci / publish-edge-image (push) Successful in 9s
ci / publish-edge-chart (push) Successful in 4s
feat(web): color the file viewer with highlight.js
The viewer rendered every file as plain text, so keywords, strings, and
comments in Rust, TypeScript, and the rest read as one color. Replace
the hand-written scanner explored for this with highlight.js, pinned to
the seventeen languages the viewer can open, so the client bundle stays
near 28KB gzip and unknown files still render as escaped text.

The output is class names only. The .hljs-* classes map onto the
--syntax-* tokens from the palette, so light and dark follow the
existing media query and no color bypasses the stylesheet.
2026-12-01 19:33:16 +01:00

129 lines
3.0 KiB
TypeScript

// Syntax coloring for the file viewer, backed by highlight.js. Only the
// languages the viewer can open are registered so the client bundle stays
// small. Unknown files and files over MAX_HIGHLIGHT render as escaped text.
import hljs from 'highlight.js/lib/core'
import bash from 'highlight.js/lib/languages/bash'
import c from 'highlight.js/lib/languages/c'
import cpp from 'highlight.js/lib/languages/cpp'
import css from 'highlight.js/lib/languages/css'
import dockerfile from 'highlight.js/lib/languages/dockerfile'
import go from 'highlight.js/lib/languages/go'
import ini from 'highlight.js/lib/languages/ini'
import java from 'highlight.js/lib/languages/java'
import javascript from 'highlight.js/lib/languages/javascript'
import json from 'highlight.js/lib/languages/json'
import markdown from 'highlight.js/lib/languages/markdown'
import python from 'highlight.js/lib/languages/python'
import rust from 'highlight.js/lib/languages/rust'
import sql from 'highlight.js/lib/languages/sql'
import typescript from 'highlight.js/lib/languages/typescript'
import xml from 'highlight.js/lib/languages/xml'
import yaml from 'highlight.js/lib/languages/yaml'
const MAX_HIGHLIGHT = 120_000
const LANGUAGES = {
bash,
c,
cpp,
css,
dockerfile,
go,
ini,
java,
javascript,
json,
markdown,
python,
rust,
sql,
typescript,
xml,
yaml,
}
for (const [name, language] of Object.entries(LANGUAGES)) {
hljs.registerLanguage(name, language)
}
const BY_EXTENSION: Record<string, string> = {
rs: 'rust',
ts: 'typescript',
tsx: 'typescript',
mts: 'typescript',
cts: 'typescript',
js: 'javascript',
jsx: 'javascript',
mjs: 'javascript',
cjs: 'javascript',
go: 'go',
c: 'c',
h: 'c',
cc: 'cpp',
cpp: 'cpp',
hpp: 'cpp',
cxx: 'cpp',
java: 'java',
kt: 'java',
kts: 'java',
py: 'python',
pyi: 'python',
sh: 'bash',
bash: 'bash',
zsh: 'bash',
json: 'json',
jsonc: 'json',
json5: 'json',
html: 'xml',
htm: 'xml',
xml: 'xml',
svg: 'xml',
vue: 'xml',
css: 'css',
scss: 'css',
less: 'css',
yml: 'yaml',
yaml: 'yaml',
toml: 'ini',
sql: 'sql',
md: 'markdown',
markdown: 'markdown',
mdx: 'markdown',
}
const BY_NAME: Record<string, string> = {
dockerfile: 'dockerfile',
'cargo.lock': 'ini',
}
const ESCAPES: Record<string, string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
}
function escapeHtml(text: string): string {
return text.replace(/[&<>]/g, (ch) => ESCAPES[ch] ?? ch)
}
function languageFor(path: string): string | null {
const base = path.slice(path.lastIndexOf('/') + 1).toLowerCase()
const dot = base.lastIndexOf('.')
if (dot > 0) {
const language = BY_EXTENSION[base.slice(dot + 1)]
if (language) return language
}
return BY_NAME[base] ?? null
}
export function highlightCode(code: string, path: string): string {
const language = languageFor(path)
if (!language || code.length > MAX_HIGHLIGHT) return escapeHtml(code)
try {
return hljs.highlight(code, { language, ignoreIllegals: true }).value
} catch {
return escapeHtml(code)
}
}