-
Notifications
You must be signed in to change notification settings - Fork 135
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,59 @@ | ||
import {URL_REGEX_WITH_REQUIRED_PROTOCOL, URL_REGEX} from '../lib/Url'; | ||
import {URL_REGEX_WITH_REQUIRED_PROTOCOL, URL_REGEX, LOOSE_URL_REGEX} from '../lib/Url'; | ||
|
||
describe('Mandatory protocol for URL', () => { | ||
it('correctly tests valid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX_WITH_REQUIRED_PROTOCOL}$`, 'i'); | ||
expect(regexToTest.test('https://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('http://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('ftp://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('https://we.are.expensify.com/how-we-got-here')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com:12')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com:65535')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com:65535/path/my')).toBeTruthy(); | ||
describe('Strict URL validation', () => { | ||
describe('Mandatory protocol for URL', () => { | ||
it('correctly tests valid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX_WITH_REQUIRED_PROTOCOL}$`, 'i'); | ||
expect(regexToTest.test('https://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('http://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('ftp://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('https://we.are.expensify.com/how-we-got-here')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com:12')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com:65535')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com:65535/path/my')).toBeTruthy(); | ||
}); | ||
it('correctly tests invalid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX_WITH_REQUIRED_PROTOCOL}$`, 'i'); | ||
expect(regexToTest.test('google.com')).toBeFalsy(); | ||
expect(regexToTest.test('https://google.com:02')).toBeFalsy(); | ||
expect(regexToTest.test('https://google.com:65536')).toBeFalsy(); | ||
expect(regexToTest.test('smtp://google.com')).toBeFalsy(); | ||
}); | ||
}); | ||
it('correctly tests invalid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX_WITH_REQUIRED_PROTOCOL}$`, 'i'); | ||
expect(regexToTest.test('google.com')).toBeFalsy(); | ||
expect(regexToTest.test('https://google.com:02')).toBeFalsy(); | ||
expect(regexToTest.test('https://google.com:65536')).toBeFalsy(); | ||
expect(regexToTest.test('smtp://google.com')).toBeFalsy(); | ||
|
||
describe('Optional protocol for URL', () => { | ||
it('correctly tests valid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX}$`, 'i'); | ||
expect(regexToTest.test('google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('ftp://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('we.are.expensify.com/how-we-got-here')).toBeTruthy(); | ||
expect(regexToTest.test('google.com:12')).toBeTruthy(); | ||
expect(regexToTest.test('google.com:65535')).toBeTruthy(); | ||
expect(regexToTest.test('google.com:65535/path/my')).toBeTruthy(); | ||
}); | ||
it('correctly tests invalid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX}$`, 'i'); | ||
expect(regexToTest.test('google.com:02')).toBeFalsy(); | ||
expect(regexToTest.test('google.com:65536')).toBeFalsy(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Optional protocol for URL', () => { | ||
it('correctly tests valid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX}$`, 'i'); | ||
expect(regexToTest.test('google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('https://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('ftp://google.com/')).toBeTruthy(); | ||
expect(regexToTest.test('we.are.expensify.com/how-we-got-here')).toBeTruthy(); | ||
expect(regexToTest.test('google.com:12')).toBeTruthy(); | ||
expect(regexToTest.test('google.com:65535')).toBeTruthy(); | ||
expect(regexToTest.test('google.com:65535/path/my')).toBeTruthy(); | ||
describe('Loose URL validation', () => { | ||
it('correctly tests urls that can be valid on local machine', () => { | ||
const regexToTest = new RegExp(`^${LOOSE_URL_REGEX}$`, 'i'); | ||
expect(regexToTest.test('http://localhost:3000')).toBeTruthy(); | ||
expect(regexToTest.test('https://local.url')).toBeTruthy(); | ||
expect(regexToTest.test('http://a.b')).toBeTruthy(); | ||
expect(regexToTest.test('http://expensify')).toBeTruthy(); | ||
expect(regexToTest.test('http://google.com/abcd')).toBeTruthy(); | ||
}); | ||
|
||
it('correctly tests invalid urls', () => { | ||
const regexToTest = new RegExp(`^${URL_REGEX}$`, 'i'); | ||
expect(regexToTest.test('google.com:02')).toBeFalsy(); | ||
expect(regexToTest.test('google.com:65536')).toBeFalsy(); | ||
const regexToTest = new RegExp(`^${LOOSE_URL_REGEX}$`, 'i'); | ||
expect(regexToTest.test('localhost:3000')).toBeFalsy(); | ||
expect(regexToTest.test('local.url')).toBeFalsy(); | ||
expect(regexToTest.test('https://otherexample.com links get rendered first')).toBeFalsy(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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|(?=_))`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @neil-marcellini You need to replace the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah got it thanks!
Can you explain why? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
const LOOSE_URL_REGEX = `((${LOOSE_URL_WEBSITE_REGEX})${URL_PATH_REGEX}(?:${URL_PARAM_REGEX}|${URL_FRAGMENT_REGEX})*)`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We allowed the URL to start by |
||
|
||
|
||
const MARKDOWN_URL_REGEX = `(${LOOSE_URL_REGEX}|${URL_REGEX})`; | ||
|
||
export { | ||
URL_WEBSITE_REGEX, | ||
URL_PATH_REGEX, | ||
|
@@ -19,4 +25,7 @@ export { | |
URL_REGEX, | ||
URL_REGEX_WITH_REQUIRED_PROTOCOL, | ||
URL_PROTOCOL_REGEX, | ||
LOOSE_URL_REGEX, | ||
LOOSE_URL_WEBSITE_REGEX, | ||
MARKDOWN_URL_REGEX, | ||
}; |
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.
Why can't we use MARKDOWN_URL_REGEX here?
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.
@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 withURL_REGEX
.