Sha256: 285d0b65afde5790759637fd280d7e32801a2f549ce392f6c825f071f0e7d59b

Contents?: true

Size: 1.73 KB

Versions: 10

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module EditorJs
  module Blocks
    class MarkdownBlock < Base
      class HTMLwithCodeRay < Redcarpet::Render::HTML
        def block_code(code, language)
          CodeRay.scan(code, language || :text).div(css: :class)
        end

        def list_item(text, list_type)
          if text.start_with?("[x]", "[X]")
            text[0..2] = %(<input type='checkbox' checked='checked' disabled>)
          elsif text.start_with?("[ ]")
            text[0..2] = %(<input type='checkbox' disabled>)
          end
          %(<li>#{text}</li>)
        end
      end

      def sanitize!; end

      def schema
        YAML.safe_load(<<~YAML)
          type: object
          additionalProperties: false
          properties:
            text:
              type: string
        YAML
      end

      def render(_options = {})
        content_tag :div, class: css_name do
          content_text = data['text'] || ''

          render_options = {
            escape_html: true,
            hard_wrap: true,
            with_toc_data: true,
            link_attributes: { rel: 'nofollow', target: '_blank' }
          }
          renderer = HTMLwithCodeRay.new(render_options)

          options = {
            autolink: true,
            fenced_code_blocks: true,
            lax_spacing: true,
            no_intra_emphasis: true,
            strikethrough: true,
            tables: true,
            superscript: true,
            highlight: true,
            quote: true,
            footnotes: true
          }
          markdown_to_html = Redcarpet::Markdown.new(renderer, options)

          markdown_to_html.render(content_text).html_safe
        end
      end

      def plain
        data['text'].strip
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
editor_js-0.3.3 lib/editor_js/blocks/markdown_block.rb
editor_js-0.3.2 lib/editor_js/blocks/markdown_block.rb
editor_js-0.3.1 lib/editor_js/blocks/markdown_block.rb
editor_js-0.3.0 lib/editor_js/blocks/markdown_block.rb
editor_js-0.2.5 lib/editor_js/blocks/markdown_block.rb
editor_js-0.2.4 lib/editor_js/blocks/markdown_block.rb
editor_js-0.2.3 lib/editor_js/blocks/markdown_block.rb
editor_js-0.2.2 lib/editor_js/blocks/markdown_block.rb
editor_js-0.2.1 lib/editor_js/blocks/markdown_block.rb
editor_js-0.2.0 lib/editor_js/blocks/markdown_block.rb