Class: RenderEditorjs::Blocks::List

Inherits:
Base
  • Object
show all
Defined in:
lib/render_editorjs/blocks/list.rb

Overview

Constant Summary collapse

SAFE_TAGS =

TODO: Consider extract it and the sanitize method with the other on Paragraph block to the DefaultRenderer

{
  "b" => nil,
  "i" => nil,
  "u" => ["class"],
  "del" => ["class"],
  "a" => ["href"],
  "mark" => ["class"],
  "code" => ["class"]
}.freeze
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

Instance Attribute Summary collapse

Attributes inherited from Base

#output_buffer, #raw

Instance Method Summary collapse

Methods inherited from Base

#valid?, #validator

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/render_editorjs/blocks/list.rb', line 38

def options
  @options
end

Instance Method Details

#render(data) ⇒ Object



40
41
42
43
44
45
# File 'lib/render_editorjs/blocks/list.rb', line 40

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

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

#render_list(tag, items) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/render_editorjs/blocks/list.rb', line 47

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

  (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 += (:li, item.html_safe)
    end
    children_tag_string.html_safe
  end
end

#sanitize(text) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/render_editorjs/blocks/list.rb', line 61

def sanitize(text)
  Sanitize.fragment(
    text,
    elements: SAFE_TAGS.keys,
    attributes: SAFE_TAGS.select { |_k, v| v },
    remove_contents: true
  )
end