Backtestshugorun-20261002-200313#15167
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.
Correctness is equivalent. Both add ClassDark and ClassLight fields to ChromaStylesOptions. Both default each to "dark"/"light" when empty, wire matching --classDark/--classLight CLI flags in gen.go, and build the prefix from the chosen class. Both rely on the existing dict-to-struct decoding for css.ChromaStyles, so the template path works the same way in each. The difference is in tests. Patch A adds only an integration test with positive assertions for the template function. Patch B adds the same integration test plus negative assertions ("! .light .chroma", "! .dark .chroma"), which confirm the default class is actually replaced rather than added alongside. B also extends the gen_chromastyles_mode testscript to cover the CLI flags the issue explicitly asks for ("gen chromastyles"): custom dark class, custom light class, and no effect without --modeSelector. Patch A leaves the new CLI flags untested. Scope is comparable and minimal in both. On craft, B's per-mode defaulting inside the switch is slightly tidier and its doc comments cross-reference the fields, but this is minor. B wins mainly on more thorough test coverage of both entry points named in the issue.
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
1h 32m
Wall-clock time in the container
Full test suite
No new failures
3,692 passed · 99 skipped · 3,800 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
64vs 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
Our patch+61 −3
commands/gen.goindex f3747722c..f4d74cbc8 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", `CSS class used for dark mode with --modeSelector`)+ _ = cmd.RegisterFlagCompletionFunc("classDark", cobra.NoFileCompletions)+ cmd.PersistentFlags().StringVar(&classLight, "classLight", "light", `CSS class used for light mode with --modeSelector`)+ _ = 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..8f30477db 100644@@ -33,8 +33,17 @@ type ChromaStylesOptions struct { Mode string // Scope selectors under a top level mode class, e.g. ".dark .chroma".+ // See ClassDark and ClassLight. ModeSelector bool + // The CSS class used for dark mode when ModeSelector is enabled.+ // Defaults to "dark".+ ClassDark string++ // The CSS class used for light mode when ModeSelector is enabled.+ // Defaults to "light".+ ClassLight string+ // Foreground and background colors for highlighted lines, e.g. "#fff000 bg:#000fff". HighlightStyle string @@ -105,15 +114,22 @@ 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.- var prefix string+ var class string switch style.Mode() { case chroma.Light:- prefix = ".light "+ class = opts.ClassLight+ if class == "" {+ class = "light"+ } case chroma.Dark:- prefix = ".dark "+ class = opts.ClassDark+ if class == "" {+ class = "dark"+ } default: return "", fmt.Errorf("style %q does not have a light or dark mode", style.Name) }+ prefix := "." + class + " " replacer := strings.NewReplacer( ".bg {", prefix+".bg {", ".chroma ", prefix+".chroma ",testscripts/commands/gen_chromastyles_mode.txtindex 229f5f8cc..381c966b4 100644@@ -22,3 +22,19 @@ stdout '\.light \.chroma' # --modeSelector without --mode on a default dark style. hugo gen chromastyles --style ashen --modeSelector stdout '\.dark \.chroma'++# Custom dark mode class.+hugo gen chromastyles --style github --mode dark --modeSelector --classDark theme-dark+stdout '\.theme-dark \.chroma'+stdout '\.theme-dark \.bg \{'+! stdout '\.dark \.chroma'++# Custom light mode class.+hugo gen chromastyles --style github --mode light --modeSelector --classLight theme-light+stdout '\.theme-light \.chroma'+stdout '\.theme-light \.bg \{'+! stdout '\.light \.chroma'++# Custom mode classes without --modeSelector have no effect.+hugo gen chromastyles --style github --mode dark --classDark theme-dark+! stdout '\.theme-dark \.chroma'tpl/css/chromastyles_integration_test.goindex 402ef2ca6..37e09c994 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 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 {", "! .light .chroma")+ b.AssertFileContent("public/css/chroma-dark.css", ".theme-dark .chroma ", ".theme-dark .bg {", "! .dark .chroma")+}+ // 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.