Explorar el Código

feat(widget): Tag Cloud: add taxonomy and font size options

Enables the widget to be used for categories etc. and not only tags.

Also fix tag ordering by title rather than by count (d0ec05a).

Fix #1267
Fix d0ec05a
George Cushen hace 6 años
padre
commit
c2650cc239
Se han modificado 2 ficheros con 21 adiciones y 6 borrados
  1. 8 0
      exampleSite/content/home/tags.md
  2. 13 6
      layouts/partials/widgets/tag_cloud.html

+ 8 - 0
exampleSite/content/home/tags.md

@@ -9,6 +9,14 @@ title = "Popular Topics"
 subtitle = ""
 subtitle = ""
 
 
 [content]
 [content]
+  # Choose the taxonomy from `config.toml` to display (e.g. tags, categories)
+  taxonomy = "tags"
+  
   # Choose how many tags you would like to display (0 = all tags)
   # Choose how many tags you would like to display (0 = all tags)
   count = 20
   count = 20
+
+[design]
+  # Minimum and maximum font sizes (1.0 = 100%).
+  font_size_min = 0.7
+  font_size_max = 2.0
 +++
 +++

+ 13 - 6
layouts/partials/widgets/tag_cloud.html

@@ -1,5 +1,11 @@
+{{/* Pages Widget */}}
+
+{{/* Initialise */}}
 {{ $ := .root }}
 {{ $ := .root }}
 {{ $st := .page }}
 {{ $st := .page }}
+{{ $taxonomy := $st.Params.content.taxonomy | default "tags" }}
+{{ $fontSmall := $st.Params.design.font_size_min | default 0.8 }}
+{{ $fontBig := $st.Params.design.font_size_max | default 2.5 }}
 
 
 {{ $items_count := $st.Params.content.count }}
 {{ $items_count := $st.Params.content.count }}
 {{ if eq $items_count 0 }}
 {{ if eq $items_count 0 }}
@@ -8,6 +14,10 @@
   {{ $items_count = $items_count | default 20 }}
   {{ $items_count = $items_count | default 20 }}
 {{ end }}
 {{ end }}
 
 
+{{/* Query */}}
+{{ $tags := first $items_count (index site.Taxonomies $taxonomy).ByCount }}
+{{ $count := len $tags }}
+
 <div class="row">
 <div class="row">
   <div class="col-12 col-lg-4 section-heading">
   <div class="col-12 col-lg-4 section-heading">
     <h1>{{ with $st.Title }}{{ . | markdownify | emojify }}{{ end }}</h1>
     <h1>{{ with $st.Title }}{{ . | markdownify | emojify }}{{ end }}</h1>
@@ -16,10 +26,7 @@
   <div class="col-12 col-lg-8">
   <div class="col-12 col-lg-8">
     {{ with $st.Content }}{{ . }}{{ end }}
     {{ with $st.Content }}{{ . }}{{ end }}
 
 
-    {{ if not (eq (len site.Taxonomies.tags) 0) }}
-      {{ $tags := first $items_count site.Taxonomies.tags.ByCount }}
-      {{ $fontSmall := 0.8 }}
-      {{ $fontBig := 2.5 }}
+    {{ if ne $count 0 }}
 
 
       {{ $fontDelta := sub $fontBig $fontSmall }}
       {{ $fontDelta := sub $fontBig $fontSmall }}
       {{ $min := len (index ($tags).Reverse 0).Pages }}
       {{ $min := len (index ($tags).Reverse 0).Pages }}
@@ -28,8 +35,8 @@
       {{ $fontStep := div $fontDelta $delta }}
       {{ $fontStep := div $fontDelta $delta }}
 
 
       <div class="tag-cloud">
       <div class="tag-cloud">
-        {{ range $name, $taxonomy := $tags }}
-          {{ $tagCount := len $taxonomy.Pages }}
+        {{ range $name, $term := (sort $tags ".Page.Title" "asc") }}
+          {{ $tagCount := len $term.Pages }}
           {{ $weight := div (sub (math.Log $tagCount) (math.Log $min)) (sub (math.Log $max) (math.Log $min)) }}
           {{ $weight := div (sub (math.Log $tagCount) (math.Log $min)) (sub (math.Log $max) (math.Log $min)) }}
           {{ $fontSize := add $fontSmall (mul (sub $fontBig $fontSmall) $weight) }}
           {{ $fontSize := add $fontSmall (mul (sub $fontBig $fontSmall) $weight) }}
           <a href="{{ .Page.RelPermalink }}" style="font-size:{{ $fontSize }}rem">{{ .Page.Title }}</a>
           <a href="{{ .Page.RelPermalink }}" style="font-size:{{ $fontSize }}rem">{{ .Page.Title }}</a>