Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol allow/deny list #328

Open
andyford opened this issue Aug 21, 2020 · 2 comments
Open

Protocol allow/deny list #328

andyford opened this issue Aug 21, 2020 · 2 comments

Comments

@andyford
Copy link

andyford commented Aug 21, 2020

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?

@gregjacobs
Copy link
Owner

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

@andyford
Copy link
Author

andyford commented Aug 22, 2020

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())),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants