Module: NokogiriTruncator::NodeWithChildren

Defined in:
lib/middleman-blog/truncate_html.rb

Instance Method Summary collapse

Instance Method Details

#truncate(max_length, ellipsis) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/middleman-blog/truncate_html.rb', line 34

def truncate(max_length, ellipsis)
  return self if inner_text.length <= max_length

  truncated_node = dup
  truncated_node.children.remove

  children.each do |node|
    remaining_length = max_length - truncated_node.inner_text.length
    break if remaining_length <= 0

    truncated_node.add_child node.truncate(remaining_length, ellipsis)
  end
  truncated_node
end