插件管理指南
插件系统概述
Hexo 拥有强大的插件系统,可以轻松扩展功能而无需修改核心代码。插件分为两种类型:
| 类型 | 说明 | 适用场景 |
|---|
| Script | 简单脚本 | 快速添加小功能 |
| Plugin | 完整插件包 | 复杂功能或发布到 NPM |
本项目已安装插件
核心插件
| 插件 | 版本 | 用途 |
|---|
| hexo | ^8.0.0 | Hexo 核心 |
| hexo-server | ^3.0.0 | 本地开发服务器 |
| hexo-renderer-ejs | ^2.0.0 | EJS 模板渲染 |
| hexo-renderer-marked | ^7.0.0 | Markdown 渲染 |
| hexo-renderer-stylus | ^3.0.1 | Stylus 样式渲染 |
生成器插件
| 插件 | 版本 | 用途 |
|---|
| hexo-generator-index | ^4.0.0 | 首页生成 |
| hexo-generator-archive | ^2.0.0 | 归档页生成 |
| hexo-generator-category | ^2.0.0 | 分类页生成 |
| hexo-generator-tag | ^2.0.0 | 标签页生成 |
| hexo-generator-sitemap | ^3.0.1 | Sitemap 生成 |
| hexo-generator-searchdb | ^1.5.0 | 本地搜索索引 |
功能插件
| 插件 | 版本 | 用途 |
|---|
| hexo-abbrlink | ^2.2.1 | 文章短链接生成 |
| hexo-prism-plugin | ^2.3.0 | 代码高亮 |
| hexo-deployer-git | ^4.0.0 | Git 部署 |
优化插件
| 插件 | 版本 | 用途 |
|---|
| hexo-clean-css | ^2.0.0 | CSS 压缩 |
| hexo-html-minifier | ^1.0.0 | HTML 压缩 |
| hexo-uglify | ^2.0.0 | JavaScript 压缩 |
主题
| 插件 | 版本 | 用途 |
|---|
| hexo-theme-butterfly | ^5.5.4 | Butterfly 主题 |
| hexo-theme-landscape | ^1.0.0 | 默认主题 |
插件操作
安装插件
1 2 3 4 5 6 7 8
| npm install 插件名称 --save
yarn add 插件名称
npm install hexo-wordcount --save
|
卸载插件
1 2 3 4 5
| npm uninstall 插件名称 --save
yarn remove 插件名称
|
更新插件
1 2 3 4 5 6 7 8
| npm update hexo-abbrlink
npm update
npm outdated
|
查看已安装插件
1 2 3 4 5
| npm list --depth=0
npm list | grep hexo
|
常用推荐插件
SEO 相关
1 2 3 4 5 6 7 8
| npm install hexo-generator-sitemap --save
npm install hexo-generator-feed --save
npm install hexo-helper-open-graph --save
|
内容相关
1 2 3 4 5 6 7 8
| npm install hexo-wordcount --save
npm install hexo-reading-time --save
npm install hexo-lazyload --save
|
样式相关
1 2 3 4 5
| npm install hexo-font-awesome --save
npm install hexo-renderer-math --save
|
工具相关
1 2 3 4 5
| npm install hexo-imagemin --save
npm install hexo-generator-md5 --save
|
插件配置
abbrlink 配置
1 2 3 4
| abbrlink: alg: crc32 rep: dec
|
搜索配置
1 2 3 4 5 6
| search: path: search.xml field: post format: html limit: 10000
|
Sitemap 配置
1 2 3 4
| sitemap: path: sitemap.xml rel: false
|
代码高亮配置
1 2 3 4 5 6
| syntax_highlighter: prismjs prismjs: preprocess: true line_number: true tab_replace: ''
|
插件开发
Script 类型
在 scripts/ 目录下创建 JavaScript 文件:
1 2 3 4 5 6 7 8
| hexo.on('generate:before', function() { console.log('开始生成...'); });
hexo.on('generate:after', function() { console.log('生成完成!'); });
|
Plugin 类型
创建独立的 npm 包:
1 2 3
| hexo-my-plugin/ ├── index.js └── package.json
|
package.json 最少需要:
1 2 3 4 5
| { "name": "hexo-my-plugin", "version": "0.0.1", "main": "index" }
|
可用事件
| 事件 | 说明 |
|---|
init | Hexo 初始化 |
generate:before | 生成前 |
generate:after | 生成后 |
deploy:before | 部署前 |
deploy:after | 部署后 |
server | 服务器启动 |
插件冲突排查
常见冲突
多个 Markdown 渲染器
- 只保留一个:hexo-renderer-marked
多个代码高亮插件
- 选择一个:hexo-prism-plugin 或 highlight.js
多个部署插件
排查步骤
1 2 3 4 5 6 7 8 9 10 11
| hexo clean
hexo server --debug
npm uninstall 插件名称
hexo server
|
插件性能影响
构建速度优化
1 2 3 4 5
| ignore: - source/**/*.psd - source/**/*.ai - source/**/*.svg
|
按需加载
某些插件支持按页面加载:
注意事项
- Node.js 版本:使用 22,不要使用 24
- 插件兼容性:安装前检查是否支持 Hexo 8
- 主题兼容性:某些插件需要主题支持
- 性能影响:过多插件会影响构建速度
- 安全更新:定期更新插件以修复安全漏洞
下一步