Guest User

Untitled

a guest
Jul 13th, 2026
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.62 KB | None | 0 0
  1. {{/*
  2.     Self-hosted Instagram feed - fully self-contained, drop-in Hugo shortcode.
  3.  
  4.     At build time this fetches the SnapWidget embed, extracts the Instagram
  5.     image URLs, downloads each image with resources.GetRemote and serves it
  6.     from your own domain (resized, aspect ratio preserved). No iframe, no
  7.     third-party script, no tracking - so it can't be blocked by Firefox or
  8.     other privacy browsers. All markup, styling and the placeholder logo are
  9.     contained in this one file; nothing else in the project needs editing.
  10.  
  11.     If the remote fetch fails at build time a grey placeholder grid with
  12.     Instagram logos is rendered as a fallback.
  13.  
  14.     Usage: {{< instagram id="1126243" count="6" >}}
  15.       id    = SnapWidget embed id (default: 1126243)
  16.       count = max number of images to show (default: 6)
  17. */}}
  18. {{ $id := .Get "id" | default "1126243" }}
  19. {{ $count := int (.Get "count" | default 6) }}
  20. {{ $opts := dict "headers" (dict "User-Agent" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36") }}
  21.  
  22. {{ $posts := slice }}
  23. {{ $embed := try (resources.GetRemote (printf "https://snapwidget.com/embed/%s" $id) $opts) }}
  24. {{ with $embed.Err }}
  25.     {{ warnf "instagram: could not fetch SnapWidget embed %s: %s" $id . }}
  26. {{ else with $embed.Value }}
  27.     {{ $imgTags := findRE `<img[^>]*data-src-medium="[^"]+"[^>]*>` .Content }}
  28.     {{ range $imgTags }}
  29.         {{ $tag := . }}
  30.         {{ $src := $tag | replaceRE `(?s).*data-src-medium="([^"]+)".*` "$1" | htmlUnescape }}
  31.         {{ $link := $tag | replaceRE `(?s).*data-link="([^"]+)".*` "$1" | htmlUnescape }}
  32.         {{ if hasPrefix $src "http" }}
  33.             {{ $img := try (resources.GetRemote $src $opts) }}
  34.             {{ with $img.Err }}
  35.                 {{ warnf "instagram: could not fetch image: %s" . }}
  36.             {{ else with $img.Value }}
  37.                 {{ $thumb := .Fill "500x500" }}
  38.                 {{ $posts = $posts | append (dict "thumb" $thumb "link" (cond (hasPrefix $link "http") $link "#")) }}
  39.             {{ end }}
  40.         {{ end }}
  41.     {{ end }}
  42. {{ end }}
  43.  
  44. {{ $posts = $posts | first $count }}
  45.  
  46. {{/* Emit the stylesheet only once per page, even if the shortcode is used multiple times. */}}
  47. {{ if not (.Page.Scratch.Get "instagramCSS") }}
  48. {{ .Page.Scratch.Set "instagramCSS" true }}
  49. <style>
  50. .instagram-widget {width: 100%; margin: 0 auto 1.5em;}
  51. .instagram-grid, .instagram-widget__placeholder {display: flex; gap: 8px;}
  52. .instagram-grid a {display: block; overflow: hidden;}
  53. .instagram-grid img {width: 100%; height: auto; display: block;}
  54. .instagram-widget__placeholder span {aspect-ratio: 1 / 1; background: #e9e9e9 url("data:image/svg+xml,<svg%20xmlns='http://www.w3.org/2000/svg'%20width='24'%20height='24'%20viewBox='0%200%2024%2024'%20fill='none'%20stroke='%23aaa'%20stroke-width='2'><rect%20x='2'%20y='2'%20width='20'%20height='20'%20rx='5'/><circle%20cx='12'%20cy='12'%20r='5'/><circle%20cx='17.5'%20cy='6.5'%20r='1.3'%20fill='%23aaa'%20stroke='none'/></svg>") center / 3rem no-repeat;}
  55. </style>
  56. {{ end }}
  57.  
  58. <div class="instagram-widget">
  59.     {{ if $posts }}
  60.     <div class="instagram-grid">
  61.         {{ range $posts }}
  62.         <a href="{{ .link }}" target="_blank" rel="noopener nofollow" aria-label="Bekijk op Instagram">
  63.             <img src="{{ .thumb.RelPermalink }}" width="{{ .thumb.Width }}" height="{{ .thumb.Height }}"
  64.                 loading="lazy" alt="Instagram" />
  65.         </a>
  66.         {{ end }}
  67.     </div>
  68.     {{ else }}
  69.     <div class="instagram-widget__placeholder" aria-hidden="true">
  70.         {{ range (seq $count) }}<span></span>{{ end }}
  71.     </div>
  72.     {{ end }}
  73. </div>
Advertisement
Add Comment
Please, Sign In to add comment