Jekyll 在 frontmatter 中错误的日期识别

Jekyll's erratic date recognition in frontmatter

我最近开始尝试使用 Jekyll(v 3.0.1,在 Ubuntu 上使用 Ruby 2.2.3p173),我不确定我是否发现了错误或它是某种 PEBKAC。

我有几篇文章,我想使用它们在 frontmatter 中定义的 "date" 值进行排序,如下所示:

---
title: Whatever
category: foo
date: 2014-09-7
---

我一直在使用以下 Liquid 标签生成排序集合:

{% assign sorted=site.pages | where: 'category', include.category | sort: 'date' %}

它工作正常,但当我添加文章时,Jekyll 突然崩溃并显示以下错误消息:

"Liquid Exception: Liquid error: comparison of Jekyll::Page with Jekyll::Page failed in (the file name)"

经过大量试验,我写了一个自定义插件,但还是不行。然而,检查集合发现日期属性的值有时会神奇地转换为日期对象,但大多数情况下它是一个字符串。

下面是我想不通的部分。

“2015-12-10”有什么特别之处?

"2015-12-9" 仍然是一个字符串 "but "2015-15-10" 成为一个日期对象。实际上,如果月份和日期都是 2数字,Jekyll 爆炸了。

有趣的是,如果我通过引用日期值修改了有问题的 frontmatter,它开始按预期工作:

---
title: Whatever 2
category: foo
date: "2015-12-15"
---

我在这里错过了什么?

Jekyll 如何看待 front matter 值?

dates:
  - "2015-12-21" # String
  - 2015-12-21   # Date
  - 2015-12-1    # String
  - 2015-12-01   # Date
  - 2015-12-21 12:21:22  # Time
  - 2015-12-21 12:21:22 +0100 # Time

排序字符串

如果您将 'dates' 排序为 date: "yyyy-mm-dd" 与 'date' 混合排序为 date: "yyyy-mm-d" 这将失败。

---
datesAsStrings:
  - "2015-12-1"
  - "2015-12-3"
  - "2015-12-12"
---
{% assign sortedDates = page.datesAsStrings | sort %}
{% for date in sortedDates %}<p>{{ date }}</p>{% endfor %}

Returns :

2015-12-1
2015-12-12
2015-12-3

日期排序

如果您对有效日期 (yyyy-mm-dd) 进行排序,则排序没问题。

---
datesAsDates:
  - 2015-12-01
  - 2015-12-03
  - 2015-12-21
---
{% assign sortedDates = page.datesAsDates | sort %}
{% for date in sortedDates %}<p>{{ date }}</p>{% endfor %}

returns :

2015-12-01
2015-12-03
2015-12-21

炸毁它

如果将日期 (yyyy-mm-dd) 与字符串 (yyyy-mm-d) 混合使用

mixed:
  - 2015-12-1
  - 2015-12-03
{% assign sortedDates = page.mixed | sort %}
{% for date in sortedDates %}<p>{{ date }}</p>{% endfor %}

Error: Liquid error: comparison of String with Date failed

或与时代 (yyyy-mm-dd hh:mm:ss)

---
datesAsDatesOrTime:
  - 2015-12-01
  - 2015-12-03 12:12:12 +0100
---
{% assign sortedDates = page.datesAsDatesOrTime | sort %}
{% for date in sortedDates %}<p>{{ date }}</p>{% endfor %}

Error: Liquid error: comparison of Date with Time failed

这与您在对具有不同日期类型的页面进行排序时遇到的问题相同。

结论

  • 在前面,Jekyll 的有效 Datemydate: yyyy-mm-dd 不是 mydate: "yyyy-mm-dd"mydate: yyyy-mm-d
  • 您只能对相同类型的元素进行排序。
  • 您只能对页面、帖子或 collections 具有相同类型的变量进行排序。