From 8deaef387242203b706162614b41066205d3a10e Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 21 Jun 2019 12:32:02 +0800 Subject: [PATCH] FIX: Don't replace img tags within anchor tags with markdown format. Follow up to 9a25b0d614daeac167e1ba1220aaefe489162fb6. --- app/jobs/regular/pull_hotlinked_images.rb | 10 ++++++- app/services/inline_uploads.rb | 18 +++++++----- spec/jobs/pull_hotlinked_images_spec.rb | 11 +++++-- spec/services/inline_uploads_spec.rb | 36 +++++++++++++++-------- 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index a42d45c08a1..46457fe8fc6 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -94,9 +94,17 @@ module Jobs replace_raw = ->(match, match_src, replacement, _index) { 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( match, - replacement.sub(InlineUploads::PLACEHOLDER, upload.short_url) + replacement ) end } diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb index 6ac8ed5dbbf..bf42ac94e87 100644 --- a/app/services/inline_uploads.rb +++ b/app/services/inline_uploads.rb @@ -214,8 +214,8 @@ class InlineUploads end def self.match_img(markdown, external_src: false) - markdown.scan(/(<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)\n]+)>([ ]*))(\n*)/) do |match| - node = Nokogiri::HTML::fragment(match[2].strip).children[0] + markdown.scan(/(([ ]*)<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)\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 diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index a1548fca3ea..cf3b2b4c6ee 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -57,7 +57,11 @@ describe Jobs::PullHotlinkedImages do end 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) +

somelink @@ -67,11 +71,12 @@ describe Jobs::PullHotlinkedImages do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.to change { Upload.count }.by(1) + upload = post.uploads.last + expect(post.reload.raw).to eq(<<~RAW.chomp) +

- - ![somelink](#{post.uploads.last.short_url}) - + somelink RAW end diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rb index d54666f1831..82ad449a9e7 100644 --- a/spec/services/inline_uploads_spec.rb +++ b/spec/services/inline_uploads_spec.rb @@ -303,17 +303,36 @@ RSpec.describe InlineUploads do MD end + it "should correctly update images sources within anchor tags with indentation" do + md = <<~MD +

+ + test + + + + test + + MD + + expect(InlineUploads.process(md)).to eq(<<~MD) +

+ + test + + + + test + + MD + end + it "should correctly update image sources within anchor or paragraph tags" do md = <<~MD test -

- - test - -

test

@@ -342,13 +361,6 @@ RSpec.describe InlineUploads do -

- - - ![test|500x500](#{upload2.short_url}) - - -

![test](#{upload2.short_url})