summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/syntax_highlight_filter.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/banzai/filter/syntax_highlight_filter.rb')
-rw-r--r--lib/banzai/filter/syntax_highlight_filter.rb13
1 files changed, 8 insertions, 5 deletions
diff --git a/lib/banzai/filter/syntax_highlight_filter.rb b/lib/banzai/filter/syntax_highlight_filter.rb
index 144d8c175dc..17f9164bcb7 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)