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

test/refactor: update isExternalLink #3839

Closed
wants to merge 4 commits 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
27 changes: 11 additions & 16 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
'use strict';

const { URL } = require('url');

const urlObj = (str) => {
try {
return new URL(str);
} catch (err) {
return str;
}
};
const { parse, URL } = require('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;
const { exclude } = config.external_link;
const sitehost = parse(config.url).hostname || config.url;
// some little dirty hacks
const data = new URL(url, `http://${sitehost}`);

if (!sitehost || typeof data === 'string') return false;
if (data.origin === 'null') return false;

const host = data.hostname;
if (exclude && exclude.length) {
for (const i of exclude) {
if (host === i) return false;
Expand All @@ -45,10 +37,13 @@ function externalLinkFilter(data) {
if (config.external_link === false || config.external_link.enable === false
|| config.external_link.field !== 'post') return;

config.external_link.exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude
: [config.external_link.exclude];

data.content = data.content.replace(/<a.*?(href=['"](.*?)['"]).*?>/gi, (str, hrefStr, href) => {
if (/target=/gi.test(str) || !isExternal(href, config)) return str;
if (str.includes('target=') || !isExternal(href, config)) return str;

if (/rel=/gi.test(str)) {
if (str.includes('rel=')) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
if (!rel.includes('noopenner')) relStr = relStr.replace(rel, `${rel} noopener`);
return relStr;
Expand Down
27 changes: 11 additions & 16 deletions lib/plugins/filter/after_render/external_link.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
'use strict';

const { URL } = require('url');

const urlObj = (str) => {
try {
return new URL(str);
} catch (err) {
return str;
}
};
const { parse, URL } = require('url');

/**
* Check whether the link is external
Expand All @@ -17,15 +9,15 @@ const urlObj = (str) => {
* @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;
const { exclude } = config.external_link;
const sitehost = parse(config.url).hostname || config.url;
// some little dirty hacks
const data = new URL(url, `http://${sitehost}`);

if (!sitehost || typeof data === 'string') return false;
if (data.origin === 'null') return false;

const host = data.hostname;
if (exclude && exclude.length) {
for (const i of exclude) {
if (host === i) return false;
Expand All @@ -51,10 +43,13 @@ function externalLinkFilter(data) {
if (config.external_link === false || config.external_link.enable === false
|| config.external_link.field !== 'site') return;

config.external_link.exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude
: [config.external_link.exclude];

data = data.replace(/<a.*?(href=['"](.*?)['"]).*?>/gi, (str, hrefStr, href) => {
if (/target=/gi.test(str) || !isExternal(href, config)) return str;
if (str.includes('target=') || !isExternal(href, config)) return str;
curbengh marked this conversation as resolved.
Show resolved Hide resolved

if (/rel=/gi.test(str)) {
if (str.includes('rel=')) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
if (!rel.includes('noopenner')) relStr = relStr.replace(rel, `${rel} noopener`);
return relStr;
Expand Down