forked from sindresorhus/get-urls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (50 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const urlRegex = require('url-regex-safe');
const normalizeUrl = require('normalize-url');
const getUrlsFromQueryParameters = url => {
const returnValue = new Set();
const {searchParams} = (new URL(url.replace(/^(?:\/\/|(?:www\.))/i, 'http://$2')));
for (const [, value] of searchParams) {
if (urlRegex({exact: true}).test(value)) {
returnValue.add(value);
}
}
return returnValue;
};
module.exports = (text, options = {}) => {
if (typeof text !== 'string') {
throw new TypeError(`The \`text\` argument should be a string, got ${typeof text}`);
}
if (typeof options.exclude !== 'undefined' && !Array.isArray(options.exclude)) {
throw new TypeError('The `exclude` option must be an array');
}
const returnValue = new Set();
const add = url => {
try {
returnValue.add(normalizeUrl(url.trim().replace(/\.+$/, ''), options));
} catch {}
};
const urls = text.match(
urlRegex(options.requireSchemeOrWww === undefined ? undefined : {
strict: options.requireSchemeOrWww
})
) || [];
for (const url of urls) {
add(url);
if (options.extractFromQueryString) {
const qsUrls = getUrlsFromQueryParameters(url);
for (const qsUrl of qsUrls) {
add(qsUrl);
}
}
}
for (const excludedItem of options.exclude || []) {
for (const item of returnValue) {
const regex = new RegExp(excludedItem);
if (regex.test(item)) {
returnValue.delete(item);
}
}
}
return returnValue;
};