refactor: centralize page filtering logic, and allow sort by lastmod (#1250)

* refactor: migrate page visibility control from `Params.hidden` to the new `build.list` parameter.

* refactor: centralize page filtering logic into a new `helper/pages.html` partial

* feat: introduce `SortBy` parameter to enable sorting pages by last modified date

* refactor: extract page sorting logic into a new `pages-sort.html` partial
This commit is contained in:
Jimmy
2026-01-27 12:34:15 +01:00
committed by GitHub
parent deceb17144
commit bc54cecf0a
8 changed files with 38 additions and 31 deletions
+5
View File
@@ -2,6 +2,11 @@ mainSections = ["post"]
featuredImageField = "image"
rssFullContent = true
# Accepted values: "default", "lastmod"
# default = see https://gohugo.io/quick-reference/glossary/#default-sort-order
# lastmod = sort by last modified date, in descending order
SortBy = "default"
[footer]
since = 2026
customText = ""
+7
View File
@@ -0,0 +1,7 @@
{{- $pages := .Pages -}}
{{- if eq .Context.Site.Params.SortBy "lastmod" -}}
{{- $pages = $pages.ByLastmod.Reverse -}}
{{- end -}}
{{- return $pages -}}
+16
View File
@@ -0,0 +1,16 @@
{{- $pages := .Pages -}}
{{- if .IsHome -}}
{{- $pages = where .Site.RegularPages "Type" "in" .Site.Params.mainSections -}}
{{- else if or (eq .Kind "section") (eq .Kind "taxonomy") (eq .Kind "term") -}}
{{- $subsections := .Sections -}}
{{- $pages = .Pages | complement $subsections -}}
{{- if eq (len $pages) 0 -}}
{{- $pages = $subsections -}}
{{- end -}}
{{- end -}}
{{- $pages := partial "helper/pages-sort.html" (dict "Pages" $pages "Context" .) -}}
{{- return $pages -}}
+5 -23
View File
@@ -1,26 +1,8 @@
{{- $pages := slice -}}
{{- $hasPages := false -}}
{{- $result := slice -}}
{{- if .IsHome -}}
{{ $pages = where .Site.RegularPages "Section" "in" .Site.Params.mainSections }}
{{- $hasPages = true -}}
{{- else if eq .Kind "term" -}}
{{ $pages = .Pages }}
{{- $hasPages = true -}}
{{- else if or (eq .Kind "section") (eq .Kind "taxonomy") -}}
{{ $subsections := .Sections }}
{{ $pages = .Pages | complement $subsections }}
{{- if eq (len $pages) 0 -}}
{{/* See complete logic in list.html */}}
{{- $pages = $subsections -}}
{{- if or .IsHome (eq .Kind "section") (eq .Kind "taxonomy") (eq .Kind "term") -}}
{{- $pages := partial "helper/pages.html" . -}}
{{- $result = .Paginate $pages -}}
{{- end -}}
{{- $hasPages = true -}}
{{- end -}}
{{- if $hasPages -}}
{{ $pages = .Paginate $pages }}
{{- end -}}
{{ return $pages }}
{{- return $result -}}
+1 -1
View File
@@ -9,7 +9,7 @@
</div>
<h2 class="widget-title section-title">{{ T "widget.archives.title" }}</h2>
{{ $pages := where $context.Site.RegularPages "Type" "in" $context.Site.Params.mainSections }}
{{ $pages := partial "helper/pages.html" $context.Site.Home }}
{{ $archives := $pages.GroupByDate "2006" }}
<div class="widget-archive--list">
+1 -1
View File
@@ -15,7 +15,7 @@
{{ end }}
</header>
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $pages := partial "helper/pages.html" .Site.Home }}
{{ range $pages.GroupByDate "2006" }}
{{ $id := lower (replace .Key " " "-") }}
+2 -5
View File
@@ -29,13 +29,10 @@
</div>
</header>
{{- $pages := partial "helper/pages.html" . -}}
{{- $subsections := .Sections -}}
{{- $pages := .Pages | complement $subsections -}}
{{- if eq (len $pages) 0 -}}
{{/* If there are no normal pages, display subsections in list style, with pagination */}}
{{/* This happens with taxonomies like categories or tags */}}
{{- $pages = $subsections -}}
{{- if and (eq (len $pages) (len $subsections)) (eq $pages $subsections) -}}
{{- $subsections = slice -}}
{{- end -}}
+1 -1
View File
@@ -1,4 +1,4 @@
{{- $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections -}}
{{- $pages := partial "helper/pages.html" .Site.Home -}}
{{- $result := slice -}}
{{- range $pages -}}