Jekyll, FeedBurner and Global URLs

One of the advantages of using a statically generated website through a system such as Jekyll is that you always have a local, working copy of your website. Of course, to make this seamless you should use relative URLs -- that way when testing locally everything is loaded from local files, not remote files.

However, if your posts contain relative URLs then your generated RSS or Atom feed does too. This can be a problem when integrating with a third-party feed management system like FeedBurner. Any relative URLs in your feed become relative to FeedBurner's domain; not your domain like they should be.

The easiest way I found to solve this was to use the unix utility sed (short for stream editor). Sed allows you to find and replace content from the command line. Integrated into my "build and publish" script, it allows me to detect relative URLs in my generated atom.xml file and replace them with static ones before publishing to my web host.

I place the following lines in a file called 'absolute_urls.sed':

# Replace 'href="/' with 'href="http://www.slightlytallerthanaverageman.com/'

s/ href=\"\// href=\"http:\/\/www.slightlytallerthanaverageman.com\//g
s/ src=\"\// src=\"http:\/\/www.slightlytallerthanaverageman.com\//g

Remember that relative URLs are indicated by a "/" at the start. This uses the 's' command to find all instances of href="/ and replace it with href="http://www.slightlytallerthanaverageman.com/.

Sed is then called as follows:

#Replace relative URLs with absolute ones in the feed
sed -f _scripts/absolute_urls.sed _site/atom.xml > /tmp/atom.xml
mv /tmp/atom.xml _site/atom.xml

Magic!

blog comments powered by Disqus