diff options
author | Bjørn Erik Pedersen <[email protected]> | 2022-02-25 07:45:37 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2022-02-25 17:22:43 +0100 |
commit | 0f80be341f5c42a50e71ced04c35550b7f1d6bdc (patch) | |
tree | 8788aa759e4a4c7b3e0c2f39427149f8f34d4a23 /markup/highlight/highlight.go | |
parent | 78afdb88ab7dc29a29ea9bde9bf66af055ef44c8 (diff) | |
download | hugo-0f80be341f5c42a50e71ced04c35550b7f1d6bdc.tar.gz hugo-0f80be341f5c42a50e71ced04c35550b7f1d6bdc.zip |
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/highlight.go')
-rw-r--r-- | markup/highlight/highlight.go | 34 |
1 files changed, 8 insertions, 26 deletions
diff --git a/markup/highlight/highlight.go b/markup/highlight/highlight.go index e9cbeb3c9..6013ba1c8 100644 --- a/markup/highlight/highlight.go +++ b/markup/highlight/highlight.go @@ -18,7 +18,6 @@ import ( gohtml "html" "html/template" "io" - "strconv" "strings" "github.com/alecthomas/chroma" @@ -98,6 +97,10 @@ func (h chromaHighlighter) HighlightCodeBlock(ctx hooks.CodeblockContext, opts i return HightlightResult{}, err } + if err := applyOptionsFromCodeBlockContext(ctx, &cfg); err != nil { + return HightlightResult{}, err + } + err := highlight(&b, ctx.Code(), ctx.Lang(), attributes, cfg) if err != nil { return HightlightResult{}, err @@ -116,6 +119,10 @@ func (h chromaHighlighter) RenderCodeblock(w hugio.FlexiWriter, ctx hooks.Codebl return err } + if err := applyOptionsFromCodeBlockContext(ctx, &cfg); err != nil { + return err + } + return highlight(w, ctx.Code(), ctx.Lang(), attributes, cfg) } @@ -133,31 +140,6 @@ func (h HightlightResult) Highlighted() template.HTML { return h.Body } -func (h chromaHighlighter) toHighlightOptionsAttributes(ctx hooks.CodeblockContext) (map[string]interface{}, map[string]interface{}) { - attributes := ctx.Attributes() - if attributes == nil || len(attributes) == 0 { - return nil, nil - } - - options := make(map[string]interface{}) - attrs := make(map[string]interface{}) - - for k, v := range attributes { - klow := strings.ToLower(k) - if chromaHightlightProcessingAttributes[klow] { - options[klow] = v - } else { - attrs[k] = v - } - } - const lineanchorsKey = "lineanchors" - if _, found := options[lineanchorsKey]; !found { - // Set it to the ordinal. - options[lineanchorsKey] = strconv.Itoa(ctx.Ordinal()) - } - return options, attrs -} - func highlight(w hugio.FlexiWriter, code, lang string, attributes []attributes.Attribute, cfg Config) error { var lexer chroma.Lexer if lang != "" { |