Class: RenderEditorjs::Blocks::Paragraph

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

Overview

Compatible with default Paragraph and paragraph-with-aligment github.com/kaaaaaaaaaaai/paragraph-with-alignment

Constant Summary collapse

DEFAULT_SAFE_TAGS =
{
  "b" => nil,
  "i" => nil,
  "u" => ["class"],
  "del" => ["class"],
  "a" => ["href"],
  "mark" => ["class"],
  "code" => ["class"]
}.freeze
DEFAULT_OPTIONS =
{
  tag: "p"
}.freeze
SCHEMA =
YAML.safe_load(<<~YAML)
  type: object
  additionalProperties: false
  properties:
    text:
      type: string
    alignment:
      type: string
      enum:
        - left
        - center
        - right
YAML

Instance Attribute Summary collapse

Attributes inherited from Base

#output_buffer, #raw

Instance Method Summary collapse

Methods inherited from Base

#valid?, #validator

Constructor Details

#initialize(options = DEFAULT_OPTIONS.dup) ⇒ Paragraph

Returns a new instance of Paragraph.



38
39
40
41
42
# File 'lib/render_editorjs/blocks/paragraph.rb', line 38

def initialize(options = DEFAULT_OPTIONS.dup)
  @options = options
  @options[:safe_tags] ||= DEFAULT_SAFE_TAGS
  super()
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



36
37
38
# File 'lib/render_editorjs/blocks/paragraph.rb', line 36

def options
  @options
end

Instance Method Details

#render(data) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/render_editorjs/blocks/paragraph.rb', line 44

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

  alignment = data["alignment"]
  css_class = alignment ? "align-#{alignment}" : nil
  (options[:tag], class: css_class) do
    sanitize(data["text"]).html_safe
  end
end

#sanitize(text) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/render_editorjs/blocks/paragraph.rb', line 54

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