Skip to content

Instantly share code, notes, and snippets.

@md-riaz
Created April 23, 2024 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save md-riaz/930ac18642ec76e764a601ef8fca1471 to your computer and use it in GitHub Desktop.
Save md-riaz/930ac18642ec76e764a601ef8fca1471 to your computer and use it in GitHub Desktop.
// Define: Linkify plugin
(function($){
var url1 = /(^|<|\s)(www\..+?\..+?)(\s|>|$)/g,
url2 = /(^|<|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|>|$)/g,
linkifyThis = function () {
var childNodes = this.childNodes,
i = childNodes.length;
while(i--)
{
var n = childNodes[i];
if (n.nodeType == 3) {
var html = $.trim(n.nodeValue);
if (html)
{
html = html.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(url1, '$1<a href="http://$2" target="_blank">$2</a>$3')
.replace(url2, '$1<a href="$2" target="_blank">$2</a>$5');
$(n).after(html).remove();
}
}
else if (n.nodeType == 1 && !/^(a|button|textarea)$/i.test(n.tagName)) {
linkifyThis.call(n);
}
}
};
$.fn.linkify = function () {
return this.each(linkifyThis);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment