Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: summary json to md frontmatter python scripts
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 从 summary.json 中读取 summary 并插入到 posts .md 文件的 frontmatter 中 | ||
|
||
import os | ||
import json | ||
|
||
# 读取 summary.json 文件 | ||
with open("summary.json", "r", encoding="utf-8") as f: | ||
summaries = json.load(f)["summaries"] | ||
|
||
# 遍历 posts 目录及其子目录 | ||
for root, dirs, files in os.walk("posts"): | ||
for file in files: | ||
if file.endswith(".md"): | ||
md_file = os.path.join(root, file) | ||
|
||
# 读取 md 文件内容 | ||
with open(md_file, "r", encoding="utf-8") as f: | ||
lines = f.readlines() | ||
|
||
# 查找 slug 行 | ||
slug_line = -1 | ||
for i, line in enumerate(lines): | ||
if line.startswith("slug:"): | ||
slug_line = i | ||
break | ||
|
||
if slug_line != -1: | ||
# 在 slug 行下一行插入 summary 行 | ||
slug = lines[slug_line].strip().split(": ")[1].strip('"') | ||
summary_text = "" | ||
for summary in summaries: | ||
if summary["slug"] == slug: | ||
summary_text = summary["summary"] | ||
break | ||
|
||
if summary_text: | ||
lines.insert(slug_line + 1, f'summary: "{summary_text}"\n') | ||
|
||
# 将修改后的内容写回 md 文件 | ||
with open(md_file, "w", encoding="utf-8") as f: | ||
f.writelines(lines) |