7

I have problem that categories are not url encoded when I use german words with Umlauts (e.g. ä, ü). I tried the cgi_escape that Liquid seems to offer, but success with the following code:

<strong>Kategorien</strong><br/>
{% for category in site.categories  do %}
  <small><a href="/categories/{{ category[0] | cgi_escape }}">{{ category[0] }} </a><br/>
         </small>    
{% endfor %}

Can anyone help?

5
  • what does the link look like now? /categories/eichhörnchen ? Apr 12, 2013 at 16:39
  • Yes: it looks like categories/eichhörnchen
    – Peterb
    Apr 12, 2013 at 19:10
  • and you want eichh%C3%B6rnchen or something like that? Apr 12, 2013 at 19:59
  • Yes that is what I expected when using the cgi_escape . At the moment I get an g error message (invalid byte sequence in US-ASCII ) if I click on the /categories/eichhörnchen link.
    – Peterb
    Apr 13, 2013 at 6:36
  • But it could also be another problem. Files seem to be created, but only Webrick is throwing an error: invalid byte sequence in US-ASCII // WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20) at 0.0.0.0:4000
    – Peterb
    Apr 13, 2013 at 10:27

2 Answers 2

7

Using cgi_escape doesn't work correctly for categories with spaces. Links were generated as /category/the+category instead of /category/the%20category.

The solution I ended up using was from this blog post:

# _plugins/url_encode.rb
require 'liquid'
require 'uri'

# Percent encoding for URI conforming to RFC 3986.
# Ref: http://tools.ietf.org/html/rfc3986#page-12
module URLEncoding
  def url_encode(url)
    return URI.escape(url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  end
end

Liquid::Template.register_filter(URLEncoding)

A plus is a literal plus anywhere but in the query portion of the URL, where it represents a space. Good URL encoding reference (archive.org mirror).

This can then be used in a layout or anywhere else:

<a href="{{ site.category_dir }}/{{ category | url_encode }}">
3
  • The blog post is not accessible anymore.
    – jcubic
    Nov 5, 2017 at 10:24
  • @jcubic Do you think I should remove the link? I quoted the important part just for such an eventuality.
    – opello
    Nov 6, 2017 at 17:08
  • You can wrap the lines in <strike></strike> you can also put {{ url | url_encode }} so it's obvious how to use the plugin.
    – jcubic
    Nov 6, 2017 at 20:39
0

@Peterb, have you upgraded to the latest version of Jekyll? The current 1.0x version supports UTF-8 and handles URLs like this much better.

You can install the latest version by running this from your Terminal command line:

$ [sudo] gem install jekyll --pre

This GitHub Issue post will shed more light on the issue: https://github.com/mojombo/jekyll/issues/960

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.