-
-
Notifications
You must be signed in to change notification settings - Fork 288
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
BB-662: URLs are turned into malformed links #837
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the right approach is to rewrite everything, and the new implementation seems to handle fewer URL formats.
I think instead we have to, look at the part of this function that adds the https://www.
which seems to be misbehaving.
In fact, we can simplify the whole ordeal and simply add //
in front of the href content if the url does not start with http(s)://.
This would instruct the browser to navigate away from the current website
Perhaps something like this?
function stringToHTMLWithLinks(string) {
// eslint-disable-next-line max-len, no-useless-escape
const urlRegex =
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%~*@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/g;
const startsWithProtocol = Boolean(string.match(/^https?:\/\//gi)?.length);
if (startsWithProtocol) {
return string.replace(urlRegex, '<a href="$1" target="_blank">$1</a>');
} else {
return string.replace(urlRegex, '<a href="//$1" target="_blank">$1</a>');
}
}
For the input www.google.com
this would produce 'www.google.com'
What do you think?
I like your suggestion for handling http(s). |
Interesting, I got that wrong then :) I think however the current regexp is too permissive. |
1b85071
to
11b936b
Compare
So, I finally got time to look at this again. I started from a clean slate. I agree that we should not rewrite the original regex. So I investigated the original issue we were facing (BB-662) and I think I'm onto something. The only problem with the original code was that it was somehow detecting all the What are your thoughts, @MonkeyDo ? |
I think that looks like a better approach, and much simpler! I have run into some cases where your suggested change does not work (such as for example when the substring is not at the beginning of a sentence), and I think the string replacement now strips the But after trying a few things I think the following regex should work well for our needs: /(\b(?<!https?:\/\/)w{3}\.\S+\.)/gmi With that and another change to the substitution string ( Things I modified or added:
I'll note that with this setup we can't match stuff like |
Thanks for the suggestions. I have made the required changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the improvement @the-good-boy !
Problem
This PR addresses ticket BB-662.
Solution
Initially, we were adding an
https://
even if it was already present in the string, now I have added a check.I have also improved the regex we were using, as I feel it was a bit insufficient and antiquated. I took help from this, and I think it works a lot better and is somewhat cleaner also.