Skip to content

Commit 26c4f4f

Browse files
committed
a
1 parent fc52cc1 commit 26c4f4f

File tree

6 files changed

+64
-60
lines changed

6 files changed

+64
-60
lines changed

scripts/events/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22

33
'use strict';
44

5+
const configLib = require('./lib/config');
6+
const stellarTagUtilsLib = require('./lib/stellar-tag-utils');
7+
const renderStylusLib = require('./lib/render-stylus');
8+
const checkEnvironmentLib = require('./lib/check-environment');
9+
const { version } = require('../../package.json');
10+
511
hexo.on('generateBefore', () => {
612
// Merge config.
7-
require('./lib/config')(hexo);
8-
require('./lib/stellar-tag-utils')(hexo);
9-
require('./lib/render-stylus')(hexo);
13+
configLib(hexo);
14+
stellarTagUtilsLib(hexo);
15+
renderStylusLib(hexo);
1016
if (hexo.theme.config.debug === "env") {
11-
require('./lib/check-environment')(hexo);
17+
checkEnvironmentLib(hexo);
1218
}
1319
});
1420

1521
hexo.on('ready', () => {
16-
const { version } = require('../../package.json');
1722
hexo.log.info(`
1823
============================================================
1924
Volantis ${version}
2025
Docs: https://volantis.js.org/
2126
Repo: https://github.com/volantis-x/hexo-theme-volantis/
2227
============================================================`);
23-
});
28+
});
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,47 @@
11
// 环境配置检查可以避免一些奇葩的报错
22

33
const exec = require('child_process').exec;
4+
5+
function getVersionNumber(str, regex) {
6+
const match = str.match(regex);
7+
return match ? match[1] : null;
8+
}
9+
410
module.exports =(hexo) => {
511
if (!hexo.checkEnvironment) {
6-
hexo.checkEnvironment=1;
12+
hexo.checkEnvironment = 1;
713
hexo.log.info(`Checking environment configuration...`);
814

915
// Checking environment
1016
exec('node -v', (err, stdout, stderr) => {
1117
if (err) {
12-
CheckError(hexo,`node.js: ${err}`);
18+
CheckError(hexo, `node.js: ${err}`);
1319
}
14-
let nodeVersion = stdout.match(/v(\d*)/)[1];
15-
if (nodeVersion<16) {
20+
const nodeVersion = getVersionNumber(stdout, /v(\d*)/);
21+
if (nodeVersion < 16) {
1622
hexo.log.info(`node.js 版本:${stdout}`);
17-
CheckError(hexo,`node.js 版本过低,请升级至 v16.x 及以上版本!`);
18-
}else{
23+
CheckError(hexo, `node.js 版本过低,请升级至 v16.x 及以上版本!`);
24+
} else {
1925
exec('hexo -v', (err, stdout, stderr) => {
2026
if (err) {
21-
CheckError(hexo,`hexo-cli: ${err}`);
27+
CheckError(hexo, `hexo-cli: ${err}`);
2228
}
23-
let hexoVersion1 = stdout.match(/hexo:\s*(\d*)/)[1];
24-
let hexoVersion2 = stdout.match(/hexo:\s*\d*\.(\d*)/)[1];
25-
if (hexoVersion1<5 || (hexoVersion1==5 && hexoVersion2<4)) {
29+
const hexoVersion1 = getVersionNumber(stdout, /hexo:\s*(\d*)/);
30+
const hexoVersion2 = getVersionNumber(stdout, /hexo:\s*\d*\.(\d*)/);
31+
if (hexoVersion1 < 5 || (hexoVersion1 === 5 && hexoVersion2 < 4)) {
2632
hexo.log.info(`hexo 版本:${stdout}`);
27-
CheckError(hexo,`hexo 版本过低,请升级至 5.4 以上版本!`);
28-
}else{
29-
let hexoClVersion1 = stdout.match(/hexo-cli:\s*(\d*)/)[1];
30-
let hexoClVersion2 = stdout.match(/hexo-cli:\s*\d*\.(\d*)/)[1];
31-
if (hexoClVersion1<4 || (hexoClVersion1==4 && hexoClVersion2<1)) {
33+
CheckError(hexo, `hexo 版本过低,请升级至 5.4 以上版本!`);
34+
} else {
35+
const hexoClVersion1 = getVersionNumber(stdout, /hexo-cli:\s*(\d*)/);
36+
const hexoClVersion2 = getVersionNumber(stdout, /hexo-cli:\s*\d*\.(\d*)/);
37+
if (hexoClVersion1 < 4 || (hexoClVersion1 === 4 && hexoClVersion2 < 1)) {
3238
hexo.log.info(`hexo-cli 版本:${stdout}`);
33-
CheckError(hexo,`hexo-cli 版本过低,请升级至 4.1 以上版本!`);
34-
}else{
39+
CheckError(hexo, `hexo-cli 版本过低,请升级至 4.1 以上版本!`);
40+
} else {
3541
// Checking configuration
3642
let checkConfiguration = require('./check-configuration')(hexo);
37-
if (checkConfiguration!==true) {
38-
CheckConfError(hexo,checkConfiguration);
43+
if (checkConfiguration !== true) {
44+
CheckConfError(hexo, checkConfiguration);
3945
}
4046
}
4147
}
@@ -45,7 +51,7 @@ module.exports =(hexo) => {
4551
}
4652
};
4753

48-
function CheckError(hexo,msg) {
54+
function CheckError(hexo, msg) {
4955
hexo.log.error(`
5056
============================================================
5157
环境配置检查失败!| Environment configuration check failed!
@@ -65,7 +71,7 @@ debug: env
6571
throw new Error('环境配置检查失败!| Environment configuration check failed!');
6672
}
6773

68-
function CheckConfError(hexo,msg) {
74+
function CheckConfError(hexo, msg) {
6975
hexo.log.error(`
7076
============================================================
7177
配置文件检查失败!| Configuration check failed!
@@ -77,4 +83,4 @@ debug: env
7783
关闭调试模式主题配置文件设置 debug: false
7884
============================================================`);
7985
throw new Error('配置文件检查失败!| Configuration check failed!');
80-
}
86+
}

scripts/filters/img.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
'use strict';
44

5+
const imgRegex = /<p><img src="(.*?)" alt="(.*?)"\/><\/p>/g;
6+
const imgReplacement = '<div class="img-wrap"><div class="img-bg"><img class="img" src="$1" alt="$2"\/><\/div><span class="image-caption">$2<\/span><\/div>';
7+
58
hexo.extend.filter.register('after_post_render', function(data) {
6-
data.content = data.content.replace(/<p><img src="(.*?)" alt="(.*?)"\/><\/p>/g, '<div class="img-wrap"><div class="img-bg"><img class="img" src="$1" alt="$2"\/><\/div><span class="image-caption">$2<\/span><\/div>');
9+
data.content = data.content.replace(imgRegex, imgReplacement);
710
return data;
8-
});
11+
});

scripts/filters/replace.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
hexo.extend.filter.register('after_render:html', function(data) {
66
if (hexo.theme.config.replace) {
7-
hexo.theme.config.replace.forEach(e => {
8-
let s = e.split(" => ")
9-
let a = s[0]
10-
let b = s[1]
11-
data = data.replace(new RegExp(a,"g"), b);
7+
const replacements = hexo.theme.config.replace.map(e => {
8+
const [a, b] = e.split(" => ");
9+
return { regex: new RegExp(a, "g"), replacement: b };
10+
});
11+
12+
replacements.forEach(({ regex, replacement }) => {
13+
data = data.replace(regex, replacement);
1214
});
1315
}
1416
return data;
15-
},999999999999);
17+
}, 999999999999);

scripts/helpers/getList.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,5 @@
22
// yml 太烂了
33

44
hexo.extend.helper.register('getList', function (list) {
5-
if (!list) return []
6-
if (typeof list == "string") {
7-
return [list]
8-
} else {
9-
return [...list]
10-
}
5+
return list ? (typeof list === "string" ? [list] : [...list]) : [];
116
});

scripts/tags/inline-labels.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
'use strict';
22

3+
function createTag(tagName) {
4+
return function(args) {
5+
return `<${tagName}>${args.join(' ')}</${tagName}>`;
6+
};
7+
}
38

4-
hexo.extend.tag.register('u', function(args) {
5-
return `<u>${args.join(' ')}</u>`;
6-
});
7-
hexo.extend.tag.register('emp', function(args) {
8-
return `<emp>${args.join(' ')}</emp>`;
9-
});
10-
hexo.extend.tag.register('wavy', function(args) {
11-
return `<wavy>${args.join(' ')}</wavy>`;
12-
});
13-
hexo.extend.tag.register('del', function(args) {
14-
return `<del>${args.join(' ')}</del>`;
15-
});
16-
hexo.extend.tag.register('kbd', function(args) {
17-
return `<kbd>${args.join(' ')}</kbd>`;
18-
});
19-
hexo.extend.tag.register('psw', function(args) {
20-
return `<psw>${args.join(' ')}</psw>`;
21-
});
9+
hexo.extend.tag.register('u', createTag('u'));
10+
hexo.extend.tag.register('emp', createTag('emp'));
11+
hexo.extend.tag.register('wavy', createTag('wavy'));
12+
hexo.extend.tag.register('del', createTag('del'));
13+
hexo.extend.tag.register('kbd', createTag('kbd'));
14+
hexo.extend.tag.register('psw', createTag('psw'));

0 commit comments

Comments
 (0)