Skip to content

Commit

Permalink
Fix tag rendering in emails
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen van Bergerem authored and denschub committed May 30, 2015
1 parent ac52cef commit fdad348
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 22 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 1 addition & 19 deletions lib/diaspora/markdownify/email.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/diaspora/taggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,14 @@ def self.format_tags(text, opts={})
%{#{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
6 changes: 3 additions & 3 deletions spec/lib/diaspora/markdownify_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
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
Expand All @@ -33,4 +33,4 @@
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
end
59 changes: 59 additions & 0 deletions spec/lib/diaspora/taggable_spec.rb
Original file line number Diff line number Diff line change
@@ -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\">#&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

0 comments on commit fdad348

Please sign in to comment.