Class: Banzai::Filter::KrokiFilter
- Inherits:
-
HTML::Pipeline::Filter
- Object
- HTML::Pipeline::Filter
- Banzai::Filter::KrokiFilter
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/banzai/filter/kroki_filter.rb
Overview
HTML that replaces all diagrams supported by Kroki with the corresponding img tags. If the source content is large then the hidden attribute is added to the img tag.
Constant Summary collapse
- MAX_CHARACTER_LIMIT =
2000
Instance Method Summary collapse
Instance Method Details
#call ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/banzai/filter/kroki_filter.rb', line 16 def call return doc unless settings.kroki_enabled diagram_selectors = ::Gitlab::Kroki.formats(settings) .map do |diagram_type| %(pre[data-canonical-lang="#{diagram_type}"] > code, pre > code[data-canonical-lang="#{diagram_type}"]) end .join(', ') xpath = Gitlab::Utils::Nokogiri.css_to_xpath(diagram_selectors) return doc unless doc.at_xpath(xpath) diagram_format = "svg" doc.xpath(xpath).each do |node| diagram_type = node.parent['data-canonical-lang'] || node['data-canonical-lang'] next unless diagram_selectors.include?(diagram_type) diagram_src = node.content image_src = create_image_src(diagram_type, diagram_format, diagram_src) img_tag = Nokogiri::HTML::DocumentFragment.parse(content_tag(:img, nil, src: image_src)) img_tag = img_tag.children.first next if img_tag.nil? lazy_load = diagram_src.length > MAX_CHARACTER_LIMIT img_tag.set_attribute('hidden', '') if lazy_load img_tag.set_attribute('class', 'js-render-kroki') img_tag.set_attribute('data-diagram', diagram_type) img_tag.set_attribute('data-diagram-src', "data:text/plain;base64,#{Base64.strict_encode64(diagram_src)}") node.parent.replace(img_tag) end doc end |