Sha256: d773bbed08eb3c325372c666f460023a882e46e6f65dc148c62bd4cf8c5bb028

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module RenderEditorjs
  module Blocks
    # Render for https://github.com/editor-js/nested-list
    class List < Base
      SCHEMA = YAML.safe_load(<<~YAML)
        type: object
        additionalProperties: false
        definitions:
          Items:
            type: array
            properties:
              content:
                type: string
              items:
                type: Items
        properties:
          style:
            type: string
            pattern: ^(un)?ordered$
          items:
            type: Items
      YAML

      attr_reader :options

      def render(data)
        return unless valid?(data)

        tag = data["style"] == "unordered" ? :ul : :ol
        render_list(tag, data["items"])
      end

      def render_list(tag, items)
        return "" unless items

        content_tag(tag) do
          children_tag_string = ""
          items.each do |v|
            item = sanitize(v["content"])
            item << render_list(tag, v["items"]) if v["items"]
            children_tag_string += content_tag(:li, item.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(text)
        Sanitize.fragment(
          text,
          elements: safe_tags.keys,
          attributes: safe_tags.select { |_k, v| v },
          remove_contents: true
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
render_editorjs-0.1.0 lib/render_editorjs/blocks/list.rb