Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

feat(settings): add code block appearance controls and renderer

Dryice Liu1ce246a5469a
diff --git a/web/app/(utility)/settings/appearance/page.tsx b/web/app/(utility)/settings/appearance/page.tsx
index 4cfcfff00..000446950 100644
--- a/web/app/(utility)/settings/appearance/page.tsx
+++ b/web/app/(utility)/settings/appearance/page.tsx
@@ -1,18 +1,91 @@
"use client";
+import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
+import type { CodeBlockThemeId } from "@/components/common/code-block-themes";
+import { CODE_BLOCK_THEME_OPTIONS } from "@/components/common/code-block-themes";
+import { Toggle } from "@/components/settings/Toggle";
import { useSettings } from "@/components/settings/SettingsContext";
import { ThemePreviewCard } from "@/components/settings/ThemePreviewCard";
+import {
+ CODE_BLOCK_SETTINGS_EVENT,
+ CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY,
+ CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY,
+ readStoredCodeBlockShowLineNumbers,
+ readStoredCodeBlockWrapLongLines,
+} from "@/context/app-shell-storage";
import {
SettingRow,
SettingSection,
SettingsPageHeader,
+ selectClass,
+ selectOptionClass,
} from "@/components/settings/shared";
export default function AppearanceSettingsPage() {
const { t } = useTranslation();
- const { theme, language, updateTheme, updateLanguage } = useSettings();
+ const {
+ theme,
+ language,
+ codeBlockTheme,
+ updateTheme,
+ updateLanguage,
+ updateCodeBlockTheme,
+ updateCodeBlockShowLineNumbers,
+ updateCodeBlockWrapLongLines,
+ } = useSettings();
+ const [showLineNumbersChecked, setShowLineNumbersChecked] = useState(false);
+ const [wrapLongLinesChecked, setWrapLongLinesChecked] = useState(false);
+
+ useEffect(() => {
+ const syncFromStorage = () => {
+ setShowLineNumbersChecked(readStoredCodeBlockShowLineNumbers());
+ setWrapLongLinesChecked(readStoredCodeBlockWrapLongLines());
+ };
+
+ const onStorage = (event: StorageEvent) => {
+ if (event.key === CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY) {
+ setShowLineNumbersChecked(readStoredCodeBlockShowLineNumbers());
+ }
+ if (event.key === CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY) {
+ setWrapLongLinesChecked(readStoredCodeBlockWrapLongLines());
+ }
+ };
+
+ const onCodeBlockSettings = (event: Event) => {
+ const detail = (
+ event as CustomEvent<{
+ codeBlockShowLineNumbers?: boolean;
+ codeBlockWrapLongLines?: boolean;
+ }>
+ ).detail;
+ if (detail?.codeBlockShowLineNumbers !== undefined) {
+ setShowLineNumbersChecked(Boolean(detail.codeBlockShowLineNumbers));
+ }
+ if (detail?.codeBlockWrapLongLines !== undefined) {
+ setWrapLongLinesChecked(Boolean(detail.codeBlockWrapLongLines));
+ }
+ };
+
+ syncFromStorage();
+ window.addEventListener("storage", onStorage);
+ window.addEventListener(CODE_BLOCK_SETTINGS_EVENT, onCodeBlockSettings);
+ return () => {
+ window.removeEventListener("storage", onStorage);
+ window.removeEventListener(CODE_BLOCK_SETTINGS_EVENT, onCodeBlockSettings);
+ };
+ }, []);
+
+ const handleShowLineNumbersChange = (next: boolean) => {
+ setShowLineNumbersChecked(next);
+ void updateCodeBlockShowLineNumbers(next);
+ };
+
+ const handleWrapLongLinesChange = (next: boolean) => {
+ setWrapLongLinesChecked(next);
+ void updateCodeBlockWrapLongLines(next);
+ };
return (
<div data-tour="tour-appearance">
@@ -87,6 +160,65 @@ export default function AppearanceSettingsPage() {
</p>
</div>
</SettingSection>
+
+ <SettingSection
+ title={t("Code blocks")}
+ description={t(
+ "Choose how code snippets look across the app. Changes apply immediately to saved and streamed responses.",
+ )}
+ >
+ <SettingRow
+ title={t("Syntax theme")}
+ description={t(
+ "Select the Prism theme used for highlighted code blocks.",
+ )}
+ control={
+ <select
+ value={codeBlockTheme}
+ onChange={(event) =>
+ void updateCodeBlockTheme(event.target.value as CodeBlockThemeId)
+ }
+ className={`${selectClass} min-w-[220px] pr-8`}
+ >
+ {CODE_BLOCK_THEME_OPTIONS.map((option) => (
+ <option
+ key={option.id}
+ value={option.id}
+ className={selectOptionClass}
+ >
+ {option.label}
+ </option>
+ ))}
+ </select>
+ }
+ />
+
+ <SettingRow
+ title={t("Show line numbers")}
+ description={t(
+ "Display a gutter with line numbers beside each code block.",
+ )}
+ control={
+ <Toggle
+ checked={showLineNumbersChecked}
+ onChange={handleShowLineNumbersChange}
+ />
+ }
+ />
+
+ <SettingRow
+ title={t("Wrap long lines")}
+ description={t(
+ "Wrap long code lines instead of forcing horizontal scrolling.",
+ )}
+ control={
+ <Toggle
+ checked={wrapLongLinesChecked}
+ onChange={handleWrapLongLinesChange}
+ />
+ }
+ />
+ </SettingSection>
</div>
);
}
diff --git a/web/components/common/RichCodeBlock.tsx b/web/components/common/RichCodeBlock.tsx
index d6c8060bf..a01ebebe2 100644
--- a/web/components/common/RichCodeBlock.tsx
+++ b/web/components/common/RichCodeBlock.tsx
@@ -1,12 +1,35 @@
"use client";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
-import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
+
+import {
+ getCodeBlockTheme,
+ getCodeBlockThemeBackground,
+} from "./code-block-themes";
+import { useAppShell } from "../../context/AppShellContext";
const MONOSPACE =
'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace';
const PLAIN_LANGS = new Set(["", "text", "txt", "plain", "plaintext", "none"]);
+const DEFAULT_CODE_BLOCK_BACKGROUND = "#1f2937";
+const DEFAULT_CODE_BLOCK_FOREGROUND = "#e5e7eb";
+
+function getCodeBlockThemeForeground(
+ style: Record<string, React.CSSProperties>,
+): string | undefined {
+ const codeStyle = style['code[class*="language-"]'];
+ if (codeStyle && typeof codeStyle.color === "string") {
+ return codeStyle.color;
+ }
+
+ const preStyle = style['pre[class*="language-"]'];
+ if (preStyle && typeof preStyle.color === "string") {
+ return preStyle.color;
+ }
+
+ return undefined;
+}
export default function RichCodeBlock({
raw,
@@ -17,57 +40,78 @@ export default function RichCodeBlock({
lang: string;
className?: string;
}) {
+ const {
+ codeBlockTheme,
+ codeBlockShowLineNumbers,
+ codeBlockWrapLongLines,
+ } = useAppShell();
const normalizedLang = (lang || "").toLowerCase();
const isPlain = PLAIN_LANGS.has(normalizedLang);
+ const syntaxTheme = getCodeBlockTheme(codeBlockTheme);
+ const backgroundColor =
+ getCodeBlockThemeBackground(syntaxTheme) ?? DEFAULT_CODE_BLOCK_BACKGROUND;
+ const textColor =
+ getCodeBlockThemeForeground(syntaxTheme) ?? DEFAULT_CODE_BLOCK_FOREGROUND;
+ const syntaxLanguage = isPlain ? "text" : normalizedLang;
return (
<div
- className={`md-code-block overflow-hidden rounded-xl border border-[var(--border)] bg-[#1f2937] ${
+ className={`md-code-block overflow-hidden rounded-xl border border-[var(--border)] ${
className || ""
}`}
+ style={{ backgroundColor, color: textColor }}
>
{!isPlain ? (
- <div className="border-b border-white/10 px-3 py-2 text-[11px] font-medium uppercase tracking-wider text-[#9ca3af]">
+ <div
+ className="border-b border-[var(--border)] px-3 py-2 text-[11px] font-medium uppercase tracking-wider"
+ style={{ color: textColor, opacity: 0.8 }}
+ >
{normalizedLang}
</div>
) : null}
- {isPlain ? (
- // Skip the highlighter entirely for unlabeled / plain blocks so we
- // don't trigger Prism "unknown language" warnings and keep a tidy
- // monospace presentation that matches the highlighted variant.
- <pre
- className="overflow-x-auto p-4 text-sm leading-[1.7] text-[#e5e7eb]"
- style={{ margin: 0, fontFamily: MONOSPACE }}
- >
- <code
- className="md-code-block__code"
- style={{ fontFamily: MONOSPACE }}
- >
- {raw}
- </code>
- </pre>
- ) : (
- <SyntaxHighlighter
- language={normalizedLang}
- style={oneDark}
- PreTag="pre"
- customStyle={{
- margin: 0,
- borderRadius: 0,
- background: "#1f2937",
- padding: "1rem",
- fontSize: "0.875rem",
- lineHeight: "1.7",
- }}
- codeTagProps={{
- className: "md-code-block__code",
- style: { fontFamily: MONOSPACE },
- }}
- wrapLongLines={false}
- >
- {raw}
- </SyntaxHighlighter>
- )}
+ <SyntaxHighlighter
+ language={syntaxLanguage}
+ style={syntaxTheme}
+ PreTag="pre"
+ customStyle={{
+ margin: 0,
+ borderRadius: 0,
+ background: backgroundColor,
+ color: textColor,
+ padding: "1rem",
+ fontSize: "0.875rem",
+ lineHeight: "1.7",
+ overflowX: codeBlockWrapLongLines ? "hidden" : "auto",
+ whiteSpace: codeBlockWrapLongLines ? "pre-wrap" : "pre",
+ overflowWrap: codeBlockWrapLongLines ? "break-word" : "normal",
+ }}
+ codeTagProps={{
+ className: "md-code-block__code",
+ style: {
+ fontFamily: MONOSPACE,
+ ...(codeBlockWrapLongLines ? { overflowWrap: "break-word" } : {}),
+ },
+ }}
+ showLineNumbers={codeBlockShowLineNumbers}
+ wrapLongLines={codeBlockWrapLongLines}
+ // react-syntax-highlighter's highlight.js forces `display:flex` onto
+ // each per-line span when BOTH wrapLongLines and showLineNumbers are
+ // on (so the line-number gutter can align in a column). That flex
+ // layout makes each token span a flex item with the default
+ // `min-width:auto`, which cannot shrink below content size — so a
+ // long unbreakable token overflows the line wrapper and defeats
+ // `overflow-wrap:break-word` set on <pre>/<code>. Overriding the
+ // per-line wrapper back to `display:block` lets the tokens flow as
+ // normal inline content, where `overflow-wrap:break-word` can
+ // actually break long tokens.
+ lineProps={
+ codeBlockWrapLongLines && codeBlockShowLineNumbers
+ ? { style: { display: "block" } }
+ : undefined
+ }
+ >
+ {raw}
+ </SyntaxHighlighter>
</div>
);
}
diff --git a/web/components/common/RichMarkdownRenderer.tsx b/web/components/common/RichMarkdownRenderer.tsx
index bf31721cd..edc08d6f4 100644
--- a/web/components/common/RichMarkdownRenderer.tsx
+++ b/web/components/common/RichMarkdownRenderer.tsx
@@ -542,6 +542,9 @@ export default function RichMarkdownRenderer({
}
if (isMultiline) {
+ // Code-block appearance settings apply only when rich code rendering is enabled.
+ // This static fallback keeps disabled-code surfaces readable without
+ // silently applying syntax theme, line-number, or wrapping preferences.
return (
<div
className={`md-code-block ${gap} overflow-hidden rounded-xl border border-[var(--border)] bg-[#1f2937]`}
diff --git a/web/components/common/SimpleMarkdownRenderer.tsx b/web/components/common/SimpleMarkdownRenderer.tsx
index 0366806f8..f618dde75 100644
--- a/web/components/common/SimpleMarkdownRenderer.tsx
+++ b/web/components/common/SimpleMarkdownRenderer.tsx
@@ -320,10 +320,10 @@ export default function SimpleMarkdownRenderer({
const raw = String(children).replace(/\n$/, "");
if (raw.includes("\n")) {
- // Use the same #1f2937 / #e5e7eb palette as RichMarkdownRenderer and
- // RichCodeBlock so any stream-time fallback from Rich → Simple (or
- // first-paint via Simple before the rich lock engages) looks visually
- // identical to its rich counterpart.
+ // Compatibility fallback: keep multiline code readable when rich
+ // rendering is unavailable. This intentionally does not consume the
+ // code-block theme registry or line-number/wrapping settings; those
+ // apply only through RichCodeBlock when rich code rendering is enabled.
return (
<div
className={`md-code-block ${gap} overflow-hidden rounded-xl border border-[var(--border)] bg-[#1f2937]`}