Rob*_*ert 27 date liquid jekyll
我知道我可以date在文件的YAML前端指定一个变量并使用它来访问它的值{{ page.date }}.这很麻烦,因为在更新文件时很容易忘记更改日期.那么如何访问文件的修改日期呢?
Cas*_*ase 30
这是一个相对较新的插件,可以满足您的需求:
https://github.com/gjtorikian/jekyll-last-modified-at
(在Google搜索此线程中其他答案的替代方法时找到它)
Jua*_*rro 17
从Jekyll 1.x开始,page.path它会为您提供当前正在处理的帖子或页面的文件名.添加以下过滤器(例如放置_plugins/myfilters.rb)以获取给定文件名的修改时间:
module Jekyll
module MyFilters
def file_date(input)
File.mtime(input)
end
end
end
Liquid::Template.register_filter(Jekyll::MyFilters)
Run Code Online (Sandbox Code Playgroud)
所以现在你可以在你的帖子和页面上呈现日期,例如
{{ page.path | file_date | date_to_string }}
Run Code Online (Sandbox Code Playgroud)
基于krlmlr的帖子,我写了一个预提交脚本来更新修改date:文件的YAML前端字段的修改时间.
#!/bin/sh
# Contents of .git/hooks/pre-commit
git diff --cached --name-status | grep "^M" | while read a b; do
cat $b | sed "/---.*/,/---.*/s/^date:.*$/date: $(date -u "+%Y-%m-%d %T %Z")/" > tmp
mv tmp $b
git add $b
done
Run Code Online (Sandbox Code Playgroud)
它对我来说很好.我实际上使用update:字段来指示修改的时间,并使用date:字段来指示创建时间.
我认为如果不使用 YAML frontmatter 就不可能做到这一点。date但请注意,在 frontmatter 中指定实际上会覆盖date帖子文件名中给定的内容。
因此,如果您有一个文件2013-02-19-my-post.md,并且其 frontmatter 中有date: 2013-02-20,则 frontmatter 优先,并且帖子的日期为2013-02-20。
如果您想跟踪帖子的修改时间,恐怕如果不在 frontmatter 中使用自定义字段就无法做到这一点。叫它modified-date什么的。是的,这有点乏味。我有一个 TextExpander 代码片段,可以以正确的格式自动输出当前日期/时间,这很方便。
| 归档时间: |
|
| 查看次数: |
6315 次 |
| 最近记录: |