在:site post_render hook中重新渲染一个Jekyll文档

时间:2018-01-03 23:24:50

标签: jekyll

我目前正在开发一个Jekyll插件,用于转换post_render :documents挂钩中的输出HTML。我想通过Liquid标签提供从这些转换结果中获得的一些信息。这显然是有问题的,因为我只能确保在调用post_render的{​​{1}}挂钩时执行了所有转换,但此时已对Liquid标签进行了评估。

这引出了我的问题:在:site被触发后,Jekyll插件是否可以触发页面的“重新呈现”,以便填充首次呈现页面时不可用的Liquid标签?

1 个答案:

答案 0 :(得分:1)

在阅读了部分Jekyll源代码(site.rbrenderer.rb)后,我学会了如何实现这样一个“两阶段”插件。

由于在渲染过程中会覆盖初始文件内容,因此必须明确存储它们:

Jekyll::Hooks.register [:documents, :pages], :pre_render do |doc|
  Jekyll::MyPlugin.unrendered_docs[doc.relative_path] = doc.content
end

在呈现网站后,可以按如下方式重新呈现文档doc

Jekyll::Hooks.register :site, :post_render do |site, payload|
  ...
  doc.content = Jekyll::MyPlugin.unrendered_docs[doc.relative_path]
  doc.output = Jekyll::Renderer.new(site, doc, payload).run()
  doc.trigger_hooks(:post_render)
  ...
end