markup/tableofcontents: Cast Fragments.ToHTML args to int

Closes #13107
This commit is contained in:
Joe Mooring
2024-12-04 09:21:21 -08:00
committed by Bjørn Erik Pedersen
parent 487bb96474
commit b529859008
5 changed files with 120 additions and 22 deletions

View File

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