Hexo 使用

Hexo 是一个静态博客站点生成工具,可以将 Markdown 格式的文档转换成静态页面,非常适合用来做个人技术博客。

使用

  1. 安装 Hexo:

    mkdir MyBlog && cd MyBlog
    npx hexo init  # 将当前目录初始化为博客站
    
  2. 创建新文章:

    hexo new "Article Title"
    
  3. 在本地服务器上预览博客:

    npx hexo server
    
  4. 生成静态文件:

    npx hexo generate  # 生成静态页面到 public 目录
    
  5. 部署到生产环境:

    npx hexo deploy
    

配置生产环境

在使用 deploy 命令之前,你需要先配置生产环境。

  1. 修改 Hexo 配置文件。

    编辑 _config.yml,将其中的 URL 改为 https://USERNAME.github.io/MyBlog。其中 USERNAME 是你的 GitHub 用户名。

    url: https://USERNAME.github.io/MyBlog
    
  2. 创建 GitHub Actions 配置文件

    mkdir .github/workflows
    vim .github/workflows/pages.yml
    
    name: Pages
    
    on:
      push:
        branches:
          - main  # default branch
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
            with:
              token: ${{ secrets.GITHUB_TOKEN }}
              # If your repository depends on submodule, please see: https://github.com/actions/checkout
              submodules: recursive
          - name: Use Node.js 20
            uses: actions/setup-node@v4
            with:
              # Examples: 20, 18.19, >=16.20.2, lts/Iron, lts/Hydrogen, *, latest, current, node
              # Ref: https://github.com/actions/setup-node#supported-version-syntax
              node-version: "20"
          - name: Cache NPM dependencies
            uses: actions/cache@v4
            with:
              path: node_modules
              key: ${{ runner.OS }}-npm-cache
              restore-keys: |
                ${{ runner.OS }}-npm-cache
          - name: Install Dependencies
            run: npm install
          - name: Build
            run: npm run build
          - name: Upload Pages artifact
            uses: actions/upload-pages-artifact@v3
            with:
              path: ./public
      deploy:
        needs: build
        permissions:
          pages: write
          id-token: write
        environment:
          name: github-pages
          url: ${{ steps.deployment.outputs.page_url }}
        runs-on: ubuntu-latest
        steps:
          - name: Deploy to GitHub Pages
            id: deployment
            uses: actions/deploy-pages@v4
    

    将文件中的 20 改为你实际使用的 Node.js 的版本。

  3. 在 GitHub 上建立新仓库并命名为 MyBlog

  4. 编辑仓库配置,打开 Settings > Pages,并将 Source 改为 GitHub Actions

  5. 将本地项目上传到 GitHub。

  6. 打开 https://USERNAME.github.io/MyBlog 查看效果。

参考:Hexo 文档

posted @ 2024-07-02 02:01  Undefined443  阅读(9)  评论(0编辑  收藏  举报