Class: Panda::Editor::HtmlToEditorJsConverter
- Inherits:
-
Object
- Object
- Panda::Editor::HtmlToEditorJsConverter
- Defined in:
- lib/panda/editor/html_to_editor_js_converter.rb,
app/services/panda/editor/html_to_editor_js_converter.rb
Overview
Converts HTML to EditorJS format Parses HTML and converts it to EditorJS blocks
Defined Under Namespace
Classes: ConversionError
Class Method Summary collapse
- .convert(html) ⇒ Object
- .create_paragraph_block(text) ⇒ Object
- .process_inline_elements(node) ⇒ Object
Instance Method Summary collapse
- #convert ⇒ Object
-
#initialize(html) ⇒ HtmlToEditorJsConverter
constructor
A new instance of HtmlToEditorJsConverter.
Constructor Details
#initialize(html) ⇒ HtmlToEditorJsConverter
Returns a new instance of HtmlToEditorJsConverter.
14 15 16 17 |
# File 'lib/panda/editor/html_to_editor_js_converter.rb', line 14 def initialize(html) @html = html @blocks = [] end |
Class Method Details
.convert(html) ⇒ Object
10 11 12 |
# File 'lib/panda/editor/html_to_editor_js_converter.rb', line 10 def self.convert(html) new(html).convert end |
.create_paragraph_block(text) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'app/services/panda/editor/html_to_editor_js_converter.rb', line 152 def self.create_paragraph_block(text) { 'type' => 'paragraph', 'data' => { 'text' => text.strip } } end |
.process_inline_elements(node) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'app/services/panda/editor/html_to_editor_js_converter.rb', line 161 def self.process_inline_elements(node) result = '' node.children.each do |child| case child.name when 'br' result += '<br>' when 'text' result += child.text when 'strong', 'b' result += "<b>#{child.text}</b>" when 'em', 'i' result += "<i>#{child.text}</i>" when 'a' href = child['href'] text = child.text.strip # Handle email links specially if href&.start_with?('mailto:') email = href.sub('mailto:', '') result += "<a href=\"mailto:#{email}\">#{text}</a>" else result += "<a href=\"#{href}\">#{text}</a>" end else result += if child.text? child.text else child.to_html end end end result.strip end |
Instance Method Details
#convert ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/panda/editor/html_to_editor_js_converter.rb', line 19 def convert doc = Nokogiri::HTML.fragment(@html) doc.children.each do |node| block = node_to_block(node) @blocks << block if block end { time: Time.now.to_i * 1000, blocks: @blocks, version: "2.28.0" } end |