FIX: Don't replace img tags within anchor tags with markdown format.

Follow up to 9a25b0d614.
This commit is contained in:
Guo Xiang Tan
2019-06-21 12:32:02 +08:00
parent f51f37eddf
commit 8deaef3872
4 changed files with 52 additions and 23 deletions
+11 -7
View File
@@ -214,8 +214,8 @@ class InlineUploads
end
def self.match_img(markdown, external_src: false)
markdown.scan(/(<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)<img ([^>\n]+)>([ ]*))(\n*)/) do |match|
node = Nokogiri::HTML::fragment(match[2].strip).children[0]
markdown.scan(/(([ ]*)<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)<img ([^>\n]+)>([ ]*))(\n*)/) do |match|
node = Nokogiri::HTML::fragment(match[3].strip).children[0]
src = node.attributes["src"]&.value
if src && (matched_uploads(src).present? || external_src)
@@ -228,24 +228,28 @@ class InlineUploads
spaces_before =
if after_html_tag && !match[0].end_with?("/>")
(match[3].length > 0 ? match[3] : " ")
(match[4].length > 0 ? match[4] : " ")
else
""
end
replacement = +"#{spaces_before}![#{text}](#{PLACEHOLDER}#{title.present? ? " \"#{title}\"" : ""})"
if after_html_tag && (num_newlines = match[1].length) <= 1
if after_html_tag && (num_newlines = match[2].length) <= 1
replacement.prepend("\n" * (num_newlines == 0 ? 2 : 1))
end
if after_html_tag && !match[0].end_with?("/>") && (num_newlines = match[6].length) <= 1
if after_html_tag && !match[0].end_with?("/>") && (num_newlines = match[7].length) <= 1
replacement += ("\n" * (num_newlines == 0 ? 2 : 1))
end
match[2].strip! if !after_html_tag
match[3].strip! if !after_html_tag
yield(match[2], src, replacement, $~.offset(0)[0]) if block_given?
if match[1].nil? || match[1].length < 4
yield(match[3], src, replacement, $~.offset(0)[0]) if block_given?
else
yield(match[3], src, match[3].sub(src, PATH_PLACEHOLDER), $~.offset(0)[0]) if block_given?
end
end
end
end