From acc545ede76e86101414c1d89c809f6184896d8a Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Tue, 1 Dec 2026 19:33:16 +0100 Subject: [PATCH] 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. --- web/app/assets/css/main.css | 75 ++++++++++++++++++ web/app/components/FilesPane.vue | 10 ++- web/app/utils/highlight.ts | 128 +++++++++++++++++++++++++++++++ web/package.json | 1 + web/pnpm-lock.yaml | 9 +++ 5 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 web/app/utils/highlight.ts diff --git a/web/app/assets/css/main.css b/web/app/assets/css/main.css index 52e68c0..56fef59 100644 --- a/web/app/assets/css/main.css +++ b/web/app/assets/css/main.css @@ -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); diff --git a/web/app/components/FilesPane.vue b/web/app/components/FilesPane.vue index b75e9fa..f478c6a 100644 --- a/web/app/components/FilesPane.vue +++ b/web/app/components/FilesPane.vue @@ -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(() => {

This file is empty.

diff --git a/web/app/utils/highlight.ts b/web/app/utils/highlight.ts new file mode 100644 index 0000000..e355597 --- /dev/null +++ b/web/app/utils/highlight.ts @@ -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 = { + 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 = { + dockerfile: 'dockerfile', + 'cargo.lock': 'ini', +} + +const ESCAPES: Record = { + '&': '&', + '<': '<', + '>': '>', +} + +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) + } +} diff --git a/web/package.json b/web/package.json index 04d812a..667dfeb 100644 --- a/web/package.json +++ b/web/package.json @@ -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", diff --git a/web/pnpm-lock.yaml b/web/pnpm-lock.yaml index 50506c5..f089558 100644 --- a/web/pnpm-lock.yaml +++ b/web/pnpm-lock.yaml @@ -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: {}