mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-25 22:00:58 +02:00
markup/tableofcontents: Cast Fragments.ToHTML args to int
Closes #13107
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
487bb96474
commit
b529859008
@@ -14,11 +14,13 @@
|
||||
package tableofcontents
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gohugoio/hugo/common/collections"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// Empty is an empty ToC.
|
||||
@@ -133,19 +135,30 @@ func (toc *Fragments) addAt(h *Heading, row, level int) {
|
||||
}
|
||||
|
||||
// ToHTML renders the ToC as HTML.
|
||||
func (toc *Fragments) ToHTML(startLevel, stopLevel int, ordered bool) template.HTML {
|
||||
func (toc *Fragments) ToHTML(startLevel, stopLevel any, ordered bool) (template.HTML, error) {
|
||||
if toc == nil {
|
||||
return ""
|
||||
return "", nil
|
||||
}
|
||||
|
||||
iStartLevel, err := cast.ToIntE(startLevel)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("startLevel: %w", err)
|
||||
}
|
||||
|
||||
iStopLevel, err := cast.ToIntE(stopLevel)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("stopLevel: %w", err)
|
||||
}
|
||||
|
||||
b := &tocBuilder{
|
||||
s: strings.Builder{},
|
||||
h: toc.Headings,
|
||||
startLevel: startLevel,
|
||||
stopLevel: stopLevel,
|
||||
startLevel: iStartLevel,
|
||||
stopLevel: iStopLevel,
|
||||
ordered: ordered,
|
||||
}
|
||||
b.Build()
|
||||
return template.HTML(b.s.String())
|
||||
return template.HTML(b.s.String()), nil
|
||||
}
|
||||
|
||||
func (toc Fragments) walk(fn func(*Heading)) {
|
||||
|
Reference in New Issue
Block a user