Skip to content

Commit a352e69

Browse files
jmooringbep
authored andcommitted
commands: Validate style argument passed to gen chromastyles
Closes #13357
1 parent 1dd27be commit a352e69

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

commands/gen.go

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"path"
2323
"path/filepath"
24+
"slices"
2425
"strings"
2526

2627
"github.com/alecthomas/chroma/v2"
@@ -60,6 +61,10 @@ func newGenCommand() *genCommand {
6061
See https://xyproto.github.io/splash/docs/all.html for a preview of the available styles`,
6162

6263
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
64+
style = strings.ToLower(style)
65+
if !slices.Contains(styles.Names(), style) {
66+
return fmt.Errorf("invalid style: %s", style)
67+
}
6368
builder := styles.Get(style).Builder()
6469
if highlightStyle != "" {
6570
builder.Add(chroma.LineHighlight, highlightStyle)

testscripts/commands/gen.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ hugo gen man --dir manpages
1111
hugo gen chromastyles -h
1212
stdout 'Generate CSS stylesheet for the Chroma code highlighter'
1313
hugo gen chromastyles --style monokai
14-
stdout '/\* LineHighlight \*/ \.chroma \.hl \{ background-color:#3c3d38 \}'
14+
stdout 'Generated using: hugo gen chromastyles --style monokai'
15+
! hugo gen chromastyles --style __invalid_style__
16+
stderr 'invalid style: __invalid_style__'

0 commit comments

Comments
 (0)