Added optional page table of contents

You can use the 'tableofcontents' front matter option to enable or
disable the display of a table of contents. Setting this option will
override the new site-wide parameter 'tableofcontentswordcount' which
will automatically add a table of contents on pages with more than
the specified number of words. Set to '0' to always show a table of
contents, or set to '-1' to never show a table of contents (the
default behaviour).
This commit is contained in:
Steven Engler
2019-09-12 17:14:59 -04:00
parent 5225065445
commit 53677f19d8
4 changed files with 58 additions and 0 deletions

View File

@ -11,5 +11,21 @@
{{- end }}
</div>
</div>
{{ if or (.Params.tableofcontents) (and (ne .Params.tableofcontents false) (isset .Site.Params "tableofcontentswordcount") (ge .Site.Params.TableOfContentsWordCount 0) (gt .WordCount .Site.Params.TableOfContentsWordCount)) }}
{{/* If the front matter parameter is true, show the toc */}}
{{/* Elif the front matter parameter is false, don't show the toc */}}
{{/* Elif the front matter parameter is not set, show the toc if the site parameter is set, non-negative, and less than the word count */}}
<div class="table-of-contents">
<details open="open">
<summary>
<span class="table-of-contents-title">Table of Contents</span>
</summary>
{{ $toc := .TableOfContents -}}
{{- $toc := replaceRE `<ul>\s?<li>\s?<ul>` `<ul>` $toc -}}
{{- $toc := replaceRE `</ul>\s?</li>\s?</ul>` `</ul>` $toc -}}
{{- safeHTML $toc }}
</details>
</div>
{{ end }}
{{ .Content }}
{{ end }}