Skip to content

Commit

Permalink
Merge pull request #3489 from Raven24/autolink-fix
Browse files Browse the repository at this point in the history
fix autolinking of links
  • Loading branch information
Raven24 committed Aug 9, 2012
2 parents fb9da74 + 1a94a24 commit 1a8e0da
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/assets/javascripts/app/helpers/text_formatter.js
Expand Up @@ -26,9 +26,11 @@

// process links
// regex copied from: https://code.google.com/p/pagedown/source/browse/Markdown.Converter.js#1198 (and slightly expanded)
var linkRegex = /(\[.*\]:\s)?(<|\()((https?|ftp):\/\/[^\/'">\s][^'">\s]+?)(>|\))/gi;
var linkRegex = /(\[.*\]:\s)?(<|\()((?:(https?|ftp):\/\/[^\/'">\s]|www)[^'">\s]+?)(>|\))/gi;
text = text.replace(linkRegex, function() {
var unicodeUrl = arguments[3];
unicodeUrl = ( unicodeUrl.match(/^www/) ) ? ('http://' + unicodeUrl) : unicodeUrl;

var addr = parse_url(unicodeUrl);
if( !addr.host ) addr.host = ""; // must not be 'undefined'

Expand Down
11 changes: 8 additions & 3 deletions spec/javascripts/app/helpers/text_formatter_spec.js
Expand Up @@ -26,11 +26,16 @@ describe("app.helpers.textFormatter", function(){
// This test will fail if our join is just (" ") -- an edge case that should be addressed.

it("autolinks", function(){
var links = ["http://google.com",
var links = [
"http://google.com",
"https://joindiaspora.com",
"http://www.yahooligans.com",
"http://obama.com",
"http://japan.co.jp"]
"http://japan.co.jp",
"www.mygreat-example-website.de",
"www.jenseitsderfenster.de", // from issue #3468
"www.google.com"
];

// The join that would make this particular test fail:
//
Expand All @@ -40,7 +45,7 @@ describe("app.helpers.textFormatter", function(){
var wrapper = $("<div>").html(formattedText);

_.each(links, function(link) {
var linkElement = wrapper.find("a[href='" + link + "']");
var linkElement = wrapper.find("a[href*='" + link + "']");
expect(linkElement.text()).toContain(link);
expect(linkElement.attr("target")).toContain("_blank");
})
Expand Down

0 comments on commit 1a8e0da

Please sign in to comment.