Parameterized Categories In Urls For Jekyll

This blog runs now on Jekyll for a while now. Since I feel this type of content management is really simple, flexible and prortable, I started to move our whole FAQ System over at gigmit to Jekyll too. There are other reasons for doing our own FAQ like Uservoice is a really crappy piece of software but thats another story. So we started with a minimal skeleton to get everything right before everyone in the company can contribute to it through Github. Builds will be triggered with a webhook from Github automatically. SEO-wise we wanted to have the category name in the URL of every article we write, so I wrote a small plugin. Jekyll only does basic (and ugly) conversion of category names to url friendly slugs, so I used Rails' ActiveSupport for it.

#Jekyll default bevaviour
"Kosten & Preise" -> ".com/kosten & preise/my_faq_article"
#with ActiveSupport
"Kosten & Preise" -> ".com/kosten-preise/my_faq_article"

In particular it isn't a real plugin but it monkey patches the conversion from category name to an URL. Just put this piece of code in a file in your _plugins/ folder, add ActiveSupport to your Gemfile (hopefully you have one for your blog!) and then you are good to go and have nice URLs!

require 'active_support/all'

module Jekyll
  class Post
    alias_method :url_placeholders_without_parameterize, :url_placeholders

    def url_placeholders
      #set this to avoid deprecation message
      I18n.enforce_available_locales = false

      url_placeholders_without_parameterize.merge({
        tags: (categories || []).map { |c| ActiveSupport::Inflector.parameterize(c.to_s) }.join('/'),
      })
    end
  end
end