如何在构建时在 Jekyll 页面中插入最后更新的时间戳?
How to insert the last updated time-stamp in Jekyll page at build time?
我想在 Jekyll 构建时为每个 post 自动插入最后更新的时间戳(不是页面的 date
变量),如何实现?我想我必须声明一个变量,但我不确定如何为该变量赋值。
例如,有一次我更新一个旧的post,除了显示post日期,我还想显示最后更新日期。
我已经试过了{{Time.now}}
但是好像不行。
唯一具有 modified_time
的集合是 site.static_files
。在我们的案例中没那么有用。
为您的 Jekyll 站点中的帖子获取 last-modified-date
的一种方法是使用挂钩 (documentation)。
_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
它现在可以在您的帖子中使用:{{ page.last-modified-date }}
。
您可以使用 {{ page.last-modified-date | date: '%B %d, %Y' }}
之类的日期过滤器来格式化此日期。见 Alan W. Smith excellent article on date Jekill Liquid date formating topic.
重要通知:挂钩在 Github 页面上不起作用。
我想在 Jekyll 构建时为每个 post 自动插入最后更新的时间戳(不是页面的 date
变量),如何实现?我想我必须声明一个变量,但我不确定如何为该变量赋值。
例如,有一次我更新一个旧的post,除了显示post日期,我还想显示最后更新日期。
我已经试过了{{Time.now}}
但是好像不行。
唯一具有 modified_time
的集合是 site.static_files
。在我们的案例中没那么有用。
为您的 Jekyll 站点中的帖子获取 last-modified-date
的一种方法是使用挂钩 (documentation)。
_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
它现在可以在您的帖子中使用:{{ page.last-modified-date }}
。
您可以使用 {{ page.last-modified-date | date: '%B %d, %Y' }}
之类的日期过滤器来格式化此日期。见 Alan W. Smith excellent article on date Jekill Liquid date formating topic.
重要通知:挂钩在 Github 页面上不起作用。