diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-25 07:45:37 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-02-25 17:22:43 +0100 |
commit | 0f80be341f5c42a50e71ced04c35550b7f1d6bdc (patch) | |
tree | 8788aa759e4a4c7b3e0c2f39427149f8f34d4a23 /markup/highlight/config.go | |
parent | 78afdb88ab7dc29a29ea9bde9bf66af055ef44c8 (diff) |
markup/goldmark: Use Ordinal to create default lineanchors
The `Ordinal` starts at 0, so with a `hl-` prefix, this gives `hl-0-1` as a starting point.
Fixes #9567
Diffstat (limited to 'markup/highlight/config.go')
-rw-r--r-- | markup/highlight/config.go | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/markup/highlight/config.go b/markup/highlight/config.go index 86ac02c3d..7b336f580 100644 --- a/markup/highlight/config.go +++ b/markup/highlight/config.go @@ -23,10 +23,19 @@ import ( "github.com/spf13/cast" "github.com/gohugoio/hugo/config" + "github.com/gohugoio/hugo/markup/converter/hooks" "github.com/mitchellh/mapstructure" ) +const ( + lineanchorsKey = "lineanchors" + lineNosKey = "linenos" + hlLinesKey = "hl_lines" + linosStartKey = "linenostart" + noHlKey = "nohl" +) + var DefaultConfig = Config{ // The highlighter style to use. // See https://xyproto.github.io/splash/docs/all.html @@ -38,7 +47,6 @@ var DefaultConfig = Config{ TabWidth: 4, } -// type Config struct { Style string @@ -133,6 +141,16 @@ func applyOptionsFromMap(optsm map[string]interface{}, cfg *Config) error { return mapstructure.WeakDecode(optsm, cfg) } +func applyOptionsFromCodeBlockContext(ctx hooks.CodeblockContext, cfg *Config) error { + if cfg.LineAnchors == "" { + const lineAnchorPrefix = "hl-" + // Set it to the ordinal with a prefix. + cfg.LineAnchors = fmt.Sprintf("%s%d", lineAnchorPrefix, ctx.Ordinal()) + } + + return nil +} + // ApplyLegacyConfig applies legacy config from back when we had // Pygments. func ApplyLegacyConfig(cfg config.Provider, conf *Config) error { @@ -191,13 +209,6 @@ func normalizeHighlightOptions(m map[string]interface{}) { return } - const ( - lineNosKey = "linenos" - hlLinesKey = "hl_lines" - linosStartKey = "linenostart" - noHlKey = "nohl" - ) - baseLineNumber := 1 if v, ok := m[linosStartKey]; ok { baseLineNumber = cast.ToInt(v) |