From bc54cecf0a4c4a73b0126b5096cd90b550b59328 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Tue, 27 Jan 2026 12:34:15 +0100 Subject: [PATCH] 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 --- config/_default/params.toml | 5 +++++ layouts/_partials/helper/pages-sort.html | 7 ++++++ layouts/_partials/helper/pages.html | 16 ++++++++++++++ layouts/_partials/helper/paginator.html | 28 +++++------------------- layouts/_partials/widget/archives.html | 2 +- layouts/archives.html | 2 +- layouts/list.html | 7 ++---- layouts/page/search.json | 2 +- 8 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 layouts/_partials/helper/pages-sort.html create mode 100644 layouts/_partials/helper/pages.html diff --git a/config/_default/params.toml b/config/_default/params.toml index b7408a5..1dbddfd 100644 --- a/config/_default/params.toml +++ b/config/_default/params.toml @@ -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 = "" diff --git a/layouts/_partials/helper/pages-sort.html b/layouts/_partials/helper/pages-sort.html new file mode 100644 index 0000000..9f767c6 --- /dev/null +++ b/layouts/_partials/helper/pages-sort.html @@ -0,0 +1,7 @@ +{{- $pages := .Pages -}} + +{{- if eq .Context.Site.Params.SortBy "lastmod" -}} + {{- $pages = $pages.ByLastmod.Reverse -}} +{{- end -}} + +{{- return $pages -}} \ No newline at end of file diff --git a/layouts/_partials/helper/pages.html b/layouts/_partials/helper/pages.html new file mode 100644 index 0000000..bca070a --- /dev/null +++ b/layouts/_partials/helper/pages.html @@ -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 -}} diff --git a/layouts/_partials/helper/paginator.html b/layouts/_partials/helper/paginator.html index 2153abf..bba32a3 100644 --- a/layouts/_partials/helper/paginator.html +++ b/layouts/_partials/helper/paginator.html @@ -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 -}} - {{- end -}} - - {{- $hasPages = true -}} +{{- if or .IsHome (eq .Kind "section") (eq .Kind "taxonomy") (eq .Kind "term") -}} + {{- $pages := partial "helper/pages.html" . -}} + {{- $result = .Paginate $pages -}} {{- end -}} -{{- if $hasPages -}} - {{ $pages = .Paginate $pages }} -{{- end -}} - -{{ return $pages }} +{{- return $result -}} diff --git a/layouts/_partials/widget/archives.html b/layouts/_partials/widget/archives.html index e42da27..49c9c69 100644 --- a/layouts/_partials/widget/archives.html +++ b/layouts/_partials/widget/archives.html @@ -9,7 +9,7 @@

{{ T "widget.archives.title" }}

- {{ $pages := where $context.Site.RegularPages "Type" "in" $context.Site.Params.mainSections }} + {{ $pages := partial "helper/pages.html" $context.Site.Home }} {{ $archives := $pages.GroupByDate "2006" }}
diff --git a/layouts/archives.html b/layouts/archives.html index 70ea6b3..41be5b8 100644 --- a/layouts/archives.html +++ b/layouts/archives.html @@ -15,7 +15,7 @@ {{ end }} - {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }} + {{ $pages := partial "helper/pages.html" .Site.Home }} {{ range $pages.GroupByDate "2006" }} {{ $id := lower (replace .Key " " "-") }} diff --git a/layouts/list.html b/layouts/list.html index efeee2e..d44dc29 100644 --- a/layouts/list.html +++ b/layouts/list.html @@ -29,13 +29,10 @@
+ {{- $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 -}} diff --git a/layouts/page/search.json b/layouts/page/search.json index 1045cc2..87bf070 100644 --- a/layouts/page/search.json +++ b/layouts/page/search.json @@ -1,4 +1,4 @@ -{{- $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections -}} +{{- $pages := partial "helper/pages.html" .Site.Home -}} {{- $result := slice -}} {{- range $pages -}}