Skip to content

Commit

Permalink
fix(NcRichText): Match IP addresses as links
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Mar 28, 2023
1 parent d1d7d1b commit 9c6ea0e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/NcRichText/NcRichText.vue
Expand Up @@ -39,6 +39,8 @@ export default {
return {
text: `Hello {username}. The file {file} was added by {username}. Go visit https://nextcloud.com
Local IP: http://127.0.0.1/status.php should be clickable
Some examples for markdown syntax: **bold text** *italic text* ~~strikethrough~~`,
autolink: true,
useMarkdown: true,
Expand Down
16 changes: 14 additions & 2 deletions src/components/NcRichText/helpers.js
@@ -1,4 +1,16 @@
/**
* Regex pattern to match links to be resolved by the link-reference provider
*
* @type {RegExp}
*/
export const URL_PATTERN = /(\s|^)(https?:\/\/)((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig

// FIXME See if we can merge those two
export const URL_PATTERN_AUTOLINK = /(\s|\(|^)((https?:\/\/)((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*))(?=\s|\)|$)/ig
/**
* Regex pattern to identify strings as links and then making them clickable
* Opposed to the above regex this one also matches IP addresses, which we would like to be clickable,
* but in general resolving references for them might mostly not work,
* as the link provider checks for local addresses and does not resolve them.
*
* @type {RegExp}
*/
export const URL_PATTERN_AUTOLINK = /(\s|\(|^)((https?:\/\/)((?:[-A-Z0-9+_]+\.)+[-A-Z0-9]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*))(?=\s|\)|$)/ig
11 changes: 11 additions & 0 deletions tests/unit/components/NcRichText/NcRichText.spec.js
Expand Up @@ -152,6 +152,17 @@ describe('Foo', () => {
expect(wrapper.find('a').attributes('href')).toEqual('https://example.com:444')
})

it('properly recognizes an url with an IP address and inserts a link', async() => {
const wrapper = mount(NcRichText, {
propsData: {
text: 'Testwith a link to https://127.0.0.1/status.php - go visit it',
autolink: true
}
})
expect(wrapper.text()).toEqual('Testwith a link to https://127.0.0.1/status.php - go visit it')
expect(wrapper.find('a').attributes('href')).toEqual('https://127.0.0.1/status.php')
})

it('properly formats markdown', async() => {
const wrapper = mount(NcRichText, {
propsData: {
Expand Down

0 comments on commit 9c6ea0e

Please sign in to comment.