Sha256: 9923ef852adc10e26f76005a79c6a1f419e591a453d1647dffc3eb0b147d5c14

Contents?: true

Size: 1.16 KB

Versions: 12

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module ContentParsers
      # A parser that searches for editor images from CarrierWave in html
      # contents and replaces them with migrated images to ActiveStorage
      #
      # @see BaseParser Examples of how to use a content parser
      class EditorImagesParser < Decidim::ContentParsers::BaseParser
        # @return [String] the content with the CarrierWave images replaced.
        def rewrite
          return content if editor_images.blank?

          replace_editor_images
          parsed_content.to_html
        end

        def editor_images
          @editor_images ||= parsed_content.search(:img).index_with do |image|
            context[:routes_mappings].find { |mapping| image.attr(:src).end_with?(mapping[:origin_path]) }
          end.compact
        end

        private

        def parsed_content
          @parsed_content ||= Nokogiri::HTML(content)
        end

        def replace_editor_images
          editor_images.each do |image, mapping|
            image.set_attribute(:src, mapping[:instance].attached_uploader(:file).path)
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.10.3 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.11.2 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.11.1 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.10.2 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.10.1 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.10.0 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.9.3 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.9.1 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.8.3 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.8.2 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.8.1 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb
decidim-decidim_awesome-0.8.0 lib/decidim/decidim_awesome/content_parsers/editor_images_parser.rb