-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export functions based on return type
- Loading branch information
Showing
4 changed files
with
76 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import {URL} from 'node:url'; | ||
import test from 'ava'; | ||
import jsdom from 'jsdom'; | ||
import linkifyUrls from './index.js'; | ||
import {linkifyUrlsToDom, linkifyUrlsToHtml} from './index.js'; | ||
|
||
const dom = new jsdom.JSDOM(); | ||
globalThis.window = dom.window; | ||
|
@@ -29,12 +29,12 @@ const html = dom => { | |
|
||
test('main', t => { | ||
t.is( | ||
linkifyUrls('See https://sindresorhus.com and https://github.com/sindresorhus/got'), | ||
linkifyUrlsToHtml('See https://sindresorhus.com and https://github.com/sindresorhus/got'), | ||
'See <a href="https://sindresorhus.com">https://sindresorhus.com</a> and <a href="https://github.com/sindresorhus/got">https://github.com/sindresorhus/got</a>', | ||
); | ||
|
||
t.is( | ||
linkifyUrls('See https://sindresorhus.com', { | ||
linkifyUrlsToHtml('See https://sindresorhus.com', { | ||
attributes: { | ||
class: 'unicorn', | ||
target: '_blank', | ||
|
@@ -44,14 +44,14 @@ test('main', t => { | |
); | ||
|
||
t.is( | ||
linkifyUrls('[](https://travis-ci.org/sindresorhus/caprine)'), | ||
linkifyUrlsToHtml('[](https://travis-ci.org/sindresorhus/caprine)'), | ||
'[](<a href="https://travis-ci.org/sindresorhus/caprine">https://travis-ci.org/sindresorhus/caprine</a>)', | ||
); | ||
}); | ||
|
||
test('supports boolean and non-string attribute values', t => { | ||
t.is( | ||
linkifyUrls('https://sindresorhus.com', { | ||
linkifyUrlsToHtml('https://sindresorhus.com', { | ||
attributes: { | ||
foo: true, | ||
bar: false, | ||
|
@@ -64,15 +64,12 @@ test('supports boolean and non-string attribute values', t => { | |
|
||
test('DocumentFragment support', t => { | ||
t.is( | ||
html(linkifyUrls('See https://sindresorhus.com and https://github.com/sindresorhus/got', { | ||
type: 'dom', | ||
})), | ||
html(linkifyUrlsToDom('See https://sindresorhus.com and https://github.com/sindresorhus/got')), | ||
html(domify('See <a href="https://sindresorhus.com">https://sindresorhus.com</a> and <a href="https://github.com/sindresorhus/got">https://github.com/sindresorhus/got</a>')), | ||
); | ||
|
||
t.is( | ||
html(linkifyUrls('See https://sindresorhus.com', { | ||
type: 'dom', | ||
html(linkifyUrlsToDom('See https://sindresorhus.com', { | ||
attributes: { | ||
class: 'unicorn', | ||
target: '_blank', | ||
|
@@ -82,110 +79,100 @@ test('DocumentFragment support', t => { | |
); | ||
|
||
t.is( | ||
html(linkifyUrls('[](https://travis-ci.org/sindresorhus/caprine)', { | ||
type: 'dom', | ||
})), | ||
html(linkifyUrlsToDom('[](https://travis-ci.org/sindresorhus/caprine)')), | ||
html(domify('[](<a href="https://travis-ci.org/sindresorhus/caprine">https://travis-ci.org/sindresorhus/caprine</a>)')), | ||
); | ||
}); | ||
|
||
test('escapes the URL', t => { | ||
t.is(linkifyUrls('https://mysite.com/?emp=1&=2'), '<a href="https://mysite.com/?emp=1&amp=2">https://mysite.com/?emp=1&amp=2</a>'); | ||
t.is(linkifyUrlsToHtml('https://mysite.com/?emp=1&=2'), '<a href="https://mysite.com/?emp=1&amp=2">https://mysite.com/?emp=1&amp=2</a>'); | ||
}); | ||
|
||
test('supports `@` in the URL path', t => { | ||
t.is(linkifyUrls('https://sindresorhus.com/@foo'), '<a href="https://sindresorhus.com/@foo">https://sindresorhus.com/@foo</a>'); | ||
t.is(linkifyUrlsToHtml('https://sindresorhus.com/@foo'), '<a href="https://sindresorhus.com/@foo">https://sindresorhus.com/@foo</a>'); | ||
}); | ||
|
||
test('supports `#!` in the URL path', t => { | ||
t.is(linkifyUrls('https://twitter.com/#!/sindresorhus'), '<a href="https://twitter.com/#!/sindresorhus">https://twitter.com/#!/sindresorhus</a>'); | ||
t.is(linkifyUrlsToHtml('https://twitter.com/#!/sindresorhus'), '<a href="https://twitter.com/#!/sindresorhus">https://twitter.com/#!/sindresorhus</a>'); | ||
}); | ||
|
||
test('supports *$ in the URL path', t => { | ||
t.is(linkifyUrls('https://sindresorhus.com/#1_*'), '<a href="https://sindresorhus.com/#1_*">https://sindresorhus.com/#1_*</a>'); | ||
t.is(linkifyUrls('https://sindresorhus.com/#1_$'), '<a href="https://sindresorhus.com/#1_$">https://sindresorhus.com/#1_$</a>'); | ||
t.is(linkifyUrlsToHtml('https://sindresorhus.com/#1_*'), '<a href="https://sindresorhus.com/#1_*">https://sindresorhus.com/#1_*</a>'); | ||
t.is(linkifyUrlsToHtml('https://sindresorhus.com/#1_$'), '<a href="https://sindresorhus.com/#1_$">https://sindresorhus.com/#1_$</a>'); | ||
}); | ||
|
||
test('supports `,` in the URL path, but not at the end', t => { | ||
t.is(linkifyUrls('https://sindresorhus.com/?id=foo,bar'), '<a href="https://sindresorhus.com/?id=foo,bar">https://sindresorhus.com/?id=foo,bar</a>'); | ||
t.is(linkifyUrls('https://sindresorhus.com/?id=foo, bar'), '<a href="https://sindresorhus.com/?id=foo">https://sindresorhus.com/?id=foo</a>, bar'); | ||
t.is(linkifyUrlsToHtml('https://sindresorhus.com/?id=foo,bar'), '<a href="https://sindresorhus.com/?id=foo,bar">https://sindresorhus.com/?id=foo,bar</a>'); | ||
t.is(linkifyUrlsToHtml('https://sindresorhus.com/?id=foo, bar'), '<a href="https://sindresorhus.com/?id=foo">https://sindresorhus.com/?id=foo</a>, bar'); | ||
}); | ||
|
||
test('supports `value` option', t => { | ||
t.is(linkifyUrls('See https://github.com/sindresorhus.com/linkify-urls for a solution', { | ||
t.is(linkifyUrlsToHtml('See https://github.com/sindresorhus.com/linkify-urls for a solution', { | ||
type: 'string', | ||
value: 0, | ||
}), 'See <a href="https://github.com/sindresorhus.com/linkify-urls">0</a> for a solution'); | ||
}); | ||
|
||
test('supports `value` option as function', t => { | ||
t.is(linkifyUrls('See https://github.com/sindresorhus.com/linkify-urls for a solution', { | ||
t.is(linkifyUrlsToHtml('See https://github.com/sindresorhus.com/linkify-urls for a solution', { | ||
value: url => new URL(url).hostname, | ||
}), 'See <a href="https://github.com/sindresorhus.com/linkify-urls">github.com</a> for a solution'); | ||
}); | ||
|
||
test.failing('skips the trailing period', t => { | ||
t.is(linkifyUrls('Visit https://fregante.com.'), 'Visit <a href="https://fregante.com">https://fregante.com</a>.'); | ||
t.is(linkifyUrlsToHtml('Visit https://fregante.com.'), 'Visit <a href="https://fregante.com">https://fregante.com</a>.'); | ||
}); | ||
|
||
test('skips URLs preceded by a `+` sign', t => { | ||
const fixture = 'git+https://github.com/sindresorhus/ava'; | ||
t.is(linkifyUrls(fixture), fixture); | ||
t.is(linkifyUrlsToHtml(fixture), fixture); | ||
}); | ||
|
||
test('supports username in url', t => { | ||
t.is(linkifyUrls('https://[email protected]/@foo'), '<a href="https://[email protected]/@foo">https://[email protected]/@foo</a>'); | ||
t.is(linkifyUrlsToHtml('https://[email protected]/@foo'), '<a href="https://[email protected]/@foo">https://[email protected]/@foo</a>'); | ||
}); | ||
|
||
test('supports a URL with a subdomain', t => { | ||
t.is(linkifyUrls('https://docs.google.com'), '<a href="https://docs.google.com">https://docs.google.com</a>'); | ||
t.is(linkifyUrlsToHtml('https://docs.google.com'), '<a href="https://docs.google.com">https://docs.google.com</a>'); | ||
}); | ||
|
||
test('skips email addresses', t => { | ||
t.is(linkifyUrls('[email protected]'), '[email protected]'); | ||
t.is(linkifyUrls('[email protected]'), '[email protected]'); | ||
t.is(linkifyUrls('[email protected]'), '[email protected]'); | ||
t.is(linkifyUrlsToHtml('[email protected]'), '[email protected]'); | ||
t.is(linkifyUrlsToHtml('[email protected]'), '[email protected]'); | ||
t.is(linkifyUrlsToHtml('[email protected]'), '[email protected]'); | ||
}); | ||
|
||
test('supports localhost URLs', t => { | ||
t.is(linkifyUrls('https://localhost'), '<a href="https://localhost">https://localhost</a>'); | ||
t.is(linkifyUrls('https://localhost/foo/bar'), '<a href="https://localhost/foo/bar">https://localhost/foo/bar</a>'); | ||
t.is(linkifyUrlsToHtml('https://localhost'), '<a href="https://localhost">https://localhost</a>'); | ||
t.is(linkifyUrlsToHtml('https://localhost/foo/bar'), '<a href="https://localhost/foo/bar">https://localhost/foo/bar</a>'); | ||
}); | ||
|
||
test('skips truncated URLs', t => { | ||
t.is(linkifyUrls('https://github.com/sindresorhus/linkify-…'), 'https://github.com/sindresorhus/linkify-…'); | ||
t.is(linkifyUrls('https://github.com/sindresorhus/linkify-… and https://github.com/sindresorhus/linkify-…'), 'https://github.com/sindresorhus/linkify-… and https://github.com/sindresorhus/linkify-…'); | ||
t.is(linkifyUrls('https://github.com/sindresorhus/linkify-urls and more…'), '<a href="https://github.com/sindresorhus/linkify-urls">https://github.com/sindresorhus/linkify-urls</a> and more…'); | ||
t.is(linkifyUrlsToHtml('https://github.com/sindresorhus/linkify-…'), 'https://github.com/sindresorhus/linkify-…'); | ||
t.is(linkifyUrlsToHtml('https://github.com/sindresorhus/linkify-… and https://github.com/sindresorhus/linkify-…'), 'https://github.com/sindresorhus/linkify-… and https://github.com/sindresorhus/linkify-…'); | ||
t.is(linkifyUrlsToHtml('https://github.com/sindresorhus/linkify-urls and more…'), '<a href="https://github.com/sindresorhus/linkify-urls">https://github.com/sindresorhus/linkify-urls</a> and more…'); | ||
|
||
t.is(linkifyUrls('https://github.com/sindresorhus/linkify-...'), 'https://github.com/sindresorhus/linkify-...'); | ||
t.is(linkifyUrls('https://github.com/sindresorhus/linkify-... and https://github.com/sindresorhus/linkify-...'), 'https://github.com/sindresorhus/linkify-... and https://github.com/sindresorhus/linkify-...'); | ||
t.is(linkifyUrls('https://github.com/sindresorhus/linkify-urls and more...'), '<a href="https://github.com/sindresorhus/linkify-urls">https://github.com/sindresorhus/linkify-urls</a> and more...'); | ||
t.is(linkifyUrlsToHtml('https://github.com/sindresorhus/linkify-...'), 'https://github.com/sindresorhus/linkify-...'); | ||
t.is(linkifyUrlsToHtml('https://github.com/sindresorhus/linkify-... and https://github.com/sindresorhus/linkify-...'), 'https://github.com/sindresorhus/linkify-... and https://github.com/sindresorhus/linkify-...'); | ||
t.is(linkifyUrlsToHtml('https://github.com/sindresorhus/linkify-urls and more...'), '<a href="https://github.com/sindresorhus/linkify-urls">https://github.com/sindresorhus/linkify-urls</a> and more...'); | ||
}); | ||
|
||
test('skips truncated URLs (DocumentFragment)', t => { | ||
t.is( | ||
html(linkifyUrls('See https://github.com/sindresorhus/linkify-urls and https://github.com/sindresorhus/linkify-…', { | ||
type: 'dom', | ||
})), | ||
html(linkifyUrlsToDom('See https://github.com/sindresorhus/linkify-urls and https://github.com/sindresorhus/linkify-…')), | ||
html(domify('See <a href="https://github.com/sindresorhus/linkify-urls">https://github.com/sindresorhus/linkify-urls</a> and https://github.com/sindresorhus/linkify-…')), | ||
); | ||
t.is( | ||
html(linkifyUrls('See https://github.com/sindresorhus/linkify-urls… and https://github.com/sindresorhus/linkify-…', { | ||
type: 'dom', | ||
})), | ||
html(linkifyUrlsToDom('See https://github.com/sindresorhus/linkify-urls… and https://github.com/sindresorhus/linkify-…')), | ||
html(domify('See https://github.com/sindresorhus/linkify-urls… and https://github.com/sindresorhus/linkify-…')), | ||
); | ||
|
||
t.is( | ||
html(linkifyUrls('See https://github.com/sindresorhus/linkify-urls and https://github.com/sindresorhus/linkify-...', { | ||
type: 'dom', | ||
})), | ||
html(linkifyUrlsToDom('See https://github.com/sindresorhus/linkify-urls and https://github.com/sindresorhus/linkify-...')), | ||
html(domify('See <a href="https://github.com/sindresorhus/linkify-urls">https://github.com/sindresorhus/linkify-urls</a> and https://github.com/sindresorhus/linkify-...')), | ||
); | ||
t.is( | ||
html(linkifyUrls('See https://github.com/sindresorhus/linkify-... and https://github.com/sindresorhus/linkify-...', { | ||
type: 'dom', | ||
})), | ||
html(linkifyUrlsToDom('See https://github.com/sindresorhus/linkify-... and https://github.com/sindresorhus/linkify-...')), | ||
html(domify('See https://github.com/sindresorhus/linkify-... and https://github.com/sindresorhus/linkify-...')), | ||
); | ||
}); |