diff --git a/app/helpers/excerpt_helper.rb b/app/helpers/excerpt_helper.rb index 9ae0d3274..b48986c26 100644 --- a/app/helpers/excerpt_helper.rb +++ b/app/helpers/excerpt_helper.rb @@ -2,9 +2,9 @@ module ExcerptHelper def format_excerpt(content, length: 200) return "" if content.blank? - text = content.to_plain_text + text = content.respond_to?(:to_plain_text) ? content.to_plain_text : content.to_s text = text.gsub(/^>\s*(.*)$/m, '> \1') - text = text.gsub(/^[-*]\s*(.*)$/m, '• \1') + text = text.gsub(/^\s*[-+]\s*(.*)$/m, '• \1') text = text.gsub(/^\d+\.\s*(.*)$/m) { |m| m } text = text.gsub(/\s+/, " ").strip text.truncate(length) diff --git a/test/helpers/excerpt_helper_test.rb b/test/helpers/excerpt_helper_test.rb new file mode 100644 index 000000000..ef685680b --- /dev/null +++ b/test/helpers/excerpt_helper_test.rb @@ -0,0 +1,31 @@ +require "test_helper" + +class ExcerptHelperTest < ActionView::TestCase + test "quote" do + assert_excerpt("> Hello world", "> Hello world") + end + + test "ul" do + assert_excerpt("• Hello world", "- Hello world") + assert_excerpt("• Hello world", " - Hello world") + end + + test "ol" do + assert_excerpt("99. Hello world", "99. Hello world") + end + + test "large spaces" do + assert_excerpt("Hello world", " Hello world ") + end + + test "long text" do + assert_excerpt("A"*197 + "...", "A"*1000) + assert_excerpt("A"*97 + "...", "A"*1000, length: 100) + end + + private + def assert_excerpt(expected, content, ...) + assert_equal expected, format_excerpt(ActionText::Content.new(content), ...), "Excerpt of Action Text Content does not match" + assert_equal expected, format_excerpt(content, ...), "Excerpt of String does not match" + end +end