From 44eaf1c67ecc7ca320105102c8c6f428cc289657 Mon Sep 17 00:00:00 2001 From: razonyang Date: Sat, 16 Dec 2023 23:32:22 +0800 Subject: [PATCH] feat(sidebar/taxonomies): allow sorting, disabling, limiting each taxonomy --- .../blog/functions/filter-taxonomies.html | 30 +++++++++++++++++++ .../hb/modules/blog/taxonomies/default.html | 20 +++++-------- .../hb/modules/blog/taxonomies/toggle.html | 29 ++++++++---------- 3 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 layouts/partials/hb/modules/blog/functions/filter-taxonomies.html diff --git a/layouts/partials/hb/modules/blog/functions/filter-taxonomies.html b/layouts/partials/hb/modules/blog/functions/filter-taxonomies.html new file mode 100644 index 00000000..fb0a6025 --- /dev/null +++ b/layouts/partials/hb/modules/blog/functions/filter-taxonomies.html @@ -0,0 +1,30 @@ +{{- $opts := . }} +{{- $defaultLimit := default 10 $opts.limit }} +{{- $defaultCount := default true $opts.count }} +{{- $taxonomies := slice }} +{{- range $name, $taxonomy := site.Taxonomies }} + {{- $limit := $defaultLimit }} + {{- $count := $defaultCount }} + {{- $weight := 10 }} + {{- with index $opts $name }} + {{- if default false .disable }} + {{- continue }} + {{- end }} + {{- with .limit }} + {{- $limit = . }} + {{- end }} + {{- if isset . "count" }} + {{- $count = .count }} + {{- end }} + {{- with .weight }} + {{- $weight = . }} + {{- end }} + {{- end }} + {{- $taxonomies = $taxonomies | append (dict + "name" $name + "terms" (first $limit $taxonomy.ByCount) + "count" $count + "weight" $weight) + }} +{{- end }} +{{- return sort $taxonomies "weight" }} diff --git a/layouts/partials/hb/modules/blog/taxonomies/default.html b/layouts/partials/hb/modules/blog/taxonomies/default.html index 411e65de..fb4fcc65 100644 --- a/layouts/partials/hb/modules/blog/taxonomies/default.html +++ b/layouts/partials/hb/modules/blog/taxonomies/default.html @@ -1,26 +1,22 @@ -{{- $limit := default 10 .limit }} -{{- $count := default true .count }} -{{- range $name, $taxonomy := site.Taxonomies }} - {{- $idx := 0 }} +{{- $taxonomies := partial "hb/modules/blog/functions/filter-taxonomies" . }} +{{- range $taxonomies }} + {{- $name := .name }} + {{- $count := .count }}
{{- with site.GetPage $name }} {{- end }} - {{- range $termName, $term := $taxonomy.ByCount }} - {{- if eq $idx $limit }} - {{- break }} - {{- end }} + {{- range .terms }} - {{- $term.Page.Title -}} + href="{{ .Page.RelPermalink }}"> + {{- .Page.Title -}} {{- if $count }} - {{- printf " (%d)" $term.Count -}} + {{- printf " (%d)" .Count -}} {{- end }} - {{- $idx = add $idx 1 }} {{- end }}
{{- end }} diff --git a/layouts/partials/hb/modules/blog/taxonomies/toggle.html b/layouts/partials/hb/modules/blog/taxonomies/toggle.html index 0853ac13..42ec38a2 100644 --- a/layouts/partials/hb/modules/blog/taxonomies/toggle.html +++ b/layouts/partials/hb/modules/blog/taxonomies/toggle.html @@ -1,13 +1,13 @@ {{- $style := default "pills" .style }} -{{- $limit := default 10 .limit }} -{{- $count := default true .count }} +{{- $taxonomies := partial "hb/modules/blog/functions/filter-taxonomies" . }}