From 99a2865e2364afcee2112a19bed22e22a99619a3 Mon Sep 17 00:00:00 2001 From: Steven Hancock Date: Tue, 29 May 2012 09:58:40 -0700 Subject: [PATCH] Make hashtags clickable in emails Refactor the Redcarpet renderer created in c2bc7272f to parse hashtags into clickable links (for HTML emails only). --- lib/diaspora/markdownify_email.rb | 6 +++++- spec/lib/diaspora/markdownify_email_spec.rb | 14 +++++++------- spec/mailers/notifier_spec.rb | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/diaspora/markdownify_email.rb b/lib/diaspora/markdownify_email.rb index c2eba16ce..bdc7983ba 100644 --- a/lib/diaspora/markdownify_email.rb +++ b/lib/diaspora/markdownify_email.rb @@ -3,6 +3,7 @@ require Rails.root.join("app", "models", "acts_as_taggable_on", "tag") 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) @@ -16,7 +17,10 @@ module Diaspora def process_tags(text) return text unless text.match(TAG_REGEX) tags(text).each do |tag| - text.gsub!(/##{tag}/i, "\\##{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 end diff --git a/spec/lib/diaspora/markdownify_email_spec.rb b/spec/lib/diaspora/markdownify_email_spec.rb index bcc744b72..f27d0afb1 100644 --- a/spec/lib/diaspora/markdownify_email_spec.rb +++ b/spec/lib/diaspora/markdownify_email_spec.rb @@ -6,17 +6,17 @@ describe Diaspora::Markdownify::Email do @html = Diaspora::Markdownify::Email.new end - it 'should escape a hashtag' do + it 'should autolink a hashtag' do markdownified = @html.preprocess("#tag") - markdownified.should == "\\#tag" + markdownified.should == "[#tag](http://localhost:9887/tags/tag)" end - it 'should escape multiple hashtags' do - markdownified = @html.preprocess("There are #two #tags") - markdownified.should == "There are \\#two \\#tags" + it 'should autolink multiple hashtags' do + markdownified = @html.preprocess("There are #two #Tags") + markdownified.should == "There are [#two](http://localhost:9887/tags/two) [#Tags](http://localhost:9887/tags/tags)" end - it 'should not escape headers' do + it 'should not autolink headers' do markdownified = @html.preprocess("# header") markdownified.should == "# header" end @@ -30,7 +30,7 @@ describe Diaspora::Markdownify::Email do it 'should render the message' do rendered = @markdown.render(@sample_text).strip - rendered.should == "

Header

\n\n

#messages containing #hashtags should render properly

" + rendered.should == "

Header

\n\n

#messages containing #hashtags should render properly

" end end end \ No newline at end of file diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index d9aeace5f..0e6498e36 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -331,7 +331,7 @@ describe Notifier do mails = Notifier.admin("#Welcome to bureaucracy!", [bob]) mails.length.should == 1 mail = mails.first - mail.body.encoded.should match /#Welcome to bureaucracy!/ + mail.body.encoded.should match "

#Welcome to bureaucracy!

" end end end