#157, Refactor side menu, simpler and renders all pages at root

This commit is contained in:
Alex Shpak 2020-04-11 14:35:29 +02:00
parent 8927a3c672
commit 15339ab6a6
5 changed files with 34 additions and 58 deletions

View File

@ -62,9 +62,10 @@ enableGitInfo = true
# When not specified file structure and weights will be used # When not specified file structure and weights will be used
# BookMenuBundle = '/menu' # BookMenuBundle = '/menu'
# (Optional, default docs) Specify section of content to render as menu # (Optional, default docs) Specify root page to render child pages as menu.
# You can also set value to '*' to render all sections to menu # Page is resoled by .GetPage function: https://gohugo.io/functions/getpage/
BookSection = 'docs' # For backward compatibility you can set '*' to render all sections to menu. Acts same as '/'
BookSection = '/'
# Set source repository location. # Set source repository location.
# Used for 'Last Modified' and 'Edit this page' links. # Used for 'Last Modified' and 'Edit this page' links.
@ -78,7 +79,7 @@ enableGitInfo = true
# Configure the date format used on the pages # Configure the date format used on the pages
# - In git information # - In git information
# - In blog posts # - In blog posts
BookDateFormat = 'Jan 2, 2006' BookDateFormat = 'January 2, 2006'
# (Optional, default true) Enables search function with flexsearch, # (Optional, default true) Enables search function with flexsearch,
# Index is built on fly, therefore it might slowdown your website. # Index is built on fly, therefore it might slowdown your website.

View File

@ -58,8 +58,9 @@ params:
# When not specified file structure and weights will be used # When not specified file structure and weights will be used
# BookMenuBundle: /menu # BookMenuBundle: /menu
# (Optional, default docs) Specify section of content to render as menu # (Optional, default docs) Specify root page to render child pages as menu.
# You can also set value to '*' to render all sections to menu # Page is resoled by .GetPage function: https://gohugo.io/functions/getpage/
# For backward compatibility you can set '*' to render all sections to menu. Acts same as '/'
BookSection: docs BookSection: docs
# Set source repository location. # Set source repository location.
@ -74,7 +75,7 @@ params:
# Configure the date format used on the pages # Configure the date format used on the pages
# - In git information # - In git information
# - In blog posts # - In blog posts
BookDateFormat: "Jan 2, 2006" BookDateFormat: "January 2, 2006"
# (Optional, default true) Enables search function with flexsearch, # (Optional, default true) Enables search function with flexsearch,
# Index is built on fly, therefore it might slowdown your website. # Index is built on fly, therefore it might slowdown your website.

View File

@ -1,48 +1,27 @@
{{ $bookSection := default "docs" .Site.Params.BookSection }} {{ $bookSection := default "docs" .Site.Params.BookSection }}
{{ if eq $bookSection "*" }} {{ if eq $bookSection "*" }}
{{ .Scratch.Set "BookSections" .Site.Sections }} {{ $bookSection = "/" }}{{/* Backward compatibility */}}
{{ else }}
{{ $bookSections := where .Site.Sections "Section" $bookSection }}
{{ .Scratch.Set "BookSections" $bookSections }}
{{ end }} {{ end }}
{{ with .Site.GetPage $bookSection }}
{{ $sections := .Scratch.Get "BookSections" }}
{{/* If there is only one section to render then render its children, else render all sections */}}
{{ if eq (len $sections) 1 }}
{{ with index $sections 0 }}
{{ template "book-section-children" (dict "Section" . "CurrentPage" $) }} {{ template "book-section-children" (dict "Section" . "CurrentPage" $) }}
{{ end }}
{{ else }}
<ul>
{{ range where $sections "Params.bookHidden" "!=" true }}
{{ template "book-section" (dict "Section" . "CurrentPage" $) }}
{{ end }}
</ul>
{{ end }} {{ end }}
{{ define "book-section" }} {{ define "book-section-children" }}{{/* (dict "Section" .Section "CurrentPage" .CurrentPage) */}}
{{ with .Section }} {{ $ancestor := .Section.IsAncestor .CurrentPage }}
<li {{- if .Params.BookFlatSection }} class="book-section-flat" {{ end }}> {{ $collapsed := .Section.Params.bookCollapseSection }}
{{ if or $ancestor (not $collapsed) }}
<ul>
{{ range (where .Section.Pages "Params.bookhidden" "ne" true) }}
{{ if .IsSection }}
<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) }}
{{ template "book-section-children" (dict "Section" . "CurrentPage" $.CurrentPage) }} {{ template "book-section-children" (dict "Section" . "CurrentPage" $.CurrentPage) }}
</li> </li>
{{ end }}
{{ end }}
{{ define "book-section-children" }}
{{ $ancestor := .Section.IsAncestor .CurrentPage }}
{{ $collapsed := .Section.Params.bookCollapseSection }}
{{ if or $ancestor (not $collapsed) }}
{{ with .Section }}
<ul>
{{ range where .Pages "Params.bookhidden" "!=" "true" }}
{{ if .IsSection }}
{{ template "book-section" (dict "Section" . "CurrentPage" $.CurrentPage) }}
{{ 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) }}
</li> </li>
{{ end }} {{ end }}
{{ end }} {{ end }}
@ -50,18 +29,14 @@
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ end }} {{ define "book-page-link" }}{{/* (dict "Page" .Page "CurrentPage" .CurrentPage) */}}
{{ if .Page.Content }}
{{ define "book-page-link" }} <a href="{{ .Page.RelPermalink }}" class="
{{ with .Page }} {{- if and .Page.Params.bookCollapseSection .Page.IsSection }}collapsed {{ end }}
{{ if .Content }} {{- if eq .CurrentPage.RelPermalink .Page.RelPermalink }}active{{ end }}">
<a href="{{ .RelPermalink }}" class=" {{- partial "docs/title" .Page -}}
{{- if and .Params.bookCollapseSection .IsSection }}collapsed {{ end }}
{{- if eq $.CurrentPage.RelPermalink .RelPermalink }}active{{ end }}">
{{- partial "docs/title" . -}}
</a> </a>
{{ else }} {{ else }}
<span>{{- partial "docs/title" . -}}</span> <span>{{- partial "docs/title" .Page -}}</span>
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ end }}

View File

@ -1,5 +1,4 @@
{{ define "main" }} {{ define "main" }}
{{ $paginator := .Paginate (where .Pages "Params.hidden" "ne" true) }}
{{ range sort .Paginator.Pages }} {{ range sort .Paginator.Pages }}
<article class="markdown book-post"> <article class="markdown book-post">
<h2> <h2>

View File

@ -1,5 +1,4 @@
{{ define "main" }} {{ define "main" }}
{{ $paginator := .Paginate (where .Pages "Params.hidden" "ne" true) }}
{{ range sort .Paginator.Pages }} {{ range sort .Paginator.Pages }}
<article class="markdown book-post"> <article class="markdown book-post">
<h2> <h2>
@ -14,6 +13,7 @@
</p> </p>
</article> </article>
{{ end }} {{ end }}
{{ template "_internal/pagination.html" . }} {{ template "_internal/pagination.html" . }}
{{ end }} {{ end }}