Auto prev/next

This commit is contained in:
Nikolay Yakimov 2017-03-14 01:31:13 +03:00
parent 23e40c6b5b
commit 1238a71e12
No known key found for this signature in database
GPG key ID: 8E9303568F9B9650
4 changed files with 15 additions and 34 deletions

View file

@ -1,9 +1,6 @@
---
title: "Some Chapter title"
weight: 0
prev: /prev/path
next: /next/path
chapter: true
icon: "<b>X. </b>" # HTML code as prefix in the menu
---

View file

@ -1,8 +1,6 @@
---
title: "Some Title"
weight: 5
prev: /prev/path
next: /next/path
toc: true
---

View file

@ -5,39 +5,29 @@
</div>
</div>
{{ with .Site.Params.menu }}
{{ range $index, $element := . }}
{{ with index $.Site.Params.menu (sub $index 1)}}
{{ $prev := index (last 1 (index $.Site.Sections .)) 0 }}
{{ $.Scratch.SetInMap $element "prev" $prev }}
{{ $.Scratch.Set "_sectionpages" (slice) }}
{{ range $sname, $spages := .Site.Sections }}
{{ $.Scratch.Add "_sectionpages" ($.Site.GetPage "section" $sname) }}
{{ end }}
{{ with index $.Site.Params.menu (add $index 1)}}
{{ $next := index (first 1 (index $.Site.Sections .)) 0 }}
{{ $.Scratch.SetInMap $element "next" $next }}
{{ end }}
{{ end }}
{{ else }}
{{ range $key, $section := $.Site.Sections }}
{{ with $prevSec := $.Scratch.Get "prevIter"}}
{{ with index $.Site.Sections . }}
{{ $.Scratch.SetInMap $key "prev" (index (last 1 .) 0) }}
{{ $.Scratch.SetInMap $prevSec "next" (index (first 1 $section) 0) }}
{{ end }}
{{ end }}
{{ $.Scratch.Set "prevIter" $key }}
{{ $.Scratch.Set "_allpages" (slice) }}
{{ range sort ($.Scratch.Get "_sectionpages") "Weight" }}
{{ $.Scratch.Add "_allpages" . }}
{{ range index $.Site.Sections .Section }}
{{ $.Scratch.Add "_allpages" .Page }}
{{ end }}
{{ end }}
{{ $allpages := $.Scratch.Get "_allpages" }}
<div id="navigation">
{{ $secpages := sort (where .Site.RegularPages "Section" .Section) "Weight" }}
{{ range $index, $element := $secpages }}
{{ range $index, $element := $allpages }}
{{if eq $element.UniqueID $.UniqueID }}
{{ if not (isset $.Params "prev") }}
{{ with index $secpages (sub $index 1) | default ($.Scratch.Get $.Section).prev.Page}}
{{ with index $allpages (sub $index 1) }}
<a class="nav nav-prev" href="{{ $.Site.BaseURL }}{{ .URL }}"> <i class="fa fa-chevron-left"></i></a>
{{ end }}
{{ end }}
{{ if not (isset $.Params "next") }}
{{ with index $secpages (add $index 1) | default ($.Scratch.Get $.Section).next.Page}}
{{ with index $allpages (add $index 1) }}
<a class="nav nav-next" href="{{ $.Site.BaseURL }}{{ .URL }}" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
{{ end }}
{{ end }}

View file

@ -26,11 +26,7 @@
{{ $value := (index $page.Site.Sections $key) }}
{{ with $index := $.Site.GetPage "section" $key }}
{{ if .Title }}
{{ $.Scratch.Set "first" $index }}
{{ else }}
{{ $.Scratch.Set (index $value 0).Page }}
{{ end }}
{{ end }}
{{ $first := $.Scratch.Get "first" }}