From e176e298117894156158e2851ce4741e26661a77 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Sun, 6 Sep 2020 13:15:38 +0200 Subject: [PATCH] refactor: use .Params.mainSections to filter posts It's recommended by Hugo's official doc: https://gohugo.io/functions/where/#mainsections --- exampleSite/config.toml | 2 +- layouts/index.html | 9 ++++----- layouts/page/archive.html | 8 ++++---- layouts/partials/data/title.html | 8 ++++---- layouts/partials/widget/archive.html | 6 +++--- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/exampleSite/config.toml b/exampleSite/config.toml index bc0766c..7c493d7 100644 --- a/exampleSite/config.toml +++ b/exampleSite/config.toml @@ -9,7 +9,7 @@ title = "Example Site" page = "/:slug/" [params] - postSection = "post" + mainSections = ["post"] favicon = "" [params.sidebar] emoji = "🍥" diff --git a/layouts/index.html b/layouts/index.html index 03af128..1d37a13 100644 --- a/layouts/index.html +++ b/layouts/index.html @@ -4,11 +4,10 @@ {{ partialCached "sidebar/left.html" . }}
- {{ $postSection := $.Site.Params.postSection }} - {{ $v1 := where .Site.RegularPages "Section" $postSection }} - {{ $v2 := where .Site.RegularPages "Params.hidden" "!=" true }} - {{ $.Scratch.Set "filtered" ($v1 | intersect $v2) }} - {{ $pag := .Paginate ($.Scratch.Get "filtered") }} + {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }} + {{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }} + {{ $filtered := ($pages | intersect $notHidden) }} + {{ $pag := .Paginate ($filtered) }}
{{ range $index, $element := $pag.Pages }} diff --git a/layouts/page/archive.html b/layouts/page/archive.html index c63544e..7c47fbc 100644 --- a/layouts/page/archive.html +++ b/layouts/page/archive.html @@ -16,10 +16,10 @@ - {{ $postSection := $.Site.Params.postSection }} - {{ $v1 := where .Site.RegularPages "Section" $postSection }} - {{ $v2 := where .Site.RegularPages "Params.hidden" "!=" true }} - {{ $filtered := $v1 | intersect $v2 }} + {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }} + {{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }} + {{ $filtered := ($pages | intersect $notHidden) }} + {{ range $filtered.GroupByDate "2006" }} {{ $id := lower (replace .Key " " "-") }}
diff --git a/layouts/partials/data/title.html b/layouts/partials/data/title.html index 7249649..91db60c 100644 --- a/layouts/partials/data/title.html +++ b/layouts/partials/data/title.html @@ -3,10 +3,10 @@ {{- $authorName := .Site.Author.name -}} {{- if .IsHome -}} - {{- $v1 := where .Site.RegularPages "Type" "post" -}} - {{- $v2 := where .Site.RegularPages "Params.hidden" "!=" true -}} - {{- $filtered := $v1 | intersect $v2 -}} - {{- $pag := .Paginate ($filtered) -}} + {{ $pages := where .Site.RegularPages "Section" "in" .Site.Params.mainSections }} + {{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }} + {{ $filtered := ($pages | intersect $notHidden) }} + {{ $pag := .Paginate ($filtered) }} {{ if .Paginator.HasPrev }}{{ .Paginator }} - {{ end }}{{ $siteTitle }} {{- else if eq .Kind "term" -}} {{- $pag := .Paginate (where .Data.Pages "Type" "post") -}} diff --git a/layouts/partials/widget/archive.html b/layouts/partials/widget/archive.html index e5363c7..7e44f63 100644 --- a/layouts/partials/widget/archive.html +++ b/layouts/partials/widget/archive.html @@ -4,9 +4,9 @@

Archive

- {{ $v1 := where .Site.RegularPages "Section" "post" }} - {{ $v2 := where .Site.RegularPages "Params.hidden" "!=" true }} - {{ $filtered := $v1 | intersect $v2 }} + {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }} + {{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }} + {{ $filtered := ($pages | intersect $notHidden) }} {{ $archive := $filtered.GroupByDate "2006" }} {{ range first .Site.Params.widgets.archive.limit ($archive) }}