Hexo 常用命令
Hexo静态博客框架的常用命令整理,按功能分类说明:
项目初始化
安装Hexo
1
npm install -g hexo-cli # 全局安装Hexo命令行工具
创建新博客
1
2
3hexo init myblog # 初始化博客项目
cd myblog # 进入项目目录
npm install # 安装依赖包
写作与生成
新建文章
1
2
3hexo new "文章标题" # 在`source/_posts`生成Markdown文件
hexo new draft "草稿标题" # 创建草稿(生成到`source/_drafts`)
hexo publish "草稿标题" # 将草稿移动到正式文章目录生成静态文件
1
2hexo generate # 生成静态文件到`public`目录(简写:`hexo g`)
hexo g --watch # 监听文件变动并自动重新生成
服务器管理
- 启动本地服务器
1
2
3
4hexo server # 启动服务(默认端口4000,简写:`hexo s`)
hexo s -p 8080 # 指定端口启动
hexo s --draft # 预览时包含草稿文章
sudo rsync -av /root/hexo/public/ /var/www/hexo/public/ #移动静态网页到nginx的网站目录
部署与发布
一键部署(需配置)
1
2hexo deploy # 部署到远程仓库(如GitHub Pages,简写:`hexo d`)
hexo d --generate # 生成后立即部署(等同于 `hexo g -d`)手动部署(直接推送)
1
2
3
4# 生成后手动提交到Git仓库
git add .
git commit -m "更新博客"
git push origin main
主题管理
安装主题
1
2git clone https://github.com/theme-name/theme.git themes/theme-name
# 然后在`_config.yml`中设置:`theme: theme-name`更新主题
1
2cd themes/theme-name
git pull
插件与配置
安装插件
1
npm install hexo-plugin-name --save # 例如:hexo-deployer-git
修改全局配置
1
2
3
4
5
6
7# 编辑根目录下的 _config.yml
title: 我的博客
subtitle: 记录技术与生活
deploy:
type: git
repo: [email protected]:username/username.github.io.git
branch: main
维护与清理
清除缓存
1
hexo clean # 清除缓存和生成的`public`文件
重置数据库
1
2
3# 删除`db.json`文件后重新生成
rm db.json
hexo generate
其他实用命令
列出所有文章
1
hexo list post # 显示所有已发布文章
查看Hexo版本
1
hexo version # 或 `hexo v`
自定义生成器
1
hexo generate --config custom.yml # 使用自定义配置文件
配置文件示例
1 | # _config.yml 关键配置项示例 |
常用工作流
本地写作与预览
1
2hexo new "Hello World" # 新建文章
hexo server --draft # 启动服务(包含草稿)发布到GitHub Pages
1
hexo clean && hexo g -d # 清理缓存、生成并部署