html: Allow limiting level of headlines to be included in toc
The org mode toc OPTION does not just support true/false - it also allows specifying the max headline level [1] to be included in the toc. [1] headline level as seen in org mode - not the html tag level
This commit is contained in:
parent
7a2cd1abb1
commit
f1361615ed
6 changed files with 59 additions and 5 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"html"
|
||||
"log"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
|
@ -184,24 +185,32 @@ func (w *HTMLWriter) WriteFootnotes(d *Document) {
|
|||
|
||||
func (w *HTMLWriter) WriteOutline(d *Document) {
|
||||
if w.document.GetOption("toc") != "nil" && len(d.Outline.Children) != 0 {
|
||||
maxLvl, _ := strconv.Atoi(w.document.GetOption("toc"))
|
||||
w.WriteString("<nav>\n<ul>\n")
|
||||
for _, section := range d.Outline.Children {
|
||||
w.writeSection(section)
|
||||
w.writeSection(section, maxLvl)
|
||||
}
|
||||
w.WriteString("</ul>\n</nav>\n")
|
||||
}
|
||||
}
|
||||
|
||||
func (w *HTMLWriter) writeSection(section *Section) {
|
||||
func (w *HTMLWriter) writeSection(section *Section, maxLvl int) {
|
||||
if maxLvl != 0 && section.Headline.Lvl > maxLvl {
|
||||
return
|
||||
}
|
||||
// NOTE: To satisfy hugo ExtractTOC() check we cannot use `<li>\n` here. Doesn't really matter, just a note.
|
||||
w.WriteString("<li>")
|
||||
h := section.Headline
|
||||
title := cleanHeadlineTitleForHTMLAnchorRegexp.ReplaceAllString(w.WriteNodesAsString(h.Title...), "")
|
||||
w.WriteString(fmt.Sprintf("<a href=\"#%s\">%s</a>\n", h.ID(), title))
|
||||
if len(section.Children) != 0 {
|
||||
hasChildren := false
|
||||
for _, section := range section.Children {
|
||||
hasChildren = hasChildren || maxLvl == 0 || section.Headline.Lvl <= maxLvl
|
||||
}
|
||||
if hasChildren {
|
||||
w.WriteString("<ul>\n")
|
||||
for _, section := range section.Children {
|
||||
w.writeSection(section)
|
||||
w.writeSection(section, maxLvl)
|
||||
}
|
||||
w.WriteString("</ul>\n")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue