Commit fdad3488 authored by Steffen van Bergerem's avatar Steffen van Bergerem Committed by Dennis Schubert

Fix tag rendering in emails

closes #6009
parent ac52cef5
......@@ -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)
......
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
......
......@@ -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
......@@ -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
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\">#&lt;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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment