Showing
1 changed file
with
20 additions
and
0 deletions
| @@ -51,6 +51,8 @@ class DocumentComposer: | @@ -51,6 +51,8 @@ class DocumentComposer: | ||
| 51 | anchor = chapter.get("anchor") or f"section-{idx}" | 51 | anchor = chapter.get("anchor") or f"section-{idx}" |
| 52 | chapter["anchor"] = self._ensure_unique_anchor(anchor) | 52 | chapter["anchor"] = self._ensure_unique_anchor(anchor) |
| 53 | chapter.setdefault("order", idx * 10) | 53 | chapter.setdefault("order", idx * 10) |
| 54 | + if chapter.get("errorPlaceholder"): | ||
| 55 | + self._ensure_heading_block(chapter) | ||
| 54 | 56 | ||
| 55 | document = { | 57 | document = { |
| 56 | "version": IR_VERSION, | 58 | "version": IR_VERSION, |
| @@ -76,5 +78,23 @@ class DocumentComposer: | @@ -76,5 +78,23 @@ class DocumentComposer: | ||
| 76 | self._seen_anchors.add(anchor) | 78 | self._seen_anchors.add(anchor) |
| 77 | return anchor | 79 | return anchor |
| 78 | 80 | ||
| 81 | + def _ensure_heading_block(self, chapter: Dict[str, object]) -> None: | ||
| 82 | + """保证占位章节仍然拥有可用于目录的heading block。""" | ||
| 83 | + blocks = chapter.get("blocks") | ||
| 84 | + if isinstance(blocks, list): | ||
| 85 | + for block in blocks: | ||
| 86 | + if isinstance(block, dict) and block.get("type") == "heading": | ||
| 87 | + return | ||
| 88 | + heading = { | ||
| 89 | + "type": "heading", | ||
| 90 | + "level": 2, | ||
| 91 | + "text": chapter.get("title") or "占位章节", | ||
| 92 | + "anchor": chapter.get("anchor"), | ||
| 93 | + } | ||
| 94 | + if isinstance(blocks, list): | ||
| 95 | + blocks.insert(0, heading) | ||
| 96 | + else: | ||
| 97 | + chapter["blocks"] = [heading] | ||
| 98 | + | ||
| 79 | 99 | ||
| 80 | __all__ = ["DocumentComposer"] | 100 | __all__ = ["DocumentComposer"] |
-
Please register or login to post a comment