Import or Cross-Post Medium Articles Into Jekyll
I like publishing articles on my Medium Page, I get nice benefits to engage with the community, statistics, a good user interface, and tons of other stuff. I do most of my writing in iA Writer, which also has support for medium.com to publish directly.
Import Medium RSS Feed
However, I also want to import all of my writing into Jekyll to make it available on another page as well. I don’t just want to blindly duplicate content on the web. Jekyll conveniently provides an RSS importer, which I make use of to import my medium.com feed into my own Jekyll blog.
Canonical URL
To tell google and other search engines that medium is still the best place for my content, I’m adding a canonical URL to each entry in Jekyll to link to the original post on medium.
A canonical link element is an HTML element that helps webmasters prevent duplicate content issues in search engine optimization by specifying the “canonical” or “preferred” version of a web page.
Read more about the Canonical link element on Wikipedia.
Since Jekyll doesn’t set the canonical URL Front Matter in the YAML block, I made this single line change myself and import my own version of the RSS importer into Jekyll.
Jekyll Configuration
To make the importer work the right way, including the canonical URL, you need to install the jekyll-seo-tag plugin and use the updated version of jekyll-import. Just add this to your Gemfile in your Jekyll project:
gem 'jekyll-seo-tag'
gem "jekyll-import", git: "https://github.com/pew/jekyll-import.git"
If you haven’t used the seo-tag plugin before, you also need to add it to your _config.yml:
plugins:
- jekyll-seo-tag
Now, when the importer is executed, it’ll add the canonical URL to your imported markdown file and point to the original post on medium.
Import the Feed
Finally, let’s import the posts. I created a teeny-tiny script in my Jekyll blog home folder called import_feed.rb, so feel free to create this file and put the following contents into it and replace the your-username-here with your own:
require "jekyll-import"
JekyllImport::Importers::RSS.run({
"source" => "https://medium.com/feed/@your-username-here"
})
You can then execute this script with bundle exec ruby import_feed.rb and all your posts should show up in your _posts folder, including the canonical URL in the YAML block. It should look like this then:
Automate All the Things
Now, you don’t want to run this manually for each post you do, but rather on a regular schedule. I’m using GitHub Actions for this. I created a simple GitHub Action script which runs on a cron trigger once a day, if anything has changed in the _posts folder, it'll commit and push the changes back to GitHub and from there on, Cloudflare Pages will automatically pick it up and publishes the changes.