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

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

折腾了一天

原理:

1
2
3
很简单,就像上一篇说的一样,将source文件夹下的每篇文章的图复制到public它们会出现的所有位置(categories,tags,achieves)
复制时候需要注意文件名不要重复(毕竟一页展示时候不会只有一篇文章),所以需要将每张封面命名成不一样的,我这里用的是文章名称
另外,背景也需要复制,不过这个背景只需要一份即可,和封面放在一个文件夹下即可,文件名不要改,我这里是hexo_bg

然后是代码展示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
### 大概是主题作者的问题,文章头图在部分分类下显示有问题
### 原因是分类后的文章那个文件夹只有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

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]]

#没背景的加一下默认背景,有时候忘加了,毕竟真的太麻烦了...
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(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)
print("archives完成")

#categories,具有层级,路径需要按序拼接
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)
#背景
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)
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)
#背景
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)
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 协议进行许可