summaryrefslogtreecommitdiffstats
path: root/layouts/partials/tag-cloud.html
blob: b896e8dc62d118093a58a30accc42ab9980f4d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{{/* tag-cloud.html
  Reusable tag cloud partial for homepage, sidebar, and 404 pages.

  Params (dict):
    page         Page    required — calling page context (provides .Site.Taxonomies.tags, .Lang)
    showCount    bool    optional — show post count per tag (default true)
    heading      string  optional — heading text override (default: i18n "tagCloud")
    headingLevel string  optional — h2|h3|p for non-widget mode (default "h2")
    wrapInWidget bool    optional — wrap in .sidebar-widget for sidebar placement (default false)
    maxTags      int     optional — max tags to show, 0 = all (default 0)
*/}}

{{- $page         := .page -}}
{{- $showCount    := .showCount    | default true -}}
{{- $heading      := .heading      | default (i18n "tagCloud") -}}
{{- $headingLevel := .headingLevel -}}
{{- $wrapInWidget := .wrapInWidget | default false -}}
{{- $maxTags      := .maxTags      | default 0 -}}

{{- $tags := $page.Site.Taxonomies.tags -}}

{{/* Early exit if no tags */}}
{{- if $tags -}}

{{/* Compute max count for continuous scaling */}}
{{- $maxCount := 0 -}}
{{- range $tags -}}
  {{- if gt .Count $maxCount -}}{{- $maxCount = .Count -}}{{- end -}}
{{- end -}}

{{/* Ordered tag list (descending by count) */}}
{{- $orderedTags := $tags.ByCount -}}
{{- if gt $maxTags 0 -}}
  {{- $orderedTags = first $maxTags $orderedTags -}}
{{- end -}}

{{/* Render based on placement mode */}}
{{- if $wrapInWidget -}}
<div class="sidebar-widget">
  <p class="sidebar-widget-label"># {{ i18n "topTags" }}</p>
  <nav aria-label="{{ i18n "exploreTopics" }}">
    <div class="tag-cloud" data-tag-cloud>
{{- else -}}
<section {{- if $headingLevel }} aria-labelledby="tag-cloud-heading"{{ end }}>
  {{- if $headingLevel -}}
  {{- if eq $headingLevel "h2" -}}
  <h2 id="tag-cloud-heading" class="text-lg font-semibold text-accent mb-4">{{ $heading }}</h2>
  {{- else if eq $headingLevel "h3" -}}
  <h3 id="tag-cloud-heading" class="text-lg font-semibold text-accent mb-4">{{ $heading }}</h3>
  {{- else -}}
  <p id="tag-cloud-heading" class="text-lg font-semibold text-accent mb-4">{{ $heading }}</p>
  {{- end -}}
  {{- end -}}
  <nav aria-label="{{ i18n "exploreTopics" }}">
    <div class="tag-cloud" data-tag-cloud>
{{- end -}}

      {{- range $orderedTags -}}
        {{- $count := .Count -}}
        {{- $ratio := (div (float $count) (float $maxCount)) -}}
        {{- $size := (add 0.6 (mul $ratio 1.2)) -}}
        {{- $opacity := (add 0.7 (mul $ratio 0.3)) -}}
        <a
          href="{{ .Page.RelPermalink }}"
          class="tag-cloud-link"
          data-weight="{{ printf "%.4f" $ratio }}"
          {{- if ge $ratio 0.5 }}
          style="font-size: {{ $size }}rem; color: var(--accent); opacity: {{ $opacity }};"
          {{- else }}
          style="font-size: {{ $size }}rem; color: var(--text-dim); opacity: {{ $opacity }};"
          {{- end }}
          aria-label="{{ .Name }}{{- if $showCount }} ({{ i18n "postCount" $count }}){{- end -}}"
        >
          {{- .Name -}}
          {{- if $showCount -}}
          <span class="tag-cloud-count" aria-hidden="true">{{ $count }}</span>
          {{- end -}}
        </a>
      {{- end -}}

    </div>
  </nav>

{{- if $wrapInWidget -}}
</div>
{{- else -}}
</section>
{{- end -}}

{{- end -}}{{/* end if $tags */}}