Perf: optimize categories and tags template lookup order

This commit is contained in:
Cell
2023-12-05 19:06:39 +08:00
parent 8416b6f757
commit 86f4630f0b
3 changed files with 54 additions and 56 deletions
+3 -56
View File
@@ -4,67 +4,14 @@
{{- define "content" -}}
{{- $taxonomies := .Data.Plural -}}
{{- $terms := .Data.Terms.ByCount -}}
{{- $type := .Type -}}
<div class="page archive">
{{- /* Title */ -}}
<h2 class="single-title animate__animated animate__pulse animate__faster">
{{- .Params.Title | default (T $taxonomies) | default $taxonomies | dict "Some" | T "allSome" }}
<sup>{{- len $terms -}}</sup>
<sup>{{- len .Data.Terms.ByCount -}}</sup>
</h2>
{{- /* Categories Page */ -}}
{{- if eq $taxonomies "categories" -}}
<div class="categories-card">
{{- range $terms -}}
{{- $term := .Term -}}
{{- $pages := .Pages -}}
{{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
<div class="card-item">
<div class="card-item-wrapper">
<h3 class="card-item-title">
<a href='{{ partial "function/escapeurl.html" .RelPermalink }}'>
{{ dict "Class" "fa-regular fa-folder fa-fw" | partial "plugin/icon.html" }}
{{ .Page.Title }}&nbsp;<sup>{{- len $pages -}}</sup>
</a>
</h3>
{{- range first 5 $pages -}}
<article class="archive-item">
<a href="{{ .RelPermalink }}" class="archive-item-link">{{- .Title -}}</a>
</article>
{{- end -}}
{{- if gt (len $pages) 5 -}}
<span class="more-post">
<a href='{{ partial "function/escapeurl.html" .RelPermalink }}' class="more-single-link">{{ T "pagination.more" }} >></a>
</span>
{{- end -}}
</div>
</div>
{{- end -}}
{{- end -}}
</div>
{{- /* Tag Cloud Page */ -}}
{{- else if eq $taxonomies "tags" -}}
{{- $tagCloud := .Site.Params.tagcloud -}}
<div class="tag-cloud-tags{{ with $tagCloud.enable }} text-center{{ end }}">
{{- if $tagCloud.enable -}}
{{- $tags := cond (eq $tagCloud.orderby "name") $.Site.Taxonomies.tags.Alphabetical $.Site.Taxonomies.tags.ByCount -}}
{{- $baseGrade := div ($tagCloud.peakCount | default 10) 10.0 -}}
{{- $minFont := $tagCloud.min | default 16 -}}
{{- $baseFont := div (sub ($tagCloud.max | default 32) $minFont) 10.0 -}}
{{- range $tags -}}
{{- $grade := math.Min (math.Floor (div (sub .Count 1) $baseGrade)) 10 -}}
{{- $fontSize := add $minFont (mul $grade $baseFont) -}}
<a href='{{ partial "function/escapeurl.html" .Page.RelPermalink }}' class="tag-cloud-{{ $grade }}" style="--tag-font: {{ $fontSize }}px">{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
{{- end -}}
{{- else -}}
{{- range $.Site.Taxonomies.tags.ByCount -}}
<a href='{{ partial "function/escapeurl.html" .Page.RelPermalink }}'>{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
{{- end -}}
{{- end -}}
</div>
{{- end -}}
{{- /* Render miscellaneous terms */ -}}
{{- .Render (add "terms/" $taxonomies) -}}
</div>
{{- end -}}
+30
View File
@@ -0,0 +1,30 @@
{{- /* Categories Page */ -}}
<div class="categories-card">
{{- range .Data.Terms.ByCount -}}
{{- $term := .Term -}}
{{- $pages := .Pages -}}
{{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $.Type $term) -}}
<div class="card-item">
<div class="card-item-wrapper">
<h3 class="card-item-title">
<a href='{{ partial "function/escapeurl.html" .RelPermalink }}'>
{{ dict "Class" "fa-regular fa-folder fa-fw" | partial "plugin/icon.html" }}
{{ .Page.Title }}&nbsp;<sup>{{- len $pages -}}</sup>
</a>
</h3>
{{- range first 5 $pages -}}
<article class="archive-item">
<a href="{{ .RelPermalink }}" class="archive-item-link">{{- .Title -}}</a>
</article>
{{- end -}}
{{- if gt (len $pages) 5 -}}
<span class="more-post">
<a href='{{ partial "function/escapeurl.html" .RelPermalink }}' class="more-single-link">{{ T "pagination.more" }} >></a>
</span>
{{- end -}}
</div>
</div>
{{- end -}}
{{- end -}}
</div>
+21
View File
@@ -0,0 +1,21 @@
{{- /* Tags Page */ -}}
{{- $tagCloud := .Site.Params.tagcloud -}}
<div class="tag-cloud-tags{{ with $tagCloud.enable }} text-center{{ end }}">
{{- /* Tags Cloud */ -}}
{{- if $tagCloud.enable -}}
{{- $tags := cond (eq $tagCloud.orderby "name") $.Site.Taxonomies.tags.Alphabetical $.Site.Taxonomies.tags.ByCount -}}
{{- $baseGrade := div ($tagCloud.peakCount | default 10) 10.0 -}}
{{- $minFont := $tagCloud.min | default 16 -}}
{{- $baseFont := div (sub ($tagCloud.max | default 32) $minFont) 10.0 -}}
{{- range $tags -}}
{{- $grade := math.Min (math.Floor (div (sub .Count 1) $baseGrade)) 10 -}}
{{- $fontSize := add $minFont (mul $grade $baseFont) -}}
<a href='{{ partial "function/escapeurl.html" .Page.RelPermalink }}' class="tag-cloud-{{ $grade }}" style="--tag-font: {{ $fontSize }}px">{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
{{- end -}}
{{- else -}}
{{- range $.Site.Taxonomies.tags.ByCount -}}
<a href='{{ partial "function/escapeurl.html" .Page.RelPermalink }}'>{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
{{- end -}}
{{- end -}}
</div>