diff --git a/index.d.ts b/index.d.ts index e3b8652..f2d8804 100644 --- a/index.d.ts +++ b/index.d.ts @@ -15,6 +15,15 @@ declare namespace getUrls { @default [] */ readonly exclude?: string[]; + + /** + Require URLs to have a scheme or leading `www.` to be considered an URL. When `false`, matches against a list of valid TLDs, so it will match URLs like `unicorn.education`. + + Does not affect URLs in query parameters if using the `extractFromQueryString` option. + + @default true + */ + readonly requireSchemeOrWww?: boolean; } } diff --git a/index.js b/index.js index c80136f..96e0431 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,11 @@ module.exports = (text, options = {}) => { } catch (_) {} }; - const urls = text.match(urlRegex()) || []; + const urls = text.match( + urlRegex(options.requireSchemeOrWww === undefined ? undefined : { + strict: options.requireSchemeOrWww + }) + ) || []; for (const url of urls) { add(url); diff --git a/readme.md b/readme.md index 8c0b28d..76c7b05 100644 --- a/readme.md +++ b/readme.md @@ -56,6 +56,15 @@ Default: `[]` Exclude URLs that match URLs in the given array. +#### requireSchemeOrWww + +Type: `boolean`
+Default: `true` + +Require URLs to have a scheme or leading `www.` to be considered an URL. When `false`, matches against a list of valid TLDs, so it will match URLs like `unicorn.education`. + +Does not affect URLs in query parameters if using the `extractFromQueryString` option. + ## Related diff --git a/test.js b/test.js index e690b61..0e60165 100644 --- a/test.js +++ b/test.js @@ -118,3 +118,15 @@ test('get schemeless url from query string', t => { ]) ); }); + +test('requireSchemeOrWww turned off', t => { + const text = 'Here is a URL: sindresorhus.com here is another: unicorn.education'; + t.deepEqual( + getUrls(text, { + requireSchemeOrWww: false + }), new Set([ + 'http://sindresorhus.com', + 'http://unicorn.education' + ]) + ); +});