refactor: add taxonomy widget

Replaces `categories` and `tag-cloud` widget.
This commit is contained in:
Jimmy Cai 2023-08-16 12:45:53 +02:00
parent 06f0f6068c
commit 0634317a55
5 changed files with 38 additions and 59 deletions

View File

@ -158,11 +158,15 @@ params:
- type: archives
params:
limit: 5
- type: categories
- type: taxonomy
params:
type: categories
icon: categories
limit: 10
- type: tag-cloud
- type: taxonomy
params:
type: tags
icon: tag
limit: 10
page:
- type: toc

View File

@ -1,17 +1,8 @@
{{- $scope := default "homepage" .Scope -}}
{{- $context := .Context -}}
{{- $isCategoriesPage := and (eq $context.Kind "taxonomy") (eq $context.Type "categories") }}
{{- $isTagsPage := and (eq $context.Kind "taxonomy") (eq $context.Type "tags") }}
{{- with (index .Context.Site.Params.widgets $scope) -}}
<aside class="sidebar right-sidebar sticky">
{{ range $widget := . }}
{{ if and $isCategoriesPage (eq .type "categories") }}
{{ continue }}
{{ end }}
{{ if and $isTagsPage (eq .type "tag-cloud") }}
{{ continue }}
{{ end }}
{{ if templates.Exists (printf "partials/widget/%s.html" .type) }}
{{ partial (printf "widget/%s" .type) (dict "Context" $context "Params" .params) }}
{{ else }}

View File

@ -1,24 +0,0 @@
{{- $query := first 1 (where (where .Context.Site.Pages "Kind" "taxonomy") "Type" "categories") -}}
{{- $context := .Context -}}
{{- $limit := default 10 .Params.limit -}}
{{- if $query -}}
{{- $categoriesPage := index $query 0 -}}
<section class="widget tagCloud">
<div class="widget-icon">
{{ partial "helper/icon" "categories" }}
</div>
<h2 class="widget-title section-title">
<a class="widget-link" href="{{ $categoriesPage.RelPermalink }}">{{ default "Categories" $categoriesPage.Title }}</a>
</h2>
<div class="tagCloud-tags">
{{ range first $limit $context.Site.Taxonomies.categories.ByCount }}
<a href="{{ .Page.RelPermalink }}" class="font_size_{{ .Count }}">
{{ .Page.Title }}
</a>
{{ end }}
</div>
</section>
{{- else -}}
{{- warnf "Categories page not found. Please enable the categories taxonomy." -}}
{{- end -}}

View File

@ -1,24 +0,0 @@
{{- $query := first 1 (where (where .Context.Site.Pages "Kind" "taxonomy") "Type" "tags") -}}
{{- $context := .Context -}}
{{- $limit := default 10 .Params.limit -}}
{{- if $query -}}
{{- $tagsPage := index $query 0 -}}
<section class="widget tagCloud">
<div class="widget-icon">
{{ partial "helper/icon" "tag" }}
</div>
<h2 class="widget-title section-title">
<a class="widget-link" href="{{ $tagsPage.RelPermalink }}">{{ default "Tags" $tagsPage.Title }}</a>
</h2>
<div class="tagCloud-tags">
{{ range first $limit $context.Site.Taxonomies.tags.ByCount }}
<a href="{{ .Page.RelPermalink }}" class="font_size_{{ .Count }}">
{{ .Page.Title }}
</a>
{{ end }}
</div>
</section>
{{- else -}}
{{- warnf "Categories page not found. Please enable the categories taxonomy." -}}
{{- end -}}

View File

@ -0,0 +1,32 @@
{{- $query := first 1 (where (where .Context.Site.Pages "Kind" "taxonomy") "Type" .Params.type) -}}
{{- $context := .Context -}}
{{- $limit := default 10 .Params.limit -}}
{{- $hideInTaxonomyPage := default true .Params.hideInTaxonomyPage -}}
{{- if $query -}}
{{- $taxonomyPage := index $query 0 -}}
{{- $isTaxonomyPage := eq .Context $taxonomyPage -}}
{{- if not (and $isTaxonomyPage $hideInTaxonomyPage) -}}
<section class="widget tagCloud">
<div class="widget-icon">
{{ partial "helper/icon" (default "tag" .Params.icon) }}
</div>
<h2 class="widget-title section-title">
<a class="widget-link" href="{{ $taxonomyPage.RelPermalink }}">
{{ default $taxonomyPage.Title .Params.title }}
</a>
</h2>
<div class="tagCloud-tags">
{{ $pages := index $context.Site.Taxonomies .Params.type }}
{{ $pagesSorted := $pages.ByCount }}
{{ range first $limit $pagesSorted }}
<a href="{{ .Page.RelPermalink }}" class="font_size_{{ .Count }}">
{{ .Page.Title }}
</a>
{{ end }}
</div>
</section>
{{- end -}}
{{- else -}}
{{- warnf "Taxonomy [%s] not found." .Params.type -}}
{{- end -}}