处理语雀导出md文档为hexo需求的md格式:

以前我是用语雀来写文档的!现在也是,只不过语雀导出md文档直接部署到hexo上有如下问题:

  • 导出的图片加了防盗链,部署到github上是无法访问的!
  • 导出的文章没有分类和标签
  • 为了好看, 文章的封面设置又麻烦

我抽空就写了一个python脚本!脚本的流程为:语雀导出的md到指定的文件夹中->脚本处理指定图片文件夹的内容->脚本读取并处理md文档->脚本把处理好的md文档剪切到_post文件夹中!

py源码如下,注意更换相关文件夹的路径:

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
import os
import shutil
import sys
import io
import random

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')


def move_images(source_folder, target_folder):
print(f"开始处理图片文件夹:{source_folder}")
print()
images = os.listdir(source_folder)
if len(images) == 0:
print(f"图片文件夹 {source_folder} 中没有图片文件。")
print()
return

for image_name in images:
image_path = os.path.join(source_folder, image_name)
if os.path.isfile(image_path):
print(f"正在处理图片:\033[31m{image_name}\033[0m")
target_path = os.path.join(target_folder, image_name)

if os.path.exists(target_path):
print(f"目标文件夹中已存在同名图片文件:{target_path}")
print("跳过当前图片。")
print()
continue

shutil.move(image_path, target_path)
print(f"已处理并移动图片:{target_path}")
print()

update_index_file(target_folder, image_name)

print(f"图片文件夹 {source_folder} 处理完成。")
print()


def update_index_file(folder, image_name):
index_file_path = os.path.join(folder, "index.md")
if not os.path.exists(index_file_path):
print(f"未找到 {index_file_path} 文件。")
return

with open(index_file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()

# Find the line number containing {% gallery %}
gallery_line_index = -1
for i, line in enumerate(lines):
if '{% gallery %}' in line:
gallery_line_index = i
break

if gallery_line_index == -1:
print("未找到 {% gallery %} 行,请确保在 index.md 中存在该行。")
return

# Insert the new content after {% gallery %}, ensuring a newline
new_content = f"![]({image_name})\n"
lines.insert(gallery_line_index + 1, new_content)

with open(index_file_path, 'w', encoding='utf-8') as file:
file.writelines(lines)

print(f"已更新 {index_file_path}")
print()



def process_markdown_file(file_path, wakawaka_images_folder):
file_name = os.path.basename(file_path)
title = os.path.splitext(file_name)[0]
print(f"正在处理Markdown文档:\033[31m{file_name}\033[0m")
print("当前文章的categories是?")
categories = input().encode(sys.stdin.encoding).decode(sys.stdin.encoding).replace(",", ",").split(",")
categories = [category.strip() for category in categories]

print("当前文章的tags是?")
tags = input().encode(sys.stdin.encoding).decode(sys.stdin.encoding).replace(",", ",").split(",")
tags = [tag.strip() for tag in tags]

cover_image = get_random_image(wakawaka_images_folder)
info = f"---\n"
info += f"title: [{title}]\n"
info += f"categories: {categories}\n"
info += f"tags: {tags}\n"
info += f"background: url(/img/wakawaka/{cover_image})\n"
info += f"cover: /img/wakawaka/{cover_image}\n"
info += f"---\n"

with open(file_path, 'r+', encoding='utf-8') as file:
content = file.read()
file.seek(0, 0)
file.write(info + "\n" + content)

html_code = '<meta name="referrer" content="no-referrer">\n'

with open(file_path, 'a', encoding='utf-8') as file:
file.write("\n" + html_code)

new_file_path = os.path.join(r"D:\博客\hexo-blog\source\_posts", file_name)

if os.path.exists(new_file_path):
print(f"目标文件夹中已存在同名文档:{new_file_path}")
print("是否覆盖原始文档?(Y/N)")
choice = input().strip().lower()
if choice != "y":
print("跳过当前文档。")
return

shutil.move(file_path, new_file_path)
print(f"已处理并移动文件:{new_file_path}")
print()


def get_random_image(folder):
images = os.listdir(folder)
if len(images) == 0:
return ""
return random.choice(images)


def main():
drafts_folder = r"D:\博客\hexo-blog\source\_drafts"
posts_folder = r"D:\博客\hexo-blog\source\_posts"
wakawaka_images_folder = r"D:\博客\img\wakawaka"
nsfw_images_folder = r"D:\博客\img\NSFW"
wakawaka_target_folder = r"D:\博客\hexo-blog\source\img\wakawaka"
nsfw_target_folder = r"D:\博客\hexo-blog\source\img\NSFW"

move_images(wakawaka_images_folder, wakawaka_target_folder)
move_images(nsfw_images_folder, nsfw_target_folder)

while True:
files = os.listdir(drafts_folder)
if len(files) == 0:
print("已处理完所有markdown文档。")
break

for file_name in files:
file_path = os.path.join(drafts_folder, file_name)
if os.path.isfile(file_path) and file_name.lower().endswith(".md"):
process_markdown_file(file_path, wakawaka_target_folder)

if len(os.listdir(posts_folder)) == 0:
print("已处理完所有markdown文档。")
break

print("请切换到命令行窗口以继续处理剩余的markdown文档。")
input("按回车键继续...")
print()


if __name__ == "__main__":
main()

指定部署脚本:

文章过多后hexo clean,hexo g,hexo d命令执行会特别费时,我也写了一个py脚本来自动执行这3个命令,并在执行完成后自动关闭命令框:

py源码如下,注意更换相关文件夹的路径:

1
2
3
4
5
6
7
8
9
10
11
import subprocess

def execute_hexo_commands():
# 进入特定的目录
directory = r'D:\博客\hexo-blog'
cmd = f'cd /d {directory} && echo hexo clean && hexo clean && echo hexo g && hexo g && echo hexo d && hexo d && exit'

# 打开cmd并执行命令
subprocess.call(['start', 'cmd', '/k', cmd], shell=True)

execute_hexo_commands()

上面的缺点也很明显!就是我们语雀导出的md和图片都要存放在指定的文件夹中!