diff --git a/Changelog.md b/Changelog.md index 1a9f3c307dd4c5455172e0026cdce02701f07986..92e20ed021c83c802b3e121662cfeef1c57e2bc0 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 1e3263127db5f018d2172824c6f282f782e20d79..f5d2dd2909f5457d5430525a5770f2de8d3645a8 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 3e340df5d84f7df337a2b846d21a90a25191514c..88d4f3290e738bc85666174d16f077e174e0849b 100644 --- a/lib/diaspora/taggable.rb +++ b/lib/diaspora/taggable.rb @@ -58,5 +58,14 @@ module Diaspora %{#{pre}<a class="tag" href="/tags/#{url_bit}">#{clickable}</a>} }.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 6a64f7f1dbf0cc06399ca29978363f7d049ca7b5..6c4b69e17274ddda9f85e484501b823683b3f7aa 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("<h1>Header</h1>\n\n<p><a href=\"http://localhost:9887/tags/messages\">#messages</a> containing <a href=\"http://localhost:9887/tags/hashtags\">#hashtags</a> should render properly</p>") end end -end \ No newline at end of file +end diff --git a/spec/lib/diaspora/taggable_spec.rb b/spec/lib/diaspora/taggable_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..bd4ab617f529eeb6089a4c3c6a227176826da0cf --- /dev/null +++ b/spec/lib/diaspora/taggable_spec.rb @@ -0,0 +1,59 @@ +require "spec_helper" + +describe Diaspora::Taggable do + describe "#format_tags" do + context "when there are no tags in the text" do + it "returns the input text" do + text = Diaspora::Taggable.format_tags("There are no tags.") + expect(text).to eq("There are no tags.") + end + end + + context "when there is a tag in the text" do + it "autolinks the hashtag" do + text = Diaspora::Taggable.format_tags("There is a #hashtag.") + expect(text).to eq("There is a <a class=\"tag\" href=\"/tags/hashtag\">#hashtag</a>.") + end + + it "autolinks #<3" do + text = Diaspora::Taggable.format_tags("#<3") + expect(text).to eq("<a class=\"tag\" href=\"/tags/<3\">#<3</a>") + end + end + + context "with multiple tags" do + it "autolinks the hashtags" do + text = Diaspora::Taggable.format_tags("#l #lol") + expect(text).to eq("<a class=\"tag\" href=\"/tags/l\">#l</a> <a class=\"tag\" href=\"/tags/lol\">#lol</a>") + end + end + end + + describe "#format_tags_for_mail" do + context "when there are no tags in the text" do + it "returns the input text" do + text = Diaspora::Taggable.format_tags_for_mail("There are no tags.") + expect(text).to eq("There are no tags.") + end + end + + context "when there is a tag in the text" do + it "autolinks and normalizes the hashtag" do + text = Diaspora::Taggable.format_tags_for_mail("There is a #hashTag.") + expect(text).to eq("There is a [#hashTag](http://localhost:9887/tags/hashtag).") + end + + it "autolinks #<3" do + text = Diaspora::Taggable.format_tags_for_mail("#<3") + expect(text).to eq("[#<3](http://localhost:9887/tags/%3C3)") + end + end + + context "with multiple tags" do + it "autolinks the hashtags" do + text = Diaspora::Taggable.format_tags_for_mail("#l #lol") + expect(text).to eq("[#l](http://localhost:9887/tags/l) [#lol](http://localhost:9887/tags/lol)") + end + end + end +end