Class: ProsemirrorToHtml::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/prosemirror_to_html.rb

Instance Method Summary collapse

Instance Method Details

#add_mark(mark) ⇒ Object



113
114
115
116
117
# File 'lib/prosemirror_to_html.rb', line 113

def add_mark(mark)
  @marks << mark

  self
end

#add_marks(marks) ⇒ Object



119
120
121
122
123
# File 'lib/prosemirror_to_html.rb', line 119

def add_marks(marks)
  @marks.push marks

  self
end

#add_node(node) ⇒ Object



101
102
103
104
105
# File 'lib/prosemirror_to_html.rb', line 101

def add_node(node)
  @nodes << node

  self
end

#add_nodes(nodes) ⇒ Object



107
108
109
110
111
# File 'lib/prosemirror_to_html.rb', line 107

def add_nodes(nodes)
  @nodes.push nodes

  self
end

#render(value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/prosemirror_to_html.rb', line 78

def render(value)
  json = case value
         when Hash then value.to_json
         when String then value
         else raise Error
         end

  @document = JSON.parse(json, symbolize_names: true)

  html = ''

  content = @document[:content].is_a?(Array) ? @document[:content] : []

  content.each_with_index do |node, index|
    prev_node = index > 0 ? content[index - 1] : nil
    next_node = content[index + 1]

    html << render_node(node, prev_node, next_node)
  end

  sanitize(html)
end

#replace_mark(search_mark, replace_mark) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/prosemirror_to_html.rb', line 135

def replace_mark(search_mark, replace_mark)
  @marks.each do |key, mark_klass|
    if mark_klass == search_mark
      @marks[key] = replace_mark
    end
  end

  self
end

#replace_node(search_node, replace_node) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/prosemirror_to_html.rb', line 125

def replace_node(search_node, replace_node)
  @nodes.each do |key, node_klass|
    if node_klass == search_node
      @nodes[key] = replace_node
    end
  end

  self
end

#with_marks(marks = nil) ⇒ Object



66
67
68
69
70
# File 'lib/prosemirror_to_html.rb', line 66

def with_marks(marks = nil)
  @marks = marks if marks.is_a? Array

  self
end

#with_nodes(nodes = nil) ⇒ Object



72
73
74
75
76
# File 'lib/prosemirror_to_html.rb', line 72

def with_nodes(nodes = nil)
  @nodes = nodes if nodes.is_a? Array

  self
end