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(() => {
-{{ file.content }}
+
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