Переглянути джерело

refactor: 重构文章详情页与首页共享卡片样式

Co-authored-by: aider (deepseek/deepseek-v4-pro) <aider@aider.chat>
Your Name 3 днів тому
батько
коміт
fc70265960
2 змінених файлів з 20 додано та 11 видалено
  1. 14 11
      app.py
  2. 6 0
      templates/post_template.html

+ 14 - 11
app.py

@@ -110,12 +110,14 @@ def render_markdown(md_content: str) -> str:
     return md.convert(md_content)
 
 
-def generate_static_page(post_id: str, title: str, html_body: str):
+def generate_static_page(post_id: str, title: str, html_body: str, date: str, thumbnail: str):
     """生成独立的静态 HTML 文件"""
     rendered = render_template(
         "post_template.html",
         title=title,
         content=html_body,
+        date=date,
+        thumbnail=thumbnail,
     )
     output_path = config.GENERATED_FOLDER / f"{post_id}.html"
     with open(output_path, "w", encoding="utf-8") as f:
@@ -138,15 +140,13 @@ def index():
         generated_path = config.GENERATED_FOLDER / f"{post_id}.html"
         if generated_path.exists():
             with open(generated_path, "r", encoding="utf-8") as f:
-                # 提取 <article class="post-article"> 内的内容
                 html_content = f.read()
-                # 简单提取 body 内的内容(实际项目中可用 BeautifulSoup)
-                start = html_content.find('<article class="post-article">')
-                end = html_content.find("</article>")
-                if start != -1 and end != -1:
-                    post["content"] = html_content[start + len('<article class="post-article">'):end]
-                else:
-                    post["content"] = ""
+            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"] = ""
         else:
             post["content"] = ""
 
@@ -209,8 +209,11 @@ def upload():
     # 提取缩略图
     thumbnail = extract_thumbnail(html_body)
 
+    # 生成日期
+    date_iso = datetime.now(timezone.utc).isoformat()
+
     # 生成静态页面
-    generate_static_page(post_id, title, html_body)
+    generate_static_page(post_id, title, html_body, date_iso, thumbnail)
 
     # 更新索引
     index = load_index()
@@ -218,7 +221,7 @@ def upload():
         {
             "id": post_id,
             "title": title,
-            "date": datetime.now(timezone.utc).isoformat(),
+            "date": date_iso,
             "summary": summary,
             "thumbnail": thumbnail,
         }

+ 6 - 0
templates/post_template.html

@@ -4,8 +4,14 @@
 
 {% block content %}
 <article class="card">
+    {% if thumbnail %}
+    <div class="card-image">
+        <img src="{{ thumbnail }}" alt="{{ title }}" />
+    </div>
+    {% endif %}
     <div class="card-body">
         <h1 class="card-title">{{ title }}</h1>
+        <time class="card-date" datetime="{{ date }}">{{ date[:10] }}</time>
         <div class="card-summary">
             {{ content | safe }}
         </div>