UNPKG

1.94 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5const url_1 = require("url");
6const cache_1 = __importDefault(require("./cache"));
7const cache = new cache_1.default();
8/**
9 * Check whether the link is external
10 * @param {String} input The url to check
11 * @param {String} input The hostname / url of website
12 * @param {Array} input Exclude hostnames. Specific subdomain is required when applicable, including www.
13 * @returns {Boolean} True if the link doesn't have protocol or link has same host with config.url
14 */
15function isExternalLink(input, sitehost, exclude) {
16 return cache.apply(`${input}-${sitehost}-${exclude}`, () => {
17 // Return false early for internal link
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 // handle relative url and invalid url
24 let data;
25 try {
26 data = new URL(input, `http://${sitehost}`);
27 }
28 catch (e) { }
29 // if input is invalid url, data should be undefined
30 if (typeof data !== 'object')
31 return false;
32 // handle mailto: javascript: vbscript: and so on
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}
50module.exports = isExternalLink;
51//# sourceMappingURL=is_external_link.js.map
\No newline at end of file