对于经常更新帖子的人来说,有必要根据帖子从新到旧进行排序最后修改日期而不是 Jekyll 默认按发布日期排序。
似乎没有简单的方法可以实现这一点。我已经阅读并测试了几乎所有的方法。
这是有效的(部分符合预期):
用过这个宝石https://github.com/gjtorikian/jekyll-last-modified-at https://github.com/gjtorikian/jekyll-last-modified-at但现在我必须手动输入last_modified_at
在每个帖子的 front-matter 中,以便 Jekyll 使用此循环对帖子进行排序:
{% assign sorted_posts = site.posts | sort: "last_modified_at" | reverse %}
{% for post in sorted_posts %}
<!-- CODE HERE -->
{% endfor %}
问题是有last_modified_at
在每个帖子中,因为每次我按 CTRL+S 保存帖子时,它都会阻止插件自动设置该值。
有什么办法可以自动化这个吗?
编辑:它只适用于开发,不知道为什么它不适用于生产。每当我部署网站时,它都会以相同的日期更新所有帖子。
我非常感谢 hooks 和 ofc 的这篇文章:https://stackoverflow.com/a/36769049 https://stackoverflow.com/a/36769049
Steps:
- 在中创建了新文件
_plugins
文件夹名为hook-add-last-modified-date.rb
-
将此代码粘贴到其中并保存:
Jekyll::Hooks.register :posts, :pre_render do |post|
# get the current post last modified time
modification_time = File.mtime( post.path )
# inject modification_time in post's datas.
post.data['last-modified-date'] = modification_time
end
当我在帖子上按 CTRL+S 时,会发生 2 件事:a) 更新我所在位置的最后修改日期{{ post.last-modified-date | date_to_xmlschema }}
b) 它bumps它位于索引页上我的帖子的顶部,因为它是按该变量排序的。
爱你们!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)