From 0163b6bfe51dac96a28c4ef736986a74c483d672 Mon Sep 17 00:00:00 2001 From: shenyu <448766534@qq.com> Date: Mon, 24 May 2021 11:28:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20hexo=20scripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/events/index.js | 11 +++++++++ scripts/events/lib/hello.js | 38 +++++++++++++++++++++++++++++ scripts/events/lib/merge-configs.js | 28 +++++++++++++++++++++ scripts/utils/join-path.js | 12 +++++++++ scripts/utils/object.js | 31 +++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 scripts/events/index.js create mode 100644 scripts/events/lib/hello.js create mode 100644 scripts/events/lib/merge-configs.js create mode 100644 scripts/utils/join-path.js create mode 100644 scripts/utils/object.js diff --git a/scripts/events/index.js b/scripts/events/index.js new file mode 100644 index 0000000..9587ed6 --- /dev/null +++ b/scripts/events/index.js @@ -0,0 +1,11 @@ +/* global hexo */ + +"use strict"; + +hexo.on("generateBefore", () => { + require("./lib/merge-configs")(hexo); +}); + +hexo.on("generateAfter", () => { + require("./lib/hello")(hexo); +}); diff --git a/scripts/events/lib/hello.js b/scripts/events/lib/hello.js new file mode 100644 index 0000000..dba62e5 --- /dev/null +++ b/scripts/events/lib/hello.js @@ -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 | +| | +---------------------------------------------------- +`); + } +}; diff --git a/scripts/events/lib/merge-configs.js b/scripts/events/lib/merge-configs.js new file mode 100644 index 0000000..2d6936d --- /dev/null +++ b/scripts/events/lib/merge-configs.js @@ -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) + ); +}; diff --git a/scripts/utils/join-path.js b/scripts/utils/join-path.js new file mode 100644 index 0000000..74d6b43 --- /dev/null +++ b/scripts/utils/join-path.js @@ -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; diff --git a/scripts/utils/object.js b/scripts/utils/object.js new file mode 100644 index 0000000..dcb2d62 --- /dev/null +++ b/scripts/utils/object.js @@ -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 +};