0

I'm trying to apply this hook to a custom collection:

Hook

Jekyll::Hooks.register :docs, :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

Collection

collections:
  docs:
    output: true

But right now the last-modified-date isn't being assigned.

I saw on this comment that using the collection name should work.

I'm trying to order them by date and list them, but right now the field is coming out empty. All my fields on doc where created before setting up the hook, so maybe I need to do something for it to work.

Any ideas? When does the hook run? (specially for pre-existent files) How should I set it up to work with a collection?

moondaisy
  • 4,303
  • 6
  • 41
  • 70

1 Answers1

1

Try this, it is supposed to output some info in your console when you build or serve you jekyll site locally. This can help to debug.

_plugins/hook-docs-pre-render.rb

Jekyll::Hooks.register :docs, :pre_render do |post|

  # debug
  puts "Firing :docs, :pre_render from : " + File.basename(__FILE__) + " for : " + post.relative_path

  # get the current post last modified time
  modification_time = File.mtime( post.path )

  # debug
  puts "modification_time = " + modification_time.strftime('%A, %B %dth %Y at %l:%M%p')

  # inject modification_time in post's datas.
  post.data['last-modified-date'] = modification_time

end

Note that hooks are not working on github pages.

David Jacquel
  • 51,670
  • 6
  • 121
  • 147