diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index 10a0c0a3b2..1166636aec 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -1,35 +1,6 @@ 'use strict'; -const { URL } = require('url'); - -const urlObj = (str) => { - try { - return new URL(str); - } catch (err) { - return str; - } -}; - -const isExternal = (url, config) => { - const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude - : [config.external_link.exclude]; - const data = urlObj(url); - const host = data.hostname; - const sitehost = typeof urlObj(config.url) === 'object' ? urlObj(config.url).hostname : config.url; - - if (!sitehost || typeof data === 'string') return false; - if (data.origin === 'null') return false; - - if (exclude && exclude.length) { - for (const i of exclude) { - if (host === i) return false; - } - } - - if (host !== sitehost) return true; - - return false; -}; +const { isExternalLink } = require('hexo-util'); function externalLinkFilter(data) { const { config } = this; @@ -46,7 +17,7 @@ function externalLinkFilter(data) { || config.external_link.field !== 'post') return; data.content = data.content.replace(//gi, (str, hrefStr, href) => { - if (/target=/gi.test(str) || !isExternal(href, config)) return str; + if (/target=/gi.test(str) || !isExternalLink.call(this, href)) return str; if (/rel=/gi.test(str)) { str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => { diff --git a/lib/plugins/filter/after_render/external_link.js b/lib/plugins/filter/after_render/external_link.js index 92ed1f57fa..e249d6c183 100644 --- a/lib/plugins/filter/after_render/external_link.js +++ b/lib/plugins/filter/after_render/external_link.js @@ -1,41 +1,6 @@ 'use strict'; -const { URL } = require('url'); - -const urlObj = (str) => { - try { - return new URL(str); - } catch (err) { - return str; - } -}; - -/** - * Check whether the link is external - * @param {String} url The url to check - * @param {Object} config The site config - * @returns {Boolean} True if the link doesn't have protocol or link has same host with config.url - */ -const isExternal = (url, config) => { - const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude - : [config.external_link.exclude]; - const data = urlObj(url); - const host = data.hostname; - const sitehost = typeof urlObj(config.url) === 'object' ? urlObj(config.url).hostname : config.url; - - if (!sitehost || typeof data === 'string') return false; - if (data.origin === 'null') return false; - - if (exclude && exclude.length) { - for (const i of exclude) { - if (host === i) return false; - } - } - - if (host !== sitehost) return true; - - return false; -}; +const { isExternalLink } = require('hexo-util'); function externalLinkFilter(data) { const { config } = this; @@ -52,7 +17,7 @@ function externalLinkFilter(data) { || config.external_link.field !== 'site') return; data = data.replace(//gi, (str, hrefStr, href) => { - if (/target=/gi.test(str) || !isExternal(href, config)) return str; + if (/target=/gi.test(str) || !isExternalLink.call(this, href)) return str; if (/rel=/gi.test(str)) { str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => { diff --git a/package.json b/package.json index 98eba926f9..41e8441541 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "hexo-fs": "^2.0.0", "hexo-i18n": "^1.0.0", "hexo-log": "^1.0.0", - "hexo-util": "^1.5.0", + "hexo-util": "github:sukkaw/hexo-util#perf-is-external", "js-yaml": "^3.12.0", "lodash": "^4.17.11", "micromatch": "^4.0.2",