mirror of
https://github.com/Wonderfall/hugo-WonderMod.git
synced 2024-11-25 12:01:37 +01:00
toc(partial): generate toc for h1 - h6
previously: h1-h4
This commit is contained in:
parent
35788daad1
commit
a34bcbd74c
@ -1,17 +1,17 @@
|
|||||||
{{- $headers := findRE "<h[1-4].*?>(.|\n])+?</h[1-4]>" .Content -}}
|
{{- $headers := findRE "<h[1-6].*?>(.|\n])+?</h[1-6]>" .Content -}}
|
||||||
{{- $has_headers := ge (len $headers) 1 -}}
|
{{- $has_headers := ge (len $headers) 1 -}}
|
||||||
{{- if $has_headers -}}
|
{{- if $has_headers -}}
|
||||||
|
|
||||||
{{- $largest := 6 -}}
|
{{- $largest := 6 -}}
|
||||||
{{- range $headers -}}
|
{{- range $headers -}}
|
||||||
{{- $headerLevel := index (findRE "[1-4]" . 1) 0 -}}
|
{{- $headerLevel := index (findRE "[1-6]" . 1) 0 -}}
|
||||||
{{- $headerLevel := len (seq $headerLevel) -}}
|
{{- $headerLevel := len (seq $headerLevel) -}}
|
||||||
{{- if lt $headerLevel $largest -}}
|
{{- if lt $headerLevel $largest -}}
|
||||||
{{- $largest = $headerLevel -}}
|
{{- $largest = $headerLevel -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
|
||||||
{{- $firstHeaderLevel := len (seq (index (findRE "[1-4]" (index $headers 0) 1) 0)) -}}
|
{{- $firstHeaderLevel := len (seq (index (findRE "[1-6]" (index $headers 0) 1) 0)) -}}
|
||||||
|
|
||||||
{{- $.Scratch.Set "bareul" slice -}}
|
{{- $.Scratch.Set "bareul" slice -}}
|
||||||
<ul>
|
<ul>
|
||||||
@ -20,7 +20,7 @@
|
|||||||
{{- $.Scratch.Add "bareul" (sub (add $largest .) 1) -}}
|
{{- $.Scratch.Add "bareul" (sub (add $largest .) 1) -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{- range $i, $header := $headers -}}
|
{{- range $i, $header := $headers -}}
|
||||||
{{- $headerLevel := index (findRE "[1-4]" . 1) 0 -}}
|
{{- $headerLevel := index (findRE "[1-6]" . 1) 0 -}}
|
||||||
{{- $headerLevel := len (seq $headerLevel) -}}
|
{{- $headerLevel := len (seq $headerLevel) -}}
|
||||||
|
|
||||||
{{/* get id="xyz" */}}
|
{{/* get id="xyz" */}}
|
||||||
@ -28,10 +28,10 @@
|
|||||||
|
|
||||||
{{- /* strip id="" to leave xyz, no way to get regex capturing groups in hugo */ -}}
|
{{- /* strip id="" to leave xyz, no way to get regex capturing groups in hugo */ -}}
|
||||||
{{- $cleanedID := replace (replace $id "id=\"" "") "\"" "" }}
|
{{- $cleanedID := replace (replace $id "id=\"" "") "\"" "" }}
|
||||||
{{- $header := replaceRE "<h[1-4].*?>((.|\n])+?)</h[1-4]>" "$1" $header -}}
|
{{- $header := replaceRE "<h[1-6].*?>((.|\n])+?)</h[1-6]>" "$1" $header -}}
|
||||||
|
|
||||||
{{- if ne $i 0 -}}
|
{{- if ne $i 0 -}}
|
||||||
{{- $prevHeaderLevel := index (findRE "[1-4]" (index $headers (sub $i 1)) 1) 0 -}}
|
{{- $prevHeaderLevel := index (findRE "[1-6]" (index $headers (sub $i 1)) 1) 0 -}}
|
||||||
{{- $prevHeaderLevel := len (seq $prevHeaderLevel) -}}
|
{{- $prevHeaderLevel := len (seq $prevHeaderLevel) -}}
|
||||||
{{- if gt $headerLevel $prevHeaderLevel -}}
|
{{- if gt $headerLevel $prevHeaderLevel -}}
|
||||||
{{- range seq $prevHeaderLevel (sub $headerLevel 1) -}}
|
{{- range seq $prevHeaderLevel (sub $headerLevel 1) -}}
|
||||||
@ -68,9 +68,9 @@
|
|||||||
<a href="#{{- $cleanedID -}}">{{- $header | safeHTML -}}</a>
|
<a href="#{{- $cleanedID -}}">{{- $header | safeHTML -}}</a>
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
<!-- {{- $firstHeaderLevel := len (seq (index (findRE "[1-4]" (index $headers 0) 1) 0)) -}} -->
|
<!-- {{- $firstHeaderLevel := len (seq (index (findRE "[1-6]" (index $headers 0) 1) 0)) -}} -->
|
||||||
{{- $firstHeaderLevel := $largest }}
|
{{- $firstHeaderLevel := $largest }}
|
||||||
{{- $lastHeaderLevel := len (seq (index (findRE "[1-4]" (index $headers (sub (len $headers) 1)) 1) 0)) -}}
|
{{- $lastHeaderLevel := len (seq (index (findRE "[1-6]" (index $headers (sub (len $headers) 1)) 1) 0)) -}}
|
||||||
</li>
|
</li>
|
||||||
{{- range seq (sub $lastHeaderLevel $firstHeaderLevel) -}}
|
{{- range seq (sub $lastHeaderLevel $firstHeaderLevel) -}}
|
||||||
{{- if in ($.Scratch.Get "bareul") (add . $firstHeaderLevel) -}}
|
{{- if in ($.Scratch.Get "bareul") (add . $firstHeaderLevel) -}}
|
||||||
|
Loading…
Reference in New Issue
Block a user