Class: RenderEditorjs::Blocks::Paragraph
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(options = DEFAULT_OPTIONS.dup) ⇒ Paragraph
constructor
A new instance of Paragraph.
- #render(data) ⇒ Object
- #sanitize(text) ⇒ Object
Methods inherited from Base
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( = DEFAULT_OPTIONS.dup) @options = @options[:safe_tags] ||= DEFAULT_SAFE_TAGS super() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
36 37 38 |
# File 'lib/render_editorjs/blocks/paragraph.rb', line 36 def @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 content_tag([: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: [:safe_tags].keys, attributes: [:safe_tags].select { |_k, v| v }, remove_contents: true ) end |