Sha256: b5b8ecb7cd175e28c6936fe2427f03d6ab1067e573320cc6169611f1404ddf06

Contents?: true

Size: 1.86 KB

Versions: 35

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Tags
    class BlockRenderTag < Liquid::Block
      # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
      def render(context)
        context.stack({}) do
          # unindent the incoming text
          content = Bridgetown::Utils.reindent_for_markdown(super)

          regions = gather_content_regions(context)

          site = context.registers[:site]
          converter = site.find_converter_instance(Bridgetown::Converters::Markdown)
          markdownified_content = converter.convert(content)
          context["processed_component_content"] = markdownified_content

          render_params = [@markup, "content: processed_component_content"]
          unless regions.empty?
            regions.each do |region_name, region_content|
              region_name = region_name.sub("content_with_region_", "")

              if region_name.end_with? ":markdown"
                region_name.sub!(%r!:markdown$!, "")
                context[region_name] = converter.convert(
                  Bridgetown::Utils.reindent_for_markdown(region_content)
                )
              else
                context[region_name] = region_content
              end
              render_params.push "#{region_name}: #{region_name}"
            end
          end

          Liquid::Render.parse("render", render_params.join(","), nil, @parse_context)
            .render_tag(context, +"")
        end
      end
      # rubocop:enable Metrics/MethodLength, Metrics/AbcSize

      private

      def gather_content_regions(context)
        unless context.scopes[0].keys.find { |k| k.to_s.start_with? "content_with_region_" }
          return {}
        end

        context.scopes[0].select { |k| k.to_s.start_with? "content_with_region_" }
      end
    end
  end
end

Liquid::Template.register_tag("rendercontent", Bridgetown::Tags::BlockRenderTag)

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
bridgetown-core-2.0.0.beta3 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-2.0.0.beta2 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-2.0.0.beta1 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.3.4 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.3.3 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.3.2 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.3.1 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.3.0 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.3.0.beta3 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.3.0.beta2 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.3.0.beta1 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.2.0 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.2.0.beta5 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.2.0.beta4 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.2.0.beta3 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.2.0.beta2 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.2.0.beta1 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.1.0 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.1.0.beta3 lib/bridgetown-core/tags/render_content.rb
bridgetown-core-1.1.0.beta2 lib/bridgetown-core/tags/render_content.rb