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 it will be translated to

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.

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=""></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=";ref_src=twsrc%5Etfw">#IDAPro</a> <a href=";ref_src=twsrc%5Etfw">#Lumina</a>
  &mdash; Naim A. (@naim94a) <a href="">September 22, 2021</a>

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 url and resolve the original text, in python it would be something like this:

import requests
r = requests.head('') # 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=""></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:

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.