Skip to content

Commit

Permalink
test(external_link): utilize hexo-util isExternalLink()
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Nov 9, 2019
1 parent 6fa7ada commit c1037f0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 69 deletions.
33 changes: 2 additions & 31 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -46,7 +17,7 @@ function externalLinkFilter(data) {
|| config.external_link.field !== 'post') return;

data.content = data.content.replace(/<a.*?(href=['"](.*?)['"]).*?>/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) => {
Expand Down
39 changes: 2 additions & 37 deletions lib/plugins/filter/after_render/external_link.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -52,7 +17,7 @@ function externalLinkFilter(data) {
|| config.external_link.field !== 'site') return;

data = data.replace(/<a.*?(href=['"](.*?)['"]).*?>/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) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c1037f0

Please sign in to comment.