Skip to content

Commit 10ec183

Browse files
unhappychoiceclaude
andcommitted
fix(frontend): make inline code readable under default 'auto' syntax theme
The inline code `<code>` fallback in MarkdownCode looked up the active syntax highlighter theme directly in SYNTAX_HIGHLIGHTER_THEMES, which has no entry for the default value 'auto'. The lookup returned undefined, so color fell through to '#fff' (white) and background to undefined (transparent), making inline code and fenced code blocks without a language unreadable on light backgrounds. Resolve 'auto' to the effective theme value ('vs' in light mode, 'atomDark' in dark mode) before the metadata lookup, and fall back to 'atomDark' for unknown values so color/background stay consistent. Fixes #749 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 5c680e8 commit 10ec183

3 files changed

Lines changed: 22 additions & 15 deletions

File tree

‎packages/frontend/src/components/Content/MarkdownContent/MarkdownRenderer/MarkdownCode.tsx‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react';
22
import { useSelector } from 'react-redux';
33
import { PrismAsyncLight } from 'react-syntax-highlighter';
4-
import { SYNTAX_HIGHLIGHTER_THEMES } from '../../../../constants';
5-
import { useSyntaxHighlighterTheme } from '../../../../hooks/useSyntaxHighlighterTheme';
4+
import { useSyntaxHighlighterTheme, useSyntaxHighlighterThemeMeta } from '../../../../hooks/useSyntaxHighlighterTheme';
65
import { RootState } from '../../../../store/store';
76
import MermaidRenderer from './MermaidRenderer';
87
import PlantUMLRenderer from './PlantUMLRenderer';
@@ -16,6 +15,7 @@ interface MarkdownCodeProps {
1615
const MarkdownCode: React.FC<MarkdownCodeProps> = ({ inline, className, children, ...props }) => {
1716
const { fontFamilyMonospace, syntaxHighlighterTheme } = useSelector((state: RootState) => state.config);
1817
const syntaxHighlighterStyle = useSyntaxHighlighterTheme(syntaxHighlighterTheme);
18+
const syntaxHighlighterThemeMeta = useSyntaxHighlighterThemeMeta(syntaxHighlighterTheme);
1919

2020
const match = /language-(\w+)/.exec(className || '');
2121
if (match && match[1] === 'mermaid') {
@@ -43,11 +43,9 @@ const MarkdownCode: React.FC<MarkdownCodeProps> = ({ inline, className, children
4343
{...props}
4444
className={className}
4545
style={{
46-
color: SYNTAX_HIGHLIGHTER_THEMES.find(theme => theme.value === syntaxHighlighterTheme)?.type === 'light'
47-
? 'rgba(0, 0, 0, .87)'
48-
: '#fff',
46+
color: syntaxHighlighterThemeMeta.type === 'light' ? 'rgba(0, 0, 0, .87)' : '#fff',
4947
fontFamily: fontFamilyMonospace,
50-
backgroundColor: SYNTAX_HIGHLIGHTER_THEMES.find(theme => theme.value === syntaxHighlighterTheme)?.color,
48+
backgroundColor: syntaxHighlighterThemeMeta.color,
5149
borderRadius: '4px',
5250
}}
5351
>

‎packages/frontend/src/hooks/useSyntaxHighlighterTheme.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ import {
4444
xonokai,
4545
zTouch
4646
} from 'react-syntax-highlighter/dist/esm/styles/prism';
47+
import { SYNTAX_HIGHLIGHTER_THEMES } from '../constants';
48+
49+
export const resolveSyntaxHighlighterThemeValue = (syntaxHighlighterTheme: string, mode: 'light' | 'dark'): string => {
50+
if (syntaxHighlighterTheme === 'auto') {
51+
return mode === 'dark' ? 'atomDark' : 'vs';
52+
}
53+
return syntaxHighlighterTheme;
54+
};
55+
56+
export const useSyntaxHighlighterThemeMeta = (syntaxHighlighterTheme: string) => {
57+
const theme = useTheme();
58+
const resolved = resolveSyntaxHighlighterThemeValue(syntaxHighlighterTheme, theme.palette.mode);
59+
return SYNTAX_HIGHLIGHTER_THEMES.find(t => t.value === resolved)
60+
?? SYNTAX_HIGHLIGHTER_THEMES.find(t => t.value === 'atomDark')!;
61+
};
4762

4863
export const useSyntaxHighlighterTheme = (syntaxHighlighterTheme: string): object => {
4964
const theme = useTheme();
@@ -94,12 +109,6 @@ export const useSyntaxHighlighterTheme = (syntaxHighlighterTheme: string): objec
94109
zTouch,
95110
};
96111

97-
const getSyntaxHighlighterTheme = () => {
98-
if (syntaxHighlighterTheme === 'auto') {
99-
return theme.palette.mode === 'dark' ? atomDark : vs;
100-
}
101-
return themeMap[syntaxHighlighterTheme] || atomDark;
102-
};
103-
104-
return getSyntaxHighlighterTheme();
112+
const resolved = resolveSyntaxHighlighterThemeValue(syntaxHighlighterTheme, theme.palette.mode);
113+
return themeMap[resolved] || atomDark;
105114
};

‎packages/frontend/test/unit/components/Content/MarkdownContent/MarkdownRenderer/__snapshots__/MarkdownCode.test.tsx.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exports[`MarkdownCode should render code block correctly 1`] = `
1111
exports[`MarkdownCode should render inline code correctly 1`] = `
1212
<DocumentFragment>
1313
<code
14-
style="color: rgb(255, 255, 255); font-family: monospace; border-radius: 4px;"
14+
style="color: rgb(255, 255, 255); font-family: monospace; background-color: rgb(29, 31, 33); border-radius: 4px;"
1515
>
1616
console.log("hello");
1717
</code>

0 commit comments

Comments
 (0)