Back to Genai Toolbox

Pager

.hugo/layouts/partials/pager.html

1.1.04.1 KB
Original Source

{{ $curr := . }} {{ $next := "" }} {{ $prev := "" }} {{ $max_skip_attempts := 5 }} {{/* 1. Calculate Previous Page */}} {{ if .Parent }} {{ $siblings := .Parent.Pages.ByWeight }} {{ $currIndex := -1 }} {{ range $index, $page := $siblings }} {{ if eq $page.RelPermalink $curr.RelPermalink }} {{ $currIndex = $index }} {{ end }} {{ end }} {{ if gt $currIndex 0 }} {{ $prev = index $siblings (sub $currIndex 1) }} {{ else }} {{ if ne .Parent.Type "home" }} {{ $prev = .Parent }} {{ end }} {{ end }} {{ end }} {{/* 2. Calculate Next Page */}} {{ if and .IsNode (gt (len .Pages) 0) }} {{ $next = index .Pages.ByWeight 0 }} {{ else }} {{ if .Parent }} {{ $siblings := .Parent.Pages.ByWeight }} {{ $currIndex := -1 }} {{ range $index, $page := $siblings }} {{ if eq $page.RelPermalink $curr.RelPermalink }} {{ $currIndex = $index }} {{ end }} {{ end }} {{ if lt $currIndex (sub (len $siblings) 1) }} {{ $next = index $siblings (add $currIndex 1) }} {{ else }} {{ $p := .Parent }} {{ $foundNext := false }} {{ range seq 3 }} {{ if and (not $foundNext) $p }} {{ if $p.Parent }} {{ $pSiblings := $p.Parent.Pages.ByWeight }} {{ $pIndex := -1 }} {{ range $index, $page := $pSiblings }} {{ if eq $page.RelPermalink $p.RelPermalink }} {{ $pIndex = $index }} {{ end }} {{ end }} {{ if and (ge $pIndex 0) (lt $pIndex (sub (len $pSiblings) 1)) }} {{ $next = index $pSiblings (add $pIndex 1) }} {{ $foundNext = true }} {{ else }} {{ $p = $p.Parent }} {{ end }} {{ else }} {{ $p = false }} {{ end }} {{ end }} {{ end }} {{ end }} {{ end }} {{ end }} {{/* 3. Apply Integration Directory Filters */}} {{ range seq $max_skip_attempts }} {{ if $prev }} {{ $isLockedPrev := false }} {{ if and $prev.IsNode (in $prev.RelPermalink "/integrations/") }} {{ if or (strings.HasSuffix $prev.RelPermalink "/tools/") (strings.HasSuffix $prev.RelPermalink "/samples/") (and $prev.Parent (strings.HasSuffix $prev.Parent.RelPermalink "/integrations/")) }} {{ $isLockedPrev = true }} {{ end }} {{ end }} {{ if $isLockedPrev }} {{ $steppingOut := strings.HasPrefix $curr.RelPermalink $prev.RelPermalink }} {{ if and (not $steppingOut) (gt (len $prev.Pages) 0) }} {{ $prev = index $prev.Pages.ByWeight (sub (len $prev.Pages) 1) }} {{ else }} {{ if $prev.Parent }} {{ if eq $prev.Parent.RelPermalink "/integrations/" }} {{ $prev = $prev.Parent }} {{ else }} {{ $sibs := $prev.Parent.Pages.ByWeight }} {{ $idx := -1 }} {{ range $i, $p := $sibs }}{{ if eq $p.RelPermalink $prev.RelPermalink }}{{ $idx = $i }}{{ end }}{{ end }} {{ if gt $idx 0 }} {{ $prev = index $sibs (sub $idx 1) }} {{ else }} {{ $prev = $prev.Parent }} {{ end }} {{ end }} {{ else }} {{ $prev = "" }} {{ end }} {{ end }} {{ else }} {{ break }} {{ end }} {{ end }} {{ end }} {{ range seq $max_skip_attempts }} {{ if $next }} {{ $isLockedNext := false }} {{ if and $next.IsNode (in $next.RelPermalink "/integrations/") }} {{ if or (strings.HasSuffix $next.RelPermalink "/tools/") (strings.HasSuffix $next.RelPermalink "/samples/") (and $next.Parent (strings.HasSuffix $next.Parent.RelPermalink "/integrations/")) }} {{ $isLockedNext = true }} {{ end }} {{ end }} {{ if $isLockedNext }} {{ if gt (len $next.Pages) 0 }} {{ $next = index $next.Pages.ByWeight 0 }} {{ else }} {{ $sibs := $next.Parent.Pages.ByWeight }} {{ $idx := -1 }} {{ range $i, $p := $sibs }}{{ if eq $p.RelPermalink $next.RelPermalink }}{{ $idx = $i }}{{ end }}{{ end }} {{ if lt $idx (sub (len $sibs) 1) }} {{ $next = index $sibs (add $idx 1) }} {{ else }} {{ $p := $next.Parent }} {{ $foundNextSibling := false }} {{ range seq 3 }} {{ if and (not $foundNextSibling) $p }} {{ if $p.Parent }} {{ $pSibs := $p.Parent.Pages.ByWeight }} {{ $pIdx := -1 }} {{ range $index, $page := $pSibs }}{{ if eq $page.RelPermalink $p.RelPermalink }}{{ $pIdx = $index }}{{ end }}{{ end }} {{ if and (ge $pIdx 0) (lt $pIdx (sub (len $pSibs) 1)) }} {{ $next = index $pSibs (add $pIdx 1) }} {{ $foundNextSibling = true }} {{ else }} {{ $p = $p.Parent }} {{ end }} {{ else }} {{ $p = false }} {{ end }} {{ end }} {{ end }} {{ if not $foundNextSibling }}{{ $next = "" }}{{ end }} {{ end }} {{ end }} {{ else }} {{ break }} {{ end }} {{ end }} {{ end }} {{/* 4. Render Navigation */}} {{ if or $prev $next }} {{ end }}