2021-08-27 18:29:50 +01:00
|
|
|
{{- $headers := findRE "<h[1-6].*?>(.|\n])+?</h[1-6]>" .Content -}}
|
|
|
|
{{- $has_headers := ge (len $headers) 1 -}}
|
|
|
|
{{- if $has_headers -}}
|
2021-03-30 11:37:20 +01:00
|
|
|
<div class="toc">
|
|
|
|
<details {{if (.Param "TocOpen") }} open{{ end }}>
|
|
|
|
<summary accesskey="c" title="(Alt + C)">
|
2021-09-03 14:27:36 +01:00
|
|
|
<span class="details">{{- i18n "toc" | default "Table of Contents" }}</span>
|
2021-03-30 11:37:20 +01:00
|
|
|
</summary>
|
2021-03-30 13:37:36 +01:00
|
|
|
|
2021-08-27 18:29:50 +01:00
|
|
|
<div class="inner">
|
2022-01-21 18:55:35 +00:00
|
|
|
{{- if (.Param "UseHugoToc") }}
|
|
|
|
{{- .TableOfContents -}}
|
|
|
|
{{- else }}
|
2021-03-30 11:37:20 +01:00
|
|
|
{{- $largest := 6 -}}
|
|
|
|
{{- range $headers -}}
|
|
|
|
{{- $headerLevel := index (findRE "[1-6]" . 1) 0 -}}
|
|
|
|
{{- $headerLevel := len (seq $headerLevel) -}}
|
|
|
|
{{- if lt $headerLevel $largest -}}
|
|
|
|
{{- $largest = $headerLevel -}}
|
2020-01-07 07:58:33 +00:00
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
2021-03-30 13:37:36 +01:00
|
|
|
|
2021-03-30 11:37:20 +01:00
|
|
|
{{- $firstHeaderLevel := len (seq (index (findRE "[1-6]" (index $headers 0) 1) 0)) -}}
|
2021-03-30 13:37:36 +01:00
|
|
|
|
2021-03-30 11:37:20 +01:00
|
|
|
{{- $.Scratch.Set "bareul" slice -}}
|
|
|
|
<ul>
|
|
|
|
{{- range seq (sub $firstHeaderLevel $largest) -}}
|
|
|
|
<ul>
|
|
|
|
{{- $.Scratch.Add "bareul" (sub (add $largest .) 1) -}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- range $i, $header := $headers -}}
|
|
|
|
{{- $headerLevel := index (findRE "[1-6]" . 1) 0 -}}
|
|
|
|
{{- $headerLevel := len (seq $headerLevel) -}}
|
2021-03-30 13:37:36 +01:00
|
|
|
|
2021-03-30 11:37:20 +01:00
|
|
|
{{/* get id="xyz" */}}
|
|
|
|
{{- $id := index (findRE "(id=\"(.*?)\")" $header 9) 0 }}
|
2021-03-30 13:37:36 +01:00
|
|
|
|
2021-03-30 11:37:20 +01:00
|
|
|
{{- /* strip id="" to leave xyz, no way to get regex capturing groups in hugo */ -}}
|
|
|
|
{{- $cleanedID := replace (replace $id "id=\"" "") "\"" "" }}
|
|
|
|
{{- $header := replaceRE "<h[1-6].*?>((.|\n])+?)</h[1-6]>" "$1" $header -}}
|
2021-03-30 13:37:36 +01:00
|
|
|
|
2021-03-30 11:37:20 +01:00
|
|
|
{{- if ne $i 0 -}}
|
|
|
|
{{- $prevHeaderLevel := index (findRE "[1-6]" (index $headers (sub $i 1)) 1) 0 -}}
|
|
|
|
{{- $prevHeaderLevel := len (seq $prevHeaderLevel) -}}
|
|
|
|
{{- if gt $headerLevel $prevHeaderLevel -}}
|
|
|
|
{{- range seq $prevHeaderLevel (sub $headerLevel 1) -}}
|
|
|
|
<ul>
|
|
|
|
{{/* the first should not be recorded */}}
|
|
|
|
{{- if ne $prevHeaderLevel . -}}
|
|
|
|
{{- $.Scratch.Add "bareul" . -}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- else -}}
|
|
|
|
</li>
|
|
|
|
{{- if lt $headerLevel $prevHeaderLevel -}}
|
|
|
|
{{- range seq (sub $prevHeaderLevel 1) -1 $headerLevel -}}
|
|
|
|
{{- if in ($.Scratch.Get "bareul") . -}}
|
|
|
|
</ul>
|
|
|
|
{{/* manually do pop item */}}
|
|
|
|
{{- $tmp := $.Scratch.Get "bareul" -}}
|
|
|
|
{{- $.Scratch.Delete "bareul" -}}
|
|
|
|
{{- $.Scratch.Set "bareul" slice}}
|
|
|
|
{{- range seq (sub (len $tmp) 1) -}}
|
|
|
|
{{- $.Scratch.Add "bareul" (index $tmp (sub . 1)) -}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- else -}}
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
2021-03-30 13:37:36 +01:00
|
|
|
{{- end }}
|
2021-03-30 11:37:20 +01:00
|
|
|
<li>
|
2021-08-03 06:08:23 +01:00
|
|
|
<a href="#{{- $cleanedID -}}" aria-label="{{- $header | plainify -}}">{{- $header | safeHTML -}}</a>
|
2021-03-30 13:37:36 +01:00
|
|
|
{{- else }}
|
2021-03-30 11:37:20 +01:00
|
|
|
<li>
|
|
|
|
<a href="#{{- $cleanedID -}}" aria-label="{{- $header | plainify -}}">{{- $header | safeHTML -}}</a>
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|
|
|
|
<!-- {{- $firstHeaderLevel := len (seq (index (findRE "[1-6]" (index $headers 0) 1) 0)) -}} -->
|
|
|
|
{{- $firstHeaderLevel := $largest }}
|
2021-03-30 13:37:36 +01:00
|
|
|
{{- $lastHeaderLevel := len (seq (index (findRE "[1-6]" (index $headers (sub (len $headers) 1)) 1) 0)) }}
|
2021-03-30 11:37:20 +01:00
|
|
|
</li>
|
|
|
|
{{- range seq (sub $lastHeaderLevel $firstHeaderLevel) -}}
|
2021-03-30 13:37:36 +01:00
|
|
|
{{- if in ($.Scratch.Get "bareul") (add . $firstHeaderLevel) }}
|
2021-03-30 11:37:20 +01:00
|
|
|
</ul>
|
2021-03-30 13:37:36 +01:00
|
|
|
{{- else }}
|
2021-03-30 11:37:20 +01:00
|
|
|
</ul>
|
2020-01-07 07:58:33 +00:00
|
|
|
</li>
|
2021-03-30 11:37:20 +01:00
|
|
|
{{- end -}}
|
2021-03-30 13:37:36 +01:00
|
|
|
{{- end }}
|
2021-03-30 11:37:20 +01:00
|
|
|
</ul>
|
2022-01-21 18:55:35 +00:00
|
|
|
{{- end }}
|
2021-03-30 11:37:20 +01:00
|
|
|
</div>
|
|
|
|
</details>
|
2021-03-30 13:37:36 +01:00
|
|
|
</div>
|
2021-08-27 18:29:50 +01:00
|
|
|
{{- end }}
|