Class: Banzai::Filter::TruncateVisibleFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/banzai/filter/truncate_visible_filter.rb

Constant Summary collapse

MATCH_CODE =

Truncates the document to ‘truncate_visible_max_chars` characters, excluding any HTML tags.

'pre > code > .line'

Instance Method Summary collapse

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/banzai/filter/truncate_visible_filter.rb', line 11

def call
  return doc unless context[:truncate_visible_max_chars].present?

  max_chars = context[:truncate_visible_max_chars]
  content_length = 0
  @truncated = false

  doc.traverse do |node|
    if node.text? || node.content.empty?
      if truncated
        node.remove
        next
      end

      handle_line_breaks(node)
      truncate_content(content_length, max_chars, node)

      content_length += node.content.length
    end

    truncate_if_block(node)
  end

  doc
end