Merge pull request #372 from chriswritescode-dev/feature/themed-syntax-highlight

feat(themes): drive code block syntax highlighting from active theme
This commit is contained in:
Chris Scott authored and GitHub committed 2026-09-29 16:29:37 -04:00
commit fa2d794239
11 files changed
+977 -48

No files matched your search

+6
View File
@@ -32,6 +32,12 @@
<img src="docs/images/mobile-repo-session-sheet.webp" alt="Mobile repository and session switcher" height="400" style="border: 1px solid rgba(128,128,128,0.4); margin-left: 12px" />
</p>
## Theme Showcase
Explore all 37 themes, including matching syntax highlighting for Markdown code blocks.
https://github.com/user-attachments/assets/4f155632-4b03-412e-976f-2a02045caf22
## Requirements
OpenCode Manager requires **OpenCode 2.x at 2.0.15 or newer** (bundled 2.0.15); OpenCode 1.x is not supported. Upgrading from 1.x? See the [v1 → v2 migration guide](https://opencode.ai/v2/docs/migrate-v1). OpenCode 2 migrates V1 session history in the background on first start, so older sessions can be incomplete until it finishes; progress is reported at `GET /api/experimental/migration/v1`.
-1
View File
@@ -35,7 +35,6 @@
"cronstrue": "^3.13.0",
"date-fns": "^4.1.0",
"diff": "^8.0.2",
"highlight.js": "^11.11.1",
"lucide-react": "^0.546.0",
"mermaid": "^11.12.2",
"react": "^19.1.1",
@@ -8,7 +8,6 @@ import { Maximize2, X, AlertCircle } from 'lucide-react'
import { CopyButton } from '@/components/ui/copy-button'
import { MarkdownLink } from '@/components/ui/markdown-link'
import { useTheme } from '@/hooks/useTheme'
import 'highlight.js/styles/github-dark.css'
interface TextPartProps {
text: string
+116 -19
View File
@@ -34,6 +34,16 @@
--color-agent-build: #89b5ff;
--color-agent-docs: #ffb224;
--color-agent-ask: #0091ff;
--color-syntax-comment: #8b949e;
--color-syntax-keyword: #ff7b72;
--color-syntax-string: #a5d6ff;
--color-syntax-primitive: #d2a8ff;
--color-syntax-property: #79c0ff;
--color-syntax-type: #ffa657;
--color-syntax-constant: #79c0ff;
--color-syntax-variable: #ffa657;
--color-syntax-operator: #79c0ff;
--color-syntax-punctuation: #d4d4d8;
}
:root:not(.dark) {
@@ -70,6 +80,16 @@
--color-agent-build: #034cff;
--color-agent-docs: #ffb224;
--color-agent-ask: #0091ff;
--color-syntax-comment: #6a737d;
--color-syntax-keyword: #d73a49;
--color-syntax-string: #032f62;
--color-syntax-primitive: #6f42c1;
--color-syntax-property: #005cc5;
--color-syntax-type: #e36209;
--color-syntax-constant: #005cc5;
--color-syntax-variable: #e36209;
--color-syntax-operator: #005cc5;
--color-syntax-punctuation: #6b7280;
}
html {
@@ -357,28 +377,105 @@ input:-webkit-autofill:active {
}
/* Syntax highlighting for markdown code blocks */
@import 'highlight.js/styles/github.css' layer(highlight-light);
@import 'highlight.js/styles/github-dark.css' layer(highlight-dark);
@layer highlight-light {
:root:not(.dark) .hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
background: var(--color-accent);
color: var(--color-foreground);
}
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
background: var(--color-accent);
color: var(--color-foreground);
}
@layer highlight-dark {
.dark .hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
background: var(--color-accent);
color: var(--color-foreground);
.hljs-comment,
.hljs-quote,
.hljs-code,
.hljs-formula,
.hljs-meta {
color: var(--color-syntax-comment);
}
.hljs-keyword,
.hljs-doctag,
.hljs-meta .hljs-keyword,
.hljs-template-tag,
.hljs-selector-tag,
.hljs-name {
color: var(--color-syntax-keyword);
}
.hljs-string,
.hljs-regexp,
.hljs-meta .hljs-string,
.hljs-char.escape_ {
color: var(--color-syntax-string);
}
.hljs-title,
.hljs-title.function_,
.hljs-built_in,
.hljs-section {
color: var(--color-syntax-primitive);
}
.hljs-type,
.hljs-title.class_,
.hljs-title.class_.inherited__ {
color: var(--color-syntax-type);
}
.hljs-literal,
.hljs-number,
.hljs-symbol,
.hljs-bullet,
.hljs-link,
.hljs-variable.language_,
.hljs-variable.constant_ {
color: var(--color-syntax-constant);
}
.hljs-attr,
.hljs-attribute,
.hljs-property,
.hljs-selector-attr,
.hljs-selector-class,
.hljs-selector-id,
.hljs-selector-pseudo {
color: var(--color-syntax-property);
}
.hljs-variable,
.hljs-template-variable,
.hljs-params,
.hljs-subst {
color: var(--color-syntax-variable);
}
.hljs-operator {
color: var(--color-syntax-operator);
}
.hljs-punctuation,
.hljs-tag {
color: var(--color-syntax-punctuation);
}
.hljs-section,
.hljs-strong {
font-weight: 600;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-addition {
color: var(--color-diff-add);
background-color: color-mix(in oklab, var(--color-diff-add) 15%, transparent);
}
.hljs-deletion {
color: var(--color-diff-delete);
background-color: color-mix(in oklab, var(--color-diff-delete) 15%, transparent);
}
}
/* Custom scrollbar styling */
.scrollbar-thin {
+28
View File
@@ -90,6 +90,34 @@ describe('applyColorTheme', () => {
expect(dark?.['destructive-foreground']).toBe(readableTextColor('#ff5555'))
})
it('resolves syntax tokens from upstream overrides and palette fallbacks', () => {
const dark = resolveColorThemeTokens('dracula', true)
expect(dark?.['syntax-keyword']).toBe('#ff79c6')
expect(dark?.['syntax-string']).toBe('#f1fa8c')
expect(dark?.['syntax-comment']).toBe('#6272a4')
expect(dark?.['syntax-primitive']).toBe('#50fa7b')
expect(dark?.['syntax-property']).toBe('#8be9fd')
expect(dark?.['syntax-constant']).toBe('#bd93f9')
expect(dark?.['syntax-variable']).toBe('#f8f8f2')
expect(dark?.['syntax-type']).toBe('#ffb86c')
expect(dark?.['syntax-operator']).toBe(dark?.['muted-foreground'])
expect(dark?.['syntax-punctuation']).toBe(dark?.['muted-foreground'])
})
it('resolves light syntax tokens and falls back to ink for operator and punctuation', () => {
const light = resolveColorThemeTokens('dracula', false)
expect(light?.['syntax-keyword']).toBe('#d16090')
expect(light?.['syntax-punctuation']).toBe('#1f1f2f')
})
it('applies syntax tokens to the root element', () => {
applyColorTheme(document.documentElement, 'dracula', true)
expect(document.documentElement.style.getPropertyValue('--color-syntax-keyword')).toBe('#ff79c6')
})
it('falls back to primary for highlight when the palette has no accent', () => {
const dark = resolveColorThemeTokens('oc-2', true)
+24 -1
View File
@@ -35,6 +35,16 @@ const MANAGER_THEME_TOKENS = [
'agent-build',
'agent-docs',
'agent-ask',
'syntax-comment',
'syntax-keyword',
'syntax-string',
'syntax-primitive',
'syntax-property',
'syntax-type',
'syntax-constant',
'syntax-variable',
'syntax-operator',
'syntax-punctuation',
] as const
type ManagerThemeToken = (typeof MANAGER_THEME_TOKENS)[number]
@@ -98,9 +108,12 @@ export function resolveColorThemeTokens(
diffAdd,
diffDelete,
textWeak,
syntax,
} = isDark ? theme.dark : theme.light
const mix = (percent: number) => `color-mix(in oklab, ${ink} ${percent}%, ${neutral})`
const highlight = paletteAccent ?? primary
const mutedForeground = textWeak ?? mix(62)
const syntaxFallback = isDark ? mutedForeground : ink
return {
background: neutral,
@@ -114,7 +127,7 @@ export function resolveColorThemeTokens(
secondary: mix(10),
border: mix(14),
input: mix(16),
'muted-foreground': textWeak ?? mix(62),
'muted-foreground': mutedForeground,
primary,
'primary-hover': `color-mix(in oklab, ${primary} 85%, ${ink})`,
'primary-foreground': readableTextColor(primary),
@@ -136,6 +149,16 @@ export function resolveColorThemeTokens(
'agent-build': primary,
'agent-docs': warning,
'agent-ask': info,
'syntax-comment': syntax?.comment ?? mutedForeground,
'syntax-keyword': syntax?.keyword ?? highlight,
'syntax-string': syntax?.string ?? success,
'syntax-primitive': syntax?.primitive ?? primary,
'syntax-property': syntax?.property ?? info,
'syntax-type': syntax?.type ?? warning,
'syntax-constant': syntax?.constant ?? highlight,
'syntax-variable': syntax?.variable ?? ink,
'syntax-operator': syntax?.operator ?? syntaxFallback,
'syntax-punctuation': syntax?.punctuation ?? syntaxFallback,
}
}
@@ -0,0 +1,41 @@
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { resolveColorThemeTokens } from './colorTheme'
const themeDir = path.dirname(fileURLToPath(import.meta.url))
const indexCss = readFileSync(path.resolve(themeDir, '../../index.css'), 'utf8')
const textPart = readFileSync(
path.resolve(themeDir, '../../components/message/TextPart.tsx'),
'utf8',
)
function countOccurrences(source: string, needle: string): number {
return source.split(needle).length - 1
}
describe('syntax highlight stylesheet', () => {
it('does not import GitHub highlight.js stylesheets', () => {
expect(indexCss).not.toContain('highlight.js/styles')
expect(textPart).not.toContain('highlight.js/styles')
})
it('does not use highlight-light or highlight-dark cascade layers', () => {
expect(indexCss).not.toContain('@layer highlight-light')
expect(indexCss).not.toContain('@layer highlight-dark')
})
it('maps every syntax token to a theme variable consumed by a .hljs rule', () => {
const dark = resolveColorThemeTokens('dracula', true)
const syntaxKeys = Object.keys(dark ?? {}).filter((key) => key.startsWith('syntax-'))
expect(syntaxKeys.length).toBeGreaterThan(0)
for (const key of syntaxKeys) {
expect(indexCss).toContain(`var(--color-${key})`)
expect(countOccurrences(indexCss, `--color-${key}:`)).toBe(2)
}
})
})
-24
View File
@@ -168,9 +168,6 @@ importers:
diff:
specifier: ^8.0.2
version: 8.0.2
highlight.js:
specifier: ^11.11.1
version: 11.11.1
lucide-react:
specifier: ^0.546.0
version: 0.546.0(react@19.2.3)
@@ -1339,7 +1336,6 @@ packages:
resolution: {integrity: sha512-VZ2sNMw1d/r1SLPjUbOP9LKscKz1CQjID8adTL6gG8Lrrq+mYcIUxutyB+P/eG0J/7oRZLPR6OMt7dUOap6RTg==}
cpu: [arm64]
os: [linux]
libc: [musl]
'@opentui/core-linux-arm64@0.5.12':
resolution: {integrity: sha512-XeKhuIaEtgipvuPHbl4qPOBj+Ut+2zObmsxMVM1jDcjz/FatG9PGeGQPx1G1SnvH2AgpT4K+eCu7DUF0+yIqoQ==}
@@ -1350,7 +1346,6 @@ packages:
resolution: {integrity: sha512-WWW0hVBoSYZ3D6AgZ4u2Y5/u/IyIq2pDb+4yI3WgJ70Wyt6ofHy+6kRGRgbXFn1p+rPInAHjCXD2v6C7iEKSrA==}
cpu: [x64]
os: [linux]
libc: [musl]
'@opentui/core-linux-x64@0.5.12':
resolution: {integrity: sha512-eZiCjEzwbb6qClPPfk32Nha9xmr9obt69Xj0+9SKsXxWLBKkjQEGOMRoh/R9ObaQF4aq8If1xV3VEY0sD9W9vg==}
@@ -1939,67 +1934,56 @@ packages:
resolution: {integrity: sha512-EHMUcDwhtdRGlXZsGSIuXSYwD5kOT9NVnx9sqzYiwAc91wfYOE1g1djOEDseZJKKqtHAHGwnGPQu3kytmfaXLQ==}
cpu: [arm]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-arm-musleabihf@4.54.0':
resolution: {integrity: sha512-+pBrqEjaakN2ySv5RVrj/qLytYhPKEUwk+e3SFU5jTLHIcAtqh2rLrd/OkbNuHJpsBgxsD8ccJt5ga/SeG0JmA==}
cpu: [arm]
os: [linux]
libc: [musl]
'@rollup/rollup-linux-arm64-gnu@4.54.0':
resolution: {integrity: sha512-NSqc7rE9wuUaRBsBp5ckQ5CVz5aIRKCwsoa6WMF7G01sX3/qHUw/z4pv+D+ahL1EIKy6Enpcnz1RY8pf7bjwng==}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-arm64-musl@4.54.0':
resolution: {integrity: sha512-gr5vDbg3Bakga5kbdpqx81m2n9IX8M6gIMlQQIXiLTNeQW6CucvuInJ91EuCJ/JYvc+rcLLsDFcfAD1K7fMofg==}
cpu: [arm64]
os: [linux]
libc: [musl]
'@rollup/rollup-linux-loong64-gnu@4.54.0':
resolution: {integrity: sha512-gsrtB1NA3ZYj2vq0Rzkylo9ylCtW/PhpLEivlgWe0bpgtX5+9j9EZa0wtZiCjgu6zmSeZWyI/e2YRX1URozpIw==}
cpu: [loong64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-ppc64-gnu@4.54.0':
resolution: {integrity: sha512-y3qNOfTBStmFNq+t4s7Tmc9hW2ENtPg8FeUD/VShI7rKxNW7O4fFeaYbMsd3tpFlIg1Q8IapFgy7Q9i2BqeBvA==}
cpu: [ppc64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-riscv64-gnu@4.54.0':
resolution: {integrity: sha512-89sepv7h2lIVPsFma8iwmccN7Yjjtgz0Rj/Ou6fEqg3HDhpCa+Et+YSufy27i6b0Wav69Qv4WBNl3Rs6pwhebQ==}
cpu: [riscv64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-riscv64-musl@4.54.0':
resolution: {integrity: sha512-ZcU77ieh0M2Q8Ur7D5X7KvK+UxbXeDHwiOt/CPSBTI1fBmeDMivW0dPkdqkT4rOgDjrDDBUed9x4EgraIKoR2A==}
cpu: [riscv64]
os: [linux]
libc: [musl]
'@rollup/rollup-linux-s390x-gnu@4.54.0':
resolution: {integrity: sha512-2AdWy5RdDF5+4YfG/YesGDDtbyJlC9LHmL6rZw6FurBJ5n4vFGupsOBGfwMRjBYH7qRQowT8D/U4LoSvVwOhSQ==}
cpu: [s390x]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-x64-gnu@4.54.0':
resolution: {integrity: sha512-WGt5J8Ij/rvyqpFexxk3ffKqqbLf9AqrTBbWDk7ApGUzaIs6V+s2s84kAxklFwmMF/vBNGrVdYgbblCOFFezMQ==}
cpu: [x64]
os: [linux]
libc: [glibc]
'@rollup/rollup-linux-x64-musl@4.54.0':
resolution: {integrity: sha512-JzQmb38ATzHjxlPHuTH6tE7ojnMKM2kYNzt44LO/jJi8BpceEC8QuXYA908n8r3CNuG/B3BV8VR3Hi1rYtmPiw==}
cpu: [x64]
os: [linux]
libc: [musl]
'@rollup/rollup-openharmony-arm64@4.54.0':
resolution: {integrity: sha512-huT3fd0iC7jigGh7n3q/+lfPcXxBi+om/Rs3yiFxjvSxbSB6aohDFXbWvlspaqjeOh+hx7DDHS+5Es5qRkWkZg==}
@@ -2153,28 +2137,24 @@ packages:
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@tailwindcss/oxide-linux-arm64-musl@4.1.18':
resolution: {integrity: sha512-1px92582HkPQlaaCkdRcio71p8bc8i/ap5807tPRDK/uw953cauQBT8c5tVGkOwrHMfc2Yh6UuxaH4vtTjGvHg==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
libc: [musl]
'@tailwindcss/oxide-linux-x64-gnu@4.1.18':
resolution: {integrity: sha512-v3gyT0ivkfBLoZGF9LyHmts0Isc8jHZyVcbzio6Wpzifg/+5ZJpDiRiUhDLkcr7f/r38SWNe7ucxmGW3j3Kb/g==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
libc: [glibc]
'@tailwindcss/oxide-linux-x64-musl@4.1.18':
resolution: {integrity: sha512-bhJ2y2OQNlcRwwgOAGMY0xTFStt4/wyU6pvI6LSuZpRgKQwxTec0/3Scu91O8ir7qCR3AuepQKLU/kX99FouqQ==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
libc: [musl]
'@tailwindcss/oxide-wasm32-wasi@4.1.18':
resolution: {integrity: sha512-LffYTvPjODiP6PT16oNeUQJzNVyJl1cjIebq/rWWBF+3eDst5JGEFSc5cWxyRCJ0Mxl+KyIkqRxk1XPEs9x8TA==}
@@ -3920,28 +3900,24 @@ packages:
engines: {node: '>= 12.0.0'}
cpu: [arm64]
os: [linux]
libc: [glibc]
lightningcss-linux-arm64-musl@1.30.2:
resolution: {integrity: sha512-5Vh9dGeblpTxWHpOx8iauV02popZDsCYMPIgiuw97OJ5uaDsL86cnqSFs5LZkG3ghHoX5isLgWzMs+eD1YzrnA==}
engines: {node: '>= 12.0.0'}
cpu: [arm64]
os: [linux]
libc: [musl]
lightningcss-linux-x64-gnu@1.30.2:
resolution: {integrity: sha512-Cfd46gdmj1vQ+lR6VRTTadNHu6ALuw2pKR9lYq4FnhvgBc4zWY1EtZcAc6EffShbb1MFrIPfLDXD6Xprbnni4w==}
engines: {node: '>= 12.0.0'}
cpu: [x64]
os: [linux]
libc: [glibc]
lightningcss-linux-x64-musl@1.30.2:
resolution: {integrity: sha512-XJaLUUFXb6/QG2lGIW6aIk6jKdtjtcffUT0NKvIqhSBY3hh9Ch+1LCeH80dR9q9LBjG3ewbDjnumefsLsP6aiA==}
engines: {node: '>= 12.0.0'}
cpu: [x64]
os: [linux]
libc: [musl]
lightningcss-win32-arm64-msvc@1.30.2:
resolution: {integrity: sha512-FZn+vaj7zLv//D/192WFFVA0RgHawIcHqLX9xuWiQt7P0PtdFEVaxgF9rjM/IRYHQXNnk61/H/gb2Ei+kUQ4xQ==}
+28 -2
View File
@@ -1,7 +1,13 @@
import { writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { OPENCODE_PINNED_VERSION } from '../shared/src/opencode/release'
import type { HexColor, OpenCodeThemeDefinition, OpenCodeThemePalette } from '../shared/src/themes/types'
import { OPENCODE_SYNTAX_TOKENS } from '../shared/src/themes/types'
import type {
HexColor,
OpenCodeSyntaxPalette,
OpenCodeThemeDefinition,
OpenCodeThemePalette,
} from '../shared/src/themes/types'
const OPENCODE_THEME_TAG = `v${OPENCODE_PINNED_VERSION}`
@@ -75,6 +81,16 @@ function readOptionalHex(value: unknown, field: string): HexColor | undefined {
return value === undefined ? undefined : readHex(value, field)
}
function readSyntax(overrides: Record<string, unknown> | undefined): OpenCodeSyntaxPalette | undefined {
if (!overrides) return undefined
const syntax: OpenCodeSyntaxPalette = {}
for (const token of OPENCODE_SYNTAX_TOKENS) {
const value = overrides[`syntax-${token}`]
if (isHexColor(value)) syntax[token] = value
}
return Object.keys(syntax).length === 0 ? undefined : syntax
}
function readPalette(raw: RawVariant | undefined, field: string): OpenCodeThemePalette {
if (!raw || typeof raw !== 'object' || !raw.palette || typeof raw.palette !== 'object') {
throw new Error(`Missing palette for ${field}`)
@@ -85,6 +101,7 @@ function readPalette(raw: RawVariant | undefined, field: string): OpenCodeThemeP
const diffDelete = readOptionalHex(palette.diffDelete, `${field}.diffDelete`)
const textWeakValue = raw.overrides?.['text-weak']
const textWeak = isHexColor(textWeakValue) ? textWeakValue : undefined
const syntax = readSyntax(raw.overrides)
return {
neutral: readHex(palette.neutral, `${field}.neutral`),
ink: readHex(palette.ink, `${field}.ink`),
@@ -97,6 +114,7 @@ function readPalette(raw: RawVariant | undefined, field: string): OpenCodeThemeP
...(diffAdd === undefined ? {} : { diffAdd }),
...(diffDelete === undefined ? {} : { diffDelete }),
...(textWeak === undefined ? {} : { textWeak }),
...(syntax === undefined ? {} : { syntax }),
}
}
@@ -127,7 +145,7 @@ function quote(value: string): string {
return `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
}
const PALETTE_FIELD_ORDER: (keyof OpenCodeThemePalette)[] = [
const PALETTE_FIELD_ORDER: Exclude<keyof OpenCodeThemePalette, 'syntax'>[] = [
'neutral',
'ink',
'primary',
@@ -147,6 +165,14 @@ function serializePalette(palette: OpenCodeThemePalette, indent: string): string
const value = palette[field]
if (value !== undefined) lines.push(`${indent} ${field}: ${quote(value)},`)
}
if (palette.syntax !== undefined) {
lines.push(`${indent} syntax: {`)
for (const token of OPENCODE_SYNTAX_TOKENS) {
const value = palette.syntax[token]
if (value !== undefined) lines.push(`${indent} ${token}: ${quote(value)},`)
}
lines.push(`${indent} },`)
}
lines.push(`${indent}}`)
return lines.join('\n')
}
File diff suppressed because it is too large. Load diff
+18
View File
@@ -1,5 +1,22 @@
export type HexColor = `#${string}`
export const OPENCODE_SYNTAX_TOKENS = [
'comment',
'keyword',
'string',
'primitive',
'property',
'type',
'constant',
'variable',
'operator',
'punctuation',
] as const
export type OpenCodeSyntaxToken = (typeof OPENCODE_SYNTAX_TOKENS)[number]
export type OpenCodeSyntaxPalette = Partial<Record<OpenCodeSyntaxToken, HexColor>>
export interface OpenCodeThemePalette {
neutral: HexColor
ink: HexColor
@@ -12,6 +29,7 @@ export interface OpenCodeThemePalette {
diffAdd?: HexColor
diffDelete?: HexColor
textWeak?: HexColor
syntax?: OpenCodeSyntaxPalette
}
export interface OpenCodeThemeDefinition {