FIX: Support carriage return in `InlineUploads`.

FIX: Support carriage return in InlineUploads.

Follow up to 8deaef387242203b706162614b41066205d3a10e.

diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb
index bf42ac94..31a3319 100644
--- a/app/services/inline_uploads.rb
+++ b/app/services/inline_uploads.rb
@@ -214,7 +214,7 @@ class InlineUploads
   end
 
   def self.match_img(markdown, external_src: false)
-    markdown.scan(/(([ ]*)<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)<img ([^>\n]+)>([ ]*))(\n*)/) do |match|
+    markdown.scan(/(([ ]*)<(?!img)[^<>]+\/?>)?([\r\n]*)(([ ]*)<img ([^>\n]+)>([ ]*))([\r\n]*)/) do |match|
       node = Nokogiri::HTML::fragment(match[3].strip).children[0]
       src =  node.attributes["src"]&.value
 
@@ -245,8 +245,12 @@ class InlineUploads
 
         match[3].strip! if !after_html_tag
 
-        if match[1].nil? || match[1].length < 4
-          yield(match[3], src, replacement, $~.offset(0)[0]) if block_given?
+        if (match[1].nil? || match[1].length < 4)
+          if (match[4].nil? || match[4].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
         else
           yield(match[3], src, match[3].sub(src, PATH_PLACEHOLDER), $~.offset(0)[0]) if block_given?
         end
diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rb
index 82ad449..434bf42 100644
--- a/spec/services/inline_uploads_spec.rb
+++ b/spec/services/inline_uploads_spec.rb
@@ -325,6 +325,10 @@ RSpec.describe InlineUploads do
                           <img src="#{upload2.url}" alt="test" width="500" height="500">
                         </a>
         MD
+
+        md = "<h1></h1>\r\n<a href=\"http://somelink.com\">\r\n        <img src=\"#{upload.url}\" alt=\"test\" width=\"500\" height=\"500\">\r\n</a>"
+
+        expect(InlineUploads.process(md)).to eq("<h1></h1>\r\n<a href=\"http://somelink.com\">\r\n        <img src=\"#{upload.short_path}\" alt=\"test\" width=\"500\" height=\"500\">\r\n</a>")
       end
 
       it "should correctly update image sources within anchor or paragraph tags" do

GitHub sha: 7c86f16a

3 Likes