Skip to content

Commit

Permalink
perf(external_link): cache config
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Feb 22, 2020
1 parent 7f0537f commit d0caac7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
30 changes: 20 additions & 10 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
'use strict';

const { isExternalLink } = require('hexo-util');
let EXTERNAL_LINK_POST_CONFIG;
let EXTERNAL_LINK_POST_ENABLED = true;

function externalLinkFilter(data) {
if (!EXTERNAL_LINK_POST_ENABLED) return;

const { config } = this;

if (typeof config.external_link === 'undefined' || typeof config.external_link === 'object'
|| config.external_link === true) {
config.external_link = Object.assign({
enable: true,
field: 'site',
exclude: []
}, config.external_link);
if (typeof EXTERNAL_LINK_POST_CONFIG === 'undefined') {
if (typeof config.external_link === 'undefined' || typeof config.external_link === 'object'
|| config.external_link === true) {
EXTERNAL_LINK_POST_CONFIG = Object.assign({
enable: true,
field: 'site',
exclude: []
}, config.external_link);
}
}

if (config.external_link === false || EXTERNAL_LINK_POST_CONFIG.enable === false
|| EXTERNAL_LINK_POST_CONFIG.field !== 'post') {
EXTERNAL_LINK_POST_ENABLED = false;
return;
}
if (config.external_link === false || config.external_link.enable === false
|| config.external_link.field !== 'post') return;

data.content = data.content.replace(/<a([\s]+|[\s]+[^<>]+[\s]+)href=["']([^<>"']+)["'][^<>]*>/gi, (str, _, href) => {
if (/target=/gi.test(str) || !isExternalLink(href, config.url, config.external_link.exclude)) return str;
if (/target=/gi.test(str) || !isExternalLink(href, config.url, EXTERNAL_LINK_POST_CONFIG.exclude)) return str;

if (/rel=/gi.test(str)) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
Expand Down
30 changes: 20 additions & 10 deletions lib/plugins/filter/after_render/external_link.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
'use strict';

const { isExternalLink } = require('hexo-util');
let EXTERNAL_LINK_SITE_CONFIG;
let EXTERNAL_LINK_SITE_ENABLED = true;

function externalLinkFilter(data) {
if (!EXTERNAL_LINK_SITE_ENABLED) return;

const { config } = this;

if (typeof config.external_link === 'undefined' || typeof config.external_link === 'object'
|| config.external_link === true) {
config.external_link = Object.assign({
enable: true,
field: 'site',
exclude: []
}, config.external_link);
if (typeof EXTERNAL_LINK_SITE_CONFIG === 'undefined') {
if (typeof config.external_link === 'undefined' || typeof config.external_link === 'object'
|| config.external_link === true) {
EXTERNAL_LINK_SITE_CONFIG = Object.assign({
enable: true,
field: 'site',
exclude: []
}, config.external_link);
}
}

if (config.external_link === false || EXTERNAL_LINK_SITE_CONFIG.enable === false
|| EXTERNAL_LINK_SITE_CONFIG.field !== 'site') {
EXTERNAL_LINK_SITE_ENABLED = false;
return;
}
if (config.external_link === false || config.external_link.enable === false
|| config.external_link.field !== 'site') return;

data = data.replace(/<a([\s]+|[\s]+[^<>]+[\s]+)href=["']([^<>"']+)["'][^<>]*>/gi, (str, _, href) => {
if (/target=/gi.test(str) || !isExternalLink(href, config.url, config.external_link.exclude)) return str;
if (/target=/gi.test(str) || !isExternalLink(href, config.url, EXTERNAL_LINK_SITE_CONFIG.exclude)) return str;

if (/rel=/gi.test(str)) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"cheerio": "0.22.0",
"decache": "^4.5.1",
"eslint": "^6.0.1",
"eslint-config-hexo": "^4.1.0",
"hexo-renderer-marked": "^2.0.0",
Expand Down
17 changes: 15 additions & 2 deletions test/scripts/filters/external_link.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict';

const decache = require('decache');

describe('External link', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const externalLink = require('../../../lib/plugins/filter/after_render/external_link').bind(hexo);
let externalLink;

beforeEach(() => {
decache('../../../lib/plugins/filter/after_render/external_link');
externalLink = require('../../../lib/plugins/filter/after_render/external_link').bind(hexo);
});

hexo.config = {
url: 'https://example.com',
Expand Down Expand Up @@ -166,7 +173,13 @@ describe('External link', () => {
describe('External link - post', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const externalLink = require('../../../lib/plugins/filter/after_post_render/external_link').bind(hexo);

let externalLink;

beforeEach(() => {
decache('../../../lib/plugins/filter/after_post_render/external_link');
externalLink = require('../../../lib/plugins/filter/after_post_render/external_link').bind(hexo);
});

hexo.config = {
url: 'https://example.com',
Expand Down

0 comments on commit d0caac7

Please sign in to comment.