|
@@ -134,21 +134,37 @@ def index():
|
|
|
# 按日期倒序排列
|
|
# 按日期倒序排列
|
|
|
posts.sort(key=lambda p: p.get("date", ""), reverse=True)
|
|
posts.sort(key=lambda p: p.get("date", ""), reverse=True)
|
|
|
|
|
|
|
|
- # 为每篇文章读取生成的静态 HTML 文件内容
|
|
|
|
|
|
|
+ # 为每篇文章获取内容
|
|
|
for post in posts:
|
|
for post in posts:
|
|
|
post_id = post.get("id", "")
|
|
post_id = post.get("id", "")
|
|
|
- generated_path = config.GENERATED_FOLDER / f"{post_id}.html"
|
|
|
|
|
- if generated_path.exists():
|
|
|
|
|
- with open(generated_path, "r", encoding="utf-8") as f:
|
|
|
|
|
- html_content = f.read()
|
|
|
|
|
- soup = BeautifulSoup(html_content, "html.parser")
|
|
|
|
|
- card_summary = soup.find("div", class_="card-summary")
|
|
|
|
|
- if card_summary:
|
|
|
|
|
- post["content"] = card_summary.decode_contents()
|
|
|
|
|
- else:
|
|
|
|
|
- post["content"] = ""
|
|
|
|
|
|
|
+ content = ""
|
|
|
|
|
+
|
|
|
|
|
+ # 优先从原始 Markdown 文件渲染内容(即使 generated 文件不存在也能显示)
|
|
|
|
|
+ md_path = config.POSTS_DATA_FOLDER / post_id / "content.md"
|
|
|
|
|
+ if md_path.exists():
|
|
|
|
|
+ try:
|
|
|
|
|
+ with open(md_path, "r", encoding="utf-8") as f:
|
|
|
|
|
+ md_content = f.read()
|
|
|
|
|
+ fixed_md = fix_image_paths(md_content, post_id)
|
|
|
|
|
+ html_body = render_markdown(fixed_md)
|
|
|
|
|
+ content = html_body
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ content = ""
|
|
|
else:
|
|
else:
|
|
|
- post["content"] = ""
|
|
|
|
|
|
|
+ # 回退到生成的静态 HTML 文件
|
|
|
|
|
+ generated_path = config.GENERATED_FOLDER / f"{post_id}.html"
|
|
|
|
|
+ if generated_path.exists():
|
|
|
|
|
+ try:
|
|
|
|
|
+ with open(generated_path, "r", encoding="utf-8") as f:
|
|
|
|
|
+ html_content = f.read()
|
|
|
|
|
+ soup = BeautifulSoup(html_content, "html.parser")
|
|
|
|
|
+ card_summary = soup.find("div", class_="card-summary")
|
|
|
|
|
+ if card_summary:
|
|
|
|
|
+ content = card_summary.decode_contents()
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ content = ""
|
|
|
|
|
+
|
|
|
|
|
+ post["content"] = content
|
|
|
|
|
|
|
|
return render_template("index.html", posts=posts)
|
|
return render_template("index.html", posts=posts)
|
|
|
|
|
|