Sha256: 9791bb7b4052a54673a8ec596453bc43e96cb7faa0cf911a2845a4057c2009e9

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require "redcarpet"

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

          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

4 entries across 4 versions & 1 rubygems

Version Path
plutonium-0.16.3 lib/plutonium/ui/display/component/markdown.rb
plutonium-0.16.1 lib/plutonium/ui/display/component/markdown.rb
plutonium-0.16.0 lib/plutonium/ui/display/component/markdown.rb
plutonium-0.15.24 lib/plutonium/ui/display/component/markdown.rb