惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
博客园 - 司徒正美
D
Darknet – Hacking Tools, Hacker News & Cyber Security
量子位
N
News | PayPal Newsroom
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LINUX DO - 热门话题
有赞技术团队
有赞技术团队
V
Visual Studio Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Project Zero
Project Zero
B
Blog RSS Feed
J
Java Code Geeks
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
Cyberwarzone
Cyberwarzone
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
博客园 - 【当耐特】
Latest news
Latest news
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
博客园_首页
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta
D
Docker
Forbes - Security
Forbes - Security
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
The Last Watchdog
The Last Watchdog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threatpost
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
Webroot Blog
Webroot Blog

半方池水半方田

小球称重问题及其引申的思考 蓝色的结构色 病毒是不是生物? 把博客发布交给 GitHub Actions 省外居民身份证的补换领(苏州) hexo-filter-titlebased-link:构建你的数字花园 Vercel 应用实践学习 通过与 Keycloak 配合实现博客文章的受限访问功能 非公开的文章 Soft Mode 非公开的文章 Strict Mode 试试用代码块在 Hexo 中插入实况照片 动态图片测试 Hexo-Butterfly 主题 Preloader 加载页定制 Keycloak 的部署以及 Hexo-Butterfly 网页应用的接入 随机访问文章的实现(Sitemap+本地缓存优化) Authentik 的部署记录 Waline 自建 Auth 认证 记一次行李托运理赔流程(南航) Docker 镜像的制作、拉取与运行 Hexo-Butterfly 主题中对 Algolia 搜索框 Power By 的定制 设计原则 VSCode 中的配置 Spring AI 中使用 PGVector 实现向量存储 提示词工程速查手册 2025 WePlay 上海两日游 和随机数生成相关的题目 一起撸串(字符串) 树状数组上手了就十分简单 缓存置换算法的实现与 Java 中相关的数据结构 关于海量数据的若干问题 访问者模式填补单分派语言的缺陷 备忘录模式:拍下照片
Hexo 动态加载配置(钩子函数)
wuanqin · 2026-02-28 · via 半方池水半方田

|总字数:938|阅读时长:3分钟|浏览量:|

|出链:0|入链:1

在捣鼓自己的主题时,有一些时候会在主题配置文件(或 Hexo 配置文件)中插入大量的 HTML 代码。虽然功能是实现了,但是会导致很多问题:

  • 配置文件冗长
  • HTML 内容无代码高亮,不方便格式化
  • 拓展性

为了解决这个问题,我的想法是通过注册钩子函数实现主题配置的动态加载。

编写脚本并注册钩子函数

默认地,Hexo 会在 Hexo 命令执行时加载博客项目 scripts 文件夹下的所有脚本。

请不要在 scripts 文件夹下放置执行不了的非脚本文件。

我们在 scripts 文件夹中编写 dynamic_config.js 脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs');
const path = require('path');








function loadHtmlToConfig({filePath, configPath, failureMsg}) {
try {

const fullPath = path.join(hexo.base_dir, filePath);


if (!fs.existsSync(fullPath)) {
throw new Error(`文件不存在:${fullPath}`);
}


const htmlContent = fs.readFileSync(fullPath, 'utf8');


setNestedConfig(hexo.theme.config, configPath, htmlContent);


hexo.log.debug(`✅ 动态配置加载成功 ${configPath}`);
} catch (error) {

hexo.log.warn(`⚠️ ${failureMsg}${error.message},使用主题默认内容`);
}
}







function setNestedConfig(obj, path, value) {
const keys = path.split('.');
const lastKey = keys.pop();


const targetObj = keys.reduce((acc, key) => {
if (!acc[key]) acc[key] = {};
return acc[key];
}, obj);


if (targetObj[lastKey]) {
targetObj[lastKey] = value;
}
}


hexo.extend.filter.register('before_generate', () => {


const dynamicConfigList = [
{
filePath: 'source/_data/dynamic_configs/announcement.html',
configPath: 'aside.card_announcement.content',
failureMsg: '读取侧边栏公告HTML文件失败'
},
{
filePath: 'source/_data/dynamic_configs/footer.html',
configPath: 'footer.custom_text',
failureMsg: '读取页脚自定义文本HTML文件失败'
}






];


dynamicConfigList.forEach(loadHtmlToConfig);

}, 1);

根据上面代码的提示,自行修改或拓展勾子函数 hexo.extend.filter.register 即可。比如像第一个 aside 的例子。原本我们在 _config.butterfly.yml 的某项配置为:

1
2
3
4
5
6
7
aside:
card_announcement:
enable: true
content: |
很长很长很长的内容
......
有很多很多行

我们可以把这些内容剪切出来放在某一个位置里,比如将内容粘贴到 source/_data/dynamic_configs/announcement.html 文件中即可。 failureMsg 随意写,用于在日志中给出相应的提示。

经测试,这个动态配置甚至可以热更新,HTML 内容修改后会实时反馈到页面中。当然,目前这个脚本还在试用中,如有任何问题,欢迎在评论区进行交流和讨论。

本文参考

版权声明 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 半方池水半方田

封面生成Seedream 4.5

Prompt生成文章封面图,主题为「Hexo的动态配置」,卡通扁平漫画风,包含六角形Hexo图标,中间的六角形颜色换为Hexo标志Logo的蓝色,元素简洁有活力,比例「4:3」。浅色背景色,附浅色漂浮纹理。

赞助

  • 微信

    微信

  • 支付宝

    支付宝


avatar

wuanqin

只能懂一点点

最新文章

蓝色的结构色