Skip to content

A platform for community discussion. Free, open, simple.

FIX: excerpts in summary email are too short when there are images or links

Neil Lalondeab2f3e43eb00
diff --git a/app/helpers/user_notifications_helper.rb b/app/helpers/user_notifications_helper.rb
index de7826586fb..3e9683359e6 100644
--- a/app/helpers/user_notifications_helper.rb
+++ b/app/helpers/user_notifications_helper.rb
@@ -32,14 +32,16 @@ module UserNotificationsHelper
"<a href='#{Discourse.base_url}' style='color: ##{color}'>#{@site_name}</a>"
end
- def first_paragraph_from(html)
+ def first_paragraphs_from(html)
doc = Nokogiri::HTML(html)
result = ""
+ length = 0
doc.css('body > p, aside.onebox').each do |node|
if node.text.present?
result << node.to_s
- return result if result.size >= SiteSetting.digest_min_excerpt_length
+ length += node.inner_text.length
+ return result if length >= SiteSetting.digest_min_excerpt_length
end
end
return result unless result.blank?
@@ -49,7 +51,7 @@ module UserNotificationsHelper
end
def email_excerpt(html_arg)
- html = (first_paragraph_from(html_arg) || html_arg).to_s
+ html = (first_paragraphs_from(html_arg) || html_arg).to_s
PrettyText.format_for_email(html).html_safe
end
diff --git a/spec/helpers/user_notifications_helper_spec.rb b/spec/helpers/user_notifications_helper_spec.rb
index 191f4a984a8..dcd5cdcfce0 100644
--- a/spec/helpers/user_notifications_helper_spec.rb
+++ b/spec/helpers/user_notifications_helper_spec.rb
@@ -20,5 +20,19 @@ describe UserNotificationsHelper do
SiteSetting.digest_min_excerpt_length = 100
expect(helper.email_excerpt(cooked)).to eq(paragraphs.join)
end
+
+ it "doesn't count emoji images" do
+ with_emoji = "<p>Hi <img src=\"/images/emoji/twitter/smile.png?v=5\" title=\":smile:\" class=\"emoji\" alt=\":smile:\"></p>"
+ arg = ([with_emoji] + paragraphs).join("\n")
+ SiteSetting.digest_min_excerpt_length = 50
+ expect(helper.email_excerpt(arg)).to eq([with_emoji, paragraphs[0]].join)
+ end
+
+ it "only counts link text" do
+ with_link = "<p>Hi <a href=\"https://really-long.essays.com/essay/number/9000/this-one-is-about-friends-and-got-a-C-minus-in-grade-9\">friends</a>!</p>"
+ arg = ([with_link] + paragraphs).join("\n")
+ SiteSetting.digest_min_excerpt_length = 50
+ expect(helper.email_excerpt(arg)).to eq([with_link, paragraphs[0]].join)
+ end
end
end