{{- /* Inputs: archived -> pages after rolling window rollingWindow -> integer (from config) context -> current page (.) basePath -> e.g. "archive" or "archive/articles" homePath -> optional extraMonths -> optional slice of "YYYY/MM" */ -}} {{- $archived := .archived | default (slice) -}} {{- $context := .context -}} {{- $basePath := trim (.basePath | default "archive") "/" -}} {{- $homePath := .homePath | default site.Home.RelPermalink -}} {{- $extraMonths := .extraMonths | default (slice) -}} {{- /* -------------------------------------- */ -}} {{- /* Build month list (HOME + real months) */ -}} {{- /* -------------------------------------- */ -}} {{- $months := slice -}} {{- $months = $months | append "HOME" -}} {{- /* From archived pages */ -}} {{- if gt (len $archived) 0 -}} {{- $archivedGroups := $archived.GroupByDate "2006/01" -}} {{- range $archivedGroups -}} {{- if not (in $months .Key) -}} {{- $months = $months | append .Key -}} {{- end -}} {{- end -}} {{- end -}} {{- /* From extraMonths argument */ -}} {{- range $extraMonths -}} {{- if and (ne . "HOME") (not (in $months .)) -}} {{- $months = $months | append . -}} {{- end -}} {{- end -}} {{- $validMonths := slice -}} {{- $validMonths = $validMonths | append "HOME" -}} {{- range $months -}} {{- if ne . "HOME" -}} {{- /* If this month came from extraMonths, always allow it */ -}} {{- if in $extraMonths . -}} {{- $validMonths = $validMonths | append . -}} {{- else -}} {{- $parts := split . "/" -}} {{- if eq (len $parts) 2 -}} {{- $year := index $parts 0 -}} {{- $month := index $parts 1 -}} {{- $p := site.GetPage (printf "%s/%s/%s" $basePath $year $month) -}} {{- if not $p -}} {{- $p = site.GetPage (printf "/%s/%s/%s" $basePath $year $month) -}} {{- end -}} {{- if $p -}} {{- $validMonths = $validMonths | append . -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- /* Separate HOME from real months */ -}} {{- $realMonths := slice -}} {{- range $validMonths -}} {{- if ne . "HOME" -}} {{- $realMonths = $realMonths | append . -}} {{- end -}} {{- end -}} {{- /* Sort descending (YYYY/MM works lexicographically) */ -}} {{- $realMonths = sort $realMonths "value" "desc" -}} {{- /* Rebuild months list */ -}} {{- $months = slice -}} {{- $months = $months | append "HOME" -}} {{- range $realMonths -}} {{- $months = $months | append . -}} {{- end -}} {{- range $months -}} {{- if ne . "HOME" -}} {{- $parts := split . "/" -}} {{- if eq (len $parts) 2 -}} {{- $year := index $parts 0 -}} {{- $month := index $parts 1 -}} {{- $p := site.GetPage (printf "%s/%s/%s" $basePath $year $month) -}} {{- if not $p -}} {{- $p = site.GetPage (printf "/%s/%s/%s" $basePath $year $month) -}} {{- end -}} {{- if $p -}} {{- $validMonths = $validMonths | append . -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- $total := len $months -}} {{- if gt $total 1 -}} {{- /* -------------------------------------- */ -}} {{- /* Determine current index */ -}} {{- /* -------------------------------------- */ -}} {{- $parts := split (trim $context.RelPermalink "/") "/" -}} {{- $currentIndex := 0 -}} {{- if gt (len $parts) 2 -}} {{- $year := index $parts (sub (len $parts) 2) -}} {{- $month := index $parts (sub (len $parts) 1) -}} {{- $currentKey := printf "%s/%s" $year $month -}} {{- range $i, $m := $months -}} {{- if eq $m $currentKey -}} {{- $currentIndex = $i -}} {{- end -}} {{- end -}} {{- end -}} {{- /* -------------------------------------- */ -}} {{- /* Sliding window (5 entries) */ -}} {{- /* -------------------------------------- */ -}} {{- $window := 5 -}} {{- $start := sub $currentIndex 2 -}} {{- if lt $start 0 }}{{- $start = 0 -}}{{- end -}} {{- if gt (add $start $window) $total -}} {{- $start = sub $total $window -}} {{- if lt $start 0 }}{{- $start = 0 -}}{{- end -}} {{- end -}} {{- $end := add $start $window -}} {{- if gt $end $total }}{{- $end = $total -}}{{- end -}} {{- end -}}