Enhances Mastodon link handling and previews

Adds functionality to identify Mastodon links in Hugo markdown files, extract relevant information like instance and ID, and generate previews.

This allows for embedding Mastodon toots directly into the generated Hugo site and provides a preview text extracted from the toot content improving the user experience and content integration. It also adds a fallback in case a toot disappears to avoid broken content.

Also adds `bleach` as a dependency to sanitize HTML content.
This commit is contained in:
fundor333
2025-07-27 19:35:30 +02:00
parent f0d39ddb48
commit dbda00405b
5 changed files with 179 additions and 2 deletions

View File

@@ -13,3 +13,15 @@
{{with .Params.Rspv}}
<p><a href="{{.}}" class="u-rsvp"><i class="fa-regular fa-calendar-heart"></i> RSPV of <span class="url-title">{{.}}</span></a></p>
{{end}}
{{with .Params.Preview_text_from_reply}}
<blockquote class="u-in-reply-to in-reply-to">
<p>{{.}}</p>
</blockquote>
{{end}}
{{ if .Params.mastodon_reply}}
{{ partial "toot" . }}
{{end}}

View File

@@ -0,0 +1,63 @@
{{ $masIns := .Params.mastodon_instance }}
{{ $id := .Params.mastodon_id }}
{{ $tootLink := "" }}
{{ $handleInst := "" }}
{{ $urlToGet := print "https://" $masIns "/api/v1/statuses/" $id }}
{{ with resources.GetRemote $urlToGet }}
{{ $json := .Content | unmarshal }}
{{ if isset $json "account" }}
{{ $tootLink = print "https://" $masIns "@" $json.account.acct "/status/" $id }}
{{ $handleInst = print "@" $json.account.acct "@" $masIns }}
{{ end }}
{{ if isset $json "content" }}
<div class="toot">
<div class="toot-header">
<a class="toot-profile" href="https://{{ $masIns }}/@{{ $json.account.acct }}" rel="noopener">
<img src="{{ $json.account.avatar }}"
alt="Avatar for {{ $handleInst }}"
loading="lazy">
</a>
<div class="toot-author">
<a class="toot-author-name"
href="https://{{ $masIns }}/@{{ $json.account.acct }}"
rel="noopener">{{ $json.account.display_name }}</a>
<a class="toot-author-handle"
href="https://{{ $masIns }}/@{{ $json.account.acct }}"
rel="noopener">{{ $handleInst }}</a>
</div>
</div>
<div class="toot-content">{{ $json.content | safeHTML }}</div>
{{ with $json.media_attachments }}
{{ $count := len . }}
<div class="toot-media-grid" data-count="{{ $count }}">
{{ range . }}
{{ if eq .type "image" }}
<div class="toot-media-item">
<img src="{{ .url }}"
alt=""
loading="lazy">
</div>
{{ end }}
{{ end }}
</div>
{{ end }}
<div class="toot-footer">
<a href="{{ $tootLink }}"
class="toot-date"
rel="noopener">{{ dateFormat "3:04 PM · Jan 2, 2006" $json.created_at }}</a>
</div>
</div>
{{ end }}
{{ else }}
<div class="toot">
<p style="text-align: center; color: var(--secondary); margin: 0;">
[Source not online at time of site build.]
</p>
</div>
{{ end }}