Add TOC to reference pages. (#1419)

I didn't realize the Hugo's TOC support features only work for markdown files.
Since our reference pages are HTML, none of these were getting TOCs. I now roll
my own TOC functionality in that case, to bring up back TOCs for our reference
pages.
This commit is contained in:
Martin Taillefer 2018-06-02 21:56:21 -07:00 committed by GitHub
parent f712e61515
commit 0af7fbd6c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 8 deletions

View File

@ -1,5 +1,5 @@
{{ $toc := (trim .TableOfContents " ") }} {{ $toc := partial "toc.html" (dict "page" .) }}
{{ $needTOC := .Params.toc | (ne $toc "") }} {{ $needTOC := .Scratch.Get "needTOC" }}
</main> </main>
@ -33,7 +33,7 @@
<nav class="toc"> <nav class="toc">
<div class="spacer"></div> <div class="spacer"></div>
<div id="toc" class="directory" role="directory"> <div id="toc" class="directory" role="directory">
{{ .TableOfContents }} {{ $toc }}
</div> </div>
</nav> </nav>
</div> </div>

View File

@ -1,3 +1,6 @@
{{ $toc := partial "toc.html" (dict "page" .) }}
{{ $needTOC := .Scratch.Get "needTOC" }}
<div class="container-fluid"> <div class="container-fluid">
<div class="row row-offcanvas"> <div class="row row-offcanvas">
<div class="col-0 col-md-3 col-xl-2 sidebar-offcanvas"> <div class="col-0 col-md-3 col-xl-2 sidebar-offcanvas">
@ -10,9 +13,6 @@
{{ end }} {{ end }}
</div> </div>
{{ $toc := (trim .TableOfContents " ") }}
{{ $needTOC := .Params.toc | (ne $toc "") }}
{{ if and $needTOC (ne .Params.force_inline_toc true) }} {{ if and $needTOC (ne .Params.force_inline_toc true) }}
<div class="col-12 col-md-9 col-xl-8"> <div class="col-12 col-md-9 col-xl-8">
{{ else }} {{ else }}
@ -44,8 +44,7 @@
{{ if $needTOC }} {{ if $needTOC }}
<nav class="toc-inlined d-xl-none d-print-none" {{ if .Params.force_inline_toc }}style="display:block!important" {{ end }}> <nav class="toc-inlined d-xl-none d-print-none" {{ if .Params.force_inline_toc }}style="display:block!important" {{ end }}>
<div class="directory" role="directory"> <div class="directory" role="directory">
{{ $t := replace .TableOfContents "TableOfContents" "InlinedTableOfContents" }} {{ replace $toc "TableOfContents" "InlinedTableOfContents" | safeHTML }}
{{ safeHTML $t }}
</div> </div>
</nav> </nav>
{{ end }} {{ end }}

39
layouts/partials/toc.html Normal file
View File

@ -0,0 +1,39 @@
{{ $page := .page }}
{{ $toc := (trim $page.TableOfContents " ") }}
{{ $page.Scratch.Set "needTOC" false }}
{{ if (eq $toc "") }}
{{ $headers := findRE "<h[23456].*?id=\".*?\".*?>.*?</h[23456]>" $page.Content }}
{{ $len := len $headers }}
{{ if gt $len 0 }}
{{ $page.Scratch.Set "needTOC" true }}
<nav id="TableOfContents">
<ul>
{{ $page.Scratch.Set "level" 50 }}
{{ range $h := $headers }}
{{ $level := index (index (findRE "<h[23456].*?" $h) 0) 2 | int }}
{{ $id := replaceRE "<h[23456].*?id=\"(.*?)\".*?>.*?</h[23456]>" "$1" $h }}
{{ $title := replaceRE "<h[23456].*?>(.*?)</h[23456]>" "$1" $h }}
{{ $current := $page.Scratch.Get "level" | int }}
{{ if gt $level $current }}
<ul>
{{ else if lt $level $current }}
{{ $delta := sub ($page.Scratch.Get "level") $level }}
{{ range $index, $num := (seq $delta) }}
</ul>
{{ end }}
{{ end }}
<li><a href="#{{ $id }}">{{ $title }}</a></li>
{{ $page.Scratch.Set "level" $level }}
{{ end }}
</ul>
</nav>
{{ end }}
{{ else }}
{{ $page.Scratch.Set "needTOC" true }}
{{ safeHTML $toc }}
{{ end }}