From b2a8dc4c0fb80a6ef7721b310f2c59eebb218670 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 29 Mar 2022 10:55:10 +0100 Subject: [PATCH] FIX: Maintain HTML ` 0 && height > 0 - url = upload&.short_url || PLACEHOLDER + node["src"] = upload&.short_url || PLACEHOLDER spaces_before = match[1].present? ? match[1][/ +$/].size : 0 - replacement = +"#{" " * spaces_before}![#{text}](#{url}#{title.present? ? " \"#{title}\"" : ""})" + replacement = +"#{" " * spaces_before}#{node.to_s}" yield(match[2], src, replacement, $~.offset(0)[0]) if block_given? end diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index 28082f01d22..d41b9d31d89 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -67,7 +67,7 @@ describe Jobs::PullHotlinkedImages do end.to change { Upload.count }.by(1) & change { UserHistory.count }.by(0) # Should not add to the staff log - expect(post.reload.raw).to eq("![](#{Upload.last.short_url})") + expect(post.reload.raw).to eq("") end it 'removes downloaded images when they are no longer needed' do @@ -91,7 +91,7 @@ describe Jobs::PullHotlinkedImages do post.rebake! end.to change { Upload.count }.by(1) - expect(post.reload.raw).to eq("![](#{Upload.last.short_url})") + expect(post.reload.raw).to eq("") # Post raw is updated back to the old value (e.g. by wordpress integration) post.update(raw: "") @@ -100,7 +100,7 @@ describe Jobs::PullHotlinkedImages do post.rebake! end.to change { Upload.count }.by(0) # We alread have the upload - expect(post.reload.raw).to eq("![](#{Upload.last.short_url})") + expect(post.reload.raw).to eq("") end it 'replaces encoded image urls' do @@ -110,10 +110,10 @@ describe Jobs::PullHotlinkedImages do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.to change { Upload.count }.by(1) - expect(post.reload.raw).to eq("![](#{Upload.last.short_url})") + expect(post.reload.raw).to eq("") end - xit 'replaces images in an anchor tag with weird indentation' do + it 'replaces images in an anchor tag with weird indentation' do # Skipped pending https://meta.discourse.org/t/152801 # This spec was previously passing, even though the resulting markdown was invalid # Now the spec has been improved, and shows the issue @@ -124,7 +124,7 @@ describe Jobs::PullHotlinkedImages do post = Fabricate(:post, raw: <<~MD)

- somelink + somelink MD @@ -137,7 +137,7 @@ describe Jobs::PullHotlinkedImages do expect(post.reload.raw).to eq(<<~MD.chomp)

- ![somelink](#{upload.short_url}) + somelink MD end @@ -163,7 +163,7 @@ describe Jobs::PullHotlinkedImages do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.to change { Upload.count }.by(1) - expect(post.reload.raw).to eq("![test](#{Upload.last.short_url})") + expect(post.reload.raw).to eq("\"test\"") end it 'replaces images without extension' do @@ -176,7 +176,7 @@ describe Jobs::PullHotlinkedImages do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.to change { Upload.count }.by(1) - expect(post.reload.raw).to eq("![](#{Upload.last.short_url})") + expect(post.reload.raw).to eq("") end it 'replaces optimized images' do @@ -195,7 +195,7 @@ describe Jobs::PullHotlinkedImages do upload = Upload.last post.reload - expect(post.raw).to eq("![](#{upload.short_url})") + expect(post.raw).to eq("") expect(post.uploads).to contain_exactly(upload) end @@ -385,7 +385,7 @@ describe Jobs::PullHotlinkedImages do post.reload expect(post.raw).to eq(<<~MD.chomp) - ![](upload://z2QSs1KJWoj51uYhDjb6ifCzxH6.gif) + https://commons.wikimedia.org/wiki/File:Brisbane_May_2013201.jpg @@ -525,7 +525,7 @@ describe Jobs::PullHotlinkedImages do post.reload - expect(post.raw).to eq("![](#{Upload.last.short_url})") + expect(post.raw).to eq("") expect(post.uploads.count).to eq(1) end diff --git a/spec/lib/email/receiver_spec.rb b/spec/lib/email/receiver_spec.rb index b690e6d26de..eb441e5cff5 100644 --- a/spec/lib/email/receiver_spec.rb +++ b/spec/lib/email/receiver_spec.rb @@ -614,7 +614,7 @@ describe Email::Receiver do expect(post.raw).to eq(<<~MD.chomp) **Before** - ![#{upload.original_filename}|#{upload.width}x#{upload.height}](#{upload.short_url}) + 内嵌图片 1 *After* MD @@ -648,7 +648,7 @@ describe Email::Receiver do
··· - ![logo.png|300x200](upload://qUm0DGR49PAZshIi7HxMd3cAlzn.png) +
MD @@ -673,7 +673,7 @@ describe Email::Receiver do expect(post.raw).to eq(<<~MD.chomp) Picture below. - ![#{upload.original_filename}|#{upload.width}x#{upload.height}](#{upload.short_url}) + Picture above. MD diff --git a/spec/services/inline_uploads_multisite_spec.rb b/spec/services/inline_uploads_multisite_spec.rb index 0c29dc0eb71..042479e583a 100644 --- a/spec/services/inline_uploads_multisite_spec.rb +++ b/spec/services/inline_uploads_multisite_spec.rb @@ -31,9 +31,9 @@ RSpec.describe InlineUploads, type: :multisite do #{Discourse.base_url}#{upload2.short_path} #{Discourse.base_url}#{upload2.short_path} #{Discourse.base_url}#{upload2.short_path} - ![some image](#{upload.short_url}) - ![some image](#{upload2.short_url}) - ![](#{upload3.short_url}) + some image + some image + MD end end diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rb index f47f3fa7203..152b1b20537 100644 --- a/spec/services/inline_uploads_spec.rb +++ b/spec/services/inline_uploads_spec.rb @@ -46,7 +46,7 @@ RSpec.describe InlineUploads do MD expect(InlineUploads.process(md)).to eq(<<~MD) - ![](#{upload.short_url}) + This is an invalid `` tag MD @@ -102,7 +102,7 @@ RSpec.describe InlineUploads do ![](#{upload3.short_url}) [/quote] - ![](#{upload2.short_url}) + MD end @@ -173,7 +173,7 @@ RSpec.describe InlineUploads do expect(InlineUploads.process(md)).to eq(<<~MD) [img]http://some.external.img[/img] ![](#{upload.short_url}) - ![](#{upload3.short_url}) + ![](#{upload2.short_url}) @@ -202,7 +202,7 @@ RSpec.describe InlineUploads do This is a [link1][1] test [link2][2] something - ![](#{upload.short_url}) + [1]: #{Discourse.base_url}#{upload.short_path} [2]: #{Discourse.base_url}#{upload2.short_path} @@ -218,7 +218,7 @@ RSpec.describe InlineUploads do expect(InlineUploads.process(md)).to eq(<<~MD) ![](#{upload.short_url}) - ![](#{upload2.short_url}) + [Text|attachment](#{upload3.short_url}) MD end @@ -237,11 +237,11 @@ RSpec.describe InlineUploads do MD expect(InlineUploads.process(md)).to eq(<<~MD) - ![](#{upload.short_url}) + - ![](#{upload.short_url}) + - ![](#{upload.short_url}) + ![](#{upload.short_url}) @@ -262,7 +262,7 @@ RSpec.describe InlineUploads do MD expect(InlineUploads.process(md)).to eq(<<~MD) - ![](#{upload.short_url}) + ![](#{upload.short_url}) MD @@ -319,7 +319,7 @@ RSpec.describe InlineUploads do MD expect(InlineUploads.process(md)).to eq(<<~MD) - test![](#{upload.short_url}) + test MD end @@ -371,13 +371,13 @@ RSpec.describe InlineUploads do ![image](#{upload2.short_url}) ![image|100x100](#{upload3.short_url}) - ![some image](#{upload.short_url} "some title") - ![some image](#{upload2.short_url})![some image](#{upload3.short_url}) + some image + some imagesome image #{Discourse.base_url}#{upload3.short_path} #{Discourse.base_url}#{upload3.short_path} - ![|5x4](#{upload.short_url}) - ![](#{upload.short_url}) + + `image inside code quotes` @@ -421,7 +421,7 @@ RSpec.describe InlineUploads do expect(InlineUploads.process(md)).to eq(<<~MD)

- ![test|500x500](#{upload2.short_url}) + test @@ -431,7 +431,7 @@ RSpec.describe InlineUploads do md = "

\r\n
\r\n \"test\"\r\n" - expect(InlineUploads.process(md)).to eq("

\r\n\r\n ![test|500x500](#{upload.short_url})\r\n") + expect(InlineUploads.process(md)).to eq("

\r\n\r\n \"test\"\r\n") end it "should correctly update image sources within anchor or paragraph tags" do @@ -463,28 +463,28 @@ RSpec.describe InlineUploads do expect(InlineUploads.process(md)).to eq(<<~MD) - ![test|500x500](#{upload.short_url}) + test

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

- ![test|500x500](#{upload3.short_url}) + test - ![test|500x500](#{upload.short_url}) + test - ![test|500x500](#{upload.short_url}) + test -

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

+

Test test


- ![test|500x500](#{upload2.short_url}) + test MD end @@ -508,9 +508,9 @@ RSpec.describe InlineUploads do This is some test - ![test](#{upload2.short_url}) + test > some quote @@ -671,9 +671,9 @@ RSpec.describe InlineUploads do expect(InlineUploads.process(md)).to eq(<<~MD) ![](#{upload.short_url}) - ![some image](#{upload.short_url}) - test![some image](#{upload2.short_url})test - ![some image](#{upload2.short_url}) + some image + testsome imagetest + some image MD end