Class: Banzai::Filter::SyntaxHighlightFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Includes:
Rouge::Plugins::Redcarpet
Defined in:
lib/banzai/filter/syntax_highlight_filter.rb

Overview

HTML Filter to highlight fenced code blocks

Instance Method Summary collapse

Instance Method Details

#callObject


10
11
12
13
14
15
16
# File 'lib/banzai/filter/syntax_highlight_filter.rb', line 10

def call
  doc.search('pre > code').each do |node|
    highlight_node(node)
  end

  doc
end

#highlight_node(node) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/banzai/filter/syntax_highlight_filter.rb', line 18

def highlight_node(node)
  language = node.attr('class')
  code     = node.text

  begin
    highlighted = block_code(code, language)
  rescue
    # Gracefully handle syntax highlighter bugs/errors to ensure
    # users can still access an issue/comment/etc.
    highlighted = "<pre>#{code}</pre>"
  end

  # Replace the parent `pre` element with the entire highlighted block
  node.parent.replace(highlighted)
end