2018-07-23 09:32:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-27 21:38:33 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
class BlockquoteFenceFilter < HTML::Pipeline::TextFilter
|
|
|
|
REGEX = %r{
|
2018-07-06 10:35:30 +00:00
|
|
|
#{::Gitlab::Regex.markdown_code_or_html_blocks}
|
2016-04-27 21:38:33 +00:00
|
|
|
|
|
2016-07-10 19:47:53 +00:00
|
|
|
(?:
|
|
|
|
# Blockquote:
|
|
|
|
# >>>
|
|
|
|
# Anything, including code and HTML blocks
|
|
|
|
# >>>
|
|
|
|
|
2018-06-18 11:28:21 +00:00
|
|
|
^>>>\ *\n
|
2016-07-10 19:47:53 +00:00
|
|
|
(?<quote>
|
2016-04-27 21:38:33 +00:00
|
|
|
(?:
|
2016-07-10 19:47:53 +00:00
|
|
|
# Any character that doesn't introduce a code or HTML block
|
|
|
|
(?!
|
|
|
|
^```
|
|
|
|
|
|
2018-06-18 11:28:21 +00:00
|
|
|
^<[^>]+?>\ *\n
|
2016-07-10 19:47:53 +00:00
|
|
|
)
|
|
|
|
.
|
2016-04-27 21:38:33 +00:00
|
|
|
|
|
2016-07-10 19:47:53 +00:00
|
|
|
# A code block
|
2016-04-27 21:38:33 +00:00
|
|
|
\g<code>
|
|
|
|
|
|
2016-07-10 19:47:53 +00:00
|
|
|
# An HTML block
|
2016-04-27 21:38:33 +00:00
|
|
|
\g<html>
|
2016-07-10 19:47:53 +00:00
|
|
|
)+?
|
|
|
|
)
|
2018-06-18 11:28:21 +00:00
|
|
|
\n>>>\ *$
|
2016-04-27 21:38:33 +00:00
|
|
|
)
|
|
|
|
}mx.freeze
|
|
|
|
|
|
|
|
def initialize(text, context = nil, result = nil)
|
|
|
|
super text, context, result
|
2016-07-10 19:47:53 +00:00
|
|
|
@text = @text.delete("\r")
|
2016-04-27 21:38:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
@text.gsub(REGEX) do
|
|
|
|
if $~[:quote]
|
2019-04-03 13:42:23 +00:00
|
|
|
# keep the same number of source lines/positions by replacing the
|
|
|
|
# fence lines with newlines
|
|
|
|
"\n" + $~[:quote].gsub(/^/, "> ").gsub(/^> $/, ">") + "\n"
|
2016-04-27 21:38:33 +00:00
|
|
|
else
|
|
|
|
$~[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|