Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(chat): code blocks read in light mode, dark comments lifted

purple7c4d155bc20d
diff --git a/src/components/chat/CodeBlock.tsx b/src/components/chat/CodeBlock.tsx
index 213a90e6..45ae28c1 100644
--- a/src/components/chat/CodeBlock.tsx
+++ b/src/components/chat/CodeBlock.tsx
@@ -6,6 +6,8 @@ import { lazy, Suspense, useState } from 'react'
import SyntaxHighlighter from 'react-syntax-highlighter/dist/esm/prism-light'
// Deep path on purpose: `styles/prism` is a barrel over 46 themes.
import oneDark from 'react-syntax-highlighter/dist/esm/styles/prism/one-dark'
+import a11yOneLight from 'react-syntax-highlighter/dist/esm/styles/prism/a11y-one-light'
+import { useSettingsStore } from '../../stores/settingsStore'
import { Copy, Check, ChevronDown, Play } from 'lucide-react'
// Loaded on the first Preview click only: most chats never open one.
@@ -170,7 +172,34 @@ export function isHtmlSnippet(code: string, language?: string): boolean {
return false
}
+/**
+ * The code colours per mode (3.1.0, P-M). Until then every block used the
+ * dark theme on a 30 % black wash, also in light mode: on white that is a
+ * mid grey under dark-theme colours, and plain code measured 1.02:1 there,
+ * comments 2.9:1. Light mode now takes a11y-one-light, whose every token
+ * reaches 4.5:1 on its pale ground. Dark mode keeps one-dark, with the
+ * comments lifted from 3.0:1 to about 5:1 and the template-string braces
+ * from 3.9:1 (__tests__/code-colours-read.test.ts).
+ */
+const LIFTED_COMMENT = 'hsl(220, 10%, 58%)'
+const lift = (key: string, color = LIFTED_COMMENT) => ({ ...oneDark[key], color })
+const INTERPOLATION = '.language-javascript .token.template-string > .token.interpolation > .token.interpolation-punctuation.punctuation'
+export const CODE_THEME_DARK: typeof oneDark = {
+ ...oneDark,
+ comment: lift('comment'),
+ prolog: lift('prolog'),
+ cdata: lift('cdata'),
+ '.language-markdown .token.blockquote.punctuation': lift('.language-markdown .token.blockquote.punctuation'),
+ '.language-markdown .token.hr.punctuation': lift('.language-markdown .token.hr.punctuation'),
+ // The `${` `}` of a template string, a dim red at 3.9:1; one step lighter.
+ [INTERPOLATION]: lift(INTERPOLATION, 'hsl(5, 60%, 62%)'),
+}
+export const CODE_THEME_LIGHT = a11yOneLight
+/** The ground each theme is read on, over the pane. */
+export const CODE_GROUND = { light: 'rgba(15, 23, 42, 0.035)', dark: 'rgba(0, 0, 0, 0.3)' } as const
+
export function CodeBlock({ code, language }: Props) {
+ const light = useSettingsStore((s) => s.settings.theme === 'light')
const [copied, setCopied] = useState(false)
const lineCount = code.split('\n').length
const isLong = lineCount > COLLAPSE_THRESHOLD
@@ -242,11 +271,11 @@ export function CodeBlock({ code, language }: Props) {
// gets the normalised one, so an unknown fence still shows what the
// model called it while rendering as plain text.
language={highlightLanguageFor(language)}
- style={oneDark}
+ style={light ? CODE_THEME_LIGHT : CODE_THEME_DARK}
customStyle={{
margin: 0,
padding: '0.75rem',
- background: 'rgba(0, 0, 0, 0.3)',
+ background: light ? CODE_GROUND.light : CODE_GROUND.dark,
fontSize: '0.75rem',
}}
// The theme gives the inner <code> its own background. It is an
diff --git a/src/components/chat/__tests__/code-colours-read.test.ts b/src/components/chat/__tests__/code-colours-read.test.ts
new file mode 100644
index 00000000..095d1043
--- /dev/null
+++ b/src/components/chat/__tests__/code-colours-read.test.ts
@@ -0,0 +1,56 @@
+/**
+ * Code blocks read in both modes (3.1.0, P-M).
+ *
+ * Until 3.1.0 every block used the one-dark theme on a 30 % black wash, also
+ * in light mode. On the white pane that is a mid grey (#b3b3b3) under colours
+ * made for a dark ground: plain code measured 1.02:1, comments 2.9:1. Now
+ * each mode has its theme, and every token colour must reach 4.5:1 on the
+ * ground it is really drawn on (the wash over the pane).
+ *
+ * Run: npx vitest run src/components/chat/__tests__/code-colours-read.test.ts
+ */
+import { describe, it, expect } from 'vitest'
+import { CODE_GROUND, CODE_THEME_DARK, CODE_THEME_LIGHT } from '../CodeBlock'
+import { PANE, contrast, type Rgb } from '../../../lib/chat-look'
+
+function parse(c: string): { rgb: Rgb; a: number } | null {
+ let m = /^#([0-9a-f]{6})$/i.exec(c)
+ if (m) return { rgb: { r: parseInt(m[1].slice(0, 2), 16), g: parseInt(m[1].slice(2, 4), 16), b: parseInt(m[1].slice(4, 6), 16) }, a: 1 }
+ m = /^rgba?\(\s*([\d.]+),\s*([\d.]+),\s*([\d.]+)(?:,\s*([\d.]+))?\)$/.exec(c)
+ if (m) return { rgb: { r: +m[1], g: +m[2], b: +m[3] }, a: m[4] === undefined ? 1 : +m[4] }
+ m = /^hsl\(\s*([\d.]+),\s*([\d.]+)%?,\s*([\d.]+)%\)$/.exec(c)
+ if (m) {
+ const h = +m[1] / 360, s = +m[2] / 100, l = +m[3] / 100
+ const f = (n: number) => { const k = (n + h * 12) % 12; const a = s * Math.min(l, 1 - l); return l - a * Math.max(-1, Math.min(k - 3, 9 - k, 1)) }
+ return { rgb: { r: f(0) * 255, g: f(8) * 255, b: f(4) * 255 }, a: 1 }
+ }
+ return null
+}
+
+function over(top: string, under: string): Rgb {
+ const t = parse(top)!
+ const u = parse(under)!.rgb
+ return { r: t.rgb.r * t.a + u.r * (1 - t.a), g: t.rgb.g * t.a + u.g * (1 - t.a), b: t.rgb.b * t.a + u.b * (1 - t.a) }
+}
+
+function weakTokens(theme: Record<string, { color?: string }>, ground: Rgb): string[] {
+ const out: string[] = []
+ for (const [key, rule] of Object.entries(theme)) {
+ // Selection colours and the whitespace markers (::before, alpha) are not text.
+ if (!rule.color || key.includes('selection') || key.includes(':before')) continue
+ const c = parse(rule.color)
+ if (!c) continue
+ const ratio = contrast(c.rgb, ground)
+ if (ratio < 4.5) out.push(`${key} ${rule.color} ${ratio.toFixed(2)}:1`)
+ }
+ return out
+}
+
+describe('code colours read', () => {
+ it('light mode: every token reaches 4.5:1 on its ground', () => {
+ expect(weakTokens(CODE_THEME_LIGHT as Record<string, { color?: string }>, over(CODE_GROUND.light, PANE.light))).toEqual([])
+ })
+ it('dark mode: every token reaches 4.5:1 on its ground', () => {
+ expect(weakTokens(CODE_THEME_DARK as Record<string, { color?: string }>, over(CODE_GROUND.dark, PANE.dark))).toEqual([])
+ })
+})
diff --git a/src/types/react-syntax-highlighter.d.ts b/src/types/react-syntax-highlighter.d.ts
index 89e3dfa9..0b3e252e 100644
--- a/src/types/react-syntax-highlighter.d.ts
+++ b/src/types/react-syntax-highlighter.d.ts
@@ -65,6 +65,13 @@ declare module 'react-syntax-highlighter/dist/esm/styles/prism/one-dark' {
export default style
}
+/** Light mode code colours (3.1.0, P-M): every token meets 4.5:1. */
+declare module 'react-syntax-highlighter/dist/esm/styles/prism/a11y-one-light' {
+ import type { CSSProperties } from 'react'
+ const style: Record<string, CSSProperties>
+ export default style
+}
+
// ── Grammars ───────────────────────────────────────────────────────────────
//
// One block per grammar `CodeBlock.tsx` registers. Listing them individually