diff --git a/app/helpers/user_notifications_helper.rb b/app/helpers/user_notifications_helper.rb index afa80d832..4c088836d 100644 --- a/app/helpers/user_notifications_helper.rb +++ b/app/helpers/user_notifications_helper.rb @@ -65,9 +65,9 @@ module UserNotificationsHelper normalize_name(post.user.name) != normalize_name(post.user.username) end - def format_for_email(post, use_excerpt, style = nil) + def format_for_email(post, use_excerpt) html = use_excerpt ? post.excerpt : post.cooked - PrettyText.format_for_email(html, post, style).html_safe + PrettyText.format_for_email(html, post).html_safe end end diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index be69a384e..dfa195b5d 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -83,11 +83,9 @@ class UserNotifications < ActionMailer::Base return unless @posts_by_topic.present? build_summary_for(user) - build_email @user.email, - from_alias: I18n.t('user_notifications.mailing_list.from', site_name: SiteSetting.title), - subject: I18n.t('user_notifications.mailing_list.subject_template', - site_name: @site_name, - date: @date) + apply_notification_styles build_email @user.email, + from_alias: I18n.t('user_notifications.mailing_list.from', site_name: SiteSetting.title), + subject: I18n.t('user_notifications.mailing_list.subject_template', site_name: @site_name, date: @date) end def digest(user, opts={}) @@ -418,4 +416,12 @@ class UserNotifications < ActionMailer::Base @markdown_linker = MarkdownLinker.new(@base_url) @unsubscribe_key = UnsubscribeKey.create_key_for(@user, "digest") end + + def apply_notification_styles(email) + email.html_part.body = Email::Styles.new(email.html_part.body.to_s).tap do |styles| + styles.format_basic + styles.format_notification + end.to_html + email + end end diff --git a/app/views/email/_post.html.erb b/app/views/email/_post.html.erb index 6a324e301..42bb7afb8 100644 --- a/app/views/email/_post.html.erb +++ b/app/views/email/_post.html.erb @@ -23,7 +23,7 @@ - <%= format_for_email(post, use_excerpt, :notification) %> + <%= format_for_email(post, use_excerpt) %> diff --git a/app/views/user_notifications/mailing_list.html.erb b/app/views/user_notifications/mailing_list.html.erb index 2cd211645..c6b424054 100644 --- a/app/views/user_notifications/mailing_list.html.erb +++ b/app/views/user_notifications/mailing_list.html.erb @@ -60,7 +60,7 @@ - <%= I18n.l(post.created_at, format: :long) %>

- <%= raw format_for_email(post, false, :notification) %> + <%= raw format_for_email(post, false) %>
<% end %> diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 9551a4197..bf6fab878 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -394,16 +394,31 @@ JS fragment.to_html end + # Given a Nokogiri doc, convert all links to absolute + def self.make_all_links_absolute(doc) + site_uri = nil + doc.css("a").each do |link| + href = link["href"].to_s + begin + uri = URI(href) + site_uri ||= URI(Discourse.base_url) + link["href"] = "#{site_uri}#{link['href']}" unless uri.host.present? + rescue URI::InvalidURIError, URI::InvalidComponentError + # leave it + end + end + end + def self.strip_image_wrapping(doc) doc.css(".lightbox-wrapper .meta").remove end - def self.format_for_email(html, post = nil, style = nil) - Email::Styles.new(html, style: style).tap do |doc| - DiscourseEvent.trigger(:reduce_cooked, doc, post) - doc.make_all_links_absolute - doc.send :"format_#{style}" if style - end.to_html + def self.format_for_email(html, post = nil) + doc = Nokogiri::HTML.fragment(html) + DiscourseEvent.trigger(:reduce_cooked, doc, post) + strip_image_wrapping(doc) + make_all_links_absolute(doc) + doc.to_html end protected diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 729c6474c..6172faf84 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -318,7 +318,7 @@ HTML it "adds base url to relative links" do html = "

@wiseguy, @trollol what do you guys think?

" output = described_class.format_for_email(html, post) - expect(output).to eq("

@wiseguy, @trollol what do you guys think?

") + expect(output).to eq("

@wiseguy, @trollol what do you guys think?

") end it "doesn't change external absolute links" do