Fix claps

This commit is contained in:
JP Hastings-Spital 2024-04-29 17:56:30 +01:00
parent c974158793
commit f120f0831b
5 changed files with 31 additions and 3 deletions

View file

@ -17,7 +17,7 @@ Here in my little career break I'm spending a lot of time thinking about _commun
Yesterday I built a clap button, the one you can see below this paragraph, and on every page at the bottom of the post (though sometimes its a heart instead of a clap, for aesthetic reasons). If you click it it lets me know you appreciate my post, and keeps track of how many times that's happened (you can press it more than once if you really like something!)
<figure style="text-align:center">{{< claps >}}<br/><small style="font-style: italic">Press this to show appreciation!</small></figure>
{{< claps >}}
I decided to make this button totally anonymous; this makes it much easier for you to use, but the lack of tracking also means that—for me—it's a feel good vibe rather than data I can get analytical over. If you like the sound of it, you can try it out 😜

View file

@ -23,4 +23,4 @@ You may also want to visit:
=> / 🏡 Home
{{ range .Site.Menus.main -}}
=> {{ .URL | relURL }} {{ .Name }}
{{ end -}}
{{ end -}}

View file

@ -0,0 +1,3 @@
_Sorry, my clap button works via HTTP, and isn't available over Gemini (yet)._
=> https://www.byjp.me{{ replace .Page.RelPermalink "index.gmi" "" }} View this page on the WWW instead

View file

@ -1 +1,4 @@
{{ partial "claps.html" (dict "url" .Page.RelPermalink) }}
<figure style="text-align:center">
{{- partial "claps.html" (dict "url" .Page.RelPermalink) }}<br/>
<small style="font-style: italic">Press this to show appreciation!</small>
</figure>

View file

@ -0,0 +1,22 @@
{{- $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 -}}
{{ $claps = $claps | append (dict "path" $path "count" $count) }}
{{- end -}}
{{- range (sort $claps "count" "desc" | first $count) -}}
{{- $count := .count -}}
{{- with site.GetPage .path }}
=> {{.RelPermalink}} {{ with .Params.emoji }}{{ . }} {{ end}}{{ with .Title -}}{{ . }}{{ else }}{{ .Type | singularize | title }}{{ end }} ({{ $count }})
{{- end -}}
{{- end }}
{{- end -}}
{{- else -}}
{{- errorf "Unable to get all clap counts" -}}
{{- end -}}