Hexo 入门

首先,这是我初步尝试使用hexo建立个人博客,并使用github进行部署。关于hexo搭建的教程很多,在bilibili上也有不少教程。

Hexo 搭建

准备工作

  • 下载安装 node.js
    • Hexo 基于 Node.js,需先安装运行环境。
    • 安装时勾选 npm package manager
    • 验证安装
      1
      2
      node -v  # 显示版本号(如 v18.16.0
      npm -v # 显示版本号(如 9.5.1
  • 下载安装 git
    • 目的是版本控制以及部署到github pages

      本地搭建

      1
      2
      3
      4
      5
      $ npm install hexo-cli -g
      $ hexo init blog
      $ cd blog
      $ npm install
      $ hexo server
  1. 安装hexo(安装 Hexo 的核心命令行工具,-g 表示全局安装)
  2. 创建并初始化一个blog的文件夹
    1
    2
    3
    4
    5
    6
    blog/
    ├── _config.yml # 全局配置文件
    ├── package.json # 依赖管理
    ├── scaffolds/ # 模板文件夹
    ├── source/ # 文章和资源
    └── themes/ # 主题文件夹
  3. 进入文件夹
  4. 安装本地依赖
    根据package.json安装Hexo运行所需依赖。
  5. 启动本地服务器
    • sserver的简写。
    • 默认是 http://localhost:4000
    • -p 5000:指定端口(如 hexo s -p 5000)。
    • -l:监听所有网络地址(允许同一局域网设备访问)。

More info: Deployment

主题配置

我使用的主题是Icarus,所以就以这一主题为例进行介绍:

主题安装与设置

我使用NPM将Icarus安装为Node.js模块,这样做的好处是方便,但会损失一定的可定制性。

1
npm install -S hexo-theme-icarus hexo-renderer-inferno

之后在站店(blog文件夹)中的_config.yml文件中更改主题:

1
theme: icarus

或者在命令行中修改:
1
hexo config theme icarus

本地服务器测试:
1
hexo s

主题修改

首先是修改_config.yml:

1
2
3
title: JH's Blog
author: LLLLAAAA
url: https://LLLLAAAA2333.github.io

其次修改_config.icarus.yml中的内容:
修改logo和icon:

1
2
3
4
5
# Path or URL to the website's logo
logo: /img/logo.svg
head:
    # URL or path to the website's icon
    favicon: /img/favicon.svg

修改导航页:

1
2
3
4
5
6
7
8
9
# Page top navigation bar configurations
navbar:
    # Navigation menu items
    menu:
        Home: /
        Archives: /archives
        Categories: /categories
        Tags: /tags
        About: /about

配置comment:

这里我使用的是utterances

A lightweight comments widget built on GitHub issues. Use GitHub issues for blog comments, wiki pages and more!

  1. 准备一个public Repository,这里我建立了一个名为comments的仓库
  2. Install utterances,访问Github Apps - utterances
    utterance
  3. 选择需要安装utterances的用户以及选择特定的仓库,
  4. 若安装成功,网页将跳转到utterances官网。 你可以阅读每个的配置项的说明,并按照配置你的utterances安装。
  5. 完成配置后,转到页面上的”开启utterances“(Enable Utterances)并从utterances的HTML代码中复制属性值到主题配置的对应配置项中。
    例如如下HTML:
    1
    2
    3
    4
    5
    6
    7
    <script src="https://utteranc.es/client.js"  
    repo="Your-GitHub-Username/Your-Public-Repo-Name"
    issue-term="pathname"
    theme="github-light"
    crossorigin="anonymous"
    async>
    </script>
    对应到_config.icarus.yml中即为:
    此处label需要确保Issue中有该label
    1
    2
    3
    4
    5
    6
    comment:
        type: utterances
        repo: LLLLAAAA2333/comments
        issue_term: pathname
        label: comments
        theme: github-light

发布到GitHub Pages

  1. 安装hexo-deployer-git,命令如下:
    1
    npm install hexo-deployer-git --save
  2. GitHub上创建一个新的仓库用于保存网页。
    填写仓库名,格式必须为<用户名>.github.io,然后点击Create repository
  3. 修改_config.yml中的内容:
    1
    2
    3
    4
    deploy:
      type: git # 不要使用github
      repo: git@github.com:your-name/your-name.github.io.git
      branch: main
  4. 生成静态文件并推送到GitHub:
    1
    2
    hexo g
    hexo d
    此时,打开浏览器,访问 http://your-name.github.io ,恭喜你,到此为止你的博客已经建设完毕了。

添加RSS链接

首先添加插件hexo-generator-feed

1
2
3
4
5
#订阅RSS
feed:
type: atom
path: atom.xml
limit: false

配置含义:

1
2
3
4
5
6
7
8
9
type: RSS的类型(atom/rss2)  
path: 文件路径,默认是 atom.xml/rss2.xml
limit: 展示文章的数量,使用 0 或则 false 代表展示全部
hub: URL of the PubSubHubbub hubs (如果使用不到可以为空)
content: (可选)设置 true 可以在 RSS 文件中包含文章全部内容,默认:false
content_limit: (可选)摘要中使用的帖子内容的默认长度。 仅在内容设置为false且未显示自定义帖子描述时才使用。
content_limit_delim: (可选)如果content_limit用于缩短post内容,则仅在此分隔符的最后一次出现时进行剪切,然后才达到字符限制。默认不使用。
icon: (可选)自定义订阅图标,默认设置为主配置中指定的图标。
order_by: 订阅内容的顺序。 (默认: -date)

之后再应用主题的_config.yml中添加

1
rss: /atom.xml

我同时更改了博客提供的rss链接:
1
2
3
RSS:
icon: fas fa-rss
url: https://llllaaaa2333.github.io/atom.xml

配置好后运行hexo g即可在public文件夹下就可以看到atom.xml这个文件了。

https://llllaaaa2333.github.io/atom.xml 即可用于进行RSS订阅,比如应用到Follow中:
Follow subscribe

可以后续使用Follow认证:
Authentication
这里选择RSS标签,复制XML 格式的内容,之后在文件:node_modules\hexo-generator-feed\atom.xml中将其粘贴到适当位置:
插入复制的内容
之后重新部署即可认证。

参考资料

  1. Hexo
  2. 从零开始搭建 Hexo 博客简明教程(2024版)
  3. Icarus快速上手
  4. Hexo Icarus 主题设置
  5. Icarus用户指南 - 用户评论插件
  6. 为Hexo添加RSS订阅
  7. 3分钟 让Follow认证我的Hexo博客订阅源
Author

LLLLAAAA

Posted on

2025-01-28

Updated on

2025-02-20

Licensed under

Comments