Sha256: 7ee75033fccaab532e38f62a5a5bdbbda58bd458be9f839d2ff73c312c1f3dfd

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module EditorJs
  module Blocks
    # list block
    class ListBlock < Base
      def schema
        YAML.safe_load(<<~YAML)
          type: object
          additionalProperties: false
          properties:
            style:
              type: string
              pattern: ^(un)?ordered$
            items:
              type: array
              items:
                type: string
        YAML
      end

      def render(_options = {})
        tag = data['style'] == 'unordered' ? :ul : :ol
        content_tag(tag, class: css_name) do
          children_tag_string = ''
          data['items'].each do |v|
            children_tag_string += content_tag(:li, v.html_safe)
          end
          children_tag_string.html_safe
        end
      end

      def safe_tags
        {
          'b' => nil,
          'i' => nil,
          'u' => ['class'],
          'del' => ['class'],
          'a' => ['href'],
          'mark' => ['class'],
          'code' => ['class']
        }
      end

      def sanitize!
        data['items'] = data['items'].map do |text|
          Sanitize.fragment(
            text,
            elements: safe_tags.keys,
            attributes: safe_tags.select { |_k, v| v },
            remove_contents: true
          )
        end
      end

      def plain
        data['items'].map do |text|
          decode_html(Sanitize.fragment(text)).strip
        end.join(', ')
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
editor_js-0.4.0 lib/editor_js/blocks/list_block.rb
editor_js-0.3.6.1 lib/editor_js/blocks/list_block.rb
editor_js-0.3.5.1 lib/editor_js/blocks/list_block.rb
editor_js-0.3.6 lib/editor_js/blocks/list_block.rb
editor_js-0.3.5 lib/editor_js/blocks/list_block.rb