Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ Default: `[]`

Exclude URLs that match URLs in the given array.

#### requireSchemeOrWww

Type: `boolean`<br>
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

Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
])
);
});