# frozen_string_literal: true require 'htmlbeautifier' # Se encarga del contenido del artículo y quizás otros campos que # requieran texto largo. class MetadataContent < MetadataTemplate def default_value super || '' end def value self[:value] || legacy_content || default_value end def front_matter? false end def document_value document.content end def indexable? true && !private? end def to_s Nokogiri::HTML5.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 # Detectar si el contenido estaba en Markdown y pasarlo a HTML def legacy_content return unless document_value return document_value if /^\s*] def allowed_styles @allowed_styles ||= %w[text-align color background-color] 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 # porque ya fue subido antes. # # @param uri [URI] # @return [String,nil] def convert_src_to_internal_path(uri) signed_id = uri.path.split('/').fifth blob = ActiveStorage::Blob.find_signed(signed_id) return unless blob return unless blob.service_name == site.name blob_path = Pathname.new(blob.service.path_for(blob.key)).realpath site_path = Pathname.new(site.path).realpath blob_path.relative_path_from(site_path).to_s rescue ActiveSupport::MessageVerifier::InvalidSignature => e ExceptionNotifier.notify_exception(e, data: { site: site.name }) nil end end