Class: Banzai::Filter::SyntaxHighlightFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Includes:
Concerns::OutputSafety, Concerns::PipelineTimingCheck, Concerns::TimeoutFilterHandler
Defined in:
lib/banzai/filter/syntax_highlight_filter.rb

Overview

HTML Filter to highlight fenced code blocks

Constant Summary collapse

CSS_CLASSES =
'code highlight js-syntax-highlight'
CSS =
'pre:not([data-kroki-style]) > code:only-child'
XPATH =
Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze

Constants included from Concerns::TimeoutFilterHandler

Concerns::TimeoutFilterHandler::COMPLEX_MARKDOWN_MESSAGE, Concerns::TimeoutFilterHandler::RENDER_TIMEOUT, Concerns::TimeoutFilterHandler::SANITIZATION_RENDER_TIMEOUT

Constants included from Concerns::PipelineTimingCheck

Concerns::PipelineTimingCheck::MAX_PIPELINE_SECONDS

Instance Method Summary collapse

Methods included from Concerns::OutputSafety

#escape_once

Methods included from Concerns::PipelineTimingCheck

#exceeded_pipeline_max?

Instance Method Details

#callObject



22
23
24
25
26
27
28
# File 'lib/banzai/filter/syntax_highlight_filter.rb', line 22

def call
  doc.xpath(XPATH).each do |node|
    highlight_node(node)
  end

  doc
end

#highlight_node(code_node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/banzai/filter/syntax_highlight_filter.rb', line 30

def highlight_node(code_node)
  return if code_node.parent&.parent.nil?

  # maintain existing attributes already added. e.g math and mermaid nodes
  pre_node = code_node.parent

  lang = pre_node['data-canonical-lang']
  retried = false

  if use_rouge?(lang)
    lexer = lexer_for(lang)
    language = lexer.tag
  else
    lexer = Rouge::Lexers::PlainText.new
    language = lang
  end

  begin
    code = Rouge::Formatters::HTMLGitlab.format(lex(lexer, code_node.text), tag: language)
  rescue StandardError
    # Gracefully handle syntax highlighter bugs/errors to ensure users can
    # still access an issue/comment/etc. First, retry with the plain text
    # filter. If that fails, then just skip this entirely, but that would
    # be a pretty bad upstream bug.
    return if retried

    language = nil
    lexer = Rouge::Lexers::PlainText.new
    retried = true

    retry
  end

  code_node.children = code

  # ensure there are no extra children, such as a text node that might
  # show up from an XSS attack
  pre_node.children = code_node

  pre_node.add_class(CSS_CLASSES)
  pre_node.add_class("language-#{language}") if language
  pre_node.set_attribute('v-pre', 'true')
  copy_code_btn = "<copy-code></copy-code>" unless language == 'suggestion'
  insert_code_snippet_btn = "<insert-code-snippet></insert-code-snippet>" unless language == 'suggestion'

  highlighted = %(<div class="gl-relative markdown-code-block js-markdown-code">#{pre_node.to_html}#{copy_code_btn}#{insert_code_snippet_btn}</div>)

  # Extracted to a method to measure it
  replace_pre_element(pre_node, highlighted)
end