feat(web): color the file viewer with highlight.js
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

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.
This commit is contained in:
vhaudiquet committed 2026-12-01 19:33:16 +01:00
1 parent 880f4e2d87
commit acc545ede7
5 files changed
+222 -1

No files matched your search

+75
View File
@@ -41,6 +41,15 @@
--member-7: #7a6a2f;
--member-8: #5a6a3f;
--syntax-keyword: #6a5a8f;
--syntax-string: #3f6f5a;
--syntax-number: #8a5a2b;
--syntax-def: #2f6f6f;
--syntax-type: #4f5f8f;
--syntax-tag: #8f4f5a;
--syntax-attr: var(--syntax-type);
--syntax-comment: var(--faint);
--font-sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
--font-mono: "FiraCode Nerd Font Mono", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
@@ -89,6 +98,13 @@
--ok: #58a58c;
--on-accent: #12110f;
--overlay: rgba(0, 0, 0, 0.62);
--syntax-keyword: #b9a8dd;
--syntax-string: #8cc4aa;
--syntax-number: #d9b37c;
--syntax-def: #7fc4c4;
--syntax-type: #a3b1e0;
--syntax-tag: #d99aa5;
}
}
@@ -1912,6 +1928,65 @@ body.files-resizing-y * {
min-width: max-content;
}
.viewer-code .hljs-comment,
.viewer-code .hljs-quote {
color: var(--syntax-comment);
}
.viewer-code .hljs-keyword,
.viewer-code .hljs-selector-tag,
.viewer-code .hljs-literal,
.viewer-code .hljs-section,
.viewer-code .hljs-doctag,
.viewer-code .hljs-meta {
color: var(--syntax-keyword);
}
.viewer-code .hljs-string,
.viewer-code .hljs-regexp,
.viewer-code .hljs-symbol,
.viewer-code .hljs-bullet,
.viewer-code .hljs-template-tag,
.viewer-code .hljs-template-variable,
.viewer-code .hljs-addition,
.viewer-code .hljs-link {
color: var(--syntax-string);
}
.viewer-code .hljs-number {
color: var(--syntax-number);
}
.viewer-code .hljs-title,
.viewer-code .hljs-title.function_,
.viewer-code .hljs-title.class_ {
color: var(--syntax-def);
}
.viewer-code .hljs-type,
.viewer-code .hljs-built_in,
.viewer-code .hljs-class .hljs-title {
color: var(--syntax-type);
}
.viewer-code .hljs-tag,
.viewer-code .hljs-name,
.viewer-code .hljs-selector-id,
.viewer-code .hljs-selector-class,
.viewer-code .hljs-selector-attr,
.viewer-code .hljs-selector-pseudo,
.viewer-code .hljs-deletion {
color: var(--syntax-tag);
}
.viewer-code .hljs-attr,
.viewer-code .hljs-attribute,
.viewer-code .hljs-variable,
.viewer-code .hljs-property,
.viewer-code .hljs-params {
color: var(--syntax-attr);
}
.git-strip {
flex: 0 0 auto;
border-top: 1px solid var(--border);
+9 -1
View File
@@ -4,6 +4,7 @@ import { ChevronRight, File, Folder, Link as LinkIcon } from 'lucide-vue-next'
import type { FileContentResponse, FileEntry, FileListResponse, GitSummaryResponse } from '~/types'
import { apiErrorMessage } from '~/utils/errors'
import { formatSize } from '~/utils/format'
import { highlightCode } from '~/utils/highlight'
const props = defineProps<{ roomId: string; roomName: string }>()
const api = useApi()
@@ -215,6 +216,13 @@ function indent(depth: number) {
return { '--tree-depth': String(depth) }
}
// highlightCode escapes the text before it adds token spans, so v-html is safe.
const codeHtml = computed(() => {
const content = file.value?.content
if (!content) return ''
return highlightCode(content, file.value?.path ?? selected.value?.path ?? '')
})
const lineNumbers = computed(() => {
const content = file.value?.content ?? ''
const count = content === '' ? 1 : content.replace(/\n$/, '').split('\n').length
@@ -387,7 +395,7 @@ onBeforeUnmount(() => {
<p v-else-if="file && file.content === ''" class="meta pane-pad-3">This file is empty.</p>
<template v-else-if="file">
<pre class="gutter" aria-hidden="true">{{ lineNumbers }}</pre>
<pre class="viewer-code">{{ file.content }}</pre>
<pre class="viewer-code" v-html="codeHtml"></pre>
</template>
</div>
+128
View File
@@ -0,0 +1,128 @@
// 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)
}
}
+1
View File
@@ -12,6 +12,7 @@
"dependencies": {
"@xterm/addon-fit": "^0.11.0",
"@xterm/xterm": "^6.0.0",
"highlight.js": "^11.12.0",
"lucide-vue-next": "^1.0.0",
"markdown-it": "^15.0.3",
"nuxt": "^4.7.0",
+9
View File
@@ -14,6 +14,9 @@ importers:
'@xterm/xterm':
specifier: ^6.0.0
version: 6.0.0
highlight.js:
specifier: ^11.12.0
version: 11.12.0
lucide-vue-next:
specifier: ^1.0.0
version: 1.0.0(vue@3.5.46(typescript@5.9.3))
@@ -1731,6 +1734,10 @@ packages:
resolution: {integrity: sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==}
hasBin: true
highlight.js@11.12.0:
resolution: {integrity: sha512-nbfWpyRMcMrPMmDwJB+dhX/eiaPKtc2RB+0QZskqJ3WjRA/FDS0e9hZrx8EC/lbEv8gXy98FcDbNa/dspAaJMg==}
engines: {node: '>=12.0.0'}
hookable@5.5.3:
resolution: {integrity: sha512-Yc+BQe8SvoXH1643Qez1zqLRmbA5rCL+sSmk6TVos0LWVfNIB7PGncdlId77WzLGSIB5KaWgTaNTs2lNVEI6VQ==}
@@ -4664,6 +4671,8 @@ snapshots:
he@1.2.0: {}
highlight.js@11.12.0: {}
hookable@5.5.3: {}
hookable@6.1.2: {}