🛠️ hexo scripts

This commit is contained in:
shenyu 2021-05-24 11:28:56 +08:00
parent 1c857a0c5f
commit 0163b6bfe5
5 changed files with 120 additions and 0 deletions

11
scripts/events/index.js Normal file
View File

@ -0,0 +1,11 @@
/* global hexo */
"use strict";
hexo.on("generateBefore", () => {
require("./lib/merge-configs")(hexo);
});
hexo.on("generateAfter", () => {
require("./lib/hello")(hexo);
});

View File

@ -0,0 +1,38 @@
"use strict";
module.exports = (hexo) => {
const isZh = hexo.theme.i18n.languages[0].search(/zh-CN/i) !== -1;
if (isZh) {
hexo.log.info(`
------------------------------------------------
| |
| __ ________ _____ |
| /\\\\ \\ / / ____| __ \\ |
| / \\\\ \\_/ /| |__ | |__) | |
| / /\\ \\\\ / | __| | _ / |
| / ____ \\| | | |____| | \\ \\ |
| /_/ \\_\\_| |______|_| \\_\\ |
| |
| 感谢使用 Ayer 主题 ! |
| 文档: https://shen-yu.gitee.io/2019/ayer/ |
| |
------------------------------------------------
`);
} else {
hexo.log.info(`
----------------------------------------------------
| |
| __ ________ _____ |
| /\\\\ \\ / / ____| __ \\ |
| / \\\\ \\_/ /| |__ | |__) | |
| / /\\ \\\\ / | __| | _ / |
| / ____ \\| | | |____| | \\ \\ |
| /_/ \\_\\_| |______|_| \\_\\ |
| |
| Thank you for using Ayer theme ! |
| Docs: https://github.com/Shen-Yu/hexo-theme-ayer |
| |
----------------------------------------------------
`);
}
};

View File

@ -0,0 +1,28 @@
"use strict";
const objUtil = require("../../utils/object");
const { isNotEmptyObject } = require("../../utils/object");
module.exports = (hexo) => {
const isZh = hexo.theme.i18n.languages[0].search(/zh-CN/i) !== -1;
if (isNotEmptyObject(hexo.config.theme_config)) {
hexo.theme.config = objUtil.merge(
{},
hexo.theme.config,
hexo.config.theme_config
);
if (isZh) {
hexo.log.info("[Ayer] 读取 _config.yml 中 theme_config 配置项覆盖配置");
} else {
hexo.log.info(
"[Ayer] Merge theme config from theme_config in _config.yml"
);
}
}
hexo.log.debug(
"[Ayer] Output theme config:\n",
JSON.stringify(hexo.theme.config, undefined, 2)
);
};

View File

@ -0,0 +1,12 @@
'use strict';
const joinPath = function(base, relative) {
if (relative && /^https*:\/\//.test(relative)) {
return relative;
}
return relative
? base.replace(/\/+$/, '') + '/' + relative.replace(/^\/+/, '')
: base;
};
module.exports = joinPath;

31
scripts/utils/object.js Normal file
View File

@ -0,0 +1,31 @@
'use strict';
const isObject = (obj) => {
return obj && typeof obj === 'object' && !Array.isArray(obj);
};
const isNotEmptyObject = (obj) => {
return obj && typeof obj === 'object' && Object.getOwnPropertyNames(obj).length !== 0;
};
const merge = (target, ...sources) => {
for (const source of sources) {
for (const key in source) {
if (!Object.prototype.hasOwnProperty.call(source, key)) {
continue;
}
if (isObject(target[key]) && isObject(source[key])) {
merge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
}
return target;
};
module.exports = {
isObject,
isNotEmptyObject,
merge
};