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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
Cloudbric
Cloudbric
I
InfoQ
V
V2EX
博客园_首页
The Register - Security
The Register - Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
Secure Thoughts
Vercel News
Vercel News
Forbes - Security
Forbes - Security
云风的 BLOG
云风的 BLOG
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
D
DataBreaches.Net
H
Hacker News: Front Page
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog RSS Feed
A
About on SuperTechFans
N
News and Events Feed by Topic
Apple Machine Learning Research
Apple Machine Learning Research
Help Net Security
Help Net Security
Attack and Defense Labs
Attack and Defense Labs
N
Netflix TechBlog - Medium
Spread Privacy
Spread Privacy
F
Full Disclosure
Recorded Future
Recorded Future
AWS News Blog
AWS News Blog
博客园 - 【当耐特】
The Cloudflare Blog
T
Threatpost
T
Tor Project blog
Google DeepMind News
Google DeepMind News
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
M
MIT News - Artificial intelligence
A
Arctic Wolf
C
Check Point Blog
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
WordPress大学
WordPress大学
Cyberwarzone
Cyberwarzone
小众软件
小众软件
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
Security Latest
Security Latest
The Last Watchdog
The Last Watchdog

半方池水半方田

小球称重问题及其引申的思考 蓝色的结构色 病毒是不是生物? 把博客发布交给 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 中的配置
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

只能懂一点点

最新文章

蓝色的结构色