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

This commit is contained in:
Neil Lalonde 2018-03-12 18:12:09 -04:00
parent 5f133e20f2
commit ab2f3e43eb
2 changed files with 19 additions and 3 deletions

View File

@ -32,14 +32,16 @@ module UserNotificationsHelper
"<a href='#{Discourse.base_url}' style='color: ##{color}'>#{@site_name}</a>" "<a href='#{Discourse.base_url}' style='color: ##{color}'>#{@site_name}</a>"
end end
def first_paragraph_from(html) def first_paragraphs_from(html)
doc = Nokogiri::HTML(html) doc = Nokogiri::HTML(html)
result = "" result = ""
length = 0
doc.css('body > p, aside.onebox').each do |node| doc.css('body > p, aside.onebox').each do |node|
if node.text.present? if node.text.present?
result << node.to_s 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
end end
return result unless result.blank? return result unless result.blank?
@ -49,7 +51,7 @@ module UserNotificationsHelper
end end
def email_excerpt(html_arg) 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 PrettyText.format_for_email(html).html_safe
end end

View File

@ -20,5 +20,19 @@ describe UserNotificationsHelper do
SiteSetting.digest_min_excerpt_length = 100 SiteSetting.digest_min_excerpt_length = 100
expect(helper.email_excerpt(cooked)).to eq(paragraphs.join) expect(helper.email_excerpt(cooked)).to eq(paragraphs.join)
end 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
end end