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.
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

+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>