Truncate rich text descriptions at word boundaries if not too short

This commit is contained in:
Anton Khorev
2025-05-31 18:42:04 +03:00
parent 0d15172792
commit 7d9a45f33a
2 changed files with 29 additions and 3 deletions

View File

@ -6,6 +6,7 @@ module RichText
].freeze
DESCRIPTION_MAX_LENGTH = 500
DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH = 450
def self.new(format, text)
case format
@ -238,7 +239,13 @@ module RichText
return nil if text.blank?
text.truncate(DESCRIPTION_MAX_LENGTH)
text_truncated_to_word_break = text.truncate(DESCRIPTION_MAX_LENGTH, :separator => " ")
if text_truncated_to_word_break.length >= DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH
text_truncated_to_word_break
else
text.truncate(DESCRIPTION_MAX_LENGTH)
end
end
def image?(element)