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}"
- 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)
+
