bryanwoods/autolink-js — pull request #2 by mac2000 (2012-07-05 07:29:58+00:00)
Title: Callback option

Callback option can be used to redefine how links will be rendered

For example if we want to display images:

"This is a image http://example.com/logo.png".autoLink({callback: function(url){
return /\.(gif|png|jpe?g)$/i.test(url) ? '' : null;
}})

Or youtube video:

"This is a youtube http://www.youtube.com/watch?v=qyM37XKkmKQ".autoLink({callback: function(url){
var matches = url.match(/youtube.com.*?v=(.*?)(&|$)/i);
return matches && matches[1] ? '' : null;
}})

Or implement [limit](https://github.com/addbrick/autolink-js/commit/d08bc4d22aae626cd53d086c0d4164857c8a8dbd) option:

"This is a link http://google.com".autoLink({callback: function(url){
var limit = 30;
var displayUrl = url.replace(/https?/i, '');
if (displayUrl.length > limit) {
displayUrl = "" + (displayUrl.substr(0, limit));
}
return "" + displayUrl + "";
}})

If callback returns nothing autoLink will create link as usual.

With this addition autoLink will be still as little as possible but will be able to render any kind of links by chaining responsibility to external callback code.