Gist – WordPress Sync
This is a tale of how this very section – hacks.ash.wine works.
Additionally I use Gistbox, which allows me to categorize/add labels to my gists and find search through them faster.
But not all my gists are important, and most of them aren’t useful for other people, so I wanted to archive all the ones that could be potentially useful for others at hacks.ash.wine.
Problems and Solutions
This could have been solved easily by creating a Jekyll repository at Github, and posting stuff there in markdown, but I like being able to have separate Gist repositories for separate things.
That too could be solved by writing a small script to sync my gists with the jekyll, but then
1. I’d have to port the WordPress theme/CSS/etc to a jekyll blog.
2. Redo changes to the theme header & footer every time it changed on the wordpress site.
3. I wouldn’t be able to keep multiple files in their own git repo for each ‘hack’.
Also, I’ve had bad luck with Jekyll (on docker on Windows) before.
The Original Solution
To do this, originally, I created a Python script which would go through all my gists, then all my WordPress posts, then compare if there were any gists that hadn’t been posted to WordPress yet, and post those.
The script searches for a
#gistblog hashtag in the gist description to identify if a gist should be synced, the rest of the hashtags as passed on as WordPress tags, the remaining gist description is used as the WordPress post title.
As far as the post content itself was concerned, the script simply posted the gist url as the body of the WordPress post, and let wordpress handle the embedding.
It worked out pretty well actually! Here’s an example of what an embedded post looks like : http://ash.wine/what-a-gist-embed-looks-like/
There’s a big problem with this approach – SEO.
There’s a chance that Google cannot see/will not index the asynchronously loaded content of the gist.
Well, what’s the point of aggregating content for others, if other can’t find it when they need it?
Introducing Gist-Wordpress-Sync 2.0
The new approach is an iteration over the previous one, but with a couple of new features:
- The application now requests the raw content of each tagged gist, runs it though the Github Markdown Parsing API, and posts the rendered HTML content to WordPress as the post’s body.
- It stores the gist’s
updated_atattributes as WordPress
custom_fieldswith every post (to track and update posts).
- Abstracts configuration settings into a
.settingsfile so the script can be easily configured for other blogs.
The biggest downside is that since WebHooks don’t work for Gists, the application has to be manually run when changes are made to Gists.
It is still very rough around the edges, and may require tweaking for it to fit your taste/preferences, and the configuration of your WordPress blog.
The repository is available here : https://github.com/Ashwinning/Gist-Wordpress-Sync
If you want to run this for your own blog and need any help, please feel free to open an issue.