1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | const url_1 = require("url");
|
6 | const cache_1 = __importDefault(require("./cache"));
|
7 | const cache = new cache_1.default();
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function isExternalLink(input, sitehost, exclude) {
|
16 | return cache.apply(`${input}-${sitehost}-${exclude}`, () => {
|
17 |
|
18 | if (!/^(\/\/|http(s)?:)/.test(input))
|
19 | return false;
|
20 | sitehost = (0, url_1.parse)(sitehost).hostname || sitehost;
|
21 | if (!sitehost)
|
22 | return false;
|
23 |
|
24 | let data;
|
25 | try {
|
26 | data = new URL(input, `http://${sitehost}`);
|
27 | }
|
28 | catch (e) { }
|
29 |
|
30 | if (typeof data !== 'object')
|
31 | return false;
|
32 |
|
33 | if (data.origin === 'null')
|
34 | return false;
|
35 | const host = data.hostname;
|
36 | if (exclude) {
|
37 | exclude = Array.isArray(exclude) ? exclude : [exclude];
|
38 | if (exclude && exclude.length) {
|
39 | for (const i of exclude) {
|
40 | if (host === i)
|
41 | return false;
|
42 | }
|
43 | }
|
44 | }
|
45 | if (host !== sitehost)
|
46 | return true;
|
47 | return false;
|
48 | });
|
49 | }
|
50 | module.exports = isExternalLink;
|
51 |
|
\ | No newline at end of file |