Indenting generated markup in Jekyll/Ruby

前端 未结 2 825
醉话见心
醉话见心 2021-02-02 14:25

Well this is probably kind of a silly question but I\'m wondering if there\'s any way to have the generated markup in Jekyll to preserve the indentation of the Liquid-tag. World

2条回答
  •  庸人自扰
    2021-02-02 14:59

    We can accomplish this by writing a custom Liquid filter to tidy the html, and then doing {{content | tidy }} to include the html.

    A quick search suggests that the ruby tidy gem may not be maintained but that nokogiri is the way to go. This will of course mean installing the nokogiri gem.

    See advice on writing liquid filters, and Jekyll example filters.

    An example might look something like this: in _plugins, add a script called tidy-html.rb containing:

    require 'nokogiri'
    module TextFilter
      def tidy(input)
      desired = Nokogiri::HTML::DocumentFragment.parse(input).to_html
      end
    end
    Liquid::Template.register_filter(TextFilter)
    

    (Untested)

提交回复
热议问题