Last active
August 29, 2015 14:15
-
-
Save damulhan/32591ae6dbe269c4ed0d to your computer and use it in GitHub Desktop.
jquery.autolink.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* USAGE: | |
This script create automatic links in html source excluding <A href=""> anchor and other tags. | |
<div class="content"> | |
example text : http://www.jeungsando.org <br> | |
example html : <a href="www.jeungsando.org">jeungsando</a> | |
</div> | |
<script> | |
$(document).ready(function() { | |
$(".content").autolink(); | |
}); | |
</script> | |
*/ | |
/** | |
* @file jqeury.autolink.js | |
* @author: patched for general jquery plugin: damulhan ([email protected]) | |
* original creation: NAVER ([email protected]) | |
*/ | |
jQuery.fn.autolink = function () { | |
var protocol_re = '(https?|ftp|news|telnet|irc)://'; | |
var domain_re = '(?:[\\w\\-]+\\.)+(?:[a-z]+)'; | |
var max_255_re = '(?:1[0-9]{2}|2[0-4][0-9]|25[0-5])'; | |
var ip_re = '(?:'+max_255_re+'\\.){3}'+max_255_re; | |
var port_re = '(?::([0-9]+))?'; | |
var path_re = '((?:/[\\w!"$-/:-@]+)*)'; | |
var hash_re = '(?:#([\\w!-@]+))?'; | |
var url_regex = new RegExp('('+protocol_re+'('+domain_re+'|'+ip_re+')'+port_re+path_re+hash_re+')', 'ig'); | |
var AutoLink = { | |
targets : [], | |
init : function() { | |
this.targets = []; | |
}, | |
start : function(_this) { | |
var thisPlugin = this; | |
// extract target text nodes | |
this.extractTargets(_this); | |
$(this.targets).each(function(){ | |
thisPlugin.do_autolink([this]); | |
}); | |
}, | |
do_autolink : function(params) { | |
var textNode = params[0]; | |
var content = textNode.nodeValue; | |
var dummy = $('<span>'); | |
content = content.replace(/</g, '<').replace(/>/g, '>'); | |
content = content.replace(url_regex, '<a href="$1" target="_blank">$1</a>'); | |
$(textNode).before(dummy); | |
$(textNode).replaceWith(content); | |
params[0] = dummy.next('a'); | |
dummy.remove(); | |
}, | |
extractTargets : function(obj) { | |
var thisPlugin = this; | |
$(obj) | |
.contents() | |
.each(function(){ | |
// FIX ME : When this meanless code wasn't executed, url_regex do not run correctly. why? | |
url_regex.exec(''); | |
if (!$(this).is('a,pre,xml,code,script,style,:input')) { | |
if (this.nodeType == 3 && url_regex.test(this.nodeValue)) { // text node | |
thisPlugin.targets.push(this); | |
} else { | |
thisPlugin.extractTargets(this); | |
} | |
} | |
}); | |
} | |
}; | |
return this.each( function() { | |
//var re = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g; | |
//$(this).html( $(this).html().replace(re, '<a href="$1" target="'+ target +'">$1</a> ') ); | |
//console.log($(this)); | |
AutoLink.init(); | |
AutoLink.start($(this)); | |
}); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment