Sha256: 55f9a43cc75577f9a8fec7418da75a6558e1f808df794c1cc9cc0632f4a09798

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module EditorJs
  module Blocks
    # markdown block
    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

2 entries across 2 versions & 1 rubygems

Version Path
editor_js-0.3.5.1 lib/editor_js/blocks/markdown_block.rb
editor_js-0.3.5 lib/editor_js/blocks/markdown_block.rb