class BlockEditor::BlockListRenderer
Handles the rendering of a block list including dynamic blocks and removing HTML comments
Public Class Methods
render(raw_html)
click to toggle source
Renders dynamic blocks within the HTML snippet then strips all HTML comments (including Gutenberg markup)
@param raw_html [String]
@return [String] Parsed content
# File lib/block_editor/block_list_renderer.rb, line 9 def self.render(raw_html) html = Nokogiri::HTML::DocumentFragment.parse(raw_html) # Find & render all instances of a dynamic block (including reusable blocks) BlockEditor.dynamic_blocks.each do |dynamic_block| dynamic_block = dynamic_block.constantize html.search('.//comment()').select {|comment| comment.inner_text.starts_with?(" wp:#{dynamic_block.name}") }.each do |block_instance| block_attributes = block_instance.inner_text.split(" wp:#{dynamic_block.name}")[1][0...-1] block_attributes = block_attributes.blank? ? {} : JSON.parse(block_attributes) block_content = render_block(dynamic_block, block_attributes) block_instance.replace(block_content) end end html.search('.//comment()').remove html.to_s.html_safe end
render_block(block, options)
click to toggle source
Renders a specific block using the provided options
@param block [String] name of block @param options [Hash] block options to use when rendering
@return [String] block content (HTML)
# File lib/block_editor/block_list_renderer.rb, line 35 def self.render_block(block, options) block.render(options) rescue StandardError => e respond_with_block_error(e) end
respond_with_block_error(error)
click to toggle source
Handles block errors
# File lib/block_editor/block_list_renderer.rb, line 42 def self.respond_with_block_error(error) Rails.logger.error("Error rendering block - #{error.message}") '' end