Skip to content

Instantly share code, notes, and snippets.

@fareesh
Created March 6, 2024 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fareesh/5bead07cbc31e317452548aafb2c43b5 to your computer and use it in GitHub Desktop.
Save fareesh/5bead07cbc31e317452548aafb2c43b5 to your computer and use it in GitHub Desktop.
Hugo Shortcode to render both light & dark mode of a tweet. CSS can be used to show/hide as required.
{{- define "render-tweet" -}}
<div class="tweet-container">
{{- $id := .id -}}
{{- $user := .user -}}
{{- $url := printf "https://twitter.com/%v/status/%v" $user $id -}}
{{/* Light Theme Tweet */}}
<div class="tweet tweet-light" data-theme="light">
{{- $queryLight := querify "url" $url "dnt" .dnt "theme" "light" -}}
{{- $requestLight := printf "https://publish.twitter.com/oembed?%s" $queryLight -}}
{{- with resources.GetRemote $requestLight -}}
{{- with .Err -}}
{{- errorf "%s" . -}}
{{- else -}}
{{- (. | transform.Unmarshal).html | safeHTML -}}
{{- end -}}
{{- end -}}
</div>
{{/* Dark Theme Tweet */}}
<div class="tweet tweet-dark" data-theme="dark">
{{- $queryDark := querify "url" $url "dnt" .dnt "theme" "dark" -}}
{{- $requestDark := printf "https://publish.twitter.com/oembed?%s" $queryDark -}}
{{- with resources.GetRemote $requestDark -}}
{{- with .Err -}}
{{- errorf "%s" . -}}
{{- else -}}
{{- (. | transform.Unmarshal).html | safeHTML -}}
{{- end -}}
{{- else -}}
{{- warnidf "shortcode-twitter-getremote" "The %q shortcode was unable to retrieve the remote data. See %s" .name .position -}}
{{- end -}}
</div>
</div>
{{- end -}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment