From 641521896c37ce67595f64defbe4bd4b3b92557f Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 12 Jun 2019 16:41:44 +0800 Subject: [PATCH] FIX: Cover more edge cases in `InlineUploads`. --- app/services/inline_uploads.rb | 24 +++++------ spec/services/inline_uploads_spec.rb | 59 +++++++++++++++++++++------- 2 files changed, 58 insertions(+), 25 deletions(-) diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb index 45afd7b06f7..cb153e53a7d 100644 --- a/app/services/inline_uploads.rb +++ b/app/services/inline_uploads.rb @@ -22,12 +22,11 @@ class InlineUploads end if seen_link = matched_uploads(node).first - link_occurences << - if (actual_link = (node.attributes["href"]&.value || node.attributes["src"]&.value)) - { link: actual_link, is_valid: true } - else - { link: seen_link, is_valid: false } - end + if (actual_link = (node.attributes["href"]&.value || node.attributes["src"]&.value)) + link_occurences << { link: actual_link, is_valid: true } + elsif node.name != "p" + link_occurences << { link: actual_link, is_valid: false } + end end end @@ -60,7 +59,7 @@ class InlineUploads ] if Discourse.store.external? - regexps << /(https?:#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/ + regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/ regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/ end @@ -235,28 +234,31 @@ class InlineUploads regexps = [ /(upload:\/\/([a-zA-Z0-9]+)[a-z0-9\.]*)/, /(\/uploads\/short-url\/([a-zA-Z0-9]+)[a-z0-9\.]*)/, + /(#{Discourse.base_url}\/uploads\/short-url\/([a-zA-Z0-9]+)[a-z0-9\.]*)/, ] db = RailsMultisite::ConnectionManagement.current_db if Discourse.store.external? if Rails.configuration.multisite - regexps << /(#{SiteSetting.Upload.s3_base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ + regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ regexps << /(#{SiteSetting.Upload.s3_cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ else - regexps << /(#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/ + regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/ regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/ regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ + regexps << /(#{Discourse.base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ end else regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ + regexps << /(#{Discourse.base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ end node = node.to_s regexps.each do |regexp| - node.scan(regexp) do |matched| - matches << matched[0] + node.scan(/(^|[\n\s"'\(>])#{regexp}($|[\n\s"'\)<])/) do |matched| + matches << matched[1] end end diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rb index e3402896e80..5bc4cafe2c3 100644 --- a/spec/services/inline_uploads_spec.rb +++ b/spec/services/inline_uploads_spec.rb @@ -60,26 +60,55 @@ RSpec.describe InlineUploads do end it "should not correct code blocks" do + md = "`In Code Block`" - md = <<~MD - `In Code Block` + expect(InlineUploads.process(md)).to eq(md) - In Code Block - - ``` - In Code Block - ``` - a [code]In Code Block[/code] b - - [code] - In Code Block - [/code] - MD + md = " In Code Block" expect(InlineUploads.process(md)).to eq(md) end - it "should not correct links in quotes" do + it "should not correct invalid links in quotes" do + post = Fabricate(:post) + user = Fabricate(:user) + + md = <<~MD + [quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"] + + MD + + expect(InlineUploads.process(md)).to eq(<<~MD) + [quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"] + some image some image MD expect(InlineUploads.process(md)).to eq(<<~MD) + ![](#{upload.short_url}) ![some image](#{upload.short_url}) ![some image](#{upload2.short_url}) MD