2021-02-01 12:09:03 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
class MarkdownPostEscapeFilter < HTML::Pipeline::Filter
|
|
|
|
LITERAL_KEYWORD = MarkdownPreEscapeFilter::LITERAL_KEYWORD
|
|
|
|
LITERAL_REGEX = %r{#{LITERAL_KEYWORD}-(.*?)-#{LITERAL_KEYWORD}}.freeze
|
|
|
|
NOT_LITERAL_REGEX = %r{#{LITERAL_KEYWORD}-((%5C|\\).+?)-#{LITERAL_KEYWORD}}.freeze
|
|
|
|
SPAN_REGEX = %r{<span>(.*?)</span>}.freeze
|
|
|
|
|
2021-11-17 15:10:28 +00:00
|
|
|
CSS_A = 'a'
|
|
|
|
XPATH_A = Gitlab::Utils::Nokogiri.css_to_xpath(CSS_A).freeze
|
2021-06-01 21:10:06 +00:00
|
|
|
|
2021-02-01 12:09:03 +00:00
|
|
|
def call
|
|
|
|
return doc unless result[:escaped_literals]
|
|
|
|
|
|
|
|
# For any literals that actually didn't get escape processed
|
|
|
|
# (for example in code blocks), remove the special sequence.
|
|
|
|
html.gsub!(NOT_LITERAL_REGEX, '\1')
|
|
|
|
|
|
|
|
# Replace any left over literal sequences with `span` so that our
|
|
|
|
# reference processing is short-circuited
|
|
|
|
html.gsub!(LITERAL_REGEX, '<span>\1</span>')
|
|
|
|
|
|
|
|
# Since literals are converted in links, we need to remove any surrounding `span`.
|
|
|
|
# Note: this could have been done in the renderer,
|
|
|
|
# Banzai::Renderer::CommonMark::HTML. However, we eventually want to use
|
|
|
|
# the built-in compiled renderer, rather than the ruby version, for speed.
|
|
|
|
# So let's do this work here.
|
2021-06-01 21:10:06 +00:00
|
|
|
doc.xpath(XPATH_A).each do |node|
|
2021-02-01 12:09:03 +00:00
|
|
|
node.attributes['href'].value = node.attributes['href'].value.gsub(SPAN_REGEX, '\1') if node.attributes['href']
|
|
|
|
node.attributes['title'].value = node.attributes['title'].value.gsub(SPAN_REGEX, '\1') if node.attributes['title']
|
|
|
|
end
|
|
|
|
|
2021-11-17 15:10:28 +00:00
|
|
|
doc.xpath(lang_tag).each do |node|
|
2021-02-01 12:09:03 +00:00
|
|
|
node.attributes['lang'].value = node.attributes['lang'].value.gsub(SPAN_REGEX, '\1') if node.attributes['lang']
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
2021-11-17 15:10:28 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def lang_tag
|
2021-12-10 18:14:42 +00:00
|
|
|
if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
|
2021-11-17 15:10:28 +00:00
|
|
|
Gitlab::Utils::Nokogiri.css_to_xpath('pre')
|
|
|
|
else
|
|
|
|
Gitlab::Utils::Nokogiri.css_to_xpath('code')
|
|
|
|
end
|
|
|
|
end
|
2021-02-01 12:09:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|