소스 검색

fix: 修复首页文章内容不显示的问题

Co-authored-by: aider (deepseek/deepseek-v4-pro) <aider@aider.chat>
Your Name 3 일 전
부모
커밋
dafdc58e23
1개의 변경된 파일28개의 추가작업 그리고 12개의 파일을 삭제
  1. 28 12
      app.py

+ 28 - 12
app.py

@@ -134,21 +134,37 @@ def index():
     # 按日期倒序排列
     posts.sort(key=lambda p: p.get("date", ""), reverse=True)
 
-    # 为每篇文章读取生成的静态 HTML 文件内容
+    # 为每篇文章获取内容
     for post in posts:
         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:
-            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)