From 3cba2a6cf100bb9c9ec1c5e1dedf0e7eb66e46a3 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 31 May 2025 18:53:10 +0300 Subject: [PATCH] Allow word separator to consist of multiple spaces Uses negative lookbehind regexp, because .truncate calls .rindex. See ruby String#rindex method for details: https://ruby-doc.org/3.2.6/String.html#method-i-rindex --- lib/rich_text.rb | 2 +- test/lib/rich_text_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/rich_text.rb b/lib/rich_text.rb index e40289531..e05d030dc 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -239,7 +239,7 @@ module RichText return nil if text.blank? - text_truncated_to_word_break = text.truncate(DESCRIPTION_MAX_LENGTH, :separator => " ") + text_truncated_to_word_break = text.truncate(DESCRIPTION_MAX_LENGTH, :separator => /(?= DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH text_truncated_to_word_break diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index 6afedad83..6d5c2a9c1 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -492,6 +492,15 @@ class RichTextTest < ActiveSupport::TestCase assert_equal "#{'x' * (t - o)}...", r.description end + def test_markdown_description_word_break_multiple_spaces + m = RichText::DESCRIPTION_MAX_LENGTH + t = RichText::DESCRIPTION_WORD_BREAK_THRESHOLD_LENGTH + o = 3 # "...".length + + r = RichText.new("markdown", "#{'x' * (t - o)} #{'y' * (m - (t - o - 1))}") + assert_equal "#{'x' * (t - o)}...", r.description + end + private def assert_html(richtext, &block) -- 2.39.5