Files
life-echo/api/app/features/memoir/chapter_markdown_compose.py
2026-03-20 15:15:35 +08:00

40 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
按 chapter_story_links 顺序将各 story 正文物化为单一 markdown无 LLM
保留 story 内 asset:// 引用不变。
"""
from typing import Any
def compose_ordered_stories_to_markdown(
ordered: list[tuple[str, str]],
) -> str:
"""
:param ordered: (story_title, canonical_markdown) 已按阅读顺序排好
:return: 章节级 markdown每个故事为 ## 标题 + 正文,故事之间用 markdown 水平线 --- 分隔
(配图在 story 正文中,自然落在该故事块内、--- 之前)
"""
parts: list[str] = []
for title, md in ordered:
title = (title or "").strip() or "故事"
body = (md or "").strip()
parts.append(f"## {title}\n\n{body}" if body else f"## {title}")
return "\n\n---\n\n".join(parts)
def materialize_chapter_markdown_from_loaded_chapter(chapter: Any) -> str:
"""要求 chapter.story_links 已 eager-load且各 link.story 可用。"""
links = sorted(
list(getattr(chapter, "story_links", None) or []),
key=lambda x: getattr(x, "order_index", 0),
)
pairs: list[tuple[str, str]] = []
for link in links:
st = getattr(link, "story", None)
if st is None:
continue
title = (getattr(st, "title", None) or "").strip()
body = (getattr(st, "canonical_markdown", None) or "").strip()
pairs.append((title, body))
return compose_ordered_stories_to_markdown(pairs)