Skip to content

Commit

Permalink
refactor(external_link): migrate config during load_config
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jul 11, 2020
1 parent 17ee23f commit 2e5d944
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 63 deletions.
19 changes: 18 additions & 1 deletion lib/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,27 @@ module.exports = async ctx => {
validateConfig(ctx);

ctx.config_path = configPath;

// Trim multiple trailing '/'
config.root = config.root.replace(/\/*$/, '/');
// Remove any trailing '/'
config.url = config.url.replace(/\/+$/, '');

// Deprecated: config.external_link boolean option will be removed in future
if (config.external_link === true) {
config.external_link = {
enable: true,
field: 'site',
exclude: []
};
}
if (config.external_link === false) {
config.external_link = {
enable: false,
field: 'site',
exclude: []
};
}

ctx.public_dir = resolve(baseDir, config.public_dir) + sep;
ctx.source_dir = resolve(baseDir, config.source_dir) + sep;
ctx.source = new Source(ctx);
Expand Down
19 changes: 3 additions & 16 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
'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 EXTERNAL_LINK_POST_CONFIG === 'undefined') {
if (typeof config.external_link === 'undefined' || typeof config.external_link === 'object'
// Deprecated: config.external_link boolean option will be removed in future
|| 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') {
if (config.external_link.enable === false
|| config.external_link.field !== 'post') {
EXTERNAL_LINK_POST_ENABLED = false;
return;
}

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

if (/rel=/gi.test(str)) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
Expand Down
20 changes: 4 additions & 16 deletions lib/plugins/filter/after_render/external_link.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
'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 EXTERNAL_LINK_SITE_CONFIG === 'undefined') {
if (typeof config.external_link === 'undefined' || typeof config.external_link === 'object'
// Deprecated: config.external_link boolean option will be removed in future
|| 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') {
if (config.external_link.enable === false
|| config.external_link.field !== 'site') {
EXTERNAL_LINK_SITE_ENABLED = false;
return;
}

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

if (/rel=/i.test(str)) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
Expand Down
30 changes: 0 additions & 30 deletions test/scripts/filters/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,36 +95,6 @@ describe('External link', () => {
].join('\n'));
});

it('old option - false', () => {
const content = 'foo'
+ '<a href="https://hexo.io/">Hexo</a>'
+ 'bar';

hexo.config.external_link = false;

should.not.exist(externalLink(content));
hexo.config.external_link = {
enable: true,
field: 'site',
exclude: ''
};
});

it('old option - true', () => {
const content = '<a href="https://hexo.io/">Hexo</a>';

hexo.config.external_link = true;

const result = externalLink(content);
result.should.eql('<a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>');

hexo.config.external_link = {
enable: true,
field: 'site',
exclude: ''
};
});

it('exclude - string', () => {
const content = [
'<a href="https://foo.com/">Hexo</a>',
Expand Down
26 changes: 26 additions & 0 deletions test/scripts/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ describe('Load config', () => {
}
});

// Deprecated: config.external_link boolean option will be removed in future
it('migrate external_link config from boolean to object', async () => {
const content1 = 'external_link: true';
const content2 = 'external_link: false';

try {
await writeFile(hexo.config_path, content1);
await loadConfig(hexo);
hexo.config.external_link.should.eql({
enable: true,
field: 'site',
exclude: []
});

await writeFile(hexo.config_path, content2);
await loadConfig(hexo);
hexo.config.external_link.should.eql({
enable: false,
field: 'site',
exclude: []
});
} finally {
await unlink(hexo.config_path);
}
});

it('custom public_dir', async () => {
try {
await writeFile(hexo.config_path, 'public_dir: foo');
Expand Down

0 comments on commit 2e5d944

Please sign in to comment.