简体   繁体   English

如何在jekyll液体中进行url编码?

[英]How to url encode in jekyll liquid?

I have problem that categories are not url encoded when I use german words with Umlauts (eg ä, ü). 我有问题,当我使用德语单词与变音符号(例如ä,ü)时,类别不是url编码的。 I tried the cgi_escape that Liquid seems to offer, but success with the following code: 我尝试了Liquid似乎提供的cgi_escape,但是使用以下代码成功:

<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? 有人可以帮忙吗?

Using cgi_escape doesn't work correctly for categories with spaces. 对于带空格的类别,使用cgi_escape无法正常工作。 Links were generated as /category/the+category instead of /category/the%20category . 链接生成为/category/the+category而不是/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. 加号是一个文字加上任何地方,但在URL的查询部分,它代表一个空格。 Good URL encoding reference 好的URL编码参考 ( archive.org mirror ). archive.org镜像 )。

This can then be used in a layout or anywhere else: 然后可以在布局或其他任何地方使用它:

<a href="{{ site.category_dir }}/{{ category | url_encode }}">

@Peterb, have you upgraded to the latest version of Jekyll? @Peterb,你升级到最新版的Jekyll? The current 1.0x version supports UTF-8 and handles URLs like this much better. 当前的1.0x版本支持UTF-8,并且更好地处理这样的URL。

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 这个GitHub问题帖子将更多地阐述这个问题: https//github.com/mojombo/jekyll/issues/960

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM