de3BDJMH
给未来的笨蛋看的————个人网站更新教程

给未来的笨蛋看的————个人网站更新教程

折腾了一天

原理:

1
2
3
4
很简单,就像上一篇说的一样,将source文件夹下的每篇文章的图复制到public它们会出现的所有位置(categories,tags,achieves)
复制时候需要注意文件名不要重复(毕竟一页展示时候不会只有一篇文章),所以需要将每张封面命名成不一样的,我这里用的是文章名称
另外,背景也需要复制,不过这个背景只需要一份即可,和封面放在一个文件夹下即可,文件名不要改,我这里是hexo_bg
最需要注意的就是每个分类(tag、categories)下面超过10个都会添加一个page,要数每个帖子在对应分类下是第几页,这个很头疼,不过还是写出来了

然后是代码展示qwq,写的很烂…

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
### 大概是主题作者的问题,文章头图在部分分类下显示有问题
### 原因是分类后的文章那个文件夹只有index.html,图片没有被复制过去,就导致找不到图显示不了了
### 虽然很占用空间,但是我认为本地资源比图床要好,所以本代码是用于复制图片的
### 使用方法:
### 1. 在hexo/blog文件夹下右键,然后git bash here
### 2. 构建:npm run build(最好先clean一下,npm run clean)
### 3. 运行该代码
### 4. 提交生产环境或预览:npm run deploy 或 npm run server
### 小小的要求:
### 1. 封面图文件名请用<标题名>.png,不然放在同一个层级下会同名
### 2. 背景图文件名请用hexo_bg.png
### 3. 均存放至_posts下对应文章文件夹下

import os
import time
import shutil

RAW_PATH="你的\source路径!"
TARGET_PATH="你的\public路径!"
# 需要存放至的目标文件夹:archives、categories、tags、page

posts={}#标题: {日期(列表[年月日]), 分类(列表), 标签(列表), 是否有封面(bool)}
for p in os.listdir(os.path.join(RAW_PATH,"_posts")):
if p.endswith(".md"):
with open(os.path.join(RAW_PATH,"_posts",p),encoding="utf-8") as f:
lines=f.readlines()
title=p[:-3]
posts[title]={"date":[],"categories":[],"tags":[],"cover":False}
date=""
for line in lines:
if line.startswith("date: "):
date=line[6:16]
elif line.startswith("categories: "):
posts[title]["categories"]=line[12:].strip()[1:-1].split(",")
elif line.startswith("tags: "):
posts[title]["tags"]=line[6:].strip()[1:-1].split(",")
elif line.startswith("cover: "):
posts[title]["cover"]=True
if not date:#默认文章会没写时间,采用文件创建时间
date=str(os.path.getctime(os.path.join(RAW_PATH,"_posts",p)))[:10]
date=time.localtime(int(date))
date=time.strftime("%Y-%m-%d",date)
posts[title]["date"]=[date[:4],date[5:7],date[8:10]]
#按日期排序,给定页码,每页10个
sort_posts=sorted(posts.keys(),key=lambda x:posts[x]["date"],reverse=True)
n={"all":0,"tags":{},"categories":{}}#每页10个循环
page={"all":1,"tags":{},"categories":{}}#需要存储每个tag和categories的页码
for p in posts:#初始化
for t in posts[p]["tags"]:
if t not in page["tags"]:
n["tags"][t]=0
page["tags"][t]=1
for c in posts[p]["categories"]:
if c not in page["categories"]:
n["categories"][c]=0
page["categories"][c]=1
for p in sort_posts:#计算每篇文章页码
posts[p]["page"]={"all":page["all"],"tags":{},"categories":{}}#给文章添加页码标签
for t in posts[p]["tags"]:
posts[p]["page"]["tags"][t]=page["tags"][t]
for c in posts[p]["categories"]:
posts[p]["page"]["categories"][c]=page["categories"][c]#这些初始都是1,下面会更新page的数值
#更新
n["all"]+=1
for t in posts[p]["tags"]:
n["tags"][t]+=1
if n["tags"][t]%10==0:
page["tags"][t]+=1
for c in posts[p]["categories"]:
n["categories"][c]+=1
if n["categories"][c]%10==0:
page["categories"][c]+=1
if n["all"]%10==0:
page["all"]+=1

#没背景的加一下默认背景,有时候忘加了,毕竟真的太麻烦了...
for p in posts:
if not os.path.exists(os.path.join(RAW_PATH,"_posts",p,f"hexo_bg.png")):#没有背景的
bg_path=os.path.join(RAW_PATH,"hexo_bg.png")
if os.path.exists(bg_path):
shutil.copy(bg_path,os.path.join(RAW_PATH,"_posts",p,"hexo_bg.png"))

#archives
path=os.path.join(TARGET_PATH,"archives")
for p in posts:
date=posts[p]["date"]
raw_path=os.path.join(RAW_PATH,"_posts",p)
target_path=os.path.join(path,date[0],date[1])
#背景
bg_path=os.path.join(RAW_PATH,"hexo_bg.png")#使用默认的,这个不需要用文章的
if not os.path.exists(os.path.join(target_path,"hexo_bg.png")):#背景图已经有了就跳过
if os.path.exists(bg_path):
shutil.copy(bg_path,target_path)
if not os.path.exists(os.path.join(path,date[0],"hexo_bg.png")):#年月都要加
if os.path.exists(bg_path):
shutil.copy(bg_path,os.path.join(path,date[0]))
if not os.path.exists(os.path.join(path,"page",str(posts[p]["page"]["all"]),"hexo_bg.png")) and posts[p]["page"]["all"]!=1:#每页也要加,第一页不用
if os.path.exists(bg_path):
shutil.copy(bg_path,os.path.join(path,"page",str(posts[p]["page"]["all"])))
#头图
if not os.path.exists(raw_path):#还没对应文件夹
os.makedirs(raw_path)#帮忙建立,以后会用
continue
raw_path=os.path.join(raw_path,f"{p}.png")
if not os.path.exists(raw_path):
print(f"{p}.md 没有封面图,请记得加上")
continue
if not os.path.exists(os.path.join(target_path,f"{p}.png")):#防止重复执行代码重复添加
shutil.copy(raw_path,target_path)
if not os.path.exists(os.path.join(path,date[0],f"{p}.png")):#也是年月
shutil.copy(raw_path,os.path.join(path,date[0]))
if not os.path.exists(os.path.join(path,"page",str(posts[p]["page"]["all"]),f"{p}.png")) and posts[p]["page"]["all"]!=1:#每页封面同理
shutil.copy(raw_path,os.path.join(path,"page",str(posts[p]["page"]["all"])))
print("archives完成")

#page
path=os.path.join(TARGET_PATH,"page")
for p in posts:
page=posts[p]["page"]
if page==1:#第一页不用加
continue
raw_path=os.path.join(RAW_PATH,"_posts",p,f"{p}.png")
target_path=os.path.join(path,str(page["all"]))
#背景
bg_path=os.path.join(RAW_PATH,"hexo_bg.png")
if not os.path.exists(os.path.join(target_path,"hexo_bg.png")):
if os.path.exists(bg_path):
shutil.copy(bg_path,target_path)
#头图
if not os.path.exists(raw_path):
continue
if os.path.exists(os.path.join(target_path,f"{p}.png")):
continue
shutil.copy(raw_path,target_path)

#categories,具有层级,路径需要按序拼接
#每个分类超过10个下面都会有一个page,需要添加
path=os.path.join(TARGET_PATH,"categories")
for p in posts:
categories=posts[p]["categories"]
raw_path=os.path.join(RAW_PATH,"_posts",p,f"{p}.png")
target_path=path
for c in categories:
target_path=os.path.join(target_path,c)#注意这里用的是target_path,每次循环会增加他的长度
#背景
bg_path=os.path.join(RAW_PATH,"hexo_bg.png")
if posts[p]["page"]["categories"][c]==1:#添加背景和头图时候需要注意第一页是不在page里的
if not os.path.exists(os.path.join(target_path,"hexo_bg.png")):
if os.path.exists(bg_path):
shutil.copy(bg_path,target_path)
else:
if not os.path.exists(os.path.join(target_path,"page",str(posts[p]["page"]["categories"][c]),"hexo_bg.png")):
if os.path.exists(bg_path):
shutil.copy(bg_path,os.path.join(target_path,"page",str(posts[p]["page"]["categories"][c])))
#头图
if not os.path.exists(raw_path):
continue
if posts[p]["page"]["categories"][c]==1:
if os.path.exists(os.path.join(target_path,f"{p}.png")):
continue
shutil.copy(raw_path,target_path)
else:
if os.path.exists(os.path.join(target_path,"page",str(posts[p]["page"]["categories"][c]),f"{p}.png")):
continue
shutil.copy(raw_path,os.path.join(target_path,"page",str(posts[p]["page"]["categories"][c])))
print("categories完成")

#tags,无序,无需按顺序
path=os.path.join(TARGET_PATH,"tags")
for p in posts:
tags=posts[p]["tags"]
raw_path=os.path.join(RAW_PATH,"_posts",p,f"{p}.png")
for t in tags:
target_path=os.path.join(path,t)#这里则是path,每次循环更新
#背景
bg_path=os.path.join(RAW_PATH,"hexo_bg.png")
if posts[p]["page"]["tags"][t]==1:
if not os.path.exists(os.path.join(target_path,"hexo_bg.png")):
if os.path.exists(bg_path):
shutil.copy(bg_path,target_path)
else:
if not os.path.exists(os.path.join(target_path,"page",str(posts[p]["page"]["tags"][t]),"hexo_bg.png")):
if os.path.exists(bg_path):
shutil.copy(bg_path,os.path.join(target_path,"page",str(posts[p]["page"]["tags"][t])))
#头图
if not os.path.exists(raw_path):
continue
if posts[p]["page"]["tags"][t]==1:
if os.path.exists(os.path.join(target_path,f"{p}.png")):
continue
shutil.copy(raw_path,target_path)
else:
if os.path.exists(os.path.join(target_path,"page",str(posts[p]["page"]["tags"][t]),f"{p}.png")):
continue
shutil.copy(raw_path,os.path.join(target_path,"page",str(posts[p]["page"]["tags"][t])))
print("tags完成")

#处理没有封面图的,复制默认图,没背景的加一下背景
for p in posts:
if not posts[p]["cover"]:#这个是没头图默认背景图
bg_path=os.path.join(RAW_PATH,"hexo_bg.png")
if os.path.exists(bg_path):
shutil.copy(bg_path,os.path.join(TARGET_PATH,posts[p]["date"][0],posts[p]["date"][1],posts[p]["date"][2],p,"hexo_bg.png"))
if not os.path.exists(os.path.join(TARGET_PATH,posts[p]["date"][0],posts[p]["date"][1],posts[p]["date"][2],p,"hexo_bg.png")):#这个是没背景加背景图,两个不一样,但是用默认图时候两个都会变成hexo_bg,就不能玩花样了,需要先指定头图才能指定背景,只指定背景会导致头图也变成背景
bg_path=os.path.join(RAW_PATH,"hexo_bg.png")
if os.path.exists(bg_path):
shutil.copy(bg_path,os.path.join(TARGET_PATH,posts[p]["date"][0],posts[p]["date"][1],posts[p]["date"][2],p,"hexo_bg.png"))
print("杂项处理完成")
print("可以 npm run deploy 上传啦!")

如果可以帮到其他人的话我很开心!不过目前是给未来的笨蛋看的…

一定要记得看上面注释哇,和未来的自己协商一下…统一一下规则

本文作者:de3BDJMH
本文链接:https://de3bdjmh.github.io/2026/04/08/个人网站修复/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可