-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtoc.html
More file actions
90 lines (81 loc) · 3.12 KB
/
toc.html
File metadata and controls
90 lines (81 loc) · 3.12 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{{- /* Last modified: 2025-03-08T12:47:53-08:00 */}}
{{- /*
Copyright 2023 Veriphor, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
*/}}
{{- /* Initialize. */}}
{{- $partialName := "toc-walk" }}
{{- /* Determine content path for warning and error messages. */}}
{{- $contentPath := "" }}
{{- with .File }}
{{- $contentPath = .context.Path }}
{{- else }}
{{- $contentPath = .context.Path }}
{{- end }}
{{- /* Check for duplicate heading IDs. */}}
{{- $duplicateIDs := slice }}
{{- range .context.Fragments.Identifiers }}
{{- if gt ($.Fragments.Identifiers.Count .) 1 }}
{{- $duplicateIDs = $duplicateIDs | append . }}
{{- end }}
{{- end }}
{{- with $duplicateIDs | uniq }}
{{- errorf "The %q partial detected duplicate heading IDs (%s) in %s" $partialName (delimit . ", ") $contentPath }}
{{- end }}
{{- /* Render. */}}
{{- if .context.Params.toc }}
{{- with .context.Fragments.Headings }}
{{- $startLevel := or ($.startLevel | int ) ($.context.Param "toc.startLevel" | int) 2 }}
{{- $endLevel := or ($.endLevel | int ) ($.context.Param "toc.endLevel" | int) 3 }}
{{- $numHeadings := where (sort $.context.Fragments.HeadingsMap) "Level" "in" (seq $startLevel $endLevel) | len }}
{{- if ge $numHeadings (or ($.context.Param "toc.minNumHeadings" | int) 2) }}
<nav class="o-aside__toc" aria-labelledby="toc">
<ul>
{{- $ctx := dict
"page" $.context
"contentPath" $contentPath
"partialName" $partialName
"startLevel" $startLevel
"endLevel" $endLevel
"headings" .
}}
{{- partial "inline/toc/walk" $ctx }}
</ul>
</nav>
{{- end }}
{{- end }}
{{- end }}
{{- /* Recursively walk the headings. */}}
{{- define "partials/inline/toc/walk.html" }}
{{- $ctx := . }}
{{- range $ctx.headings }}
{{- if and (ge .Level $ctx.startLevel) (le .Level $ctx.endLevel) }}
<li>
{{- if not .ID }}
{{- errorf "The %q partial detected that the %q heading has an empty ID attribute. See %s" $ctx.partialName .Title $ctx.contentPath }}
{{- end }}
{{- $href := printf "%s#%s" $ctx.page.RelPermalink .ID }}
<a href="{{ $href }}" class="o-aside__toc-link"
>{{ .Title | plainify | safeHTML }}</a
>
{{- with and (lt .Level $ctx.endLevel) .Headings }}
<ul>
{{- $ctx = merge $ctx (dict "headings" .) }}
{{- partial "inline/toc/walk" $ctx }}
</ul>
{{- end }}
</li>
{{- else }}
{{- $ctx = merge $ctx (dict "headings" .Headings) }}
{{- partial "inline/toc/walk" $ctx }}
{{- end }}
{{- end }}
{{- end }}