diff options
| author | Allan Chain <allanchain@pku.edu.cn> | 2020-01-07 15:58:33 +0800 |
|---|---|---|
| committer | Aditya Telange <21258296+adityatelange@users.noreply.github.com> | 2020-09-19 17:02:20 +0530 |
| commit | 43d1c68ef459d36cc7fc3266a326f9b9841ab41f (patch) | |
| tree | b47068b5ea6ab142a3fc13612bd300d0d20fb9e0 | |
| parent | 7820730b04886f2a2da89f04623cace2dcc12d10 (diff) | |
toc: add toc partial [newer impl]
* gets toc fixed
Signed-off-by: Aditya Telange <21258296+adityatelange@users.noreply.github.com>
| -rw-r--r-- | layouts/partials/toc.html | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html new file mode 100644 index 0000000..f3146dc --- /dev/null +++ b/layouts/partials/toc.html @@ -0,0 +1,84 @@ +{{- $headers := findRE "<h[1-4].*?>(.|\n])+?</h[1-4]>" .Content -}} +{{- $has_headers := ge (len $headers) 1 -}} +{{- if $has_headers -}} + +{{- $largest := 6 -}} +{{- range $headers -}} +{{- $headerLevel := index (findRE "[1-4]" . 1) 0 -}} +{{- $headerLevel := len (seq $headerLevel) -}} +{{- if lt $headerLevel $largest -}} +{{- $largest = $headerLevel -}} +{{- end -}} +{{- end -}} + +{{- $firstHeaderLevel := len (seq (index (findRE "[1-4]" (index $headers 0) 1) 0)) -}} + +{{- $.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-4]" . 1) 0 -}} + {{- $headerLevel := len (seq $headerLevel) -}} + + {{/* get id="xyz" */}} + {{ $id := index (findRE "(id=\"(.*?)\")" $header 9) 0 }} + + {{/* strip id="" to leave xyz (no way to get regex capturing groups in hugo :( */}} + {{ $cleanedID := replace (replace $id "id=\"" "") "\"" "" }} + {{- $header := replaceRE "<h[1-4].*?>((.|\n])+?)</h[1-4]>" "$1" $header -}} + + {{- if ne $i 0 -}} + {{- $prevHeaderLevel := index (findRE "[1-4]" (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 -}} + {{- end -}} + <li> + <a href="#{{- $cleanedID -}}">{{- $header | safeHTML -}}</a> + {{- else -}} + <li> + <a href="#{{- $cleanedID -}}">{{- $header | safeHTML -}}</a> + {{- end -}} + {{- end -}} + <!-- {{- $firstHeaderLevel := len (seq (index (findRE "[1-4]" (index $headers 0) 1) 0)) -}} --> + {{ $firstHeaderLevel := $largest }} + {{- $lastHeaderLevel := len (seq (index (findRE "[1-4]" (index $headers (sub (len $headers) 1)) 1) 0)) -}} + </li> + {{- range seq (sub $lastHeaderLevel $firstHeaderLevel) -}} + {{- if in ($.Scratch.Get "bareul") (add . $firstHeaderLevel) -}} +</ul> +{{- else -}} +</ul> +</li> +{{- end -}} +{{- end -}} +</ul> +{{- end -}}
\ No newline at end of file |
