Created
March 6, 2024 22:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{- 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