根据 ^p-/^t-/^c-/^f- anchor 取出单个 block 原文。 精确锚点未命中时,做一次**哈希容错回收**:按 anchor 末段的 hash6(内容指纹)在全文块里找, 仅当**恰好一个**块命中该 hash6 时才返回它(附 recovered_from)。这样能确定性修复"内容哈希抄对、 但 type/seq 写错"的引用(典型:表格被解析为 ^p- 却被引为 ^t-;或把 ^p-7-x 误写成 ^t-7-x), 而内容真不在本页的孤儿锚点(hash6 零命中)仍按原样报"未找到"、由上层降级处理。
(md_path: Path, anchor: str)
| 1609 | |
| 1610 | |
| 1611 | def read_block(md_path: Path, anchor: str) -> dict: |
| 1612 | """根据 ^p-/^t-/^c-/^f- anchor 取出单个 block 原文。 |
| 1613 | |
| 1614 | 精确锚点未命中时,做一次**哈希容错回收**:按 anchor 末段的 hash6(内容指纹)在全文块里找, |
| 1615 | 仅当**恰好一个**块命中该 hash6 时才返回它(附 recovered_from)。这样能确定性修复"内容哈希抄对、 |
| 1616 | 但 type/seq 写错"的引用(典型:表格被解析为 ^p- 却被引为 ^t-;或把 ^p-7-x 误写成 ^t-7-x), |
| 1617 | 而内容真不在本页的孤儿锚点(hash6 零命中)仍按原样报"未找到"、由上层降级处理。 |
| 1618 | """ |
| 1619 | target = anchor.lstrip("^") |
| 1620 | blocks = parse_blocks_with_anchors(md_path) |
| 1621 | for blk in blocks: |
| 1622 | if blk.anchor == target: |
| 1623 | text = ANCHOR_TAIL_RE.sub("", blk.text).rstrip() |
| 1624 | return { |
| 1625 | "path": _to_rel_posix(md_path), |
| 1626 | "anchor": target, |
| 1627 | "kind": blk.kind, |
| 1628 | "line_start": blk.line_start, |
| 1629 | "line_end": blk.line_end, |
| 1630 | "content": text, |
| 1631 | } |
| 1632 | # 哈希容错回收:仅在 hash6 全文唯一命中时恢复,避免误配 |
| 1633 | th = _anchor_hash6(target) |
| 1634 | if th: |
| 1635 | matches = [b for b in blocks if b.anchor and _anchor_hash6(b.anchor) == th] |
| 1636 | if len(matches) == 1: |
| 1637 | blk = matches[0] |
| 1638 | text = ANCHOR_TAIL_RE.sub("", blk.text).rstrip() |
| 1639 | return { |
| 1640 | "path": _to_rel_posix(md_path), |
| 1641 | "anchor": blk.anchor, |
| 1642 | "kind": blk.kind, |
| 1643 | "line_start": blk.line_start, |
| 1644 | "line_end": blk.line_end, |
| 1645 | "content": text, |
| 1646 | "recovered_from": target, # 原引用锚点的 type/seq 与真实块不符,已按内容 hash6 校正 |
| 1647 | } |
| 1648 | raise LookupError(f"未找到 anchor: {anchor}") |
| 1649 | |
| 1650 | |
| 1651 | def list_all_blocks(md_path: Path) -> dict: |
no test coverage detected