www.byjp.me/layouts/shortcodes/topclaps.html
2024-10-11 10:17:16 +01:00

43 lines
1.8 KiB
HTML

{{- $count := .Get 0 | default 5 -}}
{{- $url := printf "https://%s" site.Params.clapsHost -}}
{{- $cacheKey := print $url (now.Format "-2006-01-02-15") -}}
{{- with resources.GetRemote $url (dict "key" $cacheKey) -}}
{{- with .Err -}}
{{- errorf "%s" . -}}
{{- else -}}
{{- $data := .Content | transform.Unmarshal -}}
{{- $claps := slice -}}
{{- range $path, $count := $data -}}
{{- $interactionCount := partial "interaction-count.obj" (site.GetPage $path) -}}
{{ $claps = $claps | append (dict "path" $path "count" (add $interactionCount $count)) }}
{{- end -}}
<ol class="small-post-list">
{{- range (sort $claps "count" "desc" | first $count) -}}
{{- $count := .count -}}
{{- with site.GetPage .path -}}
{{- $postType := partial "post-type.txt" . -}}
{{- $typeSVG := index site.Params.defaultSVG (partial "kebab.txt" ($postType | singularize)) -}}
<li>
{{ partialCached "svg.html" (dict "name" $typeSVG) $typeSVG }}
<a href="{{.RelPermalink}}">
{{- with .Title -}}{{ . }}{{ else }}{{ .Type | singularize | title }}{{ end -}}
</a>
<small style="opacity: 0.6">{{ partialCached "svg.html" (dict "name" "clap") "clap" }} {{ $count }}</small>
</li>
{{- end -}}
{{- end }}
</ol>
<script>
const list = document.currentScript.previousSibling;
for (const clap of list.querySelectorAll('li')) {
// This clapKey is authoritative in main.js
const clapKey = `clap:${clap.children[1].pathname}`;
if (localStorage.getItem(clapKey)) {
clap.children[2].classList.add('accent')
}
}
</script>
{{- end -}}
{{- else -}}
{{- errorf "Unable to get all clap counts" -}}
{{- end -}}