I’m Paul Herron and I’m a web developer. I build online applications
with tools like CakePHP and jQuery.

Custom title formatting in Jekyll

by Paul Herron on 02 March 2014

Just a quick tip for this week's blog post.

Jekyll makes some useful inferences that reduces the metadata you have to explicitly declare. For example, given a Markdown file like 2014-03-02-my-test-post.markdown it'll extract the publication date and title automatically, so you don't have to specify these as a variables in the post's front matter.

In other words it's about convention over configuration and sane defaults: doing something useful with what data it has rather than forcing you to declare these things explicitly. When it came to titles though I found myself wanting to make this default just a little saner: Jekyll makes titles in a "Title Case With Every Word Capitalized" format which looks a bit weird to me. At very least I'm used to keeping smaller words like "a", "the" and "with" lowercase even within titlecased text, and in fact these days I'll usually skip the titlecasing entirely and just write titles in a normal sentence style, with only the first letter capitalised. Not to mention an awful lot of my titles contain terms like ffmpeg which shouldn't be capitalised at all. These requirements mean that for me, the autogenerated title was almost never correct.

Obviously I could override all of my titles in the post's front matter, but I figure it's better to stick with the convention over configuration approach and at least have a stab at autogenerating a useable title. Happily that's easy to do in Jekyll! You can just drop in a little plugin (I created it at _plugins/title.rb) with the following code that overrides the core titleized_slug method:

class Jekyll::Post

  def titleized_slug
    self.slug.split(/[_-]/).join(' ').capitalize
  end
end

With this plugin present I find a lot of my titles get autogenerated correctly. And if they don't, I can always add that override in the post's front matter.

Back to homepage

Next: