Browse Source

Fixes to linkify function

More and better pattern matching.
pull/33/merge
Zach Wise 13 years ago
parent
commit
3e18b93e93
  1. 76
      source/js/VMM.Util.js

76
source/js/VMM.Util.js

@ -283,25 +283,45 @@ if(typeof VMM != 'undefined' && typeof VMM.Util == 'undefined') {
// VMM.Util.linkify();
linkify: function(text,targets,is_touch) {
if(!text) return text;
// http://, https://, ftp://
var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
// www. sans http:// or https://
var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
// Email addresses
var emailAddressPattern = /(([a-zA-Z0-9_\-\.]+)@[a-zA-Z_]+?(?:\.[a-zA-Z]{2,6}))+/gim;
text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi, function(url) {
var nice = url;
var _touch = "";
if(url.search('^https?:\/\/') < 0) url = 'http://'+url;
_touch = "onclick = 'void(0)'";
if(is_touch) {
_touch = "onclick = 'void(0)'";
}
onclick = "void(0)";
if(targets === null || targets === "") return '<a href="'+ url + " " + _touch + '">'+ url +'</a>';
else return "<a href='"+ url + " " + _touch + " target='" +targets+"'>'" + url + "</a>" ;
});
return text
.replace(urlPattern, "<a target='_blank' href='$&' onclick='void(0)'>$&</a>")
.replace(pseudoUrlPattern, "$1<a target='_blank' onclick='void(0)' href='http://$2'>$2</a>")
.replace(emailAddressPattern, "<a target='_blank' onclick='void(0)' href='mailto:$1'>$1</a>");
},
linkify_with_twitter: function(text,targets,is_touch) {
return text;
// http://, https://, ftp://
var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
// www. sans http:// or https://
var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
// Email addresses
var emailAddressPattern = /(([a-zA-Z0-9_\-\.]+)@[a-zA-Z_]+?(?:\.[a-zA-Z]{2,6}))+/gim;
var twitterHandlePattern = /(@([\w]+))/g;
var twitterSearchPattern = /(#([\w]+))/g;
return text
.replace(urlPattern, "<a target='_blank' href='$&' onclick='void(0)'>$&</a>")
.replace(pseudoUrlPattern, "$1<a target='_blank' onclick='void(0)' href='http://$2'>$2</a>")
.replace(emailAddressPattern, "<a target='_blank' onclick='void(0)' href='mailto:$1'>$1</a>")
.replace(twitterHandlePattern, "<a href='http://twitter.com/$2' target='_blank' onclick='void(0)'>$1</a>")
.replace(twitterSearchPattern, "<a href='http://twitter.com/#search?q=%23$2' target='_blank' 'void(0)'>$1</a>");
},
/* Turns plain text links into real links
================================================== */
// VMM.Util.unlinkify();
@ -494,4 +514,30 @@ if(typeof VMM != 'undefined' && typeof VMM.Util == 'undefined') {
}).init();
//'string'.linkify();
if(!String.linkify) {
String.prototype.linkify = function() {
// http://, https://, ftp://
var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
// www. sans http:// or https://
var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
// Email addresses
var emailAddressPattern = /(([a-zA-Z0-9_\-\.]+)@[a-zA-Z_]+?(?:\.[a-zA-Z]{2,6}))+/gim;
var twitterHandlePattern = /(@([\w]+))/g;
var twitterSearchPattern = /(#([\w]+))/g;
return this
.replace(urlPattern, '<a target="_blank" href="$&">$&</a>')
.replace(pseudoUrlPattern, '$1<a target="_blank" href="http://$2">$2</a>')
.replace(emailAddressPattern, '<a target="_blank" href="mailto:$1">$1</a>')
.replace(twitterHandlePattern, "<a href='http://twitter.com/$2' target='_blank'>$1</a>")
.replace(twitterSearchPattern, "<a href='http://twitter.com/#search?q=%23$2' target='_blank'>$1</a>");
};
}
}
Loading…
Cancel
Save