diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 02fad11e9..a708c70b4 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -190,12 +190,23 @@ module ApplicationHelper
def process_links(message)
message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/) do |m|
escape = "\\"
- res = "#{$1}"
+ link = $1
+ url = $2
+ title = $3
+ url.gsub!("_", "\\_")
+ url.gsub!("*", "\\*")
+ protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
+ res = "#{link}"
res
end
message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m|
escape = "\\"
- res = "#{$1}"
+ link = $1
+ url = $2
+ url.gsub!("_", "\\_")
+ url.gsub!("*", "\\*")
+ protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
+ res = "#{link}"
res
end
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index 5b54bc9d1..440ccb025 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -166,12 +166,17 @@ describe ApplicationHelper do
message = '[link text](http://someurl.com "some title") [link text](http://someurl.com "some title")'
markdownify(message).should == 'link text link text'
end
+
+ it "should have a robust link parsing" do
+ 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'
+ end
end
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