| 1703 | |
| 1704 | # 给每个 block 找 owning section(包含它且 level 最深的 H 节点) |
| 1705 | def find_owner(start: int, end: int, self_anchor: str | None) -> str | None: |
| 1706 | best: tuple[int, str] | None = None # (level, anchor) |
| 1707 | for h_start, h_end, h_anchor, h_level in heading_intervals: |
| 1708 | if h_anchor == self_anchor: |
| 1709 | continue # heading 自己不当作 owner |
| 1710 | if h_start <= start < h_end: |
| 1711 | if best is None or h_level > best[0]: |
| 1712 | best = (h_level, h_anchor) |
| 1713 | return best[1] if best else None |
| 1714 | |
| 1715 | out_blocks: list[dict] = [] |
| 1716 | counts: dict[str, int] = {} |