我使用 Jekyll,我需要在将 Markdown 渲染为 HTML 后在 HTML 标签中添加类。
我可以使用 jQuery 或简单的 JavaScript 来完成这项任务,但我想了解钩子(Hook)如何与 Jekyll 一起工作。
我在 Gemfile 中写了这个:
Jekyll::Hooks.register :pages, :post_render do |post|
# here I need write something with Ruby language
end
集合中只有少数页面受此影响:我该怎么做?
我想补充一点:
class="line-numbers"
对于 HTML 渲染文件中的所有 pre
标签。
最佳答案
如果你想解析一个集合的文档,你可以用Nokogiri来完成。 .
您可以像这样使用 _plugins/hook_pre.rb
:
Jekyll::Hooks.register :documents, :post_render do |document|
require 'nokogiri'
# as posts are also a collection, we can have a document from posts
# or even from another collection
#### SET YOUR COLLECTION NAME HERE ####
isSearchable = document.collection.label == 'mycollection'
if isSearchable == true
doc = Nokogiri::HTML(document.output)
pre = doc.css("pre").add_class('line-numbers')
document.output = doc.to_html
end
end
关于ruby - 如何在使用 Jekyll 渲染后在 HTML 文件中添加一些类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35977346/