Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(external_link): utilize hexo-util isExternalLink() #3847

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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