class HtmlToProsemirror::Renderer

Your code goes here…

Public Class Methods

new() click to toggle source
# File lib/html_to_prosemirror.rb, line 25
def initialize()
  @storedMarks = []
  @marks = [
    HtmlToProsemirror::Marks::Bold,
    HtmlToProsemirror::Marks::Code,
    HtmlToProsemirror::Marks::Italic,
    HtmlToProsemirror::Marks::Link
  ]
  @nodes = [
    HtmlToProsemirror::Nodes::BulletList,
    HtmlToProsemirror::Nodes::CodeBlockWrapper,
    HtmlToProsemirror::Nodes::CodeBlock,
    HtmlToProsemirror::Nodes::HardBreak,
    HtmlToProsemirror::Nodes::Heading,
    HtmlToProsemirror::Nodes::Image,
    HtmlToProsemirror::Nodes::ListItem,
    HtmlToProsemirror::Nodes::OrderedList,
    HtmlToProsemirror::Nodes::Paragraph,
    HtmlToProsemirror::Nodes::Text,
    HtmlToProsemirror::Nodes::User
  ]
end

Public Instance Methods

render(value) click to toggle source
# File lib/html_to_prosemirror.rb, line 48
def render(value)
  minified = minify_html(value.strip! || value)
  @document = Nokogiri::HTML.fragment(minified)
  content = render_children(@document)
  return {
    type: 'doc',
    content: content,
  }
end

Private Instance Methods

get_matching_class(node, classes) click to toggle source

Find which class matches the HtmlElement

# File lib/html_to_prosemirror.rb, line 115
def get_matching_class(node, classes)
  found = classes.select do |clazz|
    instance = clazz.new(node)
    if (instance.matching())
      return instance
    end
  end
  found.first
end
get_matching_mark(item) click to toggle source

Find which Mark matches the HtmlElement

# File lib/html_to_prosemirror.rb, line 111
def get_matching_mark(item)
  return get_matching_class(item, @marks)
end
get_matching_node(item) click to toggle source

Find which Node matches the Html Node

# File lib/html_to_prosemirror.rb, line 107
def get_matching_node(item)
  return get_matching_class(item, @nodes)
end
minify_html(html) click to toggle source
# File lib/html_to_prosemirror.rb, line 125
def minify_html(html)
  # 1. Remove all comments: gsub(/(<!--(\w|\s|:|!|#|<|>|'|"|=|;|,|\.|\?)*-->|\/\*[^\*]*\*\/|^(\t|\s)*\/\/.*)/, '')
  # 1.1 html comments without special characters: <!--(\w|\s|:|!|#|<|>|'|"|=|;|,|\.|\?)*-->
  # 1.2. Remove javascript comments e.g. /* */ and // \/\*[^\*]*\*\/ and ^(\t|\s)*\/\/.*
  # 3. Replace all carrier return and all tabs by a single space gsub(/(\n|\t)/, ' ').
  # 4. Replace any consecutive spaces by a single space gsub(/\s{2,}/, ' ')
  # 5. Remove space between tags gsub(/>\s+</, '><').strip.
  html.gsub(/(<!--(\w|\s|:|!|#|<|>|'|"|=|;|,|\.|\?)*-->|\/\*[^\*]*\*\/|^(\t|\s)*\/\/.*)/, '').
    gsub(/(\n|\t)/, ' ').
    gsub(/\s{2,}/, ' ').
    gsub(/>\s+</, '><').strip
end
render_children(node) click to toggle source

def get_document_body

return @document.search('body')[0];

end

# File lib/html_to_prosemirror.rb, line 63
def render_children(node)
  nodes = []
  node.children.each do |child|
    child_node = get_matching_node(child)
    if( child_node)
      item = child_node.data()
      if (item === nil)
        if (child.children.length > 0)
          nodes = nodes + render_children(child)
        end
        next
      end
      if (child.children.length > 0)
        item = item.merge({
          content: render_children(child),
        })
      end
      if (@storedMarks.count > 0)
          item = item.merge({
            marks: @storedMarks,
          })
          @storedMarks = [];
      end
      # if (child_node.wrapper)
      #   item['content'] = [
      #     instance.wrapper.merge({
      #       content: item['content'],
      #     }),
      #   ]
      # end
      nodes.push(item)
    end

    child_mark = get_matching_mark(child)
    if (child_mark)
      @storedMarks.push(child_mark.data())
      if (child.children.length > 0)
        nodes = nodes + render_children(child)
      end
    end
  end
  return nodes;
end