class EditorJs::Blocks::TableBlock

table block

Public Instance Methods

plain() click to toggle source
# File lib/editor_js/blocks/table_block.rb, line 59
def plain
  str = data['content'].flatten.join(', ')
  decode_html Sanitize.fragment(str).gsub(/(, )+/, ', ').strip
end
render(_options = {}) click to toggle source
# File lib/editor_js/blocks/table_block.rb, line 21
def render(_options = {})
  content_tag(:div, class: css_name) do
    content_tag(:table) do
      data['content'].map do |row|
        content_tag(:tr) do
          row.map { |col| content_tag :td, col.html_safe }.join().html_safe
        end
      end.join().html_safe
    end
  end
end
safe_tags() click to toggle source
# File lib/editor_js/blocks/table_block.rb, line 33
def safe_tags
  {
    'b' => nil,
    'i' => nil,
    'u' => ['class'],
    'del' => ['class'],
    'a' => ['href'],
    'mark' => ['class'],
    'code' => ['class'],
    'br' => nil
  }
end
sanitize!() click to toggle source
# File lib/editor_js/blocks/table_block.rb, line 46
def sanitize!
  data['content'] = data['content'].map do |row|
    (row || []).map do |cell_value|
      Sanitize.fragment(
        cell_value,
        elements: safe_tags.keys,
        attributes: safe_tags.select { |_k, v| v },
        remove_contents: false
      )
    end
  end
end
schema() click to toggle source
# File lib/editor_js/blocks/table_block.rb, line 7
      def schema
        YAML.safe_load(<<~YAML)
          type: object
          additionalProperties: false
          properties:
            content:
              type: array
              items:
                type: array
                items:
                  type: string
        YAML
      end