html: Hide excluded headlines from TOC

emacs org mode did it first
This commit is contained in:
Niklas Fasching 2021-07-13 20:32:47 +02:00
parent e5ae608865
commit abb81851be
3 changed files with 14 additions and 9 deletions

View file

@ -89,6 +89,17 @@ func (h Headline) ID() string {
return fmt.Sprintf("headline-%d", h.Index)
}
func (h Headline) IsExcluded(d *Document) bool {
for _, excludedTag := range strings.Fields(d.Get("EXCLUDE_TAGS")) {
for _, tag := range h.Tags {
if tag == excludedTag {
return true
}
}
}
return false
}
func (parent *Section) add(current *Section) {
if parent.Headline == nil || parent.Headline.Lvl < current.Headline.Lvl {
parent.Children = append(parent.Children, current)

View file

@ -227,7 +227,7 @@ func (w *HTMLWriter) WriteOutline(d *Document, maxLvl int) {
}
func (w *HTMLWriter) writeSection(section *Section, maxLvl int) {
if maxLvl != 0 && section.Headline.Lvl > maxLvl {
if (maxLvl != 0 && section.Headline.Lvl > maxLvl) || section.Headline.IsExcluded(w.document) {
return
}
// NOTE: To satisfy hugo ExtractTOC() check we cannot use `<li>\n` here. Doesn't really matter, just a note.
@ -250,13 +250,9 @@ func (w *HTMLWriter) writeSection(section *Section, maxLvl int) {
}
func (w *HTMLWriter) WriteHeadline(h Headline) {
for _, excludeTag := range strings.Fields(w.document.Get("EXCLUDE_TAGS")) {
for _, tag := range h.Tags {
if excludeTag == tag {
if h.IsExcluded(w.document) {
return
}
}
}
w.WriteString(fmt.Sprintf(`<div id="outline-container-%s" class="outline-%d">`, h.ID(), h.Lvl+1) + "\n")
w.WriteString(fmt.Sprintf(`<h%d id="%s">`, h.Lvl+1, h.ID()) + "\n")

View file

@ -10,8 +10,6 @@
</li>
<li><a href="#headline-5">headline with custom status</a>
</li>
<li><a href="#headline-6">excluded headline</a>
</li>
<li><a href="#headline-7">malformed property drawer</a>
</li>
<li><a href="#headline-8">level limit for headlines to be included in the table of contents</a>