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
This commit is contained in:
Bjørn Erik Pedersen 2022-02-25 07:45:37 +01:00
parent 78afdb88ab
commit 0f80be341f
3 changed files with 41 additions and 34 deletions

View file

@ -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)

View file

@ -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 != "" {