How to insert the last updated time-stamp in Jekyll page at build time?

后端 未结 1 809
孤独总比滥情好
孤独总比滥情好 2020-12-15 09:21

I would like to automatically insert a last updated timestamp (Not the date variable for the page) for each post at Jekyll build time, how to achieve that? I th

相关标签:
1条回答
  • 2020-12-15 09:40

    The only collection that has a modified_time is site.static_files. Not so useful in our case.

    One way to get the last-modified-date for posts in your Jekyll site is to use a hook (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
    

    It's now available in your posts as : {{ page.last-modified-date }}. And you can format this date with the a date filter like {{ page.last-modified-date | date: '%B %d, %Y' }}. See the Alan W. Smith excellent article on date Jekill Liquid date formating topic.

    Important notice : hooks are not working on Github pages.

    0 讨论(0)
提交回复
热议问题