Resolve Tweet URLs

When embedding a Tweet, the tweet doesn’t include the url originally posted in the Tweet.
So, If I post a link to microsoft.com it will be translated to t.co/blablabla.

I believe that it would be a good idea to follow the redirect and resolve the original URL before rendering.

Please post an example of your call to the embedded tweet shortcode.

Okay,
Example: I embed a tweet in a post like this:

...
{{<tweet naim94a 1440468453211590658>}}
...

Which would render something like the following html:

<blockquote class="twitter-tweet">
  <p lang="en" dir="ltr">
    <a href="https://t.co/8i0INuKHA4">https://t.co/8i0INuKHA4</a>’s certificate is due to expire on October 2. Please download the new certificate and adjust the port to 1235 in order to continue using lumen.<a href="https://twitter.com/hashtag/IDAPro?src=hash&amp;ref_src=twsrc%5Etfw">#IDAPro</a> <a href="https://twitter.com/hashtag/Lumina?src=hash&amp;ref_src=twsrc%5Etfw">#Lumina</a>
  </p>
  &mdash; Naim A. (@naim94a) <a href="https://twitter.com/naim94a/status/1440468453211590658?ref_src=twsrc%5Etfw">September 22, 2021</a>
</blockquote>

The link in the beginning of the tweet is my website, while I understand Twitter likes analytics - It shouldn’t affect the link’s text. I would prefer to get hugo to make a HEAD request to any t.co url and resolve the original text, in python it would be something like this:

import requests
r = requests.head('https://t.co/8i0INuKHA4') # This should be a 301 redirect response
url = r.headers['location']
# strip off leading scheme and trailing '/' if it's only a domain

… which could render this more readable link instead:

<a href="https://t.co/8i0INuKHA4">abda.nl</a>’s certificate ...

If dnt is set, I would also replace the href

Issue: It’s not possible to know the exact url using this method. The link could have included a http/s url scheme or in may have been omitted. The url may not have had a trailing ‘/’. I don’t know if Twitter’s API provides the original text & link, it doesn’t look like oembed does.
If it’s possible to allow user’s to provide hugo with API keys for twitter - It would be possible by reading the Tweet’s entities in the Tweet object.

Unless Twitter changes their API, we have no control over this. We simply return what we receive.

You can roll your own shortcode, placing it in layouts/shortcodes/tweet.html. The embedded shortcode is here:

https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/shortcodes/twitter.html

Note that tweet is aliased to twitter.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.