debian-mirror-gitlab/lib/banzai/filter/truncate_source_filter.rb

33 lines
1 KiB
Ruby
Raw Normal View History

2021-03-08 18:12:59 +05:30
# frozen_string_literal: true
module Banzai
module Filter
class TruncateSourceFilter < HTML::Pipeline::TextFilter
2021-06-02 17:11:27 +05:30
CHARACTER_COUNT_LIMIT = 1.megabyte
USER_MSG_LIMIT = 10_000
2021-03-08 18:12:59 +05:30
def call
2021-06-02 17:11:27 +05:30
# 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
2021-03-08 18:12:59 +05:30
2021-03-11 19:13:27 +05:30
# Use three dots instead of the ellipsis Unicode character because
# some clients show the raw Unicode value in the merge commit.
2021-06-02 17:11:27 +05:30
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
2021-03-08 18:12:59 +05:30
end
end
end
end