Spaces and special characters in Jekyll categories

Jekyll, the most popular (and one of my favourite) static site generator, allows you to categorize posts. The problem arises when you want a category name consisting of more than one word or/and containing special characters.

Let's say you want to have categories "Recenzje książek" and "Recenzje filmów" (Book and Movie reviews in Polish respectively). Both of these are two words categories with special Polish characters. Assigning category like this:

category: Recenzje książek

or

category: Recenzje filmów

you'd end up with space and special characters in the url. I think I came up with a simple workaround for this problem.

In your post metadata you need to use category names already cleaned up (dashes instead of spaces, no special characters):

category: recenzje-ksiazek

or

category: recenzje-filmow

Then you create categories.yml file in your _data directory containing


- name: Recenzje książek
  slug: recenzje-ksiazek
- name: Recenzje filmów
  slug: recenzje-filmow

In your layout templates, where you'd normally use

{{ post.category }}

you put

{% for category in site.data.categories %}
  {% if post.category == category.slug %}
    {{ category.name }}
  {% endif %}
{% endfor %}

This way you have spaces and special characters visible on your site (+ Title Case if you want), but keep dashes, no special characters and lower case in the urls.

comments powered by Disqus