hexo-dynamic-config
Version:
A hexo plugin for insert variable to configuration.
20 lines (18 loc) • 642 B
JavaScript
const vm = require("vm");
const { deepMerge } = require("hexo-util");
const validateConfig = require("hexo/lib/hexo/validate_config");
hexo.extend.filter.register("after_init", function () {
require("dotenv").config();
const evalConfig = JSON.stringify(this.config, (key, value) => {
if (typeof value === "string" && /\$\{.*\}/.test(value)) {
const correctValue = value.replace(/`/g, "\\`");
return vm.runInNewContext(`\`${correctValue}\``, {
hexo: this,
env: process.env,
});
}
return value;
});
this.config = deepMerge(this.config, JSON.parse(evalConfig));
validateConfig(this);
});