FIX: `InlineUploads` should replace attachment links with markdown text.

FIX: InlineUploads should replace attachment links with markdown text.

diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb
index a015bc0..816e848 100644
--- a/app/services/inline_uploads.rb
+++ b/app/services/inline_uploads.rb
@@ -24,7 +24,9 @@ class InlineUploads
     cooked_fragment.traverse do |node|
       if node.name == "img"
         # Do nothing
-      elsif !(node.children.count == 1 && (node.children[0].name != "img" && node.children[0].children.blank?))
+      elsif !(node.children.count == 1 && (node.children[0].name != "img" && node.children[0].children.blank?)) &&
+        !(node.name == "a" && node.children.count > 1 && !node_children_names(node).include?("img"))
+
         next
       end
 
@@ -293,4 +295,18 @@ class InlineUploads
     matches
   end
   private_class_method :matched_uploads
+
+  def self.node_children_names(node, names = Set.new)
+    if node.children.blank?
+      names << node.name
+      return names
+    end
+
+    node.children.each do |child|
+      names = node_children_names(child, names)
+    end
+
+    names
+  end
+  private_class_method :node_children_names
 end
diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rb
index 0633714..e563eee 100644
--- a/spec/services/inline_uploads_spec.rb
+++ b/spec/services/inline_uploads_spec.rb
@@ -452,6 +452,8 @@ RSpec.describe InlineUploads do
 
         <a class="test attachment" href="#{upload.url}">test3</a>
         <a class="test attachment" href="#{upload2.url}">test3</a><a class="test attachment" href="#{upload3.url}">test3</a>
+
+        <a class="test attachment" href="#{upload3.url}">This is some _test_ here</a>
         MD
 
         expect(InlineUploads.process(md)).to eq(<<~MD)
@@ -463,6 +465,8 @@ RSpec.describe InlineUploads do
 
         [test3|attachment](#{upload.short_url})
         [test3|attachment](#{upload2.short_url})[test3|attachment](#{upload3.short_url})
+
+        [This is some _test_ here|attachment](#{upload3.short_url})
         MD
       end

GitHub sha: 34f382b7

2 Likes