diff --git a/markup/goldmark/toc_integration_test.go b/markup/goldmark/toc_integration_test.go index f171af600..105e1a744 100644 --- a/markup/goldmark/toc_integration_test.go +++ b/markup/goldmark/toc_integration_test.go @@ -305,101 +305,6 @@ title: home b.AssertFileExists("public/index.html", true) } -func TestTableOfContentsSkippedHeadingLevelsIssue7128(t *testing.T) { - t.Parallel() - - filesTemplate := ` --- hugo.toml -- -disableKinds = ['home','rss','section','sitemap','taxonomy','term'] -TOC_CONFIG --- layouts/page.html -- -{{ .TableOfContents }} --- content/p1.md -- ---- -title: p1 ---- -## Extra-Curriculars - -#### Heading Four Here without Heading Three Before It - -## Technology - -#### Heading Four Here without Heading Three Before It -` - - t.Run("default end level", func(t *testing.T) { - t.Parallel() - - files := strings.ReplaceAll(filesTemplate, "TOC_CONFIG", "") - b := hugolib.Test(t, files) - - b.AssertFileContentExact("public/p1/index.html", ``) - }) - - t.Run("include skipped level", func(t *testing.T) { - t.Parallel() - - files := strings.ReplaceAll(filesTemplate, "TOC_CONFIG", ` -[markup.tableOfContents] -startLevel = 2 -endLevel = -1`) - b := hugolib.Test(t, files) - - b.AssertFileContentExact("public/p1/index.html", ``) - }) - - t.Run("starts at h4", func(t *testing.T) { - t.Parallel() - - files := ` --- hugo.toml -- -disableKinds = ['home','rss','section','sitemap','taxonomy','term'] -[markup.tableOfContents] -startLevel = 2 -endLevel = -1 --- layouts/page.html -- -{{ .TableOfContents }} --- content/p1.md -- ---- -title: p1 ---- -#### Heading Four First - -###### Heading Six After Heading Four -` - - b := hugolib.Test(t, files) - - b.AssertFileContentExact("public/p1/index.html", ``) - }) -} - // Issue 12605 func TestTableOfContentsWithGoldmarkExtras(t *testing.T) { t.Parallel() diff --git a/markup/tableofcontents/tableofcontents.go b/markup/tableofcontents/tableofcontents.go index 271c44e67..6c40c9a59 100644 --- a/markup/tableofcontents/tableofcontents.go +++ b/markup/tableofcontents/tableofcontents.go @@ -195,13 +195,23 @@ func (b *tocBuilder) Build() { func (b *tocBuilder) writeNav(h Headings) { b.s.WriteString("") } func (b *tocBuilder) writeHeadings(level, indent int, h Headings) { - headings := b.collectHeadings(level, h) - hasChildren := len(headings) > 0 + if level < b.startLevel { + for _, h := range h { + b.writeHeadings(level+1, indent, h.Headings) + } + return + } + + if b.stopLevel != -1 && level > b.stopLevel { + return + } + + hasChildren := len(h) > 0 if hasChildren { b.s.WriteString("\n") @@ -213,8 +223,8 @@ func (b *tocBuilder) writeHeadings(level, indent int, h Headings) { } } - for _, h := range headings { - b.writeHeading(h.level, indent+2, h.h) + for _, h := range h { + b.writeHeading(level+1, indent+2, h) } if hasChildren { @@ -229,35 +239,13 @@ func (b *tocBuilder) writeHeadings(level, indent int, h Headings) { } } -type tocHeading struct { - h *Heading - level int -} - -func (b *tocBuilder) collectHeadings(level int, h Headings) []tocHeading { - var out []tocHeading - - for _, h := range h { - if h.IsZero() || level < b.startLevel { - out = append(out, b.collectHeadings(level+1, h.Headings)...) - continue - } - - if b.stopLevel != -1 && level > b.stopLevel { - continue - } - - out = append(out, tocHeading{h: h, level: level}) - } - - return out -} - func (b *tocBuilder) writeHeading(level, indent int, h *Heading) { b.indent(indent) b.s.WriteString("