根据 anchor 或 title 取出整段 H 段(到下一同级 heading 之前)。
(md_path: Path, anchor_or_title: str)
| 1576 | |
| 1577 | |
| 1578 | def read_section(md_path: Path, anchor_or_title: str) -> dict: |
| 1579 | """根据 anchor 或 title 取出整段 H 段(到下一同级 heading 之前)。""" |
| 1580 | outline = load_or_build_outline(md_path) |
| 1581 | sec = find_section_in_outline(outline["sections"], anchor_or_title) |
| 1582 | if not sec: |
| 1583 | raise LookupError(f"未找到 anchor 或标题: {anchor_or_title}") |
| 1584 | text = md_path.read_text(encoding="utf-8") |
| 1585 | body = text[sec["char_start"]:sec["char_end"]] |
| 1586 | return { |
| 1587 | "path": _to_rel_posix(md_path), |
| 1588 | "anchor": sec["anchor"], |
| 1589 | "title": sec["title"], |
| 1590 | "level": sec["level"], |
| 1591 | "line": sec["line"], |
| 1592 | "char_start": sec["char_start"], |
| 1593 | "char_end": sec["char_end"], |
| 1594 | "preview": sec.get("preview", ""), |
| 1595 | "agent_summary": sec.get("agent_summary"), |
| 1596 | "content": body, |
| 1597 | } |
| 1598 | |
| 1599 | |
| 1600 | def _anchor_hash6(anchor: str) -> str | None: |
no test coverage detected