BlockLune's Blog

Home Tags About |

Set up a blog with Hexo

Published at 2023-04-15
Last update over 365 days ago Licensed under CC BY-NC-SA 4.0 hexoblog-setupwebgithub-pagesnext-theme

This is a simple record of my further attempt to set up a blog with Hexo. I built one on GitHub Page before, using Hexo with the Icarus theme. This time I want to try something different. I also cleaned up my old blog and selectively migrated some old posts here.

Hexo

Installation

You need to install Node.js and Git before installing Hexo.

After that, you could run the command below to install Hexo.

npm install -g hexo-cli

The -g flag indicates that Hexo is installed globally.

Learn more in Hexo Docs.

Setup

You need to make a directory <folder> to store your Hexo blog. Here I use fishpond.

hexo init fishpond
cd fishpond
npm install

Learn more in Hexo Docs.

Theme

Before starting to use your blog, you may choose a theme you like.

Hexo themes

Here I choose the NexT theme. To install it, run:

npm install hexo-theme-next --save

After installing your own theme, you could uninstall the default theme hexo-theme-landscape:

npm uninstall hexo-theme-landscape --save

Also, clean the folder of the default theme landscape in theme/.

To enable NexT, open Hexo’s config file and set the variable theme to next.

theme: next

Basic configuration

Hexo configuration

To configure your Hexo blog, modify the _config.yml file.

Learn more in Hexo Docs.

NexT theme configuration

Before configuring, you are recommended to copy the configuration file (so that you don’t change the node module). Run:

cp node_modules/hexo-theme-next/_config.yml _config.next.yml

Then you can follow Configuration | NexT (theme-next.js.org) and customize the NexT theme as your wish.

Plugins

You can install plugins to make your blog more powerful. The plugins are actually all packages managed by npm, so to list all the plugins you have installed, just run:

npm list --depth 0

Here are some of the plugins I’m using:

hexo-abbrlink is a Hexo plugin to generate static post link based on title and data in the post front.

To install it, run:

npm install hexo-abbrlink --save

To configure it, modify _config.yml:

# URLs
permalink: "posts/:abbrlink.html"

# Extensions
## Plugins: https://hexo.io/plugins/
### hexo-abbrlink: https://github.com/rozbo/hexo-abbrlink
abbrlink:
  alg: crc32 # support crc16(default) and crc32
  rep: hex # support dec(default) and hex

hexo-auto-category

hexo-auto-category is a Hexo plugin which generates categories automatically from folder structure for each post.

To install it, run:

npm install hexo-auto-category --save

To configure it, modify _config.yml:

# Extensions
## Plugins: https://hexo.io/plugins/
### hexo-auto-category: https://github.com/xu-song/hexo-auto-category
auto_category:
  enable: true
  depth: 3

hexo-blockquote2note

hexo-blockquote2note is a little plugin that renders blocks of quotes in Markdown to the note tag of the NexT theme. If you also use the NexT theme, it maybe helpful.

To install it, run:

npm install hexo-blockquote2note --save

Preview: Introducing hexo-blockquote2note

hexo-generator-feed

hexo-generator-feed helps generate Atom 1.0 or RSS 2.0 feed.

To install it, run:

npm install hexo-generator-feed --save

To configure it, modify _config.yml. Here is my configuration:

# Extensions
## Plugins: https://hexo.io/plugins/
### hexo-generator-feed: https://github.com/hexojs/hexo-generator-feed
feed:
  enable: true
  type: rss2
  path: rss.xml
  limit: 0
  order_by: -date
  content: false
  content_limit: 100
  content_limit_delim: "<!--more-->"

hexo-generator-searchdb

hexo-generator-searchdb provides a local search service.

To install it, run:

npm install hexo-generator-searchdb --save

hexo-generator-sitemap

hexo-generator-sitemap generates sitemap.

To install it, run:

npm install hexo-generator-sitemap --save

Configuration:

### hexo-generator-sitemap
sitemap:
  path:
    - sitemap.xml
    - sitemap.txt
  tags: false
  categories: false

hexo-pangu

hexo-pangu severs side pangu.js filter for Hexo.

To install it, run:

npm install hexo-pangu --save

Deployment

Manual deployment

hexo-deployer-git helps deploy your blog to your GitHub Page.

To install it, run:

npm install hexo-deployer-git --save

Modify _config.yml:

deploy:
  type: git
  repo: <repository url>
  branch: <branch>
  message: <message>

Learn more in One-Command Deployment | Hexo.

Automatic deployment

Using GitHub Action to Deploy to GitHub Pages

By using GitHub Action, our workflow of writing posts will be like this:

  1. Writes a post.
  2. Uses git push to push your new post to your source repo.
  3. A GitHub Action automatically starts, generating our blog pages.
  4. Your GitHub Page repo updates automatically.

Here are the steps:

  1. Create a new repository to store your source *.md posts. We call this source repo. It is recommended to make it private.

  2. Add a ./.github/workflows/auto_build_and_deploy.yml file. It may look like this:

# Based on https://hexo.io/docs/github-pages.html

name: Auto build and deploy

on:
  push:
    branches:
      - <source_repo_branch>

jobs:
  pages:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: <source_repo_branch>

      - name: Use Node.js
        uses: actions/setup-node@v2
        with:
          node-version: "16" # run `node --version` to show your nodejs version

      - name: Install Hexo
        run: |
          export TZ='Asia/Shanghai'
          npm install -g hexo-cli

      - name: Cache NPM dependencies
        uses: actions/cache@v2
        id: cache
        with:
          path: node_modules
          key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

      - name: Install Dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: npm install --save

      - name: Build
        run: |
          hexo clean
          hexo g

      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          token: <your_token>
          repository-name: <your_github_name>/<your_github_name>.github.io
          branch: <github_page_repo_branch>
          folder: ./public
          commit-message: "${{ github.event.head_commit.message }} Updated By Github Actions"

<your_token>: Generate it here. Make sure that its scope includes workflow.

Learn more in Creating a personal access token - GitHub Docs.

  1. Configure the local git repo.
# initialize
$ git init

# (optional) create a branch in source repo and change to it
$ git checkout -b <source_repo_branch>

# connect to the remote repo
$ git remote add <server_short_name> <server_url>

# add and commit
$ git add .
$ git commit -m "Initial commit"

# push
$ git push -u <server_short_name> <source_repo_branch>
# if you didn't change to another branch, # the default branch is 'main'

Learn more in GitHub Pages | Hexo.

Deploying to Netlify

Go to the create a new site page, select your source repo from GitHub, and follow the prompts.

Learn more in One-Command Deployment | Hexo

Using Vercel

See

Q&A

Where is my index.html in /categories and /tags ?

To generate index.html in /categories, run:

hexo new page "categories"

or

hexo n page "categories"

Then, edit source/categories/index.md like this:

---
title: categories
date: 2023-04-12 00:00:00
type: "categories"
---

You can generate the index.html file in /tags similarly.

Why did the straight quotes in the text automatically become curved quotes?

If you also use marked (the default) renderer, add the config below to your _config.yml:

marked:
  smartypants: false

Also see 6.4.0 版本之后的两个问题 · Issue #462 · theme-next/hexo-theme-next.

Old content

Here are some old content migrated from my previous Icarus blog.

  • Use Bulma to beautify your blog

    See 活用 Bulma 美化 Icarus 文章 - iMaeGoo’s Blog

  • Change fonts of Icarus.

    Modify the <blog_path>/node_modules/hexo-theme-icarus/include/style/base.styl:

    $family-sans-serif ?= Ubuntu, Roboto, 'Open Sans', 'Microsoft YaHei', sans-serif
    $family-code ?= 'Source Code Pro', monospace, 'Microsoft YaHei'
    

Caution

This operation modifies the node module! So it’s NOT recommended.