diff options
author | Khosrow Moossavi <khos2ow@gmail.com> | 2021-04-02 00:14:02 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-04-04 20:16:05 +0300 |
commit | b2ff8980999293495dedfe66e39012487a06779e (patch) | |
tree | 45b1600c57bf6abab193831f8b7a4b187ed7aa50 /layouts | |
parent | 0a155d12226272b7e59632c046c9c73a673bd9ad (diff) |
Add support up to 3 levels deep left submenu
`maxMenuDepth` is added to parameters which is optional and defaults
to 2 if empty or not found. This will control the depth for submenu
items to be rendered for left menu table of contents.
Fixes #37
Diffstat (limited to 'layouts')
-rw-r--r-- | layouts/_default/list.html | 29 | ||||
-rw-r--r-- | layouts/partials/funcs/toc_from_pages.html | 31 |
2 files changed, 49 insertions, 11 deletions
diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 476b1fa..23cb596 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -6,6 +6,12 @@ {{ define "toc" }} <ul id="toc" class="toc-list-h1"> + {{ $maxDepth := 0 }} + {{ with $.Site.Params.maxMenuDepth }} + {{ $maxDepth = . }} + {{ else }} + {{ $maxDepth = 2 }} + {{ end }} {{ $headers := slice }} {{ with .Site.RegularPages }} {{ $headers = partial "funcs/toc_from_pages" . }} @@ -13,14 +19,23 @@ {{ range $headers }} <li> <a href="#{{ .id }}" class="toc-h{{ .level }} toc-link" data-title="{{ .title }}">{{ .title }}</a> - {{if .sub }} + {{ if and (ge $maxDepth 2) .sub }} <ul class="toc-list-h2"> - {{range .sub}} - <li><a href="#{{ .id }}" class="toc-h{{ .level }} toc-link" data-title="{{ .title }}">{{ .title }}</a></li> - {{end}} + {{ range .sub }} + <li> + <a href="#{{ .id }}" class="toc-h{{ .level }} toc-link" data-title="{{ .title }}">{{ .title }}</a> + {{ if and (ge $maxDepth 3) .sub }} + <ul class="toc-list-h3"> + {{ range .sub }} + <li><a href="#{{ .id }}" class="toc-h{{ .level }} toc-link" data-title="{{ .title }}">{{ .title }}</a></li> + {{ end }} + </ul> + {{ end }} + </li> + {{ end }} </ul> - {{end}} + {{ end }} </li> - {{end}} + {{ end }} </ul> -{{ end }}
\ No newline at end of file +{{ end }} diff --git a/layouts/partials/funcs/toc_from_pages.html b/layouts/partials/funcs/toc_from_pages.html index 1919a51..1317b74 100644 --- a/layouts/partials/funcs/toc_from_pages.html +++ b/layouts/partials/funcs/toc_from_pages.html @@ -5,30 +5,53 @@ {{ end }} {{ $toc := slice }} -{{ $previousH1 := dict }} + +{{ $previousH1 := dict}} +{{ $previousH2 := dict}} {{ $previousLevel := 0 }} + {{ $h2s := slice }} +{{ $h3s := slice }} {{ range $combined }} {{ $level := int (substr . 2 1) }} - {{ if le $level 2 }} + {{ if le $level 3 }} {{ $idTitle := split (. | replaceRE "<h\\d id=\"(.*?)\">(.*)</h\\d>" "$1|$2") "|" }} {{ $item := dict "level" $level "id" (index $idTitle 0) "title" (index $idTitle 1) }} + {{ if eq $level 1 }} {{ if ne $previousLevel 0 }} + {{ if or (eq $previousLevel 2) (eq $previousLevel 3) }} + {{ $tocItem := merge $previousH2 (dict "sub" $h3s) }} + {{ $h2s = $h2s | append $tocItem }} + {{ $h3s = slice }} + {{ end }} + {{ $tocItem := merge $previousH1 (dict "sub" $h2s) }} {{ $toc = $toc | append $tocItem }} {{ $h2s = slice }} - {{end}} + {{ $previousH2 = slice }} + {{ end }} {{ $previousH1 = $item }} {{ else }} - {{ $h2s = $h2s | append $item }} + {{ if eq $level 2 }} + {{ if and (ne $previousLevel 1) (ne $previousLevel 2) }} + {{ $tocItem := merge $previousH2 (dict "sub" $h3s) }} + {{ $h2s = $h2s | append $tocItem }} + {{ $h3s = slice }} + {{ end }} + {{ $previousH2 = $item }} + {{ else }} + {{ $h3s = $h3s | append $item }} + {{ end }} {{ end }} {{ $previousLevel = $level }} {{ end }} {{ end }} + {{ if ne $previousLevel 0 }} {{ $item := merge $previousH1 (dict "sub" $h2s) }} {{ $toc = $toc | append $item }} {{ end }} + {{ return $toc }} |