Class: Banzai::Filter::BlockquoteFenceFilter

Inherits:
HTML::Pipeline::TextFilter
  • Object
show all
Defined in:
lib/banzai/filter/blockquote_fence_filter.rb

Constant Summary collapse

REGEX =
%r{
    #{::Gitlab::Regex.markdown_code_or_html_blocks}
  |
    (?:
      # Blockquote:
      # >>>
      # Anything, including code and HTML blocks
      # >>>

      ^>>>\ *\n
      (?<quote>
        (?:
            # Any character that doesn't introduce a code or HTML block
            (?!
                ^```
              |
                ^<[^>]+?>\ *\n
            )
            .
          |
            # A code block
            \g<code>
          |
            # An HTML block
            \g<html>
        )+?
      )
      \n>>>\ *$
    )
}mx.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text, context = nil, result = nil) ⇒ BlockquoteFenceFilter

Returns a new instance of BlockquoteFenceFilter.


37
38
39
40
# File 'lib/banzai/filter/blockquote_fence_filter.rb', line 37

def initialize(text, context = nil, result = nil)
  super text, context, result
  @text = @text.delete("\r")
end

Instance Method Details

#callObject


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/banzai/filter/blockquote_fence_filter.rb', line 42

def call
  @text.gsub(REGEX) do
    if $~[:quote]
      # keep the same number of source lines/positions by replacing the
      # fence lines with newlines
      "\n" + $~[:quote].gsub(/^/, "> ").gsub(/^> $/, ">") + "\n"
    else
      $~[0]
    end
  end
end