A platform for community discussion. Free, open, simple.
DEV: More improvements to `InlineUploads`.
Guo Xiang Tanee142c2173ea
diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rbindex 4605a3fb5a8..70cbbf96208 100644--- a/app/services/inline_uploads.rb+++ b/app/services/inline_uploads.rb@@ -56,18 +56,58 @@ class InlineUploadsdb = RailsMultisite::ConnectionManagement.current_dbregexps = [- /(^|\s)?(https?:\/\/[a-zA-Z0-9\.\/-]+\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})($|\s)/,+ /(https?:\/\/[a-zA-Z0-9\.\/-]+\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/,]if Discourse.store.external?- regexps << /(^|\s)?(https?:#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})($|\s)?/- regexps << /(^|\s)?(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})($|\s)?/+ regexps << /(https?:#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/+ regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/endregexps.each do |regexp|- markdown.scan(regexp) do |match|- if matched_uploads(match[1]).present?- raw_matches << [match[1], match[1], +"", $~.offset(0)[0]]+ indexes = Set.new++ markdown.scan(/(\n{2,}|\A)#{regexp}$/) do |match|+ if match[1].present?+ index = $~.offset(2)[0]+ indexes << index+ raw_matches << [match[1], match[1], +"", index]+ end+ end++ markdown.scan(/^#{regexp}(\s)/) do |match|+ if match[0].present?+ index = $~.offset(0)[0]+ next if indexes.include?(index)+ indexes << index++ raw_matches << [+ match[0],+ match[0],+ +"#{Discourse.base_url}#{PATH_PLACEHOLDER}",+ $~.offset(0)[0]+ ]+ end+ end++ markdown.scan(/\[[^\[\]]*\]: #{regexp}/) do |match|+ if match[0].present?+ index = $~.offset(1)[0]+ next if indexes.include?(index)+ indexes << index+ end+ end++ markdown.scan(/((\n|\s)+)#{regexp}/) do |match|+ if matched_uploads(match[2]).present?+ next if indexes.include?($~.offset(3)[0])++ raw_matches << [+ match[2],+ match[2],+ +"#{Discourse.base_url}#{PATH_PLACEHOLDER}",+ $~.offset(0)[0]+ ]endendend@@ -125,7 +165,7 @@ class InlineUploadsdef self.match_md_reference(markdown)markdown.scan(/(\[([^\]]+)\]:([ ]+)(\S+))/) do |match|- if match[3] && matched_uploads(match[3]) && block_given?+ if match[3] && matched_uploads(match[3]).present? && block_given?yield(match[0],match[3],diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rbindex 4bbbc7dd04c..0ec00d0753c 100644--- a/spec/services/inline_uploads_spec.rb+++ b/spec/services/inline_uploads_spec.rb@@ -124,25 +124,37 @@ RSpec.describe InlineUploads doit "should correct markdown references" domd = <<~MD- This is a [some reference] somethign+ This is a [some reference] something[some reference]: #{Discourse.base_url}#{upload.url}++ <img src="#{upload.url}">MDexpect(InlineUploads.process(md)).to eq(<<~MD)- This is a [some reference] somethign+ This is a [some reference] something[some reference]: #{Discourse.base_url}#{upload.short_path}++ MDend- it "should correct raw image URLs to the short version" do+ it "should correct raw image URLs to the short url and paths" domd = <<~MD- #{Discourse.base_url}#{upload3.url} #{Discourse.base_url}#{upload3.url}+ #{Discourse.base_url}#{upload.url}++ #{Discourse.base_url}#{upload.url} #{Discourse.base_url}#{upload2.url}++ #{Discourse.base_url}#{upload3.url}MDexpect(InlineUploads.process(md)).to eq(<<~MD)-  + ++ #{Discourse.base_url}#{upload.short_path} #{Discourse.base_url}#{upload2.short_path}++ MDend@@ -174,7 +186,7 @@ RSpec.describe InlineUploads do-  + #{Discourse.base_url}#{upload3.short_path} #{Discourse.base_url}#{upload3.short_path}MD@@ -469,8 +481,8 @@ RSpec.describe InlineUploads doMDexpect(InlineUploads.process(md)).to eq(<<~MD)-  - + #{Discourse.base_url}#{upload2.short_path} #{Discourse.base_url}#{upload2.short_path}+ #{Discourse.base_url}#{upload2.short_path}