blob: 2b6f5321d0cb973956646c30fda7ff3e5fa81522 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<!-- ignore empty links with + -->
{{ $headers := findRE "<h[1-6].*?>(.|\n])+?</h[1-6]>" .Content }}
<!-- at least one header to link to -->
{{ $has_headers := ge (len $headers) 1 }}
<!-- a post can explicitly disable Table of Contents with toc: false -->
{{ $show_toc := (eq $.Params.toc true) }}
{{ if and $has_headers $show_toc }}
<div class="table-of-contents toc bd-callout">
<!-- TOC header -->
<h4 class="text-muted">Table of Contents</h4>
{{ range $headers }}
{{ $header := . }}
{{ range first 1 (findRE "<h[1-6]" $header 1) }}
{{ range findRE "[1-6]" . 1 }}
{{ $next_heading := (int .) }}
<!-- generate li array of the proper depth -->
{{ range seq $next_heading }}
<ul class="toc-h{{ . }}">
{{end}}
{{ $base := ($.Page.File.LogicalName) }}
{{ $anchorId := ($header | plainify | htmlEscape | urlize) }}
{{ $href := delimit (slice $base $anchorId) "#" | string }}
<a href="{{ relref $.Page $href }}">
<li>{{ $header | plainify | htmlEscape }}</li>
</a>
<!-- close list -->
{{ range seq $next_heading }}
</ul>
{{end}}
{{end}}
{{end}}
{{ end }}
</div>
{{ end }}
|