Sha256: 347d83d46e0904b18beb90634409a16510f24f29db4c31369be307e3585bb4e8

Contents?: true

Size: 1.49 KB

Versions: 12

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require "redcarpet"

module Plutonium
  module UI
    module Display
      module Components
        class Markdown < Phlexi::Display::Components::Base
          include Phlexi::Display::Components::Concerns::DisplaysValue
          include Plutonium::UI::Component::Methods

          RENDERER = Redcarpet::Markdown.new(
            Redcarpet::Render::HTML.new(
              safe_links_only: true, with_toc_data: true, hard_wrap: true,
              link_attributes: {rel: :nofollow, target: :_blank}
            ),
            autolink: true, tables: true, no_intra_emphasis: true,
            fenced_code_blocks: true, disable_indented_code_blocks: true,
            strikethrough: true, space_after_headers: true, superscript: true,
            footnotes: true, highlight: true, underline: true
          )

          def render_value(value)
            article(**attributes) {
              unsafe_raw(render_markdown(value))
            }
          end

          private

          def render_markdown(value)
            RENDERER.render(
              ActionController::Base.helpers.sanitize(
                value,
                tags: %w[strong em sub sup details summary],
                attributes: []
              )
            )
          end

          def normalize_value(value)
            if value.respond_to?(:to_plain_text)
              value.to_plain_text
            else
              value.to_s
            end
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
plutonium-0.19.2 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.19.1 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.19.0 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.18.8 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.18.7 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.18.6 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.18.5 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.18.4 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.18.3 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.18.2 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.18.1 lib/plutonium/ui/display/components/markdown.rb
plutonium-0.18.0 lib/plutonium/ui/display/components/markdown.rb