From 62aea0e820fcafa55bfb4190bca48c3efdd7236a Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Wed, 31 Aug 2011 18:06:30 -0700 Subject: [PATCH] fix for mention links, and the first tag links #ohYEAH --- app/helpers/markdownify_helper.rb | 2 +- lib/diaspora/markdownify.rb | 5 +- spec/helpers/markdownify_helper_spec.rb | 68 ++++++++++++++----------- 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/app/helpers/markdownify_helper.rb b/app/helpers/markdownify_helper.rb index a49ce2381..89f09e521 100644 --- a/app/helpers/markdownify_helper.rb +++ b/app/helpers/markdownify_helper.rb @@ -39,7 +39,7 @@ module MarkdownifyHelper if target.respond_to?(:format_mentions) message = target.format_mentions(message) end - + message = Diaspora::Taggable.format_tags(message, :no_escape => true) return message.html_safe diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb index 8515cf881..e0b9f10f0 100644 --- a/lib/diaspora/markdownify.rb +++ b/lib/diaspora/markdownify.rb @@ -15,6 +15,7 @@ module Diaspora end def autolink(link, type) + return link if type == :email autolink_youtube(link) || autolink_vimeo(link) || autolink_simple(link) end @@ -108,8 +109,7 @@ module Diaspora x =~ /\n{2}/ ? x : (x = x.strip; x << br) end end - - return "

#{text}

" + return text end def preprocess(full_document) @@ -149,6 +149,7 @@ module Diaspora end + def single_emphasis(text) "#{text}" end diff --git a/spec/helpers/markdownify_helper_spec.rb b/spec/helpers/markdownify_helper_spec.rb index a86cb13d1..eb9427cdc 100644 --- a/spec/helpers/markdownify_helper_spec.rb +++ b/spec/helpers/markdownify_helper_spec.rb @@ -9,40 +9,40 @@ describe MarkdownifyHelper do describe "#markdownify" do describe "autolinks" do it "should not allow basic XSS/HTML" do - markdownify("").should == "

<script>alert('XSS is evil')</script>

" + markdownify("").should == "<script>alert('XSS is evil')</script>" end it "should recognize basic http links (1/3)" do proto="http" url="bugs.joindiaspora.com/issues/332" full_url = "#{proto}://#{url}" - markdownify(full_url).should == %Q{

#{url}

} + markdownify(full_url).should == %Q{#{url}} end it "should recognize basic http links (2/3)" do proto="http" url="webmail.example.com?~()!*/" full_url = "#{proto}://#{url}" - markdownify(full_url).should == %Q{

#{url}

} + markdownify(full_url).should == %Q{#{url}} end it "should recognize basic http links (3/3)" do proto="http" url="127.0.0.1:3000/users/sign_in" full_url = "#{proto}://#{url}" - markdownify(full_url).should == %Q{

#{url}

} + markdownify(full_url).should == %Q{#{url}} end it "should recognize secure https links" do proto="https" url="127.0.0.1:3000/users/sign_in" full_url = "#{proto}://#{url}" - markdownify(full_url).should == %Q{

#{url}

} + markdownify(full_url).should == %Q{#{url}} end it "doesn't muck up code text" do message = %{`puts "Hello"`} - markdownify(message).should =~ %r{

puts "Hello"

} + markdownify(message).should =~ %r{puts "Hello"} message = %Q{~~~\nA\nB\n~~~\n} markdownify(message).should =~ %r{
\nA\nB\n
} end @@ -159,76 +159,76 @@ describe MarkdownifyHelper do proto="ftp" url="ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4" # I did not watch that one, but the title sounds nice :P - markdownify(proto+"://"+url).should == "

"+url+"

" + markdownify(proto+"://"+url).should == ""+url+"" end it "should recognize www links" do url="www.joindiaspora.com" - markdownify(url).should == %Q{

#{url}

} + markdownify(url).should == %Q{#{url}} end end describe "specialchars" do it "replaces <3 with ♥" do message = "i <3 you" - markdownify(message).should == "

i ♥ you

" + markdownify(message).should == "i ♥ you" end it "replaces various things with (their) HTML entities" do message = "... <-> -> <- (tm) (r) (c)" - markdownify(message).should == "

… ↔ → ← ™ ® ©

" + markdownify(message).should == "… ↔ → ← ™ ® ©" end it "skips doing it if you say so" do message = "... -> <-" - markdownify(message, :specialchars => false).should == "

... -> <-

" + markdownify(message, :specialchars => false).should == "... -> <-" end end describe "weak emphasis" do it "should be recognized (1/2)" do message = "*some text* some text *some text* some text" - markdownify(message).should == "

some text some text some text some text

" + markdownify(message).should == "some text some text some text some text" end it "should be recognized (2/2)" do message = "_some text_ some text _some text_ some text" - markdownify(message).should == "

some text some text some text some text

" + markdownify(message).should == "some text some text some text some text" end end describe "strong emphasis" do it "should be recognized (1/2)" do message = "**some text** some text **some text** some text" - markdownify(message).should == "

some text some text some text some text

" + markdownify(message).should == "some text some text some text some text" end it "should be recognized (2/2)" do message = "__some text__ some text __some text__ some text" - markdownify(message).should == "

some text some text some text some text

" + markdownify(message).should == "some text some text some text some text" end end describe "nested weak and strong emphasis" do it "should be rendered correctly" do message = "__this is _some_ text__" - markdownify(message).should == "

this is some text

" + markdownify(message).should == "this is some text" message = "*this is **some** text*" - markdownify(message).should == "

this is some text

" + markdownify(message).should == "this is some text" message = "___some text___" - markdownify(message).should == "

some text

" + markdownify(message).should == "some text" end end describe "links" do it "should be recognized without title attribute" do message = "[link text](http://someurl.com) [link text](http://someurl.com)" - markdownify(message).should == '

link text link text

' + markdownify(message).should == 'link text link text' end it "should be recognized with title attribute" do message = '[link text](http://someurl.com "some title") [link text](http://someurl.com "some title")' - markdownify(message).should == '

link text link text

' + markdownify(message).should == 'link text link text' end it "should have a robust link parsing" do @@ -237,18 +237,18 @@ describe MarkdownifyHelper do link.should =~ %r{href="http://en.wikipedia.org/wiki/Text_%28literary_theory%29"} message = "[ links]( google.com)" - markdownify(message).should == %Q{

links

} + markdownify(message).should == %Q{links} message = "[_http_](http://google.com/search?q=with_multiple__underscores*and**asterisks )" - markdownify(message).should == %Q{

http

} + markdownify(message).should == %Q{http} message = %{[___FTP___]( ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4 'File Transfer Protocol')} - markdownify(message).should == %{

FTP

} + markdownify(message).should == %{FTP} message = %{[**any protocol**](foo://bar.example.org/yes_it*makes*no_sense)} - markdownify(message).should == %{

any protocol

} + markdownify(message).should == %{any protocol} message = "This [ *text* ]( http://en.wikipedia.org/wiki/Text_(literary_theory) ) with many [ links]( google.com) tests [_http_](http://google.com/search?q=with_multiple__underscores*and**asterisks ), [___FTP___]( ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4 'File Transfer Protocol'), [**any protocol**](foo://bar.example.org/yes_it*makes*no_sense)" - markdownify(message).should == '

This text with many links tests http, FTP, any protocol

' + markdownify(message).should == 'This text with many links tests http, FTP, any protocol' end end @@ -256,32 +256,32 @@ describe MarkdownifyHelper do describe "nested emphasis and links tags" do it "should be rendered correctly" do message = '[**some *link* text**](someurl.com "some title")' - markdownify(message).should == '

some link text

' + markdownify(message).should == 'some link text' end end it "should allow escaping" do message = '*some text* \*some text* \**some text* _some text_ \_some text_ \__some text_' - markdownify(message).should == "

some text *some text* *some text some text _some text_ _some text

" + markdownify(message).should == "some text *some text* *some text some text _some text_ _some text" end describe "newlines" do it 'skips inserting newlines if you pass the newlines option' do message = "These\nare\n\some\nnew\lines" res = markdownify(message, :newlines => false) - res.should == "

#{message}

" + res.should == "#{message}" end it 'generates breaklines' do message = "These\nare\nsome\nnew\nlines" res = markdownify(message) - res.should == "

These
are
some
new
lines

" + res.should == "These
are
some
new
lines" end it 'should render newlines and basic http links correctly' do message = "Some text, then a line break and a link\nhttp://joindiaspora.com\nsome more text" res = markdownify(message) - res.should == '

Some text, then a line break and a link
joindiaspora.com
some more text

' + res.should == 'Some text, then a line break and a link
joindiaspora.com
some more text' end end @@ -307,6 +307,14 @@ describe MarkdownifyHelper do formatted.should =~ /hovercard/ end + it "should leave mentions intact for real diaspora handles" do + new_person = Factory(:person, :diaspora_handle => 'maxwell@joindiaspora.com') + message = Factory.create(:status_message, + :author => alice.person, + :text => "Hey @{maxwell@joindiaspora.com; #{new_person.diaspora_handle}}!") + formatted = markdownify(message) + formatted.should =~ /hovercard/ + end end context 'performance' do