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

textlink failing on specific case #62

Open
pintassilgo opened this issue Jan 22, 2021 · 1 comment
Open

textlink failing on specific case #62

pintassilgo opened this issue Jan 22, 2021 · 1 comment

Comments

@pintassilgo
Copy link

Example:
[link](google.com/images)

Is trying to load "https://www.(google.com/images" instead of "https://www.google.com/images".

@pintassilgo
Copy link
Author

pintassilgo commented Jan 22, 2021

Fixed by replacing

const urlRegex1 = /([-_.~*'()|a-zA-Z0-9;:\/?,@&=+$%#\uff0d\uff3f\u301c\uffe3\uff0e\uff01\uff5e\uff0a\u2019\uff08\uff09\uff5c\uff41-\uff5a\uff21-\uff3a\uff10-\uff19\uff1b\uff1a\uff0f\uff1f\uff20\uff06\uff1d\uff0b\uff04\uff0c\uff05\uff03\uff5c\uff3b\uff3d]*[.\uff0e]+[-_.~*'\[\]|a-zA-Z0-9;:\/?,@&=+$%#\uff0d\uff3f\u301c\uffe3\uff0e\uff01\uff5e\uff0a\u2019\uff08\uff09\uff5c\uff41-\uff5a\uff21-\uff3a\uff10-\uff19\uff1b\uff1a\uff0f\uff1f\uff1a\uff20\uff06\uff1d\uff0b\uff04\uff0c\uff05\uff03\uff5c]+[.\uff0e/\uff0f]*[-_,.~*\[\]|a-zA-Z0-9;!:\/?@&=+$%#\uff0d\uff3f\u301c\uffe3\uff0e\uff01\uff5e\uff0a\u2019\uff5c\uff41-\uff5a\uff21-\uff3a\uff10-\uff19\uff1b\uff1a\uff0f\uff1f\uff1a\uff20\uff06\uff1d\uff0b\uff04\uff0c\uff05\uff03\uff5c]+)/ig;

with

const urlRegex1 = /(?:(?:(?:https?|ftp):)?\/\/)?(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#](?:(?![\)\'\"\]]?(?:$|\s)).)*)?/ig;

Not sure yet if this will have a side effect.

Got the regex from here:
https://gist.github.com/dperini/729294#file-regex-weburl-js-L62
then

  • removed ^ from the start and $ from the end because we're searching for URL in any part of the text, not testing if the entire string is an URL.
  • added ? after the protocol (?:(?:(?:https?|ftp):)?\/\/) because sometimes the URL we want to linkify doesn't have protocol (as in the example).
  • at the end, the original regex captures anything except whitespace (?:[/?#]\S*)?, so I replaced it with (?:[/?#](?:(?![\)\'\"\]]?(?:$|\s)).)*)? to not catch ). ', " and ] as the last char of the URL (that is, when followed by linebreak or whitespace). I know it's possible to an URL to end with ), but it's MUCH more likely that, when we see a non-linked URL ended in ), the ) is NOT part of the link.

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

1 participant