|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import date, datetime |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import yaml |
| 8 | + |
| 9 | +DOCS_DIR = Path(__file__).resolve().parent.parent / "docs" |
| 10 | +POST_DIRECTORIES = ( |
| 11 | + ("blog", DOCS_DIR / "blog" / "posts"), |
| 12 | + ("changelog", DOCS_DIR / "changelog"), |
| 13 | +) |
| 14 | +SECTION_LABELS = { |
| 15 | + "blog": "文章", |
| 16 | + "changelog": "更新日志", |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +def _load_front_matter(path: Path) -> dict[str, Any]: |
| 21 | + content = path.read_text(encoding="utf-8") |
| 22 | + if not content.startswith("---\n"): |
| 23 | + return {} |
| 24 | + |
| 25 | + _, _, remainder = content.partition("---\n") |
| 26 | + front_matter, _, _ = remainder.partition("\n---\n") |
| 27 | + if not front_matter: |
| 28 | + return {} |
| 29 | + |
| 30 | + data = yaml.safe_load(front_matter) |
| 31 | + return data if isinstance(data, dict) else {} |
| 32 | + |
| 33 | + |
| 34 | +def _normalize_created(value: Any) -> date | None: |
| 35 | + if isinstance(value, datetime): |
| 36 | + return value.date() |
| 37 | + if isinstance(value, date): |
| 38 | + return value |
| 39 | + if isinstance(value, str): |
| 40 | + try: |
| 41 | + return date.fromisoformat(value) |
| 42 | + except ValueError: |
| 43 | + return None |
| 44 | + return None |
| 45 | + |
| 46 | + |
| 47 | +def _iter_posts(): |
| 48 | + for section, base_dir in POST_DIRECTORIES: |
| 49 | + for path in sorted(base_dir.rglob("*.md")): |
| 50 | + relative = path.relative_to(base_dir) |
| 51 | + if "drafts" in relative.parts: |
| 52 | + continue |
| 53 | + |
| 54 | + meta = _load_front_matter(path) |
| 55 | + date_meta = meta.get("date") |
| 56 | + if isinstance(date_meta, dict): |
| 57 | + created = _normalize_created(date_meta.get("created")) |
| 58 | + else: |
| 59 | + created = _normalize_created(date_meta) |
| 60 | + slug = meta.get("slug") |
| 61 | + title = meta.get("title") |
| 62 | + if not created or not slug or not title: |
| 63 | + continue |
| 64 | + |
| 65 | + yield { |
| 66 | + "section": section, |
| 67 | + "created": created, |
| 68 | + "slug": slug, |
| 69 | + "title": title, |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | +def on_config(config, **kwargs): |
| 74 | + posts = list(_iter_posts()) |
| 75 | + if not posts: |
| 76 | + config.extra["latest_announcement"] = None |
| 77 | + return config |
| 78 | + |
| 79 | + latest_by_section = {} |
| 80 | + for section in SECTION_LABELS: |
| 81 | + section_posts = [post for post in posts if post["section"] == section] |
| 82 | + latest_post = max(section_posts, key=lambda item: item["created"], default=None) |
| 83 | + if not latest_post: |
| 84 | + continue |
| 85 | + |
| 86 | + latest_post = latest_post.copy() |
| 87 | + latest_post["url"] = f"/latest/{section}/{latest_post['slug']}/" |
| 88 | + latest_post["label"] = SECTION_LABELS[section] |
| 89 | + latest_post["created_display"] = latest_post["created"].strftime("%Y-%m-%d") |
| 90 | + latest_post["created"] = latest_post["created"].isoformat() |
| 91 | + latest_by_section[section] = latest_post |
| 92 | + |
| 93 | + overall_latest = max(posts, key=lambda item: item["created"]).copy() |
| 94 | + overall_latest["label"] = SECTION_LABELS[overall_latest["section"]] |
| 95 | + overall_latest["url"] = f"/latest/{overall_latest['section']}/{overall_latest['slug']}/" |
| 96 | + overall_latest["created_display"] = overall_latest["created"].strftime("%Y-%m-%d") |
| 97 | + overall_latest["created"] = overall_latest["created"].isoformat() |
| 98 | + |
| 99 | + config.extra["latest_announcement"] = { |
| 100 | + "overall": overall_latest, |
| 101 | + "sections": latest_by_section, |
| 102 | + } |
| 103 | + return config |
0 commit comments