![Hexo + github 博客搭建](/2024/06/03/hexo/image.png)
Hexo + github 博客搭建
前言
本来准备使用django写一个博客网站,但是感觉时间不够用,就先使用Hexo凑合用着吧。
推荐参考:
- https://hexo.io/zh-cn/docs/ # hexo官网
安装
安装 Hexo 相当简单,只需要先安装下列应用程序即可:
- Node.js # 安装参考 https://github.com/nodesource/distributions
- Git
然后即可安装hexo:
npm install -g hexo-cli
初始化
安装 Hexo 完成后,请执行下列命令,Hexo 将会在指定文件夹中新建所需要的文件。
1 | $ hexo init <folder> |
新建完成后,指定文件夹的目录如下:
1 | . |
预览,这一步可以不用管
1 | hexo g # 生成静态网页 |
GitHub 一键部署
-
建立名为 <你的 GitHub 用户名>.github.io 的储存库,若之前已将 Hexo 上传至其他储存库,将该储存库重命名即可。
-
安装 hexo-deployer-git
npm install hexo-deployer-git --save
-
配置 _config.yml
配置 deploy
1 | deploy: |
配置 url:
1 | url: https://johnsmith.github.io/johnsmith.github.io |
-
部署
hexo clean && hexo d
架构
主代码: node_modules/hexo/dist/hexo/index.js
过程
- constructor -> _bindLocals
- init
- ready
- render
- generate
其他
图片文件链接设置
新方法 使用 hexo-renderer-markdown-it
hexo-renderer-marked 难用,又不太行,直接卸载,换 hexo-renderer-markdown-it
再使用以下配置,问题就解决了。
1 | post_asset_folder: true |
1 | 卸载原有的 |
旧方法 使用 hexo-renderer-marked
- 配置 _config.yml
1 | post_asset_folder: true |
- vscode 配置 markdown.copy (paste image 不好用),新增配置项 key 为 *.md , value 为 ${documentBaseName}/${fileName}
- 以上配置之后,hexo 只会识别 ![Alt text](image-1.png) 不会识别 ![Alt text](solitude/image-1.png) ,还需要改代码
hexo/node_modules/hexo-renderer-marked/lib/renderer.js
1 | # 234 行 |
捕获 Hexo 渲染过程中的 LaTeX 警告
使用 hexo-renderer-markdown-it 之后,因为 LaTeX 公式中不支持 Unicode 字符,所以中文会抛出警告:
LaTeX-incompatible input and strict mode is set to 'warn': Unicode text character "等" used in math mode [unicodeTextInMathMode]
hexo 的渲染是在 node_modules/hexo/dist/plugins/filter/before_generate/render_post.js 中进行的,进行以下修改忽略warn。
1 | //修改: |
或者也可以改一下 hexo-renderer-markdown-it ,例如 node_modules/@renbaoshuo/markdown-it-katex/index.js 文件。
1 | //修改: |
创建“分类”选项
-
运行:
hexo new page categories
-
显示以下信息创建成功:
INFO Created: ~/Documents/blog/source/categories/index.md
-
添加 type: “categories” 到 index.md 中,添加后是这样的:
1 | --- |
- 给文章添加 categories 属性, hexo 一篇文章只能属于一个分类,多个分类会嵌套(即该文章属于 web前端 下的 jQuery 分类)
1 | --- |
创建“标签”选项
-
运行:
hexo new page tags
-
显示以下信息创建成功:
INFO Created: ~/Documents/blog/source/tags/index.md
-
添加 type: “tags” 到 index.md 中,添加后是这样的:
1 | --- |
- 给文章添加 tags 属性, hexo 一篇文章可以有多个标签。
1 | --- |