syntax_highlight_filter.rb 1.2 KB
Newer Older
1 2
require 'rouge/plugins/redcarpet'

3 4
module Banzai
  module Filter
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    # HTML Filter to highlight fenced code blocks
    #
    class SyntaxHighlightFilter < HTML::Pipeline::Filter
      include Rouge::Plugins::Redcarpet

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

        doc
      end

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

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

30 31
        # Extracted to a method to measure it
        replace_parent_pre_element(node, highlighted)
32 33 34 35
      end

      private

36 37 38 39 40
      def replace_parent_pre_element(node, highlighted)
        # Replace the parent `pre` element with the entire highlighted block
        node.parent.replace(highlighted)
      end

41 42 43 44 45 46 47 48
      # Override Rouge::Plugins::Redcarpet#rouge_formatter
      def rouge_formatter(lexer)
        Rouge::Formatters::HTMLGitlab.new(
          cssclass: "code highlight js-syntax-highlight #{lexer.tag}")
      end
    end
  end
end