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

fix: add support for localhost urls in markdown #535

Merged
merged 3 commits into from
May 19, 2023

Conversation

allroundexperts
Copy link
Contributor

This PR adds support for auto linking of local urls like http://my-localhost, http://localhost:3000

Fixed Issues

$ Expensify/App#17828

Tests

  1. What unit/integration tests cover your change? What autoQA tests cover your change?
    URL-test, ExpensiMark-HTML-test and ExpensiMark-HTMLToText-test

  2. What tests did you perform that validates your changed worked?
    Added new test cases that auto link local urls.

QA

  1. What does QA need to do to validate your changes?
    Run the new test cases

  2. What areas to they need to test for regressions?
    Check if urls are getting auto linked correctly

@mountiny
Copy link
Contributor

@Santhosh-Sellavel any chance you can test this in App?

mountiny
mountiny previously approved these changes May 14, 2023
Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @allroundexperts looking good, is there an easy way to test this with the App before merging?

lib/Url.js Outdated
Comment on lines 15 to 16
const LOOSE_URL_REGEX = `(${LOOSE_URL_WEBSITE_REGEX}${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|${URL_FRAGMENT_REGEX})*)`;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we include the LOOSE_URL_REGEX along with the exported URL_REGEX as OR from here?

@allroundexperts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Santhosh-Sellavel Of course.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. But we'll still need to import the both as well because of this condition.

@allroundexperts
Copy link
Contributor Author

Thanks @allroundexperts looking good, is there an easy way to test this with the App before merging?

@mountiny I've been testing it by applying the changes directly in node_modules 😄

Copy link
Contributor

@neil-marcellini neil-marcellini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests look good but I'm a bit confused about the regex. I think you should also include test steps in App.

@@ -11,6 +11,12 @@ const URL_REGEX = `(${URL_WEBSITE_REGEX}${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|$

const URL_REGEX_WITH_REQUIRED_PROTOCOL = URL_REGEX.replace(`${URL_PROTOCOL_REGEX}?`, URL_PROTOCOL_REGEX);

const LOOSE_URL_WEBSITE_REGEX = `${URL_PROTOCOL_REGEX}([-\\w]+(\\.[-\\w]+)*)(?:\\:${ALLOWED_PORTS}|\\b|(?=_))`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this chunk of the regex please? How did you come up with it and what is it supposed to do?

([-\\w]+(\\.[-\\w]+)*)

My understanding is that it should match the domain name, however it isn't explained that way on regex101. Maybe I'm missing something...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neil-marcellini You need to replace the \\ with \ when testing it on regex101. The mentioned regex matches any word character along with a hyphen followed by an option . and any word character (hyphens included) zero or more times. This is added to cover domain names on local machines. These could be: localhost, a.b.c etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah got it thanks!

You need to replace the \\ with \ when testing it on regex101

Can you explain why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure but I think it might be due to the fact that we're having the regex in string form in our code and then we're converting it using new Regex.

mountiny
mountiny previously approved these changes May 15, 2023
Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allroundexperts could you show tests with the App please?

@allroundexperts
Copy link
Contributor Author

@allroundexperts could you show tests with the App please?

@mountiny Sure.

Screenshot 2023-05-16 at 4 43 44 PM

@@ -454,7 +454,7 @@ export default class ExpensiMark {
if (abort) {
replacedText = replacedText.concat(textToCheck.substr(match.index, (match[0].length)));
} else {
const urlRegex = new RegExp(`^${URL_REGEX}$`, 'i');
const urlRegex = new RegExp(`^${LOOSE_URL_REGEX}$|^${URL_REGEX}$`, 'i');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we use MARKDOWN_URL_REGEX here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Santhosh-Sellavel We can't use it because ^${LOOSE_URL_REGEX}$|^${URL_REGEX}$ is different from ^${LOOSE_URL_REGEX}|${URL_REGEX}$. We want it to EITHER start and end with loose url regex OR start and end with URL_REGEX.

Copy link
Contributor

@Santhosh-Sellavel Santhosh-Sellavel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, tests well. Will continue on App PR thanks!

cc: @neil-marcellini @mountiny

@Santhosh-Sellavel
Copy link
Contributor

bump @mountiny or @neil-marcellini

Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks everyone!

@@ -11,6 +11,12 @@ const URL_REGEX = `((${URL_WEBSITE_REGEX})${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}

const URL_REGEX_WITH_REQUIRED_PROTOCOL = URL_REGEX.replace(`${URL_PROTOCOL_REGEX}?`, URL_PROTOCOL_REGEX);

const LOOSE_URL_WEBSITE_REGEX = `${URL_PROTOCOL_REGEX}([-\\w]+(\\.[-\\w]+)*)(?:\\:${ALLOWED_PORTS}|\\b|(?=_))`;
const LOOSE_URL_REGEX = `((${LOOSE_URL_WEBSITE_REGEX})${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|${URL_FRAGMENT_REGEX})*)`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We allowed the URL to start by hyphen which is not allowed according to the RFC1034. This caused Expensify/App#27080

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

Successfully merging this pull request may close these issues.

4 participants