Skip to content

Commit

Permalink
refactor(external_link): run exclude array once a page
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Nov 6, 2019
1 parent 60acb95 commit 6d9ed7a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const urlObj = (str) => {
};

const isExternal = (url, config) => {
const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude
: [config.external_link.exclude];
const { exclude } = config.external_link;
const data = urlObj(url);
const sitehost = urlObj(config.url).hostname || config.url;

Expand Down Expand Up @@ -45,6 +44,9 @@ 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;

Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/filter/after_render/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ 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 { exclude } = config.external_link;
const data = urlObj(url);
const sitehost = urlObj(config.url).hostname || config.url;

Expand Down Expand Up @@ -51,6 +50,9 @@ 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;

Expand Down

0 comments on commit 6d9ed7a

Please sign in to comment.