From 087ea995eea21df2dee4b2f99ed86e72e58cd1b6 Mon Sep 17 00:00:00 2001 From: eltrac Date: Thu, 12 Mar 2026 14:52:11 +0800 Subject: feat(shortcode): social card from https://github.com/thumbsupdotme/social-cards by thumbsupdotme --- layouts/_shortcodes/fedicard.html | 148 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 layouts/_shortcodes/fedicard.html (limited to 'layouts/_shortcodes') diff --git a/layouts/_shortcodes/fedicard.html b/layouts/_shortcodes/fedicard.html new file mode 100644 index 00000000..ab24542b --- /dev/null +++ b/layouts/_shortcodes/fedicard.html @@ -0,0 +1,148 @@ +{{- $url := .Get 0 -}} +{{- $parsed := urls.Parse $url -}} +{{- $parts := split $parsed.Path "/" -}} +{{- $id := index $parts (sub (len $parts) 1) -}} +{{- $instance := printf "%s://%s" $parsed.Scheme $parsed.Host -}} +{{- $apiUrl := printf "%s/api/v1/statuses/%s" $instance $id -}} +{{- $data := resources.GetRemote $apiUrl -}} +{{- $json := $data | transform.Unmarshal -}} +{{- $author := $json.account -}} + +{{- $publishedAt := time $json.created_at -}} + +{{- $avatarSrc := $author.avatar -}} +{{- with resources.GetRemote $author.avatar -}} + {{- with .Resize "88x88 q85 webp" -}} + {{- $avatarSrc = .RelPermalink -}} + {{- end -}} +{{- end -}} + +{{- /* Count image attachments */ -}} +{{- $imgCount := 0 -}} +{{- range $json.media_attachments -}}{{- if eq .type "image" -}}{{- $imgCount = add $imgCount 1 -}}{{- end -}}{{- end -}} +{{- $cId := printf "sc-%s" (md5 $url) -}} +{{- $dId := printf "sd-%s" (md5 $url) -}} + +
+
+
+ +
+ {{ $author.display_name }} + @{{ $author.acct }} +
+
+ + + + + + + + + + + + + + + + + + + +
+ + {{- $bodyText := $json.content -}} + {{- $bodyText = $bodyText | replaceRE `(

[\s]*(]*class="mention hashtag"[^>]*>#[^<]+[\s]*)+

[\s]*)+$` "" -}} + {{- $bodyText = $bodyText | replaceRE `` "" -}} + {{- $bodyText = $bodyText | replaceRE `` `

` -}} + {{- $bodyText = $bodyText | replaceRE `

]+>[\s]*

` "" -}} + +
+ {{ $bodyText | safeHTML }} +
+ + {{- if gt $imgCount 0 -}} + {{- if le $imgCount 2 -}} + {{- /* 1–2 images: flex row (2 images split 50/50, cropped) */ -}} +
+ {{- range $json.media_attachments -}} + {{- if eq .type "image" -}} + {{- $imgSrc := .preview_url -}} + {{- with resources.GetRemote .preview_url -}} + {{- with .Process "q85 webp" -}} + {{- $imgSrc = .RelPermalink -}} + {{- end -}} + {{- end -}} + {{ .description }} + {{- end -}} + {{- end -}} +
+ {{- else -}} + {{- /* 3+ images: scroll-snap carousel */ -}} + +
+
+ {{- range $json.media_attachments -}} + {{- if eq .type "image" -}} + {{- $imgSrc := .preview_url -}} + {{- with resources.GetRemote .preview_url -}} + {{- with .Process "q85 webp" -}} + {{- $imgSrc = .RelPermalink -}} + {{- end -}} + {{- end -}} + {{ .description }} + {{- end -}} + {{- end -}} +
+ {{- $imgAttachments := slice -}} + {{- range $json.media_attachments -}}{{- if eq .type "image" -}}{{- $imgAttachments = $imgAttachments | append . -}}{{- end -}}{{- end -}} +
+ {{- range $i, $_ := $imgAttachments -}} + + {{- end -}} +
+
+ + {{- end -}} + {{- end -}} + + {{- /* Link preview card — use original image URL, skip processing (CDN images can return 403) */ -}} + {{- with $json.card -}} + {{- if or (eq .type "link") (eq .type "photo") (eq .type "video") -}} +
+ {{- if .image -}} + {{ .title }} + {{- end -}} +
+
{{ .title }}
+ {{- if .description -}}
{{ .description }}
{{- end -}} +
{{ (urls.Parse .url).Host }}
+
+
+ {{- end -}} + {{- end -}} + +
+ + View on Fediverse +
+
+ +{{- /* Shared lightbox — injected once per page via DOM API (no innerHTML) */ -}} + -- cgit v1.2.3