diff --git a/commands/gen.go b/commands/gen.go index 6ec6a9916..d95f190c1 100644 --- a/commands/gen.go +++ b/commands/gen.go @@ -88,6 +88,7 @@ See https://gohugo.io/quick-reference/syntax-highlighting-styles/ for a preview } options := []html.Option{ html.WithCSSComments(!omitClassComments), + html.WithCustomCSS(chromaCSSOverrides(style)), } formatter := html.New(options...) @@ -275,6 +276,33 @@ url: %s } } +func chromaCSSOverrides(style *chroma.Style) map[chroma.TokenType]string { + bg := style.Get(chroma.Background) + m := make(map[chroma.TokenType]string) + for tt := range chroma.StandardTypes { + if tt == chroma.Background || !style.Has(tt) || !chromaLeafToken(tt) { + continue + } + entry := style.Get(tt) + if !entry.Sub(bg).IsZero() || !entry.Colour.IsSet() { + continue + } + if css := html.StyleEntryToCSS(chroma.StyleEntry{Colour: entry.Colour}); css != "" { + m[tt] = css + } + } + return m +} + +func chromaLeafToken(tt chroma.TokenType) bool { + for other := range chroma.StandardTypes { + if other != tt && (other.Category() == tt || other.SubCategory() == tt) { + return false + } + } + return true +} + type genCommand struct { rootCmd *rootCommand diff --git a/go.mod b/go.mod index 85f182347..13f17ecb6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/gohugoio/hugo require ( github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69 github.com/JohannesKaufmann/html-to-markdown/v2 v2.5.0 - github.com/alecthomas/chroma/v2 v2.23.1 + github.com/alecthomas/chroma/v2 v2.23.2-0.20260408022300-8d04def94bbc github.com/aws/aws-sdk-go-v2 v1.41.6 github.com/aws/aws-sdk-go-v2/service/cloudfront v1.61.1 github.com/bep/clocks v0.5.0 diff --git a/go.sum b/go.sum index 6927eea20..4a3fb7ee9 100644 --- a/go.sum +++ b/go.sum @@ -100,6 +100,8 @@ github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8v github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= github.com/alecthomas/chroma/v2 v2.23.1 h1:nv2AVZdTyClGbVQkIzlDm/rnhk1E9bU9nXwmZ/Vk/iY= github.com/alecthomas/chroma/v2 v2.23.1/go.mod h1:NqVhfBR0lte5Ouh3DcthuUCTUpDC9cxBOfyMbMQPs3o= +github.com/alecthomas/chroma/v2 v2.23.2-0.20260408022300-8d04def94bbc h1:DeAUK41O6QGArswbgBhFnsCQ9cZr6F905IqvuSCQTBs= +github.com/alecthomas/chroma/v2 v2.23.2-0.20260408022300-8d04def94bbc/go.mod h1:NqVhfBR0lte5Ouh3DcthuUCTUpDC9cxBOfyMbMQPs3o= github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs= github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/aws/aws-sdk-go-v2 v1.41.6 h1:1AX0AthnBQzMx1vbmir3Y4WsnJgiydmnJjiLu+LvXOg= diff --git a/markup/highlight/highlight_test.go b/markup/highlight/highlight_test.go index 732dbfa64..5c1406ce9 100644 --- a/markup/highlight/highlight_test.go +++ b/markup/highlight/highlight_test.go @@ -17,6 +17,9 @@ package highlight import ( "testing" + "github.com/alecthomas/chroma/v2" + "github.com/alecthomas/chroma/v2/styles" + qt "github.com/frankban/quicktest" ) @@ -145,3 +148,12 @@ User-Agent: foo c.Assert(result, qt.Contains, "}") }) } + +func TestGitHubDarkStyleIncludesNameOther(t *testing.T) { + c := qt.New(t) + + style := styles.Get("github-dark") + c.Assert(style, qt.Not(qt.IsNil)) + c.Assert(style.Has(chroma.NameOther), qt.Equals, true) + c.Assert(style.Get(chroma.NameOther).Colour.String(), qt.Equals, "#e6edf3") +} diff --git a/testscripts/commands/gen.txt b/testscripts/commands/gen.txt index 136273367..3316219e9 100644 --- a/testscripts/commands/gen.txt +++ b/testscripts/commands/gen.txt @@ -22,8 +22,13 @@ hugo gen chromastyles --style monokai hugo gen chromastyles --omitEmpty --style monokai --logLevel info stderr 'deprecated: --omitEmpty was deprecated' +# Keep same-colour token classes in generated CSS. This is needed when pairing +# styles such as github and github-dark in dual-theme sites. +hugo gen chromastyles --style github-dark +stdout 'NameOther.*\.chroma \.nx \{ color:#e6edf3 \}' + # Disable class comments. hugo gen chromastyles --style monokai stdout 'GenericSubheading' hugo gen chromastyles --omitClassComments --style monokai -! stdout 'GenericSubheading' \ No newline at end of file +! stdout 'GenericSubheading'