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 239 forks source link

Should not autolink filenames #270

Open kxmbrian opened 5 years ago

kxmbrian commented 5 years ago
image

Not sure about the full breadth of use cases, but would it be feasible not to match filenames while still matching TLDs?

One solution is to enforce that all URL matches are either at the start of a line or are preceded by whitespace, but from #159, it seems that that might not fit everyone's needs.

kxmbrian commented 5 years ago

Would be happy to do a PR for this if you could point me in the right direction

adamdavidcole commented 5 years ago

Running into the same issue with java file names which end with standard TLD like: /home/data/httpd/download.eclipse.org

where the download.eclipse.org is being auto-linked.

I think maybe if a URL is preceded by a / it is likely a file path and should not be auto-linked.

Unsure what cases I may be missing though 🤷‍♂

gregjacobs commented 5 years ago

Hey guys. Yeah, it's tough to say whether we should always prevent linking when there is a slash in front of the url. I'll have to think about this a bit more, but in the meantime you can try using the replaceFn as a workaround. Try something like this:

Autolinker.link(inputText, {
    replaceFn: function(match) {
        if (match.getType() === 'url') {
            var previousChar = inputText.charAt(match.getOffset() - 1);
            if (previousChar === '/') {
                return false;  // don't autolink this match
            }
       }
    }
});
adamdavidcole commented 5 years ago

Solid suggestion, thank you!

adamdavidcole commented 5 years ago

Just a heads up if you consider adding this rule: on my end I skip the match if the url is preceded by / and is not prefixed by www. or common protocol like http://.

So I'll match on:

But not: