diff --git a/Changelog.md b/Changelog.md index 1a9f3c307..92e20ed02 100644 --- a/Changelog.md +++ b/Changelog.md @@ -46,6 +46,7 @@ * Add case insensitive unconfirmed email addresses as authentication key [#5967](https://github.com/diaspora/diaspora/pull/5967) * Fix liking on single post views when accessed via GUID [#5978](https://github.com/diaspora/diaspora/pull/5978) * Only return the current_users participation for post interactions [#6007](https://github.com/diaspora/diaspora/pull/6007) +* Fix tag rendering in emails [#6009](https://github.com/diaspora/diaspora/pull/6009) ## Features * Hide post title of limited post in comment notification email [#5843](https://github.com/diaspora/diaspora/pull/5843) diff --git a/lib/diaspora/markdownify/email.rb b/lib/diaspora/markdownify/email.rb index 1e3263127..f5d2dd290 100644 --- a/lib/diaspora/markdownify/email.rb +++ b/lib/diaspora/markdownify/email.rb @@ -1,26 +1,8 @@ module Diaspora module Markdownify class Email < Redcarpet::Render::HTML - include Rails.application.routes.url_helpers - TAG_REGEX = /(?:^|\s)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+)/u def preprocess(text) - process_tags(text) - end - - private - def tags(text) - text.scan(TAG_REGEX).map { |match| match[0] } - end - - def process_tags(text) - return text unless text.match(TAG_REGEX) - tags(text).each do |tag| - text.gsub!(/##{tag}/) do |tag| - opts = {:name => ActsAsTaggableOn::Tag.normalize(tag)}.merge(Rails.application.config.action_mailer.default_url_options) - "[#{tag}](#{tag_url(opts)})" - end - end - text + Diaspora::Taggable.format_tags_for_mail text end end end diff --git a/lib/diaspora/taggable.rb b/lib/diaspora/taggable.rb index 3e340df5d..88d4f3290 100644 --- a/lib/diaspora/taggable.rb +++ b/lib/diaspora/taggable.rb @@ -58,5 +58,14 @@ module Diaspora %{#{pre}#{clickable}} }.html_safe end + + def self.format_tags_for_mail(text) + regex = /(?<=^|\s|>)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+|<3)/u + text.gsub(regex) do |tag| + opts = {name: ActsAsTaggableOn::Tag.normalize(tag)} + .merge(Rails.application.config.action_mailer.default_url_options) + "[#{tag}](#{Rails.application.routes.url_helpers.tag_url(opts)})" + end + end end end diff --git a/spec/lib/diaspora/markdownify_email_spec.rb b/spec/lib/diaspora/markdownify_email_spec.rb index 6a64f7f1d..6c4b69e17 100644 --- a/spec/lib/diaspora/markdownify_email_spec.rb +++ b/spec/lib/diaspora/markdownify_email_spec.rb @@ -12,8 +12,8 @@ describe Diaspora::Markdownify::Email do end it 'should autolink multiple hashtags' do - markdownified = @html.preprocess("There are #two #Tags") - expect(markdownified).to eq("There are [#two](http://localhost:9887/tags/two) [#Tags](http://localhost:9887/tags/tags)") + markdownified = @html.preprocess("oh #l #loL") + expect(markdownified).to eq("oh [#l](http://localhost:9887/tags/l) [#loL](http://localhost:9887/tags/lol)") end it 'should not autolink headers' do @@ -33,4 +33,4 @@ describe Diaspora::Markdownify::Email do expect(rendered).to eq("