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

View File

@ -94,9 +94,17 @@ module Jobs
replace_raw = ->(match, match_src, replacement, _index) { replace_raw = ->(match, match_src, replacement, _index) {
if src.include?(match_src) if src.include?(match_src)
replacement =
if replacement.include?(InlineUploads::PLACEHOLDER)
replacement.sub(InlineUploads::PLACEHOLDER, upload.short_url)
elsif replacement.include?(InlineUploads::PATH_PLACEHOLDER)
replacement.sub(InlineUploads::PATH_PLACEHOLDER, upload.short_path)
end
raw = raw.gsub( raw = raw.gsub(
match, match,
replacement.sub(InlineUploads::PLACEHOLDER, upload.short_url) replacement
) )
end end
} }

View File

@ -214,8 +214,8 @@ class InlineUploads
end end
def self.match_img(markdown, external_src: false) def self.match_img(markdown, external_src: false)
markdown.scan(/(<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)<img ([^>\n]+)>([ ]*))(\n*)/) do |match| markdown.scan(/(([ ]*)<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)<img ([^>\n]+)>([ ]*))(\n*)/) do |match|
node = Nokogiri::HTML::fragment(match[2].strip).children[0] node = Nokogiri::HTML::fragment(match[3].strip).children[0]
src = node.attributes["src"]&.value src = node.attributes["src"]&.value
if src && (matched_uploads(src).present? || external_src) if src && (matched_uploads(src).present? || external_src)
@ -228,24 +228,28 @@ class InlineUploads
spaces_before = spaces_before =
if after_html_tag && !match[0].end_with?("/>") if after_html_tag && !match[0].end_with?("/>")
(match[3].length > 0 ? match[3] : " ") (match[4].length > 0 ? match[4] : " ")
else else
"" ""
end end
replacement = +"#{spaces_before}![#{text}](#{PLACEHOLDER}#{title.present? ? " \"#{title}\"" : ""})" 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)) replacement.prepend("\n" * (num_newlines == 0 ? 2 : 1))
end 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)) replacement += ("\n" * (num_newlines == 0 ? 2 : 1))
end 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 end
end end

View File

@ -57,7 +57,11 @@ describe Jobs::PullHotlinkedImages do
end end
it 'replaces images in an anchor tag with weird indentation' do it 'replaces images in an anchor tag with weird indentation' do
stub_request(:get, "http://test.localhost/uploads/short-url/z2QSs1KJWoj51uYhDjb6ifCzxH6.gif")
.to_return(status: 200, body: "")
post = Fabricate(:post, raw: <<~RAW) post = Fabricate(:post, raw: <<~RAW)
<h1></h1>
<a href="https://somelink.com"> <a href="https://somelink.com">
<img alt="somelink" src="#{image_url}" /> <img alt="somelink" src="#{image_url}" />
</a> </a>
@ -67,11 +71,12 @@ describe Jobs::PullHotlinkedImages do
Jobs::PullHotlinkedImages.new.execute(post_id: post.id) Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
end.to change { Upload.count }.by(1) end.to change { Upload.count }.by(1)
upload = post.uploads.last
expect(post.reload.raw).to eq(<<~RAW.chomp) expect(post.reload.raw).to eq(<<~RAW.chomp)
<h1></h1>
<a href="https://somelink.com"> <a href="https://somelink.com">
<img alt="somelink" src="#{upload.short_path}" />
![somelink](#{post.uploads.last.short_url})
</a> </a>
RAW RAW
end end

View File

@ -303,17 +303,36 @@ RSpec.describe InlineUploads do
MD MD
end end
it "should correctly update images sources within anchor tags with indentation" do
md = <<~MD
<h1></h1>
<a href="http://somelink.com">
<img src="#{upload2.url}" alt="test" width="500" height="500">
</a>
<a href="http://somelink.com">
<img src="#{upload2.url}" alt="test" width="500" height="500">
</a>
MD
expect(InlineUploads.process(md)).to eq(<<~MD)
<h1></h1>
<a href="http://somelink.com">
<img src="#{upload2.short_path}" alt="test" width="500" height="500">
</a>
<a href="http://somelink.com">
<img src="#{upload2.url}" alt="test" width="500" height="500">
</a>
MD
end
it "should correctly update image sources within anchor or paragraph tags" do it "should correctly update image sources within anchor or paragraph tags" do
md = <<~MD md = <<~MD
<a href="http://somelink.com"> <a href="http://somelink.com">
<img src="#{upload.url}" alt="test" width="500" height="500"> <img src="#{upload.url}" alt="test" width="500" height="500">
</a> </a>
<h1></h1>
<a href="http://somelink.com">
<img src="#{upload2.url}" alt="test" width="500" height="500">
</a>
<p> <p>
<img src="#{upload2.url}" alt="test"> <img src="#{upload2.url}" alt="test">
</p> </p>
@ -342,13 +361,6 @@ RSpec.describe InlineUploads do
</a> </a>
<h1></h1>
<a href="http://somelink.com">
![test|500x500](#{upload2.short_url})
</a>
<p> <p>
![test](#{upload2.short_url}) ![test](#{upload2.short_url})