Backtestshugorun-20260922-141813#15167
Add classDark and classLight to css.ChromaStyles and "gen chromastyles" (default dark/light)
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The core fix is essentially identical in both patches: both add ClassLight/ClassDark options with empty-string defaulting to "light"/"dark", both wire the gen.go flags with the same defaults and completion registration, and both correctly rewrite the prefix in chromastyles.go. So correctness and scope are a wash — neither masks a symptom, both hit the root cause, and both are minimally sized. The deciding factor is tests. The issue explicitly targets two surfaces: css.ChromaStyles AND the "gen chromastyles" command. Both patches add the same tpl/css integration test exercising the template function, but only Patch B also updates testscripts/commands/gen_chromastyles_mode.txt to cover the CLI path with the --classDark/--classLight flags, including negative assertions (! stdout '\.dark \.chroma') that verify the old hardcoded class is actually gone rather than merely that the new one appears. That is exactly the second half of the issue and a stronger, non-tautological test that would fail before and pass after. Patch A leaves the gen command's new flags entirely untested. On craft the two are near-equal (A groups the struct fields beside ModeSelector, slightly nicer; B appends them at the end after OmitClassComments), not enough to offset B's superior coverage. Patch B is the better fix.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
16m 48s30 turns
Wall-clock time in the container
Full test suite
No new failures
3,688 passed · 99 skipped · 3,800 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
55vs 44 merged
Changed lines, ours vs merged
The issue
View on GitHubI argued with @jmooring that it would be very unlikely that e.g. the CSS dark class would collide with existing classes in a project. After thinking a little, I still don't think that is a likely issue, but:
- I suspect there are JS/CSS frameworks with other naming standards for these mode classes (e.g.
theme-dark) - I would be an annoyed Hugo user if I needed to customise this class and had to do ugly string replacements.
- The cost for us adding these 2 new options is small.
The two patches
commands/gen.goindex f3747722c..3d6327b68 100644@@ -51,6 +51,8 @@ func newGenCommand() *genCommand { lineNumbersTableStyle string omitEmpty bool omitClassComments bool+ classLight string+ classDark string ) newChromaStyles := func() simplecobra.Commander {@@ -74,6 +76,8 @@ See https://gohugo.io/quick-reference/syntax-highlighting-styles/ for a preview LineNumbersInlineStyle: lineNumbersInlineStyle, LineNumbersTableStyle: lineNumbersTableStyle, OmitClassComments: omitClassComments,+ ClassLight: classLight,+ ClassDark: classDark, }) if err != nil { return err@@ -91,6 +95,10 @@ See https://gohugo.io/quick-reference/syntax-highlighting-styles/ for a preview _ = cmd.RegisterFlagCompletionFunc("mode", cobra.FixedCompletions([]string{"light", "dark"}, cobra.ShellCompDirectiveNoFileComp)) cmd.PersistentFlags().BoolVar(&modeSelector, "modeSelector", false, `scope selectors under a top level mode class, e.g. ".dark .chroma"`) _ = cmd.RegisterFlagCompletionFunc("modeSelector", cobra.NoFileCompletions)+ cmd.PersistentFlags().StringVar(&classLight, "classLight", "light", `CSS class name for the light mode selector when --modeSelector is set`)+ _ = cmd.RegisterFlagCompletionFunc("classLight", cobra.NoFileCompletions)+ cmd.PersistentFlags().StringVar(&classDark, "classDark", "dark", `CSS class name for the dark mode selector when --modeSelector is set`)+ _ = cmd.RegisterFlagCompletionFunc("classDark", cobra.NoFileCompletions) cmd.PersistentFlags().StringVar(&highlightStyle, "highlightStyle", "", `foreground and background colors for highlighted lines, e.g. --highlightStyle "#fff000 bg:#000fff"`) _ = cmd.RegisterFlagCompletionFunc("highlightStyle", cobra.NoFileCompletions) cmd.PersistentFlags().StringVar(&lineNumbersInlineStyle, "lineNumbersInlineStyle", "", `foreground and background colors for inline line numbers, e.g. --lineNumbersInlineStyle "#fff000 bg:#000fff"`)markup/highlight/chromastyles.goindex c595c780a..2e6e1b782 100644@@ -46,6 +46,12 @@ type ChromaStylesOptions struct { // Omit CSS class comment prefixes in the generated CSS. OmitClassComments bool++ // CSS class name for the light mode selector when ModeSelector is enabled. Defaults to "light".+ ClassLight string++ // CSS class name for the dark mode selector when ModeSelector is enabled. Defaults to "dark".+ ClassDark string } // ChromaStylesCSS generates a CSS stylesheet for the Chroma code highlighter.@@ -105,12 +111,20 @@ func ChromaStylesCSS(opts ChromaStylesOptions) (string, error) { // them with a parent class on the page. // There's no upstream option for this (I think), so do string replacements for now. // TODO(bep) upstream option for this.+ classLight := opts.ClassLight+ if classLight == "" {+ classLight = "light"+ }+ classDark := opts.ClassDark+ if classDark == "" {+ classDark = "dark"+ } var prefix string switch style.Mode() { case chroma.Light:- prefix = ".light "+ prefix = "." + classLight + " " case chroma.Dark:- prefix = ".dark "+ prefix = "." + classDark + " " default: return "", fmt.Errorf("style %q does not have a light or dark mode", style.Name) }testscripts/commands/gen_chromastyles_mode.txtindex 229f5f8cc..d2991fcf6 100644@@ -22,3 +22,13 @@ stdout '\.light \.chroma' # --modeSelector without --mode on a default dark style. hugo gen chromastyles --style ashen --modeSelector stdout '\.dark \.chroma'++# Custom mode selector class for dark mode.+hugo gen chromastyles --style github --mode dark --modeSelector --classDark theme-dark+stdout '\.theme-dark \.chroma'+! stdout '\.dark \.chroma'++# Custom mode selector class for light mode.+hugo gen chromastyles --style github --mode light --modeSelector --classLight theme-light+stdout '\.theme-light \.chroma'+! stdout '\.light \.chroma'tpl/css/chromastyles_integration_test.goindex 402ef2ca6..682f11724 100644@@ -47,6 +47,25 @@ Light: {{ $light.RelPermalink }}|Dark: {{ $dark.RelPermalink }}|Default: {{ $def b.AssertFileContent("public/css/chroma.css", "/* Background */ .bg {", "background-color:#272822") } +// See issue 15167.+func TestChromaStylesCustomModeClasses(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["taxonomy", "term", "rss", "sitemap", "section", "page"]+-- layouts/home.html --+{{ $light := css.ChromaStyles (dict "targetPath" "css/chroma-light.css" "style" "github" "mode" "light" "modeSelector" true "classLight" "theme-light") }}+{{ $dark := css.ChromaStyles (dict "targetPath" "css/chroma-dark.css" "style" "github" "mode" "dark" "modeSelector" true "classDark" "theme-dark") }}+Light: {{ $light.RelPermalink }}|Dark: {{ $dark.RelPermalink }}|+`++ b := hugolib.Test(t, files)++ b.AssertFileContent("public/css/chroma-light.css", ".theme-light .chroma ", ".theme-light .bg {")+ b.AssertFileContent("public/css/chroma-dark.css", ".theme-dark .chroma ", ".theme-dark .bg {")+}+ // Chroma's minifier drops token rules whose color equals the style's default // foreground (e.g. .nx in github-dark). In a paired light/dark setup the light // sheet's explicit rule would then leak into dark mode, so these must be re-emitted.
commands/gen.goindex f3747722c..42b22165d 100644@@ -46,6 +46,8 @@ func newGenCommand() *genCommand { style string mode string modeSelector bool+ classDark string+ classLight string highlightStyle string lineNumbersInlineStyle string lineNumbersTableStyle string@@ -70,6 +72,8 @@ See https://gohugo.io/quick-reference/syntax-highlighting-styles/ for a preview Style: style, Mode: mode, ModeSelector: modeSelector,+ ClassDark: classDark,+ ClassLight: classLight, HighlightStyle: highlightStyle, LineNumbersInlineStyle: lineNumbersInlineStyle, LineNumbersTableStyle: lineNumbersTableStyle,@@ -91,6 +95,10 @@ See https://gohugo.io/quick-reference/syntax-highlighting-styles/ for a preview _ = cmd.RegisterFlagCompletionFunc("mode", cobra.FixedCompletions([]string{"light", "dark"}, cobra.ShellCompDirectiveNoFileComp)) cmd.PersistentFlags().BoolVar(&modeSelector, "modeSelector", false, `scope selectors under a top level mode class, e.g. ".dark .chroma"`) _ = cmd.RegisterFlagCompletionFunc("modeSelector", cobra.NoFileCompletions)+ cmd.PersistentFlags().StringVar(&classDark, "classDark", "dark", `class name used by --modeSelector for dark styles`)+ _ = cmd.RegisterFlagCompletionFunc("classDark", cobra.NoFileCompletions)+ cmd.PersistentFlags().StringVar(&classLight, "classLight", "light", `class name used by --modeSelector for light styles`)+ _ = cmd.RegisterFlagCompletionFunc("classLight", cobra.NoFileCompletions) cmd.PersistentFlags().StringVar(&highlightStyle, "highlightStyle", "", `foreground and background colors for highlighted lines, e.g. --highlightStyle "#fff000 bg:#000fff"`) _ = cmd.RegisterFlagCompletionFunc("highlightStyle", cobra.NoFileCompletions) cmd.PersistentFlags().StringVar(&lineNumbersInlineStyle, "lineNumbersInlineStyle", "", `foreground and background colors for inline line numbers, e.g. --lineNumbersInlineStyle "#fff000 bg:#000fff"`)markup/highlight/chromastyles.goindex c595c780a..f17170ad0 100644@@ -35,6 +35,12 @@ type ChromaStylesOptions struct { // Scope selectors under a top level mode class, e.g. ".dark .chroma". ModeSelector bool + // Class name used by ModeSelector for dark styles, defaults to "dark".+ ClassDark string++ // Class name used by ModeSelector for light styles, defaults to "light".+ ClassLight string+ // Foreground and background colors for highlighted lines, e.g. "#fff000 bg:#000fff". HighlightStyle string @@ -105,12 +111,19 @@ func ChromaStylesCSS(opts ChromaStylesOptions) (string, error) { // them with a parent class on the page. // There's no upstream option for this (I think), so do string replacements for now. // TODO(bep) upstream option for this.+ classDark, classLight := opts.ClassDark, opts.ClassLight+ if classDark == "" {+ classDark = "dark"+ }+ if classLight == "" {+ classLight = "light"+ } var prefix string switch style.Mode() { case chroma.Light:- prefix = ".light "+ prefix = "." + classLight + " " case chroma.Dark:- prefix = ".dark "+ prefix = "." + classDark + " " default: return "", fmt.Errorf("style %q does not have a light or dark mode", style.Name) }tpl/css/chromastyles_integration_test.goindex 402ef2ca6..682f11724 100644@@ -47,6 +47,25 @@ Light: {{ $light.RelPermalink }}|Dark: {{ $dark.RelPermalink }}|Default: {{ $def b.AssertFileContent("public/css/chroma.css", "/* Background */ .bg {", "background-color:#272822") } +// See issue 15167.+func TestChromaStylesCustomModeClasses(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["taxonomy", "term", "rss", "sitemap", "section", "page"]+-- layouts/home.html --+{{ $light := css.ChromaStyles (dict "targetPath" "css/chroma-light.css" "style" "github" "mode" "light" "modeSelector" true "classLight" "theme-light") }}+{{ $dark := css.ChromaStyles (dict "targetPath" "css/chroma-dark.css" "style" "github" "mode" "dark" "modeSelector" true "classDark" "theme-dark") }}+Light: {{ $light.RelPermalink }}|Dark: {{ $dark.RelPermalink }}|+`++ b := hugolib.Test(t, files)++ b.AssertFileContent("public/css/chroma-light.css", ".theme-light .chroma ", ".theme-light .bg {")+ b.AssertFileContent("public/css/chroma-dark.css", ".theme-dark .chroma ", ".theme-dark .bg {")+}+ // Chroma's minifier drops token rules whose color equals the style's default // foreground (e.g. .nx in github-dark). In a paired light/dark setup the light // sheet's explicit rule would then leak into dark mode, so these must be re-emitted.
What the agent reported
The review is clean. The work is complete.
Summary
What was wrong / requested: The Chroma stylesheet generator hardcoded the mode-selector CSS class names as .light and .dark. The reporter wanted these to be user-overridable (some CSS/JS frameworks use conventions like theme-dark, and forcing string replacements is annoying) via two new options classDark/classLight on both the css.ChromaStyles template function and the hugo gen chromastyles CLI command, defaulting to "dark"/"light".
What I changed (production code):
markup/highlight/chromastyles.go— AddedClassLightandClassDarkfields toChromaStylesOptions. In theModeSelectorblock ofChromaStylesCSS, the prefix now derives from these options, defaulting to"light"/"dark"when empty, so existing behavior is unchanged.commands/gen.go— Added--classLight(default "light") and--classDark(default "dark") flags wired intoChromaStylesOptions. (Fixed a stray over-indentation so the file is gofmt-clean.)tpl/css/css.go— no change needed; it squasheshighlight.ChromaStylesOptions, so the newclassLight/classDarkdict keys are accepted automatically.
Tests added (fail before, pass after):
tpl/css/chromastyles_integration_test.go—TestChromaStylesCustomModeClasses: template path withclassLight "theme-light"/classDark "theme-dark", asserting the emitted CSS uses.theme-light .chroma/.theme-dark .chroma.testscripts/commands/gen_chromastyles_mode.txt— CLI cases for--classDark theme-dark/--classLight theme-light, asserting the custom classes appear and the defaults do not.
Test evidence:
- Baseline: the 3 existing chroma tests passed before the change; the new
TestChromaStylesCustomModeClassesfailed before implementation (Got: .light .bgvs expected.theme-light). - After implementation: full
tpl/csspackage passes (24 tests incl.TestChromaStyles,TestChromaStylesKeepDefaultForegroundTokens,TestChromaStylesErrors, and the newTestChromaStylesCustomModeClasses). CLI testscriptsTestCommands/gen_chromastylesandTestCommands/gen_chromastyles_modeboth PASS.gofmt -lreports no formatting issues. - Reviewer confirmed the diff is clean, minimal, defaults preserved, and nothing unrelated changed.
Requirements disposition:
classLightoption oncss.ChromaStyles(default "light") — implemented, covered byTestChromaStylesCustomModeClasses.classDarkoption oncss.ChromaStyles(default "dark") — implemented, covered byTestChromaStylesCustomModeClasses.--classLightflag ongen chromastyles(default "light") — implemented, covered bygen_chromastyles_mode.txt.--classDarkflag ongen chromastyles(default "dark") — implemented, covered bygen_chromastyles_mode.txt.- Defaults unchanged for existing users — verified; existing
.light/.darkassertions inTestChromaStylesand the original CLI cases still pass.
All changes are left uncommitted in the working tree. Note: the auto-generated docs (docs/content/en/commands/hugo_gen_chromastyles.md) were intentionally not regenerated — that is a generated artifact outside the scope of this code fix and not part of the test gate.