Autolinker.js
Autolinker.js copied to clipboard
Protocol allow/deny list
Sorry this is not an issue but more of a feature request (or maybe I just didn't find it in the readme)
Is there a way to allow or deny specific protocols? In my case I really only want to link http
and https
protocol urls to get linked. (no ftp
or any special application specific protocols)
Does this option already exist and I just missed it?
Hey @andyford,
So there's no way to set a protocol allow or deny list at this time, but as a workaround for the moment, you could try this in the replaceFn
. Something like this:
var linkedText = Autolinker.link(inputText, {
replaceFn : function(match) {
if (match.getType() === 'url') {
if (!/https?:\/\//.test(match.getUrl()) {
// url does not start with 'http://' or 'https://', do not autolink
return false;
}
}
return true; // autolink everything else as usual
}
});
Best, Greg
Thanks @gregjacobs! That's exactly what I needed! I feel a little silly for missing replaceFn
in the docs.
Quick note: if any one else needs this bit of code, there's a missing closing paren in the inner if
statement
it should be if (!/https?:\/\//.test(match.getUrl())) {
(@gregjacobs - feel free to delete this note [if you can] and update your comment if you like)
I keep waffling between more verbose but easier to read code and Perl-esque one-liners. Here's a one-liner version of the workaround:
replaceFn: (match) => !(match.getType() === 'url' && !/https?:\/\//.test(match.getUrl())),