diff --git a/lib/banzai/filter/syntax_highlight_filter.rb b/lib/banzai/filter/syntax_highlight_filter.rb index 144d8c175dc2aab2e43a1449a3070f7a243c382a..17f9164bcb798eba80c43f8f8e67456eb2079331 100644 --- a/lib/banzai/filter/syntax_highlight_filter.rb +++ b/lib/banzai/filter/syntax_highlight_filter.rb @@ -19,8 +19,15 @@ module Banzai language = node.attr('class') code = node.text + lexer = Rouge::Lexer.find_fancy(language) + formatter = Rouge::Formatters::HTML.new + css_classes = "code highlight js-syntax-highlight #{lexer.tag}" + begin - highlighted = %<<pre class="#{css_classes}"><code>#{block_code(code, language)}</code></pre>> + highlighted = '' + highlighted << %<<pre class="#{css_classes}"><code>> + highlighted << formatter.format(lexer.lex(code)) + highlighted << %<</code></pre>> rescue # Gracefully handle syntax highlighter bugs/errors to ensure # users can still access an issue/comment/etc. @@ -31,10 +38,6 @@ module Banzai replace_parent_pre_element(node, highlighted) end - def css_classes - "code highlight js-syntax-highlight #{lexer.tag}" - end - private def replace_parent_pre_element(node, highlighted)