mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 07:18:54 +00:00
markup/asciidocext: Fix TOC parsing for asciidoctor-html5s
Closes #15121 Co-authored-by: mike.szewil <szewil_michael@bah.com>
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
89b8c32200
commit
f961093ea9
@@ -1153,3 +1153,59 @@ func validatePublishedSite_20(b *hugolib.IntegrationTestBuilder) {
|
||||
b.AssertFileExists(path, true)
|
||||
}
|
||||
}
|
||||
|
||||
// See issue 15121
|
||||
func TestAsciiDocHTML5BackendTOC(t *testing.T) {
|
||||
if !htesting.IsRealCI() {
|
||||
t.Skip()
|
||||
}
|
||||
if ok, err := asciidocext.Supports(); !ok {
|
||||
t.Skip(err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
resetDefaultAsciiDocExtConfig()
|
||||
})
|
||||
|
||||
files := `
|
||||
-- hugo.toml --
|
||||
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
|
||||
|
||||
[markup.asciidocExt]
|
||||
backend = 'html5s'
|
||||
extensions = ['asciidoctor-html5s']
|
||||
|
||||
[security.exec]
|
||||
allow = ['^asciidoctor$']
|
||||
-- content/p1.adoc --
|
||||
---
|
||||
title: p1
|
||||
---
|
||||
|
||||
:toc:
|
||||
|
||||
## Section 1
|
||||
|
||||
### Section 1.1
|
||||
|
||||
### Section 1.2
|
||||
|
||||
## Section 2
|
||||
-- layouts/page.html --
|
||||
{{ .TableOfContents }}
|
||||
`
|
||||
|
||||
b := hugolib.Test(t, files)
|
||||
|
||||
b.AssertFileContentExact("public/p1/index.html", `<nav id="TableOfContents">
|
||||
<ul>
|
||||
<li><a href="#_section_1">Section 1</a>
|
||||
<ul>
|
||||
<li><a href="#_section_1_1">Section 1.1</a></li>
|
||||
<li><a href="#_section_1_2">Section 1.2</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#_section_2">Section 2</a></li>
|
||||
</ul>
|
||||
</nav>`)
|
||||
}
|
||||
|
||||
@@ -318,7 +318,7 @@ func (a *AsciiDocConverter) extractTOC(src []byte) ([]byte, *tableofcontents.Fra
|
||||
toVisit []*html.Node
|
||||
)
|
||||
f = func(n *html.Node) bool {
|
||||
if n.Type == html.ElementNode && n.Data == "div" && attr(n, "id") == "toc" {
|
||||
if n.Type == html.ElementNode && (n.Data == "div" || n.Data == "nav") && attr(n, "id") == "toc" {
|
||||
toc = parseTOC(n)
|
||||
if !a.Cfg.MarkupConfig().AsciiDocExt.PreserveTOC {
|
||||
n.Parent.RemoveChild(n)
|
||||
@@ -364,7 +364,7 @@ func parseTOC(doc *html.Node) *tableofcontents.Fragments {
|
||||
f = func(n *html.Node, row, level int) {
|
||||
if n.Type == html.ElementNode {
|
||||
switch n.Data {
|
||||
case "ul":
|
||||
case "ul", "ol":
|
||||
if level == 0 {
|
||||
row++
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user