@@ -8,18 +8,24 @@ import (
"fmt"
"net/url"
"github.com/unknwon/i18n"
"github.com/yuin/goldmark/ast"
)
func createTOCNode(toc []Header, lang string) ast.Node {
details := NewDetails()
summary := NewSummary()
summary.AppendChild(summary, ast.NewString([]byte(i18n.Tr(lang, "toc"))))
details.AppendChild(details, summary)
sidebar := ast.NewDocument()
sidebar.SetAttributeString("class", []byte("markdown_catalog"))
scrollContainer := ast.NewDocument()
scrollContainer.SetAttributeString("class", []byte("scroll-container"))
toggleContainer := ast.NewDocument()
toggleContainer.SetAttributeString("class", []byte("toggle-container"))
toggleIcon := ast.NewDocument()
toggleIcon.SetAttributeString("class", []byte("icon ri-arrow-drop-left-line"))
container := ast.NewDocument()
container.SetAttributeString("class", []byte("container"))
ul := ast.NewList('-')
details.AppendChild(details, ul)
ul.SetAttributeString("class", []byte("markdown_toc"))
topul:=ul
currentLevel := 6
for _, header := range toc {
if header.Level < currentLevel {
@@ -28,22 +34,34 @@ func createTOCNode(toc []Header, lang string) ast.Node {
}
for _, header := range toc {
for currentLevel > header.Level {
ul = ul.Parent().(*ast.List)
ul = ul.Parent().Parent(). (*ast.List)
currentLevel--
}
for currentLevel < header.Level {
newLi := ast.NewListItem(currentLevel * 2)
newLi.SetAttributeString("class", []byte("no-catalog-li"))
newL := ast.NewList('-')
ul.AppendChild(ul, newL)
newL.SetAttributeString("class", []byte("markdown_toc"))
newLi.AppendChild(newLi, newL)
ul.AppendChild(ul, newLi)
currentLevel++
ul = newL
}
li := ast.NewListItem(currentLevel * 2)
li.SetAttributeString("class", []byte("catalog-li"))
a := ast.NewLink()
a.Destination = []byte(fmt.Sprintf("#%s", url.PathEscape(header.ID)))
a.AppendChild(a, ast.NewString([]byte(header.Text)))
a.SetAttributeString("title", []byte(header.Text))
li.AppendChild(li, a)
ul.AppendChild(ul, li)
}
container.AppendChild(container,topul)
scrollContainer.AppendChild(scrollContainer,container)
toggleContainer.AppendChild(toggleContainer,toggleIcon)
sidebar.AppendChild(sidebar, toggleContainer)
sidebar.AppendChild(sidebar, scrollContainer)
return details
//return details
return sidebar
}