| 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 highlight |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"fmt" |
| 19 |
"slices" |
| 20 |
"strings" |
| 21 |
|
| 22 |
"github.com/alecthomas/chroma/v2" |
| 23 |
"github.com/alecthomas/chroma/v2/formatters/html" |
| 24 |
"github.com/alecthomas/chroma/v2/styles" |
| 25 |
) |
| 26 |
|
| 27 |
// ChromaStylesOptions holds options for ChromaStylesCSS. |
| 28 |
type ChromaStylesOptions struct { |
| 29 |
// Highlighter style, e.g. "monokai". |
| 30 |
Style string |
| 31 |
|
| 32 |
// Style mode, "light" or "dark". |
| 33 |
Mode string |
| 34 |
|
| 35 |
// Scope selectors under a top level mode class, e.g. ".dark .chroma". |
| 36 |
ModeSelector bool |
| 37 |
|
| 38 |
// Class name used by ModeSelector for dark styles, defaults to "dark". |
| 39 |
ClassDark string |
| 40 |
|
| 41 |
// Class name used by ModeSelector for light styles, defaults to "light". |
| 42 |
ClassLight string |
| 43 |
|
| 44 |
// Foreground and background colors for highlighted lines, e.g. "#fff000 bg:#000fff". |
| 45 |
HighlightStyle string |
| 46 |
|
| 47 |
// Foreground and background colors for inline line numbers. |
| 48 |
LineNumbersInlineStyle string |
| 49 |
|
| 50 |
// Foreground and background colors for table line numbers. |
| 51 |
LineNumbersTableStyle string |
| 52 |
|
| 53 |
// Omit CSS class comment prefixes in the generated CSS. |
| 54 |
OmitClassComments bool |
| 55 |
} |
| 56 |
|
| 57 |
// ChromaStylesCSS generates a CSS stylesheet for the Chroma code highlighter. |
| 58 |
// This stylesheet is needed if markup.highlight.noClasses is disabled in config. |
| 59 |
func ChromaStylesCSS(opts ChromaStylesOptions) (string, error) { |
| 60 |
name := strings.ToLower(opts.Style) |
| 61 |
if !slices.Contains(styles.Names(), name) { |
| 62 |
return "", fmt.Errorf("invalid style: %s", name) |
| 63 |
} |
| 64 |
var chromaStyle *chroma.Style |
| 65 |
if opts.Mode != "" { |
| 66 |
var mode chroma.Mode |
| 67 |
switch opts.Mode { |
| 68 |
case "light": |
| 69 |
mode = chroma.Light |
| 70 |
case "dark": |
| 71 |
mode = chroma.Dark |
| 72 |
default: |
| 73 |
return "", fmt.Errorf("invalid mode: %s", opts.Mode) |
| 74 |
} |
| 75 |
|
| 76 |
chromaStyle = styles.GetForMode(name, mode) |
| 77 |
if chromaStyle.Mode() != mode { |
| 78 |
return "", fmt.Errorf("style %q does not have a %q mode", name, opts.Mode) |
| 79 |
} |
| 80 |
} else { |
| 81 |
chromaStyle = styles.Get(name) |
| 82 |
} |
| 83 |
builder := chromaStyle.Builder() |
| 84 |
if opts.HighlightStyle != "" { |
| 85 |
builder.Add(chroma.LineHighlight, opts.HighlightStyle) |
| 86 |
} |
| 87 |
if opts.LineNumbersInlineStyle != "" { |
| 88 |
builder.Add(chroma.LineNumbers, opts.LineNumbersInlineStyle) |
| 89 |
} |
| 90 |
if opts.LineNumbersTableStyle != "" { |
| 91 |
builder.Add(chroma.LineNumbersTable, opts.LineNumbersTableStyle) |
| 92 |
} |
| 93 |
style, err := builder.Build() |
| 94 |
if err != nil { |
| 95 |
return "", err |
| 96 |
} |
| 97 |
|
| 98 |
options := []html.Option{ |
| 99 |
html.WithCSSComments(!opts.OmitClassComments), |
| 100 |
html.WithCustomCSS(chromaCSSOverrides(style)), |
| 101 |
} |
| 102 |
|
| 103 |
var buf bytes.Buffer |
| 104 |
if err := html.New(options...).WriteCSS(&buf, style); err != nil { |
| 105 |
return "", err |
| 106 |
} |
| 107 |
css := buf.String() |
| 108 |
if opts.ModeSelector { |
| 109 |
// Scope every selector under a top level mode class, e.g. ".dark .chroma". |
| 110 |
// This allows generating both light and dark stylesheets and toggling |
| 111 |
// them with a parent class on the page. |
| 112 |
// There's no upstream option for this (I think), so do string replacements for now. |
| 113 |
// TODO(bep) upstream option for this. |
| 114 |
classDark, classLight := opts.ClassDark, opts.ClassLight |
| 115 |
if classDark == "" { |
| 116 |
classDark = "dark" |
| 117 |
} |
| 118 |
if classLight == "" { |
| 119 |
classLight = "light" |
| 120 |
} |
| 121 |
var prefix string |
| 122 |
switch style.Mode() { |
| 123 |
case chroma.Light: |
| 124 |
prefix = "." + classLight + " " |
| 125 |
case chroma.Dark: |
| 126 |
prefix = "." + classDark + " " |
| 127 |
default: |
| 128 |
return "", fmt.Errorf("style %q does not have a light or dark mode", style.Name) |
| 129 |
} |
| 130 |
replacer := strings.NewReplacer( |
| 131 |
".bg {", prefix+".bg {", |
| 132 |
".chroma ", prefix+".chroma ", |
| 133 |
) |
| 134 |
css = replacer.Replace(css) |
| 135 |
} |
| 136 |
|
| 137 |
return css, nil |
| 138 |
} |
| 139 |
|
| 140 |
// chromaCSSOverrides re-emits leaf token colors that Chroma's minifier drops |
| 141 |
// because they equal the style's default foreground (the .chroma color). |
| 142 |
// In a paired light/dark setup, the other sheet's explicit rule (e.g. .chroma .nx) |
| 143 |
// would otherwise leak into this mode, since an explicit declaration beats |
| 144 |
// inheritance from .chroma. |
| 145 |
// TODO(bep): replace with an upstream option, see issue 15161. |
| 146 |
func chromaCSSOverrides(style *chroma.Style) map[chroma.TokenType]string { |
| 147 |
bg := style.Get(chroma.Background) |
| 148 |
m := make(map[chroma.TokenType]string) |
| 149 |
for tt := range chroma.StandardTypes { |
| 150 |
if tt == chroma.Background || !style.Has(tt) || !chromaLeafToken(tt) { |
| 151 |
continue |
| 152 |
} |
| 153 |
entry := style.Get(tt) |
| 154 |
if !entry.Sub(bg).IsZero() || !entry.Colour.IsSet() { |
| 155 |
continue |
| 156 |
} |
| 157 |
if css := html.StyleEntryToCSS(chroma.StyleEntry{Colour: entry.Colour}); css != "" { |
| 158 |
m[tt] = css |
| 159 |
} |
| 160 |
} |
| 161 |
return m |
| 162 |
} |
| 163 |
|
| 164 |
func chromaLeafToken(tt chroma.TokenType) bool { |
| 165 |
for other := range chroma.StandardTypes { |
| 166 |
if other != tt && (other.Category() == tt || other.SubCategory() == tt) { |
| 167 |
return false |
| 168 |
} |
| 169 |
} |
| 170 |
return true |
| 171 |
} |