Backtestshugorun-20260917-015623#15167

Issue #15167 Merged fix PR # Run 17 Sep 2026 · run-20260917-015623 gohugoio/hugo

Add classDark and classLight to css.ChromaStyles and "gen chromastyles" (default dark/light)

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Both patches are functionally equivalent at the core: they add ClassDark/ClassLight to ChromaStylesOptions and substitute the class into the modeSelector prefix with an empty-string fallback to "dark"/"light". Both resolve the issue's root cause correctly and both produce identical CSS output in every scenario (CLI default, CLI override, and template-function override). The deciding difference is tests and default handling. (1) The issue explicitly names two surfaces: css.ChromaStyles AND the `gen chromastyles` command. Patch B adds a testscripts/commands/gen_chromastyles_mode.txt CLI test that actually exercises the new commands/gen.go flag wiring (--classDark/--classLight, asserting both the new class appears and the old default does not), whereas Patch A only adds a template-function integration test and leaves the entire gen.go flag path untested — the very code Patch A changed is unverified. Patch B's integration test is otherwise essentially the same as A's. (2) Default handling: Patch B keeps a single source of truth for the default by setting the cobra flag default to "" and resolving ""->dark/light only in ChromaStylesCSS, with the help text documenting `(default "dark")`. Patch A hardcodes "dark"/"light" as the flag default AND repeats the same fallback in ChromaStylesCSS, duplicating the default in two places that could drift. (3) Scope and craft are otherwise comparable; B's extra test file is beneficial coverage, not churn. Net: equal correctness, but B has broader, non-tautological tests covering both affected code paths and marginally cleaner default centralization.

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

10m 50s1 turn

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

54vs 44 merged

Changed lines, ours vs merged

I 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:

  1. I suspect there are JS/CSS frameworks with other naming standards for these mode classes (e.g. theme-dark)
  2. I would be an annoyed Hugo user if I needed to customise this class and had to do ugly string replacements.
  3. The cost for us adding these 2 new options is small.

The two patches

Our patch+52 −2
commands/gen.goindex f3747722c..736cca80f 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", "", `class name for the dark mode selector when --modeSelector is set (default "dark")`)+				_ = cmd.RegisterFlagCompletionFunc("classDark", cobra.NoFileCompletions)+				cmd.PersistentFlags().StringVar(&classLight, "classLight", "", `class name for the light mode selector when --modeSelector is set (default "light")`)+				_ = 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..80374e139 100644@@ -35,6 +35,12 @@ type ChromaStylesOptions struct { 	// Scope selectors under a top level mode class, e.g. ".dark .chroma". 	ModeSelector bool +	// Class name for the dark mode selector, defaults to "dark".+	ClassDark string++	// Class name for the light mode selector, defaults to "light".+	ClassLight string+ 	// Foreground and background colors for highlighted lines, e.g. "#fff000 bg:#000fff". 	HighlightStyle string @@ -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..3100a6103 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 dark class with mode selector.+hugo gen chromastyles --style github --mode dark --modeSelector --classDark theme-dark+stdout '\.theme-dark \.chroma'+! stdout '\.dark \.chroma'++# Custom light class with mode selector.+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..05b32e3ec 100644@@ -47,6 +47,24 @@ Light: {{ $light.RelPermalink }}|Dark: {{ $dark.RelPermalink }}|Default: {{ $def 	b.AssertFileContent("public/css/chroma.css", "/* Background */ .bg {", "background-color:#272822") } +func TestChromaStylesCustomModeClass(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. 
Merged fix · PR #+42 −2
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

Review passed clean. The feature is complete and verified.

Summary

Feature request: Add classDark and classLight options to css.ChromaStyles and hugo gen chromastyles, so users can customize the CSS mode-selector class names (instead of the hardcoded .dark/.light), defaulting to dark/light for backward compatibility.

What was changed (all uncommitted in the working tree):

  1. markup/highlight/chromastyles.go — Added ClassDark and ClassLight string fields to ChromaStylesOptions. In the ModeSelector block, the scoping prefix is now built from these options, falling back to "light"/"dark" when empty — so existing output is byte-identical unless a user opts in.
  1. commands/gen.go — Added --classDark / --classLight persistent string flags, wired them into the ChromaStylesOptions literal, and registered completion funcs following the existing flag pattern. Help text notes the defaults and the --modeSelector dependency.
  1. tpl/css/css.go — No change needed; it embeds ChromaStylesOptions via mapstructure:",squash", so the new dict keys flow through automatically.

Tests added (test-writer only):

  • tpl/css/chromastyles_integration_test.go — TestChromaStylesCustomModeClass asserts custom .theme-light .chroma / .theme-dark .chroma output via the template function.
  • testscripts/commands/gen_chromastyles_mode.txt — new CLI cases for --classDark/--classLight with positive and negative assertions.

Test evidence (./bt-test -run 'TestChromaStyles|TestCommands/gen_chromastyles', parsed from test-report.json):

  • TestChromaStylesCustomModeClass — pass (new regression test)
  • TestCommands/gen_chromastyles_mode — pass (new CLI cases)
  • Previously passing: TestChromaStyles, TestChromaStylesKeepDefaultForegroundTokens, TestChromaStylesErrors, TestCommands/gen_chromastyles — all still pass
  • 0 failures and no build errors across the full compiled tree.

The reviewer confirmed the mapping is correct (light→light, dark→dark, not swapped), defaults preserve backward compatibility, no existing test was weakened, and nothing unrelated changed. (The AGENTS.md/CLAUDE.md deletions in git status pre-date this task and were left untouched.)