前言

目前有这样的场景,博主有两个域名,两个域名都是备案的个人博客,网站的页脚需要带上备案信息,但是我不想写两个博客,那怎么才能让Hexo生成两个内容相同但是页脚内容不同的博客网站呢?

之前为了体验新主题的样式,写过一篇一个Hexo博客两个主题的方案,但是这样会有一些问题

例如:一个主题特有的语法,另一个主题不支持,导致博客生成失败。

所以,现在直接一个主题,弄成两个配置呢,这样就不会出现编译问题了。

本质就是修改下主题配置然后再重新编译一下,改进点就是能保存你的多配置,不用每次都进行修改,下面记录下。

开始

首先,根据Hexo-使用代替配置文件得知:可使用hexo server --config custom.yml,custom2.yml的命令指定编译的配置文件,并且如果不同的配置文件中有相同的配置,那位于后面的配置文件内容会覆盖前面的配置文件内容。

主题的配置跟Hexo的配置是分离的,根据Hexo-使用代替主题配置文件得知:需要定义在theme_config中才能覆盖主题的配置

定义配置

相信你博客的配置已经配好了,一个是Hexo的配置_config.yml,一个是主题的配置,例如我使用的是butterfly主题,这里的主题配置文件就是_config.butterfly.yml,这就是第一个博客站点的配置。

第二个博客站点的配置,你只需要在第一个博客站点的配置上做修改,新建一个_config.site2.yml(名字任意),编辑好后就可以直接拼接在hexo server --config _config.yml,_config.site2.yml,就可以进行覆盖了。

例如,我的覆盖配置如下,主要修改了Butterfly主题的页脚ICP备案信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 冲突配置
title: "InsectMk的个人空间"
subtitle: "今天特别开心!"
description: "每天都要微笑" # 签名
keywords:
- "个人博客"
- "InsectMk"
author: "InsectMk"
language: zh-CN
timezone: Asia/Shanghai #中国的时区
email: "xxxxxxx@qq.com"

# 多博客区分配置
url: https://insectmk.top
public_dir: public_top
theme: butterfly
theme_config:
# --------------------------------------
# Footer Settings 页脚设置
# --------------------------------------
footer:
owner:
enable: true
since: 2022
# 页脚添加信息,这里添加ICP备案号
custom_text: >-
<p>
<a style="margin-inline:5px" target="_blank" href="https://hexo.io/">
<img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为 Hexo" alt="HEXO">
</a>
<a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/">
<img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用 Butterfly" alt="Butterfly">
</a>
</p>
<img class="icp-icon" src="/static/img/website/icp.png">
<a href="https://beian.miit.gov.cn/#/Integrated/index">
<span>蜀ICP备2023013851号-2</span>
</a>
|
<a href="https://beian.mps.gov.cn/#/query/webSearch?code=51142202000154">
<span>川公网安备 51142202000154号</span>
</a>
# Copyright of theme and framework 版权信息(框架与主题)
copyright: false
# --------------------------------------
# Search 搜索
# --------------------------------------

search:
# Choose: algolia_search / local_search / docsearch
# leave it empty if you don't need search
use: algolia_search # 使用algolia_search
placeholder:

# Algolia Search
algolia_search:
# Number of search results per page
hitsPerPage: 6

# Local Search
local_search:
# Preload the search data when the page loads.
preload: false
# Show top n results per article, show all results by setting to -1
top_n_per_article: 1
# Unescape html strings to the readable one.
unescape: false
CDN:

# Docsearch
# https://docsearch.algolia.com/
docsearch:
appId:
apiKey:
indexName:
option:

# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
# 发布
deploy:
# Gitee上传
- type: git
repo: git@gitee.com:insectmk/hexo-blog-anzhiyu.git
branch: master
message: # 'blog update'
# Github上传
- type: git
repo: git@github.com:insectmk/hexo-blog-anzhiyu.git
branch: master
message: # 'blog update'

# 部分插件的关闭
## 首页轮播图
swiper:
enable: false # 开关

注意:关于主题的配置,一定要写在theme_config节点下

编译博客

一般来讲编译博客就是老三样

1
2
3
hexo clean
hexo generate
hexo deploy

但是,想要覆盖配置,就需要指定参数了,例如:

1
2
3
hexo clean --config _config.yml,_config.top.yml
hexo generate --config _config.yml,_config.top.yml
hexo deploy --config _config.yml,_config.top.yml

要是每次都要敲命令还是太麻烦了,可以直接记录到package.json的脚本中,然后执行npm run xxx就行了:

1
2
3
4
5
6
"scripts": {
"deploy": "npm run hexo-clean && npm run hexo-douban && npm run hexo-generate && npm run hexo-deploy",
"server": "npm run hexo-clean && npm run hexo-douban && npm run hexo-generate && npm run hexo-serve",
"server:top": "hexo clean --config _config.yml,_config.top.yml && hexo douban --config _config.yml,_config.top.yml && hexo generate --config _config.yml,_config.top.yml && hexo algolia --config _config.yml,_config.top.yml && hexo serve --config _config.yml,_config.top.yml",
"deploy:top": "hexo clean --config _config.yml,_config.top.yml && hexo douban --config _config.yml,_config.top.yml && hexo generate --config _config.yml,_config.top.yml && hexo algolia --config _config.yml,_config.top.yml && hexo deploy --config _config.yml,_config.top.yml"
},

由于博客还使用了部分插件,所以有doubanalgolia等插件的运行配置,如果没有可直接移除。

这样,每次发布版本,只需要依次运行对应的npm脚本就行了。

参考文档

Hexo-使用代替配置文件

Hexo-使用代替主题配置文件