Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
trickkiste authored Dec 6, 2024
2 parents dc5335a + b8c15f2 commit 801a798
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 26 deletions.
4 changes: 2 additions & 2 deletions common/maps/scratch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestScratchSetInMap(t *testing.T) {
scratch.SetInMap("key", "zyx", "Zyx")
scratch.SetInMap("key", "abc", "Abc (updated)")
scratch.SetInMap("key", "def", "Def")
c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []any{0: "Abc (updated)", 1: "Def", 2: "Lux", 3: "Zyx"})
c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, any([]any{"Abc (updated)", "Def", "Lux", "Zyx"}))
}

func TestScratchDeleteInMap(t *testing.T) {
Expand All @@ -199,7 +199,7 @@ func TestScratchDeleteInMap(t *testing.T) {
scratch.DeleteInMap("key", "abc")
scratch.SetInMap("key", "def", "Def")
scratch.DeleteInMap("key", "lmn") // Do nothing
c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []any{0: "Def", 1: "Lux", 2: "Zyx"})
c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, any([]any{"Def", "Lux", "Zyx"}))
}

func TestScratchGetSortedMapValues(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/bep/debounce v1.2.0
github.com/bep/gitmap v1.6.0
github.com/bep/goat v0.5.0
github.com/bep/godartsass/v2 v2.3.1
github.com/bep/godartsass/v2 v2.3.2
github.com/bep/golibsass v1.2.0
github.com/bep/gowebp v0.3.0
github.com/bep/helpers v0.5.0
Expand Down Expand Up @@ -162,7 +162,7 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.35.1 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
howett.net/plist v1.0.0 // indirect
software.sslmate.com/src/go-pkcs12 v0.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ github.com/bep/goat v0.5.0 h1:S8jLXHCVy/EHIoCY+btKkmcxcXFd34a0Q63/0D4TKeA=
github.com/bep/goat v0.5.0/go.mod h1:Md9x7gRxiWKs85yHlVTvHQw9rg86Bm+Y4SuYE8CTH7c=
github.com/bep/godartsass/v2 v2.3.1 h1:Lh9v0IxAaB+toyZrZ+XYNYcGXI1PzGzvZ4u8tRWTnLw=
github.com/bep/godartsass/v2 v2.3.1/go.mod h1:AcP8QgC+OwOXEq6im0WgDRYK7scDsmZCEW62o1prQLo=
github.com/bep/godartsass/v2 v2.3.2 h1:meuc76J1C1soSCAnlnJRdGqJ5S4m6/GW+8hmOe9tOog=
github.com/bep/godartsass/v2 v2.3.2/go.mod h1:Qe5WOS9nVJy7G0jHssXPd3c+Pqk/f7+Tm6k/vahbVgs=
github.com/bep/golibsass v1.2.0 h1:nyZUkKP/0psr8nT6GR2cnmt99xS93Ji82ZD9AgOK6VI=
github.com/bep/golibsass v1.2.0/go.mod h1:DL87K8Un/+pWUS75ggYv41bliGiolxzDKWJAq3eJ1MA=
github.com/bep/gowebp v0.3.0 h1:MhmMrcf88pUY7/PsEhMgEP0T6fDUnRTMpN8OclDrbrY=
Expand Down Expand Up @@ -852,6 +854,8 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
13 changes: 6 additions & 7 deletions hugolib/page__content.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,16 +730,15 @@ func (c *cachedContentScope) contentToC(ctx context.Context) (contentTableOfCont
isHTML := cp.po.p.m.pageConfig.ContentMediaType.IsHTML()

if !isHTML {
createAndSetToC := func(tocProvider converter.TableOfContentsProvider) {
createAndSetToC := func(tocProvider converter.TableOfContentsProvider) error {
cfg := p.s.ContentSpec.Converters.GetMarkupConfig()
ct.tableOfContents = tocProvider.TableOfContents()
ct.tableOfContentsHTML = template.HTML(
ct.tableOfContents.ToHTML(
cfg.TableOfContents.StartLevel,
cfg.TableOfContents.EndLevel,
cfg.TableOfContents.Ordered,
),
ct.tableOfContentsHTML, err = ct.tableOfContents.ToHTML(
cfg.TableOfContents.StartLevel,
cfg.TableOfContents.EndLevel,
cfg.TableOfContents.Ordered,
)
return err
}

// If the converter supports doing the parsing separately, we do that.
Expand Down
4 changes: 2 additions & 2 deletions markup/goldmark/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ unsafe = true

toc, ok := b.(converter.TableOfContentsProvider)
c.Assert(ok, qt.Equals, true)
tocString := string(toc.TableOfContents().ToHTML(1, 2, false))
c.Assert(tocString, qt.Contains, "TableOfContents")
tocHTML, _ := toc.TableOfContents().ToHTML(1, 2, false)
c.Assert(string(tocHTML), qt.Contains, "TableOfContents")
}

func TestConvertAutoIDAsciiOnly(t *testing.T) {
Expand Down
23 changes: 18 additions & 5 deletions markup/tableofcontents/tableofcontents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)) {
Expand Down
78 changes: 78 additions & 0 deletions markup/tableofcontents/tableofcontents_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tableofcontents_test

import (
"strings"
"testing"

"github.com/gohugoio/hugo/hugolib"
Expand Down Expand Up @@ -43,3 +44,80 @@ disableKinds = ['page','rss','section','sitemap','taxonomy','term']
"heading-l5|5|Heading L5",
)
}

// Issue #13107
func TestToHTMLArgTypes(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
disableKinds = ['home','section','rss','sitemap','taxonomy','term']
-- layouts/_default/single.html --
{{ .Fragments.ToHTML .Params.toc.startLevel .Params.toc.endLevel false }}
-- content/json.md --
{
"title": "json",
"params": {
"toc": {
"startLevel": 2,
"endLevel": 4
}
}
}
CONTENT
-- content/toml.md --
+++
title = 'toml'
[params.toc]
startLevel = 2
endLevel = 4
+++
CONTENT
-- content/yaml.md --
---
title: yaml
params:
toc:
startLevel: 2
endLevel: 4
---
CONTENT
`

content := `
# Level One
## Level Two
### Level Three
#### Level Four
##### Level Five
###### Level Six
`

want := `
<nav id="TableOfContents">
<ul>
<li><a href="#level-two">Level Two</a>
<ul>
<li><a href="#level-three">Level Three</a>
<ul>
<li><a href="#level-four">Level Four</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
`

files = strings.ReplaceAll(files, "CONTENT", content)

b := hugolib.Test(t, files)
b.AssertFileContentEquals("public/json/index.html", strings.TrimSpace(want))
b.AssertFileContentEquals("public/toml/index.html", strings.TrimSpace(want))
b.AssertFileContentEquals("public/yaml/index.html", strings.TrimSpace(want))

files = strings.ReplaceAll(files, `2`, `"x"`)

b, _ = hugolib.TestE(t, files)
b.AssertLogMatches(`error calling ToHTML: startLevel: unable to cast "x" of type string`)
}
24 changes: 16 additions & 8 deletions markup/tableofcontents/tableofcontents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func TestToc(t *testing.T) {
toc.addAt(&Heading{Title: "1-H3-1", ID: "1-h2-2"}, 0, 2)
toc.addAt(&Heading{Title: "Heading 2", ID: "h1-2"}, 1, 0)

got := string(toc.ToHTML(1, -1, false))
tocHTML, _ := toc.ToHTML(1, -1, false)
got := string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#h1-1">Heading 1</a>
Expand All @@ -62,15 +63,17 @@ func TestToc(t *testing.T) {
</ul>
</nav>`, qt.Commentf(got))

got = string(toc.ToHTML(1, 1, false))
tocHTML, _ = toc.ToHTML(1, 1, false)
got = string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#h1-1">Heading 1</a></li>
<li><a href="#h1-2">Heading 2</a></li>
</ul>
</nav>`, qt.Commentf(got))

got = string(toc.ToHTML(1, 2, false))
tocHTML, _ = toc.ToHTML(1, 2, false)
got = string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#h1-1">Heading 1</a>
Expand All @@ -83,15 +86,17 @@ func TestToc(t *testing.T) {
</ul>
</nav>`, qt.Commentf(got))

got = string(toc.ToHTML(2, 2, false))
tocHTML, _ = toc.ToHTML(2, 2, false)
got = string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#1-h2-1">1-H2-1</a></li>
<li><a href="#1-h2-2">1-H2-2</a></li>
</ul>
</nav>`, qt.Commentf(got))

got = string(toc.ToHTML(1, -1, true))
tocHTML, _ = toc.ToHTML(1, -1, true)
got = string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ol>
<li><a href="#h1-1">Heading 1</a>
Expand All @@ -118,7 +123,8 @@ func TestTocMissingParent(t *testing.T) {
toc.addAt(&Heading{Title: "H3", ID: "h3"}, 1, 2)
toc.addAt(&Heading{Title: "H3", ID: "h3"}, 1, 2)

got := string(toc.ToHTML(1, -1, false))
tocHTML, _ := toc.ToHTML(1, -1, false)
got := string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li>
Expand All @@ -139,15 +145,17 @@ func TestTocMissingParent(t *testing.T) {
</ul>
</nav>`, qt.Commentf(got))

got = string(toc.ToHTML(3, 3, false))
tocHTML, _ = toc.ToHTML(3, 3, false)
got = string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ul>
<li><a href="#h3">H3</a></li>
<li><a href="#h3">H3</a></li>
</ul>
</nav>`, qt.Commentf(got))

got = string(toc.ToHTML(1, -1, true))
tocHTML, _ = toc.ToHTML(1, -1, true)
got = string(tocHTML)
c.Assert(got, qt.Equals, `<nav id="TableOfContents">
<ol>
<li>
Expand Down

0 comments on commit 801a798

Please sign in to comment.