html: Hide excluded headlines from TOC
emacs org mode did it first
This commit is contained in:
parent
e5ae608865
commit
abb81851be
3 changed files with 14 additions and 9 deletions
|
@ -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)
|
||||
|
|
|
@ -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,12 +250,8 @@ 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 {
|
||||
return
|
||||
}
|
||||
}
|
||||
if h.IsExcluded(w.document) {
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteString(fmt.Sprintf(`<div id="outline-container-%s" class="outline-%d">`, h.ID(), h.Lvl+1) + "\n")
|
||||
|
|
2
org/testdata/headlines.html
vendored
2
org/testdata/headlines.html
vendored
|
@ -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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue