UNPKG

detect-external-link

Version:

Node.js plugin that analyzes the given link and determines if the URL is internal or external.

30 lines (29 loc) 1.01 kB
export default function detectExternalLink(linkToCheck, options) { const configHosts = []; const finalConfig = Object.assign({}, options); let isExternal = false; let linkOrigin = ''; if (finalConfig.hosts) { let singleHost; for (singleHost of finalConfig.hosts) { if (singleHost.indexOf('http://') === 0 || singleHost.indexOf('https://') === 0) { configHosts.push(new URL(singleHost).hostname); } else { configHosts.push(singleHost); } } } if (linkToCheck.indexOf('http://') === 0 || linkToCheck.indexOf('https://') === 0) { linkOrigin = new URL(linkToCheck).hostname; } else if (linkToCheck.indexOf('//') === 0) { linkOrigin = new URL('https:' + linkToCheck).hostname; } if (linkOrigin) { if (configHosts.length === 0 || configHosts.indexOf(linkOrigin) === -1) { isExternal = true; } } return isExternal; }