Sha256: e510ad596b6800338b626afff3d15ddfee2c96c440e98ea2bf1f715f483a1302

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

require 'rails'
require 'administrate/field/text'
require 'administrate/engine'
require 'redcarpet'
require 'redcarpet/render_strip'

module Administrate
  module Field
    class SimpleMarkdown < Administrate::Field::Text
      class Engine < ::Rails::Engine
        Administrate::Engine.add_javascript 'administrate-field-simple_markdown/application'
        Administrate::Engine.add_stylesheet 'administrate-field-simple_markdown/application'
      end

      def data
        @data || ''
      end

      def to_html
        markdown(html_renderer).render(data).html_safe
      end

      def to_s
        markdown(plaintext_renderer).render(data)
      end

      private

      def html_renderer
        @html_renderer ||= Redcarpet::Render::HTML.new({
          safe_links_only: true,
          filter_html: true,
          with_toc_data: true,
          hard_wrap: true,
          link_attributes: { rel: 'nofollow' }
        })
      end

      def plaintext_renderer
        @plaintext_renderer ||= Redcarpet::Render::StripDown
      end

      def markdown(renderer)
        @markdown ||= Redcarpet::Markdown.new(renderer, {
          autolink: true,
          tables: true,
          no_intra_emphasis: true,
          strikethrough: true,
          highlight: true,
          space_after_headers: true
        })
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
administrate-field-simple_markdown-0.2.1 lib/administrate/field/simple_markdown.rb
administrate-field-simple_markdown-0.2.0 lib/administrate/field/simple_markdown.rb
administrate-field-simple_markdown-0.1.2 lib/administrate/field/simple_markdown.rb
administrate-field-simple_markdown-0.1.1 lib/administrate/field/simple_markdown.rb
administrate-field-simple_markdown-0.1.0 lib/administrate/field/simple_markdown.rb