Class: RenderEditorjs::Blocks::Image
- Defined in:
- lib/render_editorjs/blocks/image.rb
Overview
Render for github.com/editor-js/image
Constant Summary collapse
- SCHEMA =
YAML.safe_load(<<~YAML) type: object additionalProperties: false properties: file: type: object properties: url: type: string required: - url caption: type: string stretched: type: boolean withBackground: type: boolean withBorder: type: boolean required: - file YAML
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #render(data) ⇒ Object
- #sanitize(data) ⇒ Object
- #sanitize_caption(caption) ⇒ Object
- #sanitize_url(url) ⇒ Object
Methods inherited from Base
Instance Method Details
#render(data) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/render_editorjs/blocks/image.rb', line 30 def render(data) return unless valid?(data) url = sanitize_url(data["file"]["url"]) = (data["caption"]) with_border = data["withBorder"] with_background = data["withBackground"] stretched = data["stretched"] html_class = "picture" html_class += " picture--stretched" if stretched html_class += " picture--with-background" if with_background html_class += " picture--with-border" if with_border html_str = content_tag :div, class: html_class do content_tag :img, "", src: url end html_str << content_tag(:div, .html_safe, class: "caption").html_safe if .presence html_str end |
#sanitize(data) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/render_editorjs/blocks/image.rb', line 51 def sanitize(data) %w[caption url].each do |key| str = Sanitize.fragment(data[key], remove_contents: true).strip str.gsub!("&", "&") if key == "url" data[key] = str end data end |
#sanitize_caption(caption) ⇒ Object
65 66 67 |
# File 'lib/render_editorjs/blocks/image.rb', line 65 def () Sanitize.fragment(, remove_contents: true).strip end |
#sanitize_url(url) ⇒ Object
61 62 63 |
# File 'lib/render_editorjs/blocks/image.rb', line 61 def sanitize_url(url) Sanitize.fragment(url, remove_contents: true).strip.gsub("&", "&") end |