gitHub hexo 个人博客升级版

9年前自己开始学习gitHub hexo搭建个人博客,查了很多资料,最后用hexo 搭建一个个人博客,托管在gitHub上,前段时间换了一个电脑,我在新的电脑上想再发布一篇文章,才发现不行了。因为之前只在GitHub托管了hexo生成的静态文件(public),忘记备份Hexo的源文件。
source/_posts/(所有文章)
_config.yml(Hexo 主配置)
themes/(主题文件)
package.json(依赖列表)
如果你遇到这种情况,跟着我进入接下来的重新部署过程。

gitHub hexo 个人博客基础版

首先确认你本地已经不存在source/_posts文件夹,再查一查github是否有备份,如何远程和本地都没有,那就只能重新部署了。

1.静态文件(如 public/ 下的 HTML)可通过浏览器右键「查看页面源代码」复制正文,或使用工具解析 HTML 结构。

2.使用 Markdown 渲染,HTML 中的 “article ”标签内通常包含原始文本的转换结果。

3.生成.md文件放在_posts文件夹里面。

1.备份Hexo的源文件

到这里就可以重新发布文章了,但是为了下一次不要再出现这种情况,我们需要对Hexo的源文件进行备份。

备份方式共有两种:

1.在当前gitHub管理的hexo生成的静态文件仓库中再开一个分支,用于备份Hexo的源文件。

2.单独创建一个私有仓库用于备份Hexo的源文件。

因为博客需要对外展示,所以当前gitHub管理的hexo生成的静态文件仓库必须是公开的,所以如果你选择第一种方式你的原文件也只能放在公开的仓库分支。如果不想将自己的Hexo的源文件公开就可以选择第二种方式:单独创建一个私有的仓库用来备份Hexo的源文件。

2.自动化

上文创建完成后每次发布博客,都需要去git提交备份文件,这样太麻烦了。我们可以创建一个自动化脚本,将这些重复步骤自动化。

创建一个deploy.sh放在博客根目录:

# 设置绝对路径
BLOG_DIR="实际路径/blog"

# 进入博客目录
cd "$BLOG_DIR" || exit 1

# Hexo操作
hexo clean
hexo generate
hexo deploy

# 源文件备份
git add .
git commit -m "自动备份: $(date +"%Y-%m-%d %H:%M")"
git push origin main

这样一个简单的发布博客并且源文件备份的自动化脚本就好了。
以后每一次写完文章后再在终端进入"实际路径/blog" 执行./deploy.sh 就行了

执行./deploy.sh

首次执行需要先设置执行权限:

chmod +x deploy.sh

3.多博客自动化

当我们有多个博客需要同步是就需要对Hexo的源文件进行再次修改。

我以github为例:

1.首先我们的有两个GitHub账号并且本地配置好SSH

本地SSH配置

2.添加配置文件

_config.yml:为其中一个GitHub账号的配置

url: https://gavincarter1991.github.io# 博客完整URL

source_dir: source # 这个值会被脚本覆盖,但需要存在

public_dir: public_kind

deploy

_config_other.yml:为另外一个GitHub账号的配置

url: https://kindyourself.github.io # 博客完整URL

source_dir: /Users/dianyin/Desktop/blog/temp_source_gavin

public_dir: public_gavin

deploy

考虑到多个博客内容可能不一样:在根目录新建一个scripts文件夹创建一个JS脚本multi-site.js进行内容配置,公共文章放在source/_posts,不同的文章各自放在自己的目录里面(source/_posts_gavin source/_posts_gavin _posts_kind)脚本会分开发布。

multi-site.js 内容:

const fs = require('fs-extra');
const path = require('path');
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);

hexo.extend.filter.register('before_generate', async function() {
  const baseDir = hexo.base_dir;
  const currentSite = process.env.HEXO_SITE || 'kind';
  
  const sites = {
    kind: {
      exclusiveDir: 'source/_posts_kind',
      commonDir: 'source/_posts',
      output: 'public_kind',
      config: '_config.yml'
    },
    gavin: {
      exclusiveDir: 'source/_posts_gavin',
      commonDir: 'source/_posts',
      output: 'public_gavin',
      config: '_config_gavin.yml'
    }
  };
  
  const site = sites[currentSite];
  if (!site) return;
  
  // 创建临时源目录
  const tempSourceDir = path.join(baseDir, `temp_source_${currentSite}`);
  fs.ensureDirSync(tempSourceDir);
  fs.emptyDirSync(tempSourceDir);
  
  // 1. 复制整个source目录(包括所有共享文件)
  fs.copySync(path.join(baseDir, 'source'), tempSourceDir);
  
  // 2. 清空临时源目录的_posts文件夹
  const tempPostsDir = path.join(tempSourceDir, '_posts');
  fs.ensureDirSync(tempPostsDir);
  fs.emptyDirSync(tempPostsDir);
  
  // 3. 复制共同文章
  const commonPosts = path.join(baseDir, site.commonDir);
  if (fs.existsSync(commonPosts)) {
    fs.readdirSync(commonPosts).forEach(file => {
      if (file.endsWith('.md')) {
        fs.copySync(
          path.join(commonPosts, file),
          path.join(tempPostsDir, file)
        );
      }
    });
  }
  
  // 4. 复制专属文章
  const exclusivePosts = path.join(baseDir, site.exclusiveDir);
  if (fs.existsSync(exclusivePosts)) {
    fs.readdirSync(exclusivePosts).forEach(file => {
      if (file.endsWith('.md')) {
        fs.copySync(
          path.join(exclusivePosts, file),
          path.join(tempPostsDir, file)
        );
      }
    });
  }
  
  // 5. 动态应用配置
  hexo.config.source_dir = tempSourceDir;
  hexo.config.public_dir = site.output;
  
  // 6. 验证复制结果
  const postCount = fs.readdirSync(tempPostsDir).length;
  hexo.log.info(`✅ ${currentSite} 站点准备完成: ${postCount} 篇文章`);
});

// 添加部署后清理钩子
hexo.extend.filter.register('after_deploy', function() {
  const baseDir = hexo.base_dir;
  const currentSite = process.env.HEXO_SITE;
  
  if (currentSite) {
    const tempSourceDir = path.join(baseDir, `temp_source_${currentSite}`);
    if (fs.existsSync(tempSourceDir)) {
      fs.removeSync(tempSourceDir);
      hexo.log.info(`🧹 清理临时目录: ${tempSourceDir}`);
    }
  }
});

当然deploy.sh也需要更改为:

#!/usr/bin/env bash

# 配置区
BLOG_DIR="/Users/dianyin/Desktop/blog"
SOURCE_BRANCH="master"
PRIVATE_REPO_KIND="git@github.com-kind:kindyourself/blog-source.git"
PRIVATE_REPO_GAVIN="git@github.com-gavin:gavincarter1991/blog-source.git"

cd "$BLOG_DIR" || { echo "❌ 无法进入博客目录"; exit 1; }

# 清理上次生成的临时文件
rm -rf temp_source_* public*
hexo clean

# 生成 Kind 站点
echo "===== 🚀 生成 Kind 站点 ====="
export HEXO_SITE="kind"
hexo generate --config _config.yml

# 验证生成结果
echo "Kind 站点内容:"
ls -lh public_kind
tree -L 2 public_kind | head -20

# 生成 Gavin 站点
echo "===== 🚀 生成 Gavin 站点 ====="
export HEXO_SITE="gavin"
hexo generate --config _config_gavin.yml

# 验证生成结果
echo "Gavin 站点内容:"
ls -lh public_gavin
tree -L 2 public_gavin | head -20

# 部署到GitHub Pages
echo "===== 🚀 部署到 GitHub Pages ====="
echo "部署 Kind 站点..."
export HEXO_SITE="kind"
hexo deploy --config _config.yml

echo "部署 Gavin 站点..."
export HEXO_SITE="gavin"
hexo deploy --config _config_gavin.yml

# 备份到私有仓库
echo "===== 📦 备份源文件到私有仓库 ====="
git add .
if git diff-index --quiet HEAD --; then
    echo "🔄 无文件变更"
else
    git commit -m "自动备份: $(date +"%Y-%m-%d %H:%M:%S")"
    
    # 推送到两个私有仓库
    git push "$PRIVATE_REPO_KIND" "$SOURCE_BRANCH" && \
        echo "✅ kindyourself/blog-source 备份成功" || \
        echo "❌ kindyourself/blog-source 备份失败"
    
    git push "$PRIVATE_REPO_GAVIN" "$SOURCE_BRANCH" && \
        echo "✅ gavincarter1991/blog-source 备份成功" || \
        echo "❌ gavincarter1991/blog-source 备份失败"
fi

echo "===== ✅ 部署完成 ====="
echo "博客地址1: https://kindyourself.github.io"
echo "博客地址2: https://gavincarter1991.github.io"

以上就是gitHub hexo 个人博客升级版

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容