Improve IR Binding Logic

This commit is contained in:
马一丁
2025-11-15 15:23:05 +08:00
parent 90d12a092d
commit fd1a23c7fb

View File

@@ -51,6 +51,8 @@ class DocumentComposer:
anchor = chapter.get("anchor") or f"section-{idx}"
chapter["anchor"] = self._ensure_unique_anchor(anchor)
chapter.setdefault("order", idx * 10)
if chapter.get("errorPlaceholder"):
self._ensure_heading_block(chapter)
document = {
"version": IR_VERSION,
@@ -76,5 +78,23 @@ class DocumentComposer:
self._seen_anchors.add(anchor)
return anchor
def _ensure_heading_block(self, chapter: Dict[str, object]) -> None:
"""保证占位章节仍然拥有可用于目录的heading block。"""
blocks = chapter.get("blocks")
if isinstance(blocks, list):
for block in blocks:
if isinstance(block, dict) and block.get("type") == "heading":
return
heading = {
"type": "heading",
"level": 2,
"text": chapter.get("title") or "占位章节",
"anchor": chapter.get("anchor"),
}
if isinstance(blocks, list):
blocks.insert(0, heading)
else:
chapter["blocks"] = [heading]
__all__ = ["DocumentComposer"]