Closed lcolladotor closed 2 years ago
Here's some R code I wrote to help migrate my blog posts. https://gist.github.com/lcolladotor/fb6d0cfd08b0baa26a9d114726d9bb62
Yes, I'll update the docs as well as the shortcode()
example and mention that the username is required for Hugo >= 0.89.0. Thanks!
Ok, sounds good! Thanks!
Awesome, thanks Yihui for https://github.com/rstudio/blogdown/commit/3c1ea2c7afcab5d735eb005615ece1d400bcc352. I looked at it and it's great. Thanks again!
Hello, if you'd like to update your tweet shortcodes with a python script (I used it for Hugo academic) I have a gist for you: https://gist.github.com/SolomonMg/2ad779d06ff0c0609d323014b2ec3b32
import os
import requests
# Translated from https://www.isjw.uk/post/blogging/fix-hugo-tweet-shortcodes/
# Using Chat GPT-3. Minor edits to allow for leading whitespace.
DIR = "/WHEREVER YOU WANT TO START/"
BearerToken = "INSERT BEARER TOKEN GET IT HERE https://developer.twitter.com/en/portal/dashboard"
# Function to use Twitter API to convert a Tweet ID to a Twitter Handle
def Get_TwitterHandleFromTweetID(TweetID, BearerToken):
URL = f"https://api.twitter.com/2/tweets/{TweetID}?expansions=author_id"
headers = {"Authorization": f"Bearer {BearerToken}"}
response = requests.get(URL, headers=headers)
return response.json()["includes"]["users"][0]["username"]
# Define what our current shortcode starts with
shortcode = "{{"+"< tweet "
# Get all the Markdown files in the Hugo folder
files = []
for dirpath, dirnames, filenames in os.walk(DIR):
for filename in filenames:
if filename.endswith('.md'):
files.append(os.path.join(dirpath, filename))
# Loop through the files
for file in files:
# Reset our Changed flag to false, until we make any changes to this file.
changed = False
# Get Lines in file containing Tweet shortcodes that we haven't yet updated
with open(file, 'r') as f:
content = f.read()
for tweetcode in [line for line in content.splitlines() if shortcode in line and 'user=' not in line]:
print("file: " + file)
# Extract the Tweet ID from the shortcode
tweetID = tweetcode.split(shortcode)[-1].split(" >}}")[0]
# Get TwitterHandle from Tweet ID
twitterHandle = Get_TwitterHandleFromTweetID(tweetID, BearerToken)
# Update the shortcode in the file content
content = content.replace(shortcode+tweetID, f'{shortcode}id="{tweetID}" user="{twitterHandle}"')
# Set our Changed flag to true as we've changed the file content
changed = True
# Update file if changes were made.
if changed:
# Write the updated file
with open(file, 'w') as f:
f.write(content)
# Make a note on the console that we've done this
print(f"Updated {file}")
Hi,
Given the changes in Twitter's API, hugo had to change their
shortcode
for it. See https://discourse.gohugo.io/t/the-tweet-shortcode-will-soon-require-two-named-parameters-user-and-id/35871/2 which links to https://github.com/gohugoio/hugo/commit/0cc39af68232f1a4981aae2e72cf65da762b5768.With that in mind, https://github.com/rstudio/blogdown/blob/34fd54646b35de50ad68002750fa74e6f93e70ee/docs/02-hugo.Rmd#L202 will likely need to change to
{{< tweet user="jtleek" id="852205086956818432" }}
and https://github.com/rstudio/blogdown/blob/34fd54646b35de50ad68002750fa74e6f93e70ee/docs/02-hugo.Rmd#L211 toblogdown::shortcode('tweet', user='jtleek', id='852205086956818432')
.Here's an example:
Created on 2022-04-30 by the reprex package (v2.0.1)
However, I'm not sure if this will affect users who are using older versions of Hugo. So I'm not sure if changing the documentation makes sense.
In any case,
hugo
will still work with the old shortcodes for a while.In my personal case, it's a pain since I used this shortcode a lot at https://github.com/lcolladotor/lcolladotorsource:
Best, Leo