From a34bcbd74cbeaa1282fd50c3813c04fbd649b363 Mon Sep 17 00:00:00 2001
From: Aditya Telange <21258296+adityatelange@users.noreply.github.com>
Date: Sun, 4 Oct 2020 18:52:14 +0530
Subject: [PATCH] toc(partial): generate toc for h1 - h6 previously: h1-h4
---
layouts/partials/toc.html | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html
index 06053b6a..68d5091c 100644
--- a/layouts/partials/toc.html
+++ b/layouts/partials/toc.html
@@ -1,17 +1,17 @@
-{{- $headers := findRE "(.|\n])+?" .Content -}}
+{{- $headers := findRE "(.|\n])+?" .Content -}}
{{- $has_headers := ge (len $headers) 1 -}}
{{- if $has_headers -}}
{{- $largest := 6 -}}
{{- range $headers -}}
-{{- $headerLevel := index (findRE "[1-4]" . 1) 0 -}}
+{{- $headerLevel := index (findRE "[1-6]" . 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)) -}}
+{{- $firstHeaderLevel := len (seq (index (findRE "[1-6]" (index $headers 0) 1) 0)) -}}
{{- $.Scratch.Set "bareul" slice -}}
@@ -20,7 +20,7 @@
{{- $.Scratch.Add "bareul" (sub (add $largest .) 1) -}}
{{- end -}}
{{- range $i, $header := $headers -}}
- {{- $headerLevel := index (findRE "[1-4]" . 1) 0 -}}
+ {{- $headerLevel := index (findRE "[1-6]" . 1) 0 -}}
{{- $headerLevel := len (seq $headerLevel) -}}
{{/* get id="xyz" */}}
@@ -28,10 +28,10 @@
{{- /* strip id="" to leave xyz, no way to get regex capturing groups in hugo */ -}}
{{- $cleanedID := replace (replace $id "id=\"" "") "\"" "" }}
- {{- $header := replaceRE "((.|\n])+?)" "$1" $header -}}
+ {{- $header := replaceRE "((.|\n])+?)" "$1" $header -}}
{{- 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) -}}
{{- if gt $headerLevel $prevHeaderLevel -}}
{{- range seq $prevHeaderLevel (sub $headerLevel 1) -}}
@@ -68,9 +68,9 @@
{{- $header | safeHTML -}}
{{- end -}}
{{- end -}}
-
+
{{- $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)) -}}
{{- range seq (sub $lastHeaderLevel $firstHeaderLevel) -}}
{{- if in ($.Scratch.Get "bareul") (add . $firstHeaderLevel) -}}