Class: Banzai::Filter::TruncateSourceFilter

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

Constant Summary collapse

CHARACTER_COUNT_LIMIT =
1.megabyte
USER_MSG_LIMIT =
10_000

Instance Method Summary collapse

Instance Method Details

#callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/banzai/filter/truncate_source_filter.rb', line 9

def call
  # don't truncate if it's a :blob and no limit is set
  return text if context[:text_source] == :blob && !context.key?(:limit)

  limit = context[:limit] || CHARACTER_COUNT_LIMIT

  # no sense in allowing `truncate_bytes` to duplicate a large
  # string unless it's too big
  return text if text.bytesize <= limit

  # Use three dots instead of the ellipsis Unicode character because
  # some clients show the raw Unicode value in the merge commit.
  trunc = text.truncate_bytes(limit, omission: '...')

  # allows us to indicate to the user that what they see is a truncated copy
  if limit > USER_MSG_LIMIT
    trunc.prepend("_The text is longer than #{limit} characters and has been visually truncated._\n\n")
  end

  trunc
end