Hexo+Github-Pages搭建个人博客

Windows安装Node.js

安装Node.js

  1. Node.js下载地址: https://nodejs.org/en
  2. 测试是否安装成功: npm -v

配置模块和cache路径

  1. 全局模块路径: npm config set prefix "D:\software\nodejs\node_global"
  2. cache路径: npm config set cache "D:\software\nodejs\node_cache"
  3. 查看配置: npm config list

安装包到全局路径

  1. 安装包: npm install @vue/cli -g (将全局模块路径添加到环境变量才能使得安装的包直接使用)

安装Hexo并配置站点

安装Hexo

以下安装和发布等操作均在Git Bash中执行

  1. 安装Hexo: npm install hexo -g
  2. 查看Hexo版本: hexo -v
  3. 初始化Hexo: hexo init

修改站点信息及发布配置

  1. Github创建public仓库: 仓库名为username.github.io
  2. 修改站点_config.yml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # Site
    title: Blog
    subtitle: ''
    description: ''
    keywords:
    author: test
    language: en
    timezone: ''

    post_asset_folder: true #创建文章时自动创建文章同名目录用来存放文章中的图片,插入图片时使用图片名插入不需带路径

    deploy:
    type: git
    repo: https://github.com/username/username.github.io.git
    branch: master

更换主题

本例以melody主题为例,Hexo版本<5.0

  1. git clone -b master https://github.com/Molunerfinn/hexo-theme-melody themes/melody
  2. npm install hexo-renderer-pug hexo-renderer-stylus
  3. 创建目录source/_data,将melody的配置文件拷贝至该目录并重命名为melody.yml
  4. 修改站点_config.yml文件的theme为melody

Hexo使用

  • 新建文章: hexo new "article" (也可以直接在source/_posts目录下新建article.md)
    • 文章中插入图片:
      • 设置post_asset_folder: true, ![](image.png)
      • 创建目录source/images, ![](/images/image.png)
  • 新建页面: hexo new page categories
    • 在source/categories/index.md中添加type: “categories”即可添加分类属性,此时在文章中可使用categories属性添加分类
      1
      2
      3
      4
      5
      6
      ---
      title: Hexo+Github-Pages搭建个人博客
      date: 2023-02-17 15:59:33
      categories: Hexo
      password: hexo
      ---
    • 文章加密: 安装npm install --save hexo-blog-encrypt,在文章头信息添加password
  • 预览站点: hexo server/hexo s (hexo s --debug可以测试站点,站点默认地址是http://localhost:4000/)
  • 清除缓存: hexo clean/hexo c
  • 生成待发布文件: hexo generate/hexo g
  • 部署: hexo deploy/hexo d
    • 部署到Github需要安装依赖: npm install hexo-deployer-git --save
    • 部署到Github: hexo c && hexo g && hexo d
    • 访问: https://username.github.io/

melody主题设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#设置导航栏
menu:
Home: /
Archives: /archives
Categories: /categories
#设置主页的社交链接
social:
github fa: https://github.com/username
#设置个人头像
avatar: /images/avatar.png
#首页文章内容自动截取,点击阅读更多或通过在文章中加入<!-- more -->达到同样效果
auto_excerpt:
enable: true
length: 150
#关注按钮
follow:
enable: true
url: 'https://github.com/username'
text: 'Follow'

*具体主题设置参考: https://molunerfinn.com/hexo-theme-melody-doc/zh-Hans/

Author: Giftbear
Link: https://giftbear.github.io/2022/10/21/Hexo+Github-Pages搭建个人博客/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.