From 04622751efcaff344818977213d548340e51ca8f Mon Sep 17 00:00:00 2001 From: f Date: Wed, 1 Feb 2023 17:12:09 -0300 Subject: [PATCH] feat: realiza el paso inverso sutty/editor#59 --- app/models/metadata_content.rb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/metadata_content.rb b/app/models/metadata_content.rb index 233d7448..06c266a5 100644 --- a/app/models/metadata_content.rb +++ b/app/models/metadata_content.rb @@ -24,7 +24,11 @@ class MetadataContent < MetadataTemplate end def to_s - sanitizer.sanitize value, tags: [], attributes: [] + Nokogiri::HTML.fragment(value).tap do |html| + html.css('[src^="public/"]').each do |element| + element['src'] = convert_internal_path_to_src element['src'] + end + end.to_s end private @@ -76,6 +80,21 @@ class MetadataContent < MetadataTemplate html.to_s.html_safe end + # Convierte una ubicación local al sitio en una URL de ActiveStorage + # + # XXX: Por qué son tan díficiles de encontrar las rutas de AS + # + # @param path [String] + # @return [String] + def convert_internal_path_to_src(path) + key = path.split('/').second + blob = ActiveStorage::Blob.find_by(service_name: site.name, key: key) + + return unless blob + + "/rails/active_storage/blobs/#{blob.signed_id}/#{blob.filename}" + end + # Convierte una URI en una ruta interna del sitio actual # # XXX: No verifica si el archivo existe o no. Se supone que existe