Skip to content

A platform for community discussion. Free, open, simple.

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

Guo Xiang Tan8deaef387242
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*)(([ ]*)<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
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)
+ <h1></h1>
<a href="https://somelink.com">
<img alt="somelink" src="#{image_url}" />
</a>
@@ -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)
+ <h1></h1>
<a href="https://somelink.com">
-
- ![somelink](#{post.uploads.last.short_url})
-
+ <img alt="somelink" src="#{upload.short_path}" />
</a>
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,16 +303,35 @@ RSpec.describe InlineUploads do
MD
end
- it "should correctly update image sources within anchor or paragraph tags" do
+ it "should correctly update images sources within anchor tags with indentation" do
md = <<~MD
- <a href="http://somelink.com">
- <img src="#{upload.url}" alt="test" width="500" height="500">
- </a>
+ <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
+ md = <<~MD
+ <a href="http://somelink.com">
+ <img src="#{upload.url}" alt="test" width="500" height="500">
+ </a>
<p>
<img src="#{upload2.url}" alt="test">
@@ -342,13 +361,6 @@ RSpec.describe InlineUploads do
</a>
- <h1></h1>
- <a href="http://somelink.com">
-
- ![test|500x500](#{upload2.short_url})
-
- </a>
-
<p>
![test](#{upload2.short_url})