Fix: Unit page pagination

This commit is contained in:
hyzen
2026-05-22 14:09:31 +05:30
parent b285fe7e4d
commit 465ff2d672
5 changed files with 29 additions and 18 deletions

View File

@@ -50,34 +50,45 @@
{{ partial "page/terms.html" (dict "taxonomy" "tags" "page" .) }}
{{ partial "page/terms.html" (dict "taxonomy" "categories" "page" .) }}
{{/* Next/prev among sibling UNITS in the same subjectcode — deduplicated by unit label */}}
{{/* Next/prev among sibling UNITS in the same subjectcode — sorted numerically */}}
{{ if $subjectcode }}
{{ $subPage := site.GetPage (printf "/subjectcode/%s" ($subjectcode | urlize)) }}
{{ if $subPage }}
{{/* Build a deduplicated list of units: one entry per unit label, using the Self page as representative */}}
{{ $unitMap := slice }}
{{/* Build deduplicated unit list with numeric sort key */}}
{{ $seenUnits := slice }}
{{ range $subPage.Pages.ByWeight }}
{{ $unitDicts := slice }}
{{ range $subPage.Pages }}
{{ $unitLabel := (.Params.unit | default .Title) }}
{{ if not (in $seenUnits $unitLabel) }}
{{ $seenUnits = $seenUnits | append $unitLabel }}
{{ $unitMap = $unitMap | append . }}
{{/* Extract unit number for numeric sort, e.g. "Unit 11" → 11 */}}
{{ $uParts := split $unitLabel " " }}
{{ $uLast := index $uParts (sub (len $uParts) 1) }}
{{ $uNumStr := "" }}
{{ range (split $uLast "") }}
{{ if ge . "0" }}{{ if le . "9" }}{{ $uNumStr = printf "%s%s" $uNumStr . }}{{ end }}{{ end }}
{{ end }}
{{ $uNum := int ($uNumStr | default "0") }}
{{ $unitDicts = $unitDicts | append (dict "label" $unitLabel "page" . "num" $uNum) }}
{{ end }}
{{ end }}
{{ $sortedUnitDicts := sort $unitDicts "num" }}
{{ $currentUnit := (.Params.unit | default .Title) }}
{{ $currentIdx := 0 }}
{{ range $i, $p := $unitMap }}
{{ if eq ($p.Params.unit | default $p.Title) $currentUnit }}{{ $currentIdx = $i }}{{ end }}
{{ range $i, $d := $sortedUnitDicts }}
{{ if eq $d.label $currentUnit }}{{ $currentIdx = $i }}{{ end }}
{{ end }}
{{ if gt (len $unitMap) 1 }}
{{ if gt (len $sortedUnitDicts) 1 }}
<nav class="page-nav">
{{ if gt $currentIdx 0 }}
{{ $prev := index $unitMap (sub $currentIdx 1) }}
<a class="page-nav__previous-link" href="{{ $prev.Params.uniturl }}">← {{ $prev.Params.unit | default $prev.Title }}</a>
{{ $prev := (index $sortedUnitDicts (sub $currentIdx 1)) }}
<a class="page-nav__previous-link" href="{{ $prev.page.Params.uniturl }}">← {{ $prev.label }}</a>
{{ end }}
{{ if lt $currentIdx (sub (len $unitMap) 1) }}
{{ $next := index $unitMap (add $currentIdx 1) }}
<a class="page-nav__next-link" href="{{ $next.Params.uniturl }}">{{ $next.Params.unit | default $next.Title }} →</a>
{{ if lt $currentIdx (sub (len $sortedUnitDicts) 1) }}
{{ $next := (index $sortedUnitDicts (add $currentIdx 1)) }}
<a class="page-nav__next-link" href="{{ $next.page.Params.uniturl }}">{{ $next.label }} →</a>
{{ end }}
</nav>
{{ end }}