Skip to content

Instantly share code, notes, and snippets.

@kokodev
Last active June 16, 2020 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kokodev/ae9bb1bacef2ea1428a59bada3c76b5d to your computer and use it in GitHub Desktop.
Save kokodev/ae9bb1bacef2ea1428a59bada3c76b5d to your computer and use it in GitHub Desktop.
Tag cloud code for a Hugo theme
<div class="sidepanel">
<div class="box tags">
<div class="tagcloud">
{{ $weights := 10 }}
{{ $tags := $.Site.Taxonomies.categories.ByCount }}
{{ range $key, $value := $tags }}
{{ $.Scratch.Add $value.Name $value.Count }}
{{ $.Scratch.SetInMap "buckets" (string $value.Count) $value.Count }}
{{ end }}
{{ range $index, $value := $.Site.Taxonomies.categories.Alphabetical }}
<span
{{ $buckets := $.Scratch.GetSortedMapValues "buckets" }}
{{ range $bucketIndex, $bucketEntry := sort $buckets }}
{{ $val := ($.Scratch.Get $value.Name) }}
{{ if eq $bucketEntry $val }}
{{ if ge (sub (sub (len $buckets) 1) $bucketIndex) 0 }}
{{ $backIndex := sub (sub (len $buckets) 1) $bucketIndex }}
{{ $weight := sub $weights $backIndex }}
{{ if ge $weight 1 }}
class="weight-{{ $weight }}"
{{ else }}
class="weight-0"
{{ end }}
{{ else }}
class="weight-0"
{{ end }}
{{ end }}
{{ end }}>
<a href="/tags/{{ $value.Name | urlize }}">{{ lower (humanize $value.Name) }}</a>
</span>
{{ end }}
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment