“没有将整数整数隐式转换为字符串"具有标题标题的Jekyll帖子出现错误

问题描述:

我有一个Jekyll博客托管了 .这样做时,我发现Jekyll的最新更新导致我的博客不再正确构建.运行本地安装时,我一直遇到以下错误:

I have a Jekyll blog hosted on GitHub Pages that I recently updated to HTTPS. In doing so, I discovered that recent updates to Jekyll were causing my blog to no longer build properly. Running a local install I kept encountering the following error:

Liquid Exception: no implicit conversion of Integer into String in /_layouts/default.html

经过反复试验,我能够确定以下引起问题的帖子:

After some trial and error I was able to identify the following posts as causing the problem:

2003-09-21-100.md
2004-02-10-10000.md
2004-02-28-228.md
2004-09-10-1.md
2004-10-10-1969.md
2004-11-06-1896.md
2005-05-14-616.md

这些都是我导入到Jekyll中的所有旧Wordpress帖子,并且您可以看到,它们都带有数字作为标题.但是,它并没有在Jekyll的早期版本上引起构建问题.虽然我可以重命名所有这些旧帖子,但是这会有些麻烦,因为我必须更新任何Disqus评论的URL,并且网络上与这些帖子的任何现有链接都将被破坏.有谁知道是否有更简单的方法来解决此问题?谢谢!

These are all old Wordpress posts that I imported into Jekyll, and as you can see, they all have numbers as titles. It didn't cause build problems on earlier versions of Jekyll however. While I could rename all of these old posts, it would be a bit of a pain since I would have to update any Disqus comments URLs, and any existing links on the web to those posts would be broken. Does anyone know if there is an easier way to fix this problem? Thanks!

真正的问题是escape过滤器无法处理数字,例如:

The real problem is that the escape filter isn't able to handle numbers, for example:

{{ 1618 | escape }}

投掷:

/tmp/vendor/ruby/2.3.0/gems/liquid-4.0.0/lib/liquid/standardfilters.rb:36:in `escapeHTML': no implicit conversion of Fixnum into Strin
g (TypeError)                  

当您在标题中使用escape时:<title>{% if page.title %}{{ page.title | escape }}{% else %}{{ site.title | escape }}{% endif %}</title>并且许多标题只有数字,那么它将失败.

As you are using escape in titles: <title>{% if page.title %}{{ page.title | escape }}{% else %}{{ site.title | escape }}{% endif %}</title> and many titles has only numbers, then it will fail.

要解决的一个快速技巧是在使用escape过滤器之前,使用capture变量标签将它们转换为字符串:

A quick hack to fix it is to convert them to strings before using the escape filter, using the capture variable tag:

捕获开始和结束标记内的字符串并分配 它到一个变量.使用捕获创建的变量将被存储 作为字符串.

Captures the string inside of the opening and closing tags and assigns it to a variable. Variables that you create using capture are stored as strings.

_includes/head.html中:

{% capture mytitle%}{{page.title}}{%endcapture%}
<title>{% if mytitle %}{{ mytitle | escape }}{% else %}{{ site.title | escape }}{% endif %}</title>