gregjacobs / Autolinker.js

Utility to Automatically Link URLs, Email Addresses, Phone Numbers, Twitter handles, and Hashtags in a given block of text/HTML
MIT License
1.48k stars 238 forks source link

Option to apply "rel=nofollow" at will #28

Closed choxnox closed 10 years ago

choxnox commented 10 years ago

It would be great for user-managed content to have an option to enable/disable rel attribute, and more specifically it's nofollow attribute.

gregjacobs commented 10 years ago

Hey choxnox, how are you looking to specify this? Would a rel option that applies the "rel" attribute to all generated anchor tags work? Or are you looking for more control that you can specify it on a per-anchor basis?

jamcole commented 10 years ago

I'd like this too.

For now, I pipe the output through jQuery to do it reliably, but obviously it's a little heavy because it has to create a dom and such.

var retHtml = Autolinker.link(html, {newWindow: false, stripPrefix: false, truncate: 100, twitter: false, className: 'autolinked'});
var retSel = $($.parseHTML('<div class="autolink-outer-wrapper"><div class="autolink-inner-wrapper">'+retHtml+'</div><div>'));
retSel.find('a.autolinked').attr('rel', 'nofollow');
var newHtml = retSel.find('.autolink-inner-wrapper').first().html();
pruhstal commented 10 years ago

Have a fix here: 02f2e81a1f6718b47b46c17718599404f2b7020c

gregjacobs commented 10 years ago

Finally possible using replaceFn! Example:

var html = Autolinker.link( text, {
    replaceFn : function( autolinker, match ) {
        var tag = autolinker.getTagBuilder().build( match );  // Returns an Autolinker.HtmlTag instance
        tag.setAttr( 'rel', 'nofollow' );

        return tag;
    }
} );