/sRect.github.io

hexo blog

Primary LanguageShellGNU Affero General Public License v3.0AGPL-3.0

Hexo 快速上手

Hexo 是一个快速、简洁且高效的博客框架。可以使用 Markdown 写文章,方便高效。

1. 安装

npm install -g hexo-cli

2. 初始化

hexo init <project-name>
cd <project-name>
npm install

3. 主要 api

  1. 创建新文章
hexo new <title>
  • source/_posts/下新建

例如创建 foo 页,在source/_posts/目录下就会多出一个foo.md文件,就在那里面写文章

hexo new foo
  • source下新建其它的目录

例如在source/about/下创建bar.md文件

hexo new page --path about/bar "bar"
  1. 生成静态文件

项目根目录会多出一个public文件夹,就是编译过后的html静态文件

hexo generate
  1. 清除缓存

把上面生成的public文件夹删掉

hexo clean
  1. 启动服务

默认情况下,http://localhost:4000访问

hexo server

4. 安装心仪的主题

  1. hexo themes

这么多主题,总有一款适合你

  1. 本文选择的主题hexo-theme-fluid
  • 主题安装
npm install --save hexo-theme-fluid

然后在博客目录下创建 _config.fluid.yml,将主题的 _config.yml 内容复制进去

  • 配置主题

修改 Hexo 博客目录中的 _config.yml:

theme: fluid  # 指定主题

language: zh-CN  # 指定语言,会影响主题显示的语言,按需修改

5. github pages 部署

  1. 本地生成 ssh 密钥对
ssh-keygen -t rsa -C "用户名@example.com "
  1. 在仓库Settings > Deploy Keys中添加公钥内容,并勾选访问权限,最后确定

  2. 在仓库Settings > Secrets中添加私钥,key 为DEPLOY_KEY,内容为私钥内容

  3. 项目_config.yml配置文件和主题配置文件配置deploy字段

deploy:
  type: git
  repo: <你的github仓库 SSH下载链接>
  branch: gh-pages
  1. .github/workflows/deploy.yml
name: Hexo Github Pages Deploy

# https://github.com/marketplace/actions/hexo-action?version=v1.0.4

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    name: A job to deploy blog.
    steps:
      - name: Checkout
        uses: actions/checkout@v1
        with:
          submodules: true # Checkout private submodules(themes or something else).

      # Caching dependencies to speed up workflows. (GitHub will remove any cache entries that have not been accessed in over 7 days.)
      - name: Cache node modules
        uses: actions/cache@v1
        id: cache
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - name: Install Dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: npm ci

      # Deploy hexo blog website.
      - name: Deploy
        id: deploy
        uses: sma11black/hexo-action@v1.0.3
        with:
          deploy_key: ${{ secrets.DEPLOY_KEY }}
          user_name: <你的github用户名> # (or delete this input setting to use bot account)
          user_email: <你的github邮箱> # (or delete this input setting to use bot account)
          commit_msg: ${{ github.event.head_commit.message }} # (or delete this input setting to use hexo default settings)
      # Use the output from the `deploy` step(use for test action)
      - name: Get the output
        run: |
          echo "${{ steps.deploy.outputs.notify }}"

6. 参考资料

  1. Hexo 官方文档
  2. Hexo Fluid 主题用户手册
  3. Hexo Github Action