#43, Update tree menu rendering to support multiple sections

This commit is contained in:
Alex Shpak
2019-04-22 19:17:39 +02:00
parent 0084e069de
commit 0b30289d9f
18 changed files with 249 additions and 69 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
<div class="align-center book-git-footer {{ if not .GitInfo }}justify-end{{ else }}justify-between{{ end }}">
{{ with .GitInfo }}
<div>
{{- $date := .AuthorDate.Local.Format (default "January 2, 2006" $.Site.Params.BookDateFormat) -}}
{{ $date := .AuthorDate.Local.Format (default "January 2, 2006" $.Site.Params.BookDateFormat) }}
<a href="{{ $.Site.Params.BookRepo }}/commit/{{ .Hash }}" title='Last modified {{ $date }} by {{ .AuthorName }}' target="_blank" rel="noopener">
<img src="{{ "svg/code-merge.svg" | relURL }}" /> {{ $date }}
</a>
+1 -4
View File
@@ -1,7 +1,4 @@
{{- template "hrefhack" . -}}
{{ template "hrefhack" . }}
{{ with .Site.GetPage .Site.Params.BookMenuBundle }}
{{- .Content -}}
{{ end }}
{{ if .Site.Params.BookEnableJS }}
{{- template "jsmenu" . -}}
{{ end }}
+45 -33
View File
@@ -1,47 +1,59 @@
<!-- Put configured sections list to .Scratch -->
{{ template "book-get-root-section" . }}
{{ $bookSection := default "docs" .Site.Params.BookSection }}
{{ if eq $bookSection "*" }}
{{ .Scratch.Set "BookSections" .Site.Sections }}
{{ else }}
{{ $bookSections := where .Site.Sections "Section" $bookSection }}
{{ .Scratch.Set "BookSections" $bookSections }}
{{ end }}
{{- range .Scratch.Get "BookSections" -}}
{{ template "book-section" (dict "Section" . "CurrentPage" $.Permalink) }}
{{- end -}}
{{ define "book-section" }} <!-- Single section of menu (recursive) -->
{{ $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" $.Permalink) }}
{{ end }}
{{ else }}
<ul>
{{ range .Section.Sections }}
<li {{- if .Params.bookflatsection}} class="flat-section" {{ end }}>
{{- if .Content -}}
{{ template "book-page-link" (dict "Page" . "CurrentPage" $.CurrentPage) }}
{{- else -}}
{{- template "title" . -}}
{{- end -}}
{{ range $sections }}
{{ template "book-section" (dict "Section" . "CurrentPage" $.Permalink) }}
{{ end }}
</ul>
{{ end }}
{{ template "book-section" (dict "Section" . "CurrentPage" $.CurrentPage) }}
</li>
{{ define "book-section" }}
{{ with .Section }}
<li {{ if .Params.bookFlatSection}} class="book-section-flat" {{ end }}>
{{ if .Content }}
{{ template "book-page-link" (dict "Page" . "CurrentPage" $.CurrentPage) }}
{{ else }}
<span>{{ template "title" . }}</span>
{{ end }}
{{ range .Section.Pages }}
{{ template "book-section-children" (dict "Section" . "CurrentPage" $.CurrentPage) }}
</li>
{{ end }}
{{ end }}
{{ define "book-section-children" }}
{{ with .Section }}
<ul>
{{ range .Sections }}
{{ template "book-section" (dict "Section" . "CurrentPage" $.CurrentPage) }}
{{ end }}
{{ range .Pages }}
<li>
{{ template "book-page-link" (dict "Page" . "CurrentPage" $.CurrentPage) }}
</li>
{{ end }}
</ul>
{{ end }}
{{ define "book-page-link" }}
{{- with .Page -}}
<a href="{{ .RelPermalink }}" {{- if eq $.CurrentPage .Permalink }} class="active"{{ end }}>
{{- template "title" . -}}
</a>
{{- end -}}
{{ end }}
{{ define "book-get-root-section" }}
<!-- Complex logic to guess page title without .Title specified -->
{{ $bookSection := default "docs" .Site.Params.BookSection }}
{{ if eq $bookSection "*" }}
{{ .Scratch.Set "BookSections" .Site.Sections }}
{{ else }}
{{ $bookSections := where .Site.Sections "Section" $bookSection }}
{{ .Scratch.Set "BookSections" $bookSections }}
{{ end }}
{{ end }}
{{ define "book-page-link" }}
{{ with .Page }}
<a href="{{ .RelPermalink }}" {{ if eq $.CurrentPage .Permalink }} class="active"{{ end }}>
{{ template "title" . }}
</a>
{{ end }}
{{ end }}
+5 -1
View File
@@ -9,4 +9,8 @@
{{ end }}
{{ partial "docs/inject/menu-after" . }}
</nav>
</nav>
{{ if .Site.Params.BookEnableJS }}
{{ template "jsmenu" . }}
{{ end }}
+12 -13
View File
@@ -1,16 +1,16 @@
{{/*These templates contains some more complex logic and shared between partials*/}}
{{- define "title" -}}
{{- if and .File .Pages -}}
{{ define "title" }}
{{ if and .File .Pages }}
{{ $sections := split (trim .File.Dir "/") "/" }}
{{ $title := index ($sections | last 1) 0 | humanize | title }}
{{- default $title .Title -}}
{{- else if .File -}}
{{ $title := .File | humanize | title }}
{{- default $title .Title -}}
{{- end -}}
{{- end -}}
{{ default $title .Title }}
{{ else if .File }}
{{ $title := .File.BaseFileName | humanize | title }}
{{ default $title .Title }}
{{ end }}
{{ end }}
{{- define "hrefhack" -}}
{{ define "hrefhack" }}
{{ $attrEq := "$=" }}
{{ $attrVal := .RelPermalink }}
{{ if eq .RelPermalink "/" }}
@@ -23,17 +23,16 @@
color: {{ default "#004ed0" .Site.Params.BookMenuBundleActiveLinkColor }};
}
</style>
{{- end -}}
{{ end }}
{{- define "jsmenu" -}}
{{ define "jsmenu" }}
<script>
(function() {
var menu = document.querySelector('aside.book-menu nav')
addEventListener('beforeunload', function(event) {
localStorage.setItem('menu.scrollTop', menu.scrollTop)
});
menu.scrollTop = localStorage.getItem('menu.scrollTop')
})()
</script>
{{- end -}}
{{ end }}