hexo一键部署脚本与优化

前言

接前hexo初步搭建,本文将介绍如何使用一键部署脚本,并对其进行博客优化。

butterfly主题优化

主题优化

1,下载butterfly主题

1
git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly

2,修改主题配置文件

在博客主文件夹创建_config.butterfly.yml文件,并修改主题配置文件_config.yml,将主题修改为butterfly。

1
2
3
4
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: butterfly

并且进入node_modules,找到hexo-theme-butterfly文件夹,将_config.yml文件复制到博客主文件夹的_config.butterfly.yml

这个时候,你的博客应该已经成功切换到butterfly主题了。

使用hexo命令启动本地服务器,访问http://localhost:4000,即可预览你的博客。

1
2
3
hexo cl
hexo g
hexo s

3,主题优化

3.1,设置头像

_config.butterfly.yml中,找到avatar,将url修改为你的头像链接。

3.2,设置博客信息
1
2
3
4
5
6
7
8
# Site
title: 迷路的小朋友 # 博客标题
subtitle: # 博客副标题
description: # 博客描述
keywords: # 博客关键词
author: 欣冻 # 作者
language: zh-CN # 语言
timezone: Asia/Shanghai # 时区
3.3,设置博客背景

_config.butterfly.yml中,找到background_image,将url修改为你的背景链接。

这样你的博客基础优化完成,更深度优化可以参考官方文档
另外,这位大佬的博客有更多关于butterfly主题的优化,感谢大佬。

一键部署脚本

1,创建一键部署脚本

在博客主文件夹创建python脚本,名字随意(建议一键部署.py):

2,脚本内容

2.1,引入库
1
2
3
import os
import time
import subprocess
2.2,定义要执行的命令
1
2
# 定义要执行的命令
commands = ['hexo cl', 'hexo g', 'hexo d']
2.3,执行命令
2.3.1,设置PowerShell执行策略
1
2
3
4
5
6
poewr_state=subprocess.run(['powershell','-Command','Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned'], shell=True)

if poewr_state.returncode == 0:
print('PowerShell 执行策略已设置为 RemoteSigned')
else:
print('PowerShell 执行策略设置失败')
2.3.2,循环执行命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 执行命令
# 进入博客目录
os.chdir('E:/BlogFloder')

for command in commands:
print(f'执行命令: {command}')
# 执行命令
poewr_state=subprocess.run(command, shell=True)
if poewr_state.returncode == 0:
print('命令执行成功!')
else:
print('命令执行失败!')
print('------------------------')

if poewr_state.returncode == 0:
print(f'部署完成!用时:{time.time() - start_time}秒')

3,运行脚本

在命令行中,进入博客主文件夹,执行脚本:

1
python 一键部署.py

即可一键部署博客。