From 2bb04d59e9d9f6dd1ae4dfede5f3250675a3687f Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 22 Aug 2019 21:56:18 +0800 Subject: [PATCH 1/6] refactor(external_link): drop cheerio and use regex --- .../filter/after_post_render/external_link.js | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index f43ea74716..962e103224 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -9,31 +9,28 @@ function externalLinkFilter(data) { if (!cheerio) cheerio = require('cheerio'); - const $ = cheerio.load(data.content, {decodeEntities: false}); + // const $ = cheerio.load(data.content, {decodeEntities: false}); const siteHost = url.parse(config.url).hostname || config.url; - $('a').each(function() { - // Exit if the link has target attribute - if ($(this).attr('target')) return; - - // Exit if the href attribute doesn't exists - const href = $(this).attr('href'); - if (!href) return; + data.content = data.content.replace(//gi, (str, hrefStr, href) => { + if (/target=/gi.test(str)) return str; const data = url.parse(href); - // Exit if the link doesn't have protocol, which means it's a internal link - if (!data.protocol) return; - - // Exit if the url has same host with config.url - if (data.hostname === siteHost) return; - - $(this) - .attr('target', '_blank') - .attr('rel', 'noopener'); + if (!data.protocol) return str; + // Exit if the url has same host with config.ur + if (data.hostname === siteHost) return str; + + if (/rel=/gi.test(str)) { + str = str.replace(/rel="(.*?)"/gi, (relStr, p1) => { + return relStr.replace(p1, `${p1} noopener`); + }); + return str.replace(hrefStr, `${hrefStr} target="_blank"`); + } + + return str.replace(hrefStr, `${hrefStr} target="_blank" rel="noopener"`); }); - data.content = $.html(); } module.exports = externalLinkFilter; From 7733f27805a4df670fae1949aac9ffdcf638fd7c Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 22 Aug 2019 22:04:17 +0800 Subject: [PATCH 2/6] test(external_link): rel attribute test cases --- test/scripts/filters/external_link.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/scripts/filters/external_link.js b/test/scripts/filters/external_link.js index 18f6d696a1..7d1fe777de 100644 --- a/test/scripts/filters/external_link.js +++ b/test/scripts/filters/external_link.js @@ -26,13 +26,16 @@ describe('External link', () => { '# External link test', '1. External link', 'Hexo', - '2. Internal link', + '2. External link with rel Attribute', + 'Hexo', + 'Hexo', + '3. Internal link', 'Link', - '3. Ignore links have "target" attribute', + '4. Ignore links have "target" attribute', 'Hexo', - '4. Ignore links don\'t have "href" attribute', + '5. Ignore links don\'t have "href" attribute', 'Anchor', - '5. Ignore links whose hostname is same as config', + '6. Ignore links whose hostname is same as config', 'Example Domain' ].join('\n'); @@ -44,13 +47,16 @@ describe('External link', () => { '# External link test', '1. External link', 'Hexo', - '2. Internal link', + '2. External link with rel Attribute', + 'Hexo', + 'Hexo', + '3. Internal link', 'Link', - '3. Ignore links have "target" attribute', + '4. Ignore links have "target" attribute', 'Hexo', - '4. Ignore links don\'t have "href" attribute', + '5. Ignore links don\'t have "href" attribute', 'Anchor', - '5. Ignore links whose hostname is same as config', + '6. Ignore links whose hostname is same as config', 'Example Domain' ].join('\n')); }); From c6a46f62bcaf3693ae16b470b192b21ac0e8c9f2 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 22 Aug 2019 22:11:01 +0800 Subject: [PATCH 3/6] test(external_link): add attributes test cases --- test/scripts/filters/external_link.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/test/scripts/filters/external_link.js b/test/scripts/filters/external_link.js index 7d1fe777de..b8a473cefc 100644 --- a/test/scripts/filters/external_link.js +++ b/test/scripts/filters/external_link.js @@ -26,16 +26,19 @@ describe('External link', () => { '# External link test', '1. External link', 'Hexo', - '2. External link with rel Attribute', + '2. External link with "rel" Attribute', 'Hexo', 'Hexo', - '3. Internal link', + '3. External link with Other Attributes', + 'Hexo', + 'Hexo', + '4. Internal link', 'Link', - '4. Ignore links have "target" attribute', + '5. Ignore links have "target" attribute', 'Hexo', - '5. Ignore links don\'t have "href" attribute', + '6. Ignore links don\'t have "href" attribute', 'Anchor', - '6. Ignore links whose hostname is same as config', + '7. Ignore links whose hostname is same as config', 'Example Domain' ].join('\n'); @@ -47,16 +50,19 @@ describe('External link', () => { '# External link test', '1. External link', 'Hexo', - '2. External link with rel Attribute', + '2. External link with "rel" Attribute', 'Hexo', 'Hexo', - '3. Internal link', + '3. External link with Other Attributes', + 'Hexo', + 'Hexo', + '4. Internal link', 'Link', - '4. Ignore links have "target" attribute', + '5. Ignore links have "target" attribute', 'Hexo', - '5. Ignore links don\'t have "href" attribute', + '6. Ignore links don\'t have "href" attribute', 'Anchor', - '6. Ignore links whose hostname is same as config', + '7. Ignore links whose hostname is same as config', 'Example Domain' ].join('\n')); }); From e2657d0538f1555fe179ea15ad5377d403e173bc Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 22 Aug 2019 22:18:40 +0800 Subject: [PATCH 4/6] refactor(external_link): remove cheerio --- lib/plugins/filter/after_post_render/external_link.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index 962e103224..be5298d7bb 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -1,15 +1,11 @@ 'use strict'; const url = require('url'); -let cheerio; function externalLinkFilter(data) { const { config } = this; if (!config.external_link) return; - if (!cheerio) cheerio = require('cheerio'); - - // const $ = cheerio.load(data.content, {decodeEntities: false}); const siteHost = url.parse(config.url).hostname || config.url; data.content = data.content.replace(//gi, (str, hrefStr, href) => { From 55ca5462fa59cca767d7ae7246a65e250deb5b88 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 23 Aug 2019 10:38:02 +0800 Subject: [PATCH 5/6] refactor(open_graph): avoid duplicate noopenner --- lib/plugins/filter/after_post_render/external_link.js | 7 ++++--- test/scripts/filters/external_link.js | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index be5298d7bb..fbb6eb1794 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -14,12 +14,13 @@ function externalLinkFilter(data) { const data = url.parse(href); // Exit if the link doesn't have protocol, which means it's a internal link if (!data.protocol) return str; - // Exit if the url has same host with config.ur + // Exit if the url has same host with config.url if (data.hostname === siteHost) return str; if (/rel=/gi.test(str)) { - str = str.replace(/rel="(.*?)"/gi, (relStr, p1) => { - return relStr.replace(p1, `${p1} noopener`); + str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => { + if (rel.includes('noopenner')) return relStr; + return relStr.replace(rel, `${rel} noopener`); }); return str.replace(hrefStr, `${hrefStr} target="_blank"`); } diff --git a/test/scripts/filters/external_link.js b/test/scripts/filters/external_link.js index b8a473cefc..1e095d6a82 100644 --- a/test/scripts/filters/external_link.js +++ b/test/scripts/filters/external_link.js @@ -29,6 +29,10 @@ describe('External link', () => { '2. External link with "rel" Attribute', 'Hexo', 'Hexo', + 'Hexo', + 'Hexo', + 'Hexo', + 'Hexo', '3. External link with Other Attributes', 'Hexo', 'Hexo', @@ -53,6 +57,10 @@ describe('External link', () => { '2. External link with "rel" Attribute', 'Hexo', 'Hexo', + 'Hexo', + 'Hexo', + 'Hexo', + 'Hexo', '3. External link with Other Attributes', 'Hexo', 'Hexo', From 07d83c7621bdb7cca5f439a78c1021c9b30e05d7 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 23 Aug 2019 12:06:58 +0800 Subject: [PATCH 6/6] refactor(open_graph): try to make climate happy --- lib/plugins/filter/after_post_render/external_link.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/plugins/filter/after_post_render/external_link.js b/lib/plugins/filter/after_post_render/external_link.js index fbb6eb1794..229f3a3f41 100644 --- a/lib/plugins/filter/after_post_render/external_link.js +++ b/lib/plugins/filter/after_post_render/external_link.js @@ -8,19 +8,18 @@ function externalLinkFilter(data) { const siteHost = url.parse(config.url).hostname || config.url; - data.content = data.content.replace(//gi, (str, hrefStr, href) => { + data.content = data.content.replace(//gi, (str, hrefStr, href) => { if (/target=/gi.test(str)) return str; const data = url.parse(href); // Exit if the link doesn't have protocol, which means it's a internal link - if (!data.protocol) return str; // Exit if the url has same host with config.url - if (data.hostname === siteHost) return str; + if (!data.protocol || data.hostname === siteHost) return str; if (/rel=/gi.test(str)) { str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => { - if (rel.includes('noopenner')) return relStr; - return relStr.replace(rel, `${rel} noopener`); + if (!rel.includes('noopenner')) relStr = relStr.replace(rel, `${rel} noopener`); + return relStr; }); return str.replace(hrefStr, `${hrefStr} target="_blank"`); }