Colmjude

/ notes
Notes
A collection of thoughts, things I've learnt and things I want to remember

Embedding tweets in markdown notes

I wanted to embed a tweet in a note. I thought, everyone does this so it must be easy.

Now it is easy if you want to manually go to twitter each time you want to embed a tweet and copy the embed code.

But I wanted to be able to use the URL so that it would be marginally quicker (and easier to read in the markdown).

I looked for an existing python markdown extension but couldn't find one. I'm sure there must be one somewhere but finding it was beyond me.

So, instead, I wrote my own 🫣

Firstly, I used this blog post about embedding a tweet in a flask blog as inspiration.

Task one was setting up a Twitter developer account - to get credentials for accessing twitter. It's easy and free. You need to create a project, an app and then generate an access token to go with the API key.

I used the tweepy library which has a get_oembed function. This function takes a twitter url and returns the HTML for embedding the tweet.

I then created a markdown extension that would match a pattern like

{{:twitter <twitter-url>}}

For example

{{:twitter https://twitter.com/ColmBritton/status/1583455055088271360}}

Which should embed one of my tweets...

Useful links