1

I am using Autolinker.js to linkify text inputted from a form, but I would like to exclude example.com from being linked.

var formInput = "Don't link example.com but link google.com and www.kbb.com please";
var linkedText = Autolinker.link(formInput);

naturally yields linkedText having both urls linkified. I tried changing the url options using

var options = {urls: { schemeMatches: true, wwwMatches: true, tldMatches: false }};
var linkedText = Autolinker.link(formInput, options);

but this eliminates the link on google.com as well as the example.com while leaving www.kbb.com linked.

Basically I just want to exclude a single specific url, namely example.com, from being linked.

wogsland
  • 9,106
  • 19
  • 57
  • 93
  • Do you only want to exclude example.com or are there a list of domains you want to exclude? – Matt Feb 01 '16 at 16:36
  • @mkaatman Today it is just `example.com`, but there is a good chance it could become a list. – wogsland Feb 01 '16 at 16:37
  • The simplest solution i can think of is to create an array of urls you don't want to be linked. Before you convert with autolinker, look for any of those urls in the text and replace them with the key from the array surrounded by some token you can parse out. Then use autolinker, and finally replace your tokens with the original text again. – Matt Feb 01 '16 at 16:40
  • @mkaatman Yeah, I was hoping that being an Autolinker.js n00b I'd missed something in the setup or the docs before I headed down that path. Can't imagine no one else has run into this. – wogsland Feb 01 '16 at 16:45

3 Answers3

3

Sorry that I just noticed this thread. I'm the library author. I know it's pretty late but leaving this reply for anyone else who comes across it.

The easiest way to exclude a particular url from being autolinked is to leverage the replaceFn option. For example:

var formInput = "Don't link example.com but link google.com and www.kbb.com please";

var linkedText = Autolinker.link( formInput, {
    replaceFn: function( match ) {
        if( match.getType() === 'url' ) {
            var url = match.getUrl();

            if( url.indexOf( 'example.com' ) !== -1 ) {
                return false;  // don't autolink matches for example.com
            }
        }

        return true;  // autolink everything else as usual
    }
} );

This produces the result of:

Don't link example.com but link <a href="http://google.com" target="_blank" rel="noopener noreferrer">google.com</a> and <a href="http://www.kbb.com" target="_blank" rel="noopener noreferrer">kbb.com</a> please

Here is some documentation on the methods that can be called on UrlMatch objects: http://greg-jacobs.com/Autolinker.js/api/#!/api/Autolinker.match.Url

And a more in-depth example of using replaceFn: https://github.com/gregjacobs/Autolinker.js#custom-replacement-function

1

Replace text with a token, run autolink, replace token with original text. The obvious weakness here is that if formInput contained ||anything|| it would break.

var formInput = "Don't link example.com but link google.com and www.kbb.com please";

var stuffIdontwanttolink = ['example.com', 'google.com'];

stuffIdontwanttolink.forEach(function(entry, index) {
  formInput = formInput.replace(entry, '||' + index + '||');
});

console.log(formInput);

//var linkedText = Autolinker.link(formInput);
var linkedText = "Don't link ||0|| but link ||1|| and <a href='//www.kbb.com'>www.kbb.com</a> please"; // Simulated

stuffIdontwanttolink.forEach(function(entry, index) {
  linkedText = linkedText.replace('||' + index + '||', entry);
});

console.log(linkedText);
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
Matt
  • 5,315
  • 1
  • 30
  • 57
  • Nice! This is pretty similar to what I did minus making it function, and maybe a little better since you don't need to define a temp string to replace each with. – wogsland Feb 01 '16 at 17:19
1

So I wrote a function to wrap the Autolinker in:

function excludedLinkify (inputText) {
  var exclusions = [
    {url:'example.com', temp:'7g578v43gc7n3744c'}
  ];
  $.each(exclusions, function (i, e) {
    inputText = inputText.replace(e.url, e.temp);
  });
  inputText = Autolinker.link(inputText);
  $.each(exclusions, function (i, e) {
    inputText = inputText.replace(e.temp, e.url);
  });
  return inputText;
}

So that to achieve the desired result I can now

var formInput = "Don't link example.com but link google.com and www.kbb.com please";
var linkedText = excludedLinkify(formInput);
wogsland
  • 9,106
  • 19
  • 57
  • 93