diff --git a/app/helpers/markdownify_helper.rb b/app/helpers/markdownify_helper.rb
index 587a50ad1..e9af64fb1 100644
--- a/app/helpers/markdownify_helper.rb
+++ b/app/helpers/markdownify_helper.rb
@@ -22,11 +22,11 @@ module MarkdownifyHelper
end
def process_links(message)
- message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/) do |m|
+ message.gsub!(/\[\s*([^\[]+)\s*\]\(\s*([^ ]+\s*) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\s*\)/) do |m|
escape = "\\"
- link = $1
- url = $2
- title = $3
+ link = $1.strip
+ url = $2.strip
+ title = $3.strip
url.gsub!("_", "\\_")
url.gsub!("*", "\\*")
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
@@ -34,10 +34,10 @@ module MarkdownifyHelper
res
end
- message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m|
+ message.gsub!(/\[\s*([^\[]+)\s*\]\(\s*([^ ]+)\s*\)/) do |m|
escape = "\\"
- link = $1
- url = $2
+ link = $1.strip
+ url = $2.strip
url.gsub!("_", "\\_")
url.gsub!("*", "\\*")
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
diff --git a/spec/helpers/markdownify_helper_spec.rb b/spec/helpers/markdownify_helper_spec.rb
index a30ee28e0..7bfb4b3b8 100644
--- a/spec/helpers/markdownify_helper_spec.rb
+++ b/spec/helpers/markdownify_helper_spec.rb
@@ -214,7 +214,7 @@ describe MarkdownifyHelper do
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)"
+ 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