| 1 |
// Copyright 2026 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package css_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
"github.com/gohugoio/hugo/hugolib" |
| 22 |
"github.com/gohugoio/hugo/tpl/css" |
| 23 |
) |
| 24 |
|
| 25 |
// See issue 15112. |
| 26 |
func TestChromaStyles(t *testing.T) { |
| 27 |
t.Parallel() |
| 28 |
|
| 29 |
files := ` |
| 30 |
-- hugo.toml -- |
| 31 |
disableKinds = ["taxonomy", "term", "rss", "sitemap", "section", "page"] |
| 32 |
[markup.highlight] |
| 33 |
style = "monokai" |
| 34 |
-- layouts/home.html -- |
| 35 |
{{ $light := css.ChromaStyles (dict "targetPath" "css/chroma-light.css" "style" "github" "mode" "light" "modeSelector" true) }} |
| 36 |
{{ $dark := css.ChromaStyles (dict "targetPath" "css/chroma-dark.css" "style" "github" "mode" "dark" "modeSelector" true) }} |
| 37 |
{{ $default := css.ChromaStyles (dict "targetPath" "css/chroma.css") }} |
| 38 |
Light: {{ $light.RelPermalink }}|Dark: {{ $dark.RelPermalink }}|Default: {{ $default.RelPermalink }}| |
| 39 |
` |
| 40 |
|
| 41 |
b := hugolib.Test(t, files) |
| 42 |
|
| 43 |
b.AssertFileContent("public/index.html", "Light: /css/chroma-light.css|Dark: /css/chroma-dark.css|Default: /css/chroma.css|") |
| 44 |
b.AssertFileContent("public/css/chroma-light.css", ".light .chroma ", ".light .bg {") |
| 45 |
b.AssertFileContent("public/css/chroma-dark.css", ".dark .chroma ", ".dark .bg {") |
| 46 |
// Style defaults to markup.highlight.style, monokai background. |
| 47 |
b.AssertFileContent("public/css/chroma.css", "/* Background */ .bg {", "background-color:#272822") |
| 48 |
} |
| 49 |
|
| 50 |
// See issue 15167. |
| 51 |
func TestChromaStylesCustomModeClasses(t *testing.T) { |
| 52 |
t.Parallel() |
| 53 |
|
| 54 |
files := ` |
| 55 |
-- hugo.toml -- |
| 56 |
disableKinds = ["taxonomy", "term", "rss", "sitemap", "section", "page"] |
| 57 |
-- layouts/home.html -- |
| 58 |
{{ $light := css.ChromaStyles (dict "targetPath" "css/chroma-light.css" "style" "github" "mode" "light" "modeSelector" true "classLight" "theme-light") }} |
| 59 |
{{ $dark := css.ChromaStyles (dict "targetPath" "css/chroma-dark.css" "style" "github" "mode" "dark" "modeSelector" true "classDark" "theme-dark") }} |
| 60 |
Light: {{ $light.RelPermalink }}|Dark: {{ $dark.RelPermalink }}| |
| 61 |
` |
| 62 |
|
| 63 |
b := hugolib.Test(t, files) |
| 64 |
|
| 65 |
b.AssertFileContent("public/css/chroma-light.css", ".theme-light .chroma ", ".theme-light .bg {") |
| 66 |
b.AssertFileContent("public/css/chroma-dark.css", ".theme-dark .chroma ", ".theme-dark .bg {") |
| 67 |
} |
| 68 |
|
| 69 |
// Chroma's minifier drops token rules whose color equals the style's default |
| 70 |
// foreground (e.g. .nx in github-dark). In a paired light/dark setup the light |
| 71 |
// sheet's explicit rule would then leak into dark mode, so these must be re-emitted. |
| 72 |
// See issue 15161. |
| 73 |
func TestChromaStylesKeepDefaultForegroundTokens(t *testing.T) { |
| 74 |
t.Parallel() |
| 75 |
|
| 76 |
files := ` |
| 77 |
-- hugo.toml -- |
| 78 |
disableKinds = ["taxonomy", "term", "rss", "sitemap", "section", "page"] |
| 79 |
-- layouts/home.html -- |
| 80 |
{{ $light := css.ChromaStyles (dict "targetPath" "css/chroma-light.css" "style" "github" "mode" "light") }} |
| 81 |
{{ $dark := css.ChromaStyles (dict "targetPath" "css/chroma-dark.css" "style" "github" "mode" "dark" "modeSelector" true) }} |
| 82 |
Light: {{ $light.RelPermalink }}|Dark: {{ $dark.RelPermalink }}| |
| 83 |
` |
| 84 |
|
| 85 |
b := hugolib.Test(t, files) |
| 86 |
|
| 87 |
b.AssertFileContent("public/css/chroma-light.css", "/* NameOther */ .chroma .nx { color:#1f2328 }") |
| 88 |
b.AssertFileContent("public/css/chroma-dark.css", "/* NameOther */ .dark .chroma .nx { color:#e6edf3 }") |
| 89 |
} |
| 90 |
|
| 91 |
func TestChromaStylesErrors(t *testing.T) { |
| 92 |
t.Parallel() |
| 93 |
|
| 94 |
filesTemplate := ` |
| 95 |
-- hugo.toml -- |
| 96 |
disableKinds = ["taxonomy", "term", "rss", "sitemap", "section", "page"] |
| 97 |
-- layouts/home.html -- |
| 98 |
{{ css.ChromaStyles OPTS }} |
| 99 |
` |
| 100 |
|
| 101 |
for _, test := range []struct { |
| 102 |
opts string |
| 103 |
match string |
| 104 |
}{ |
| 105 |
{`(dict "style" "monokai")`, `.*targetPath cannot be empty.*`}, |
| 106 |
{`(dict "targetPath" "css/chroma.css" "style" "doesnotexist")`, `.*invalid style: doesnotexist.*`}, |
| 107 |
{`(dict "targetPath" "css/chroma.css" "mode" "blue")`, `.*invalid mode: blue.*`}, |
| 108 |
{`(dict "targetPath" "css/chroma.css" "style" "friendly" "mode" "dark")`, `.*style "friendly" does not have a "dark" mode.*`}, |
| 109 |
} { |
| 110 |
files := strings.ReplaceAll(filesTemplate, "OPTS", test.opts) |
| 111 |
b, err := hugolib.TestE(t, files) |
| 112 |
b.Assert(err, qt.ErrorMatches, test.match) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func BenchmarkChromaStyles(b *testing.B) { |
| 117 |
files := ` |
| 118 |
-- hugo.toml -- |
| 119 |
disableKinds = ["taxonomy", "term", "rss", "sitemap", "section", "page"] |
| 120 |
-- layouts/all.html -- |
| 121 |
All. |
| 122 |
` |
| 123 |
|
| 124 |
bb := hugolib.Test(b, files) |
| 125 |
|
| 126 |
bb.Assert(true, qt.IsTrue) |
| 127 |
s := bb.H.Sites[0] |
| 128 |
ns := s.TemplateStore.GetTemplateFuncsNamespace("css").(*css.Namespace) |
| 129 |
|
| 130 |
opts := map[string]any{"targetPath": "css/chroma.css", "style": "monokai", "mode": "light", "modeSelector": true} |
| 131 |
|
| 132 |
b.ResetTimer() |
| 133 |
|
| 134 |
for b.Loop() { |
| 135 |
_, err := ns.ChromaStyles(opts) |
| 136 |
bb.Assert(err, qt.IsNil) |
| 137 |
} |
| 138 |
} |