前言

在本教程中,我们将一步步介绍如何使用 Hexo 框架搭建一个 Butterfly 主题的博客,并将其部署到 GitHub Pages。

安装 Hexo

首先,确保你已经安装了 Node.js 和 npm。然后在命令行中运行以下命令来安装 Hexo CLI:

npm install -g hexo-cli
初始化 Hexo 项目
在你希望创建博客的目录下,执行以下命令:

bash
复制代码
# 创建博客文件夹并进入
mkdir my-blog
cd my-blog

# 初始化 Hexo 项目
hexo init
npm install
安装 Butterfly 主题
进入 themes 文件夹,然后克隆 Butterfly 主题仓库:

bash
复制代码
# 进入 themes 文件夹
cd themes

# 克隆 Butterfly 主题仓库
git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git butterfly
配置主题
编辑项目根目录下的 _config.yml 文件,将 theme 设置为 butterfly:

yaml
复制代码
# _config.yml
theme: butterfly
安装主题依赖
返回项目根目录,并运行以下命令安装主题依赖:

bash
复制代码
cd ..
npm install hexo-renderer-pug hexo-renderer-stylus
配置博客信息
编辑项目根目录下的 _config.yml 文件,配置博客的基本信息:

yaml
复制代码
# _config.yml
title: 我的博客
author: 我的名字
language: zh-CN
配置 Butterfly 主题
编辑 themes/butterfly 文件夹下的 _config.yml 文件,按照 Butterfly 文档 进行详细配置。

创建新文章
在项目根目录下运行以下命令创建新文章:

bash
复制代码
hexo new post "我的第一篇文章"
然后编辑 source/_posts 文件夹中的新文章文件,编写你的内容。

本地预览
在项目根目录下运行以下命令启动本地服务器:

bash
复制代码
hexo s
打开浏览器访问 http://localhost:4000 查看博客效果。

部署到 GitHub Pages
配置 GitHub 仓库
创建一个新的 GitHub 仓库,命名为 yourusername.github.io,将 yourusername 替换为你的 GitHub 用户名。

安装部署插件
在项目根目录下运行以下命令安装 Hexo Git 部署插件:

bash
复制代码
npm install hexo-deployer-git --save
配置部署信息
编辑项目根目录下的 _config.yml 文件,添加以下部署配置:

yaml
复制代码
# _config.yml
deploy:
  type: git
  repo: https://github.com/yourusername/yourusername.github.io.git
  branch: main
生成并部署
在项目根目录下运行以下命令生成静态文件并部署到 GitHub:

bash
复制代码
hexo clean
hexo generate
hexo deploy
查看博客
部署完成后,打开浏览器访问 https://yourusername.github.io 查看你的博客。

希望这个教程对你有帮助!如果在任何步骤中遇到问题,请随时联系我。

bash
复制代码

将以上内容保存为一个 Markdown 文件,放入你的 Hexo 博客的 `source/_posts` 文件夹中即可。
怎么生成文章?
ChatGPT
生成文章有以下步骤:

创建新文章
编辑文章内容
生成静态文件
预览或部署文章
以下是详细步骤:

1. 创建新文章
在你的 Hexo 项目根目录下,使用以下命令创建新文章:

bash
复制代码
hexo new post "文章标题"
例如,创建一篇名为 "如何用 Hexo 搭建 Butterfly 主题博客并部署到 GitHub" 的文章:

bash
复制代码
hexo new post "如何用 Hexo 搭建 Butterfly 主题博客并部署到 GitHub"
2. 编辑文章内容
新文章的文件会生成在 source/_posts 目录下,你可以使用任何文本编辑器打开并编辑该文件。以下是生成的 Markdown 文件内容,你可以将之前的教程内容放入其中:

markdown
复制代码
---
title: 如何用 Hexo 搭建 Butterfly 主题博客并部署到 GitHub
date: 2024-07-12
categories:
- 教程
tags:
- Hexo
- Butterfly
- GitHub Pages
---

## 前言

在本教程中,我们将一步步介绍如何使用 Hexo 框架搭建一个 Butterfly 主题的博客,并将其部署到 GitHub Pages。

## 安装 Hexo

首先,确保你已经安装了 Node.js 和 npm。然后在命令行中运行以下命令来安装 Hexo CLI:

```bash
npm install -g hexo-cli
初始化 Hexo 项目
在你希望创建博客的目录下,执行以下命令:

bash
复制代码
# 创建博客文件夹并进入
mkdir my-blog
cd my-blog

# 初始化 Hexo 项目
hexo init
npm install
安装 Butterfly 主题
进入 themes 文件夹,然后克隆 Butterfly 主题仓库:

bash
复制代码
# 进入 themes 文件夹
cd themes

# 克隆 Butterfly 主题仓库
git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git butterfly
配置主题
编辑项目根目录下的 _config.yml 文件,将 theme 设置为 butterfly:

yaml
复制代码
# _config.yml
theme: butterfly
安装主题依赖
返回项目根目录,并运行以下命令安装主题依赖:

bash
复制代码
cd ..
npm install hexo-renderer-pug hexo-renderer-stylus
配置博客信息
编辑项目根目录下的 _config.yml 文件,配置博客的基本信息:

yaml
复制代码
# _config.yml
title: 我的博客
author: 我的名字
language: zh-CN
配置 Butterfly 主题
编辑 themes/butterfly 文件夹下的 _config.yml 文件,按照 Butterfly 文档 进行详细配置。

创建新文章
在项目根目录下运行以下命令创建新文章:

bash
复制代码
hexo new post "我的第一篇文章"
然后编辑 source/_posts 文件夹中的新文章文件,编写你的内容。

本地预览
在项目根目录下运行以下命令启动本地服务器:

bash
复制代码
hexo s
打开浏览器访问 http://localhost:4000 查看博客效果。

部署到 GitHub Pages
配置 GitHub 仓库
创建一个新的 GitHub 仓库,命名为 yourusername.github.io,将 yourusername 替换为你的 GitHub 用户名。

安装部署插件
在项目根目录下运行以下命令安装 Hexo Git 部署插件:

bash
复制代码
npm install hexo-deployer-git --save
配置部署信息
编辑项目根目录下的 _config.yml 文件,添加以下部署配置:

yaml
复制代码
# _config.yml
deploy:
  type: git
  repo: https://github.com/yourusername/yourusername.github.io.git
  branch: main
生成并部署
在项目根目录下运行以下命令生成静态文件并部署到 GitHub:

bash
复制代码
hexo clean
hexo generate
hexo deploy
查看博客
部署完成后,打开浏览器访问 https://yourusername.github.io 查看你的博客。