diff --git a/app/models/communication/website/imported/with_rich_text.rb b/app/models/communication/website/imported/with_rich_text.rb index a1ac94cdaca424f38ac642a8ab04cc07fffff8bc..7111383447d45bc732416861e7224b130ebc89cd 100644 --- a/app/models/communication/website/imported/with_rich_text.rb +++ b/app/models/communication/website/imported/with_rich_text.rb @@ -5,16 +5,22 @@ module Communication::Website::Imported::WithRichText def rich_text_with_attachments(text) fragment = Nokogiri::HTML.fragment(text) - images = fragment.css("img[src*=\"#{website.website.uploads_url}\"]") - images.each do |image| + fragment = replace_tags_with_attachments(fragment, 'a', 'href') + fragment = replace_tags_with_attachments(fragment, 'img', 'src') + fragment.to_html + end + + def replace_tags_with_attachments(fragment, tag_name, attribute_name) + nodes = fragment.css("#{tag_name}[#{attribute_name}*=\"#{website.website.uploads_url}\"]") + nodes.each do |node| begin - url = image.attr('src') + url = node.attr(attribute_name) blob = load_blob_from_url(url) - image.replace ActionText::Attachment.from_attachable(blob).node.to_s + node.replace ActionText::Attachment.from_attachable(blob).node.to_s rescue end end - fragment.to_html + fragment end def load_blob_from_url(url) diff --git a/app/views/active_storage/blobs/_blob.html.erb b/app/views/active_storage/blobs/_blob.html.erb index 3daa58bc67b70fe52af1b1f38a6711885bc044bd..6ad1c8ba6ac06f84ce0c78ec30fdce2006851a6d 100644 --- a/app/views/active_storage/blobs/_blob.html.erb +++ b/app/views/active_storage/blobs/_blob.html.erb @@ -1,11 +1,21 @@ <figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>"> - <% if blob.representable? %> + <% if blob.image? %> <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %> + <% elsif blob.video? %> + <video> + <source src="<%= rails_blob_path(blob) %>" type="<%= blob.content_type %>"> + </video> <% else %> - <p> - <span class="attachment__name"><%= blob.filename %></span> - <span class="attachment__size"><%= number_to_human_size blob.byte_size %></span> - </p> + <%= link_to blob, target: :blank do %> + <% if blob.representable? %> + <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %> + <% else %> + <p> + <span class="attachment__name"><%= blob.filename %></span> + <span class="attachment__size"><%= number_to_human_size blob.byte_size %></span> + </p> + <% end %> + <% end %> <% end %> <% if caption = blob.try(:caption) %>