Improved sections menu and added a new site parameters: BookCollapseSection (was front matter only, global now) and BookCollapseSectionLevel (used for auto-expand up to specified nest level).

Signed-off-by: raspopov <raspopov@cherubicsoft.com>
This commit is contained in:
raspopov 2021-05-06 20:38:46 +03:00
parent 4d936a55d8
commit f8f5a95661
No known key found for this signature in database
GPG key ID: 2B35203E97E80E13

View file

@ -4,32 +4,32 @@
{{ end }} {{ end }}
{{ with .Site.GetPage $bookSection }} {{ with .Site.GetPage $bookSection }}
{{ template "book-section-children" (dict "Section" . "CurrentPage" $) }} {{ template "book-section-children" (dict "Section" . "CurrentPage" $ "Level" 1) }}
{{ end }} {{ end }}
{{ define "book-section-children" }}{{/* (dict "Section" .Section "CurrentPage" .CurrentPage) */}} {{ define "book-section-children" }}{{/* (dict "Section" .Section "CurrentPage" .CurrentPage "Level" .Level) */}}
<ul> <ul>
{{ range (where .Section.Pages "Params.bookhidden" "ne" true) }} {{ range (where .Section.Pages "Params.bookhidden" "ne" true) }}
{{ if .IsSection }} {{ if .IsSection }}
<li {{- if .Params.BookFlatSection }} class="book-section-flat" {{ end -}}> <li {{- if .Params.BookFlatSection }} class="book-section-flat" {{ end -}}>
{{ template "book-page-link" (dict "Page" . "CurrentPage" $.CurrentPage) }} {{ template "book-page-link" (dict "Page" . "CurrentPage" $.CurrentPage "Level" $.Level) }}
{{ template "book-section-children" (dict "Section" . "CurrentPage" $.CurrentPage) }} {{ template "book-section-children" (dict "Section" . "CurrentPage" $.CurrentPage "Level" (add $.Level 1)) }}
</li> </li>
{{ else if and .IsPage .Content }} {{ else if and .IsPage .Content }}
<li> <li>
{{ template "book-page-link" (dict "Page" . "CurrentPage" $.CurrentPage) }} {{ template "book-page-link" (dict "Page" . "CurrentPage" $.CurrentPage "Level" $.Level) }}
</li> </li>
{{ end }} {{ end }}
{{ end }} {{ end }}
</ul> </ul>
{{ end }} {{ end }}
{{ define "book-page-link" }}{{/* (dict "Page" .Page "CurrentPage" .CurrentPage) */}} {{ define "book-page-link" }}{{/* (dict "Page" .Page "CurrentPage" .CurrentPage "Level" .Level) */}}
{{ $current := eq .CurrentPage .Page }} {{ $current := eq .CurrentPage .Page }}
{{ $ancestor := .Page.IsAncestor .CurrentPage }} {{ $ancestor := .Page.IsAncestor .CurrentPage }}
{{ if .Page.Params.bookCollapseSection }} {{ if (and .Page.IsSection (default .Page.Site.Params.BookCollapseSection .Page.Params.BookCollapseSection)) }}
<input type="checkbox" id="section-{{ md5 .Page }}" class="toggle" {{ if or $current $ancestor }}checked{{ end }} /> <input type="checkbox" id="section-{{ md5 .Page }}" class="toggle"{{ if or (or $current $ancestor) (le .Level (default 0 .Page.Site.Params.BookCollapseSectionLevel)) }} checked{{ end }}/>
<label for="section-{{ md5 .Page }}" class="flex justify-between"> <label for="section-{{ md5 .Page }}" class="flex justify-between">
<a {{ if .Page.Content }}href="{{ .Page.Permalink }}"{{ end }} class="{{ if $current }}active{{ end }}"> <a {{ if .Page.Content }}href="{{ .Page.Permalink }}"{{ end }} class="{{ if $current }}active{{ end }}">
{{- partial "docs/title" .Page -}} {{- partial "docs/title" .Page -}}