Skip to content

Commit bb844fb

Browse files
committed
feat(tree_renderer): add type+description to PageIndex summary frontmatter
1 parent 0209e2c commit bb844fb

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

openkb/indexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def index_long_document(
174174
# Write wiki/summaries/ (no images, just summaries)
175175
summaries_dir = kb_dir / "wiki" / "summaries"
176176
summaries_dir.mkdir(parents=True, exist_ok=True)
177-
summary_md = render_summary_md(tree, source_name, doc_id)
177+
summary_md = render_summary_md(tree, source_name, doc_id, description=description)
178178
(summaries_dir / f"{source_name}.md").write_text(summary_md, encoding="utf-8")
179179

180180
return IndexResult(doc_id=doc_id, description=description, tree=tree)

openkb/tree_renderer.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
"""Markdown renderers for PageIndex tree structures."""
22
from __future__ import annotations
33

4+
import json
45

5-
def _yaml_frontmatter(source_name: str, doc_id: str) -> str:
6-
"""Return a YAML frontmatter block for a PageIndex wiki page."""
7-
return (
8-
"---\n"
9-
"doc_type: pageindex\n"
10-
f"full_text: sources/{source_name}.json\n"
11-
"---\n"
12-
)
6+
7+
def _yaml_kv_line(key: str, value: str) -> str:
8+
"""Return a single YAML key-value line with the value JSON-quoted."""
9+
return f"{key}: {json.dumps(value, ensure_ascii=False)}"
1310

1411

12+
def _yaml_frontmatter(source_name: str, doc_id: str, description: str = "") -> str:
13+
"""Return a YAML frontmatter block for a PageIndex wiki page."""
14+
lines = [_yaml_kv_line("type", "Summary")]
15+
if description:
16+
lines.append(_yaml_kv_line("description", description))
17+
lines.append("doc_type: pageindex")
18+
lines.append(f"full_text: sources/{source_name}.json")
19+
return "---\n" + "\n".join(lines) + "\n---\n"
20+
1521

1622
def _render_nodes_summary(nodes: list[dict], depth: int) -> str:
1723
"""Recursively render nodes for the *summary* view (summaries only)."""
@@ -24,7 +30,7 @@ def _render_nodes_summary(nodes: list[dict], depth: int) -> str:
2430
summary = node.get("summary", "")
2531
children = node.get("nodes", [])
2632

27-
lines.append(f"{heading_prefix} {title} (pages {start}\u2013{end})\n")
33+
lines.append(f"{heading_prefix} {title} (pages {start}{end})\n")
2834
if summary:
2935
lines.append(f"Summary: {summary}\n")
3036
if children:
@@ -33,13 +39,15 @@ def _render_nodes_summary(nodes: list[dict], depth: int) -> str:
3339
return "\n".join(lines)
3440

3541

36-
37-
def render_summary_md(tree: dict, source_name: str, doc_id: str) -> str:
42+
def render_summary_md(tree: dict, source_name: str, doc_id: str,
43+
description: str = "") -> str:
3844
"""Render the summary Markdown page for a PageIndex tree.
3945
4046
Renders each node as a heading with page range and its summary text.
47+
Includes a YAML frontmatter block with ``type: "Summary"`` and an
48+
optional ``description`` field.
4149
"""
42-
frontmatter = _yaml_frontmatter(source_name, doc_id)
50+
frontmatter = _yaml_frontmatter(source_name, doc_id, description)
4351
structure = tree.get("structure", [])
4452
body = _render_nodes_summary(structure, depth=1)
4553
return frontmatter + "\n" + body

tests/test_tree_renderer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ def test_summary_included_not_text(self, sample_tree):
3838
assert "Summary: Historical context." in output
3939
# Raw text should NOT appear in summary view
4040
assert "This document introduces the core concepts of the system." not in output
41+
42+
43+
def test_summary_md_has_type_and_description():
44+
tree = {"structure": [{"title": "Intro", "start_index": 1,
45+
"end_index": 2, "summary": "x", "nodes": []}]}
46+
md = render_summary_md(tree, "my-doc", "doc-123", description="Quarterly report.")
47+
assert 'type: "Summary"' in md
48+
assert 'description: "Quarterly report."' in md
49+
assert "doc_type: pageindex" in md
50+
assert "full_text: sources/my-doc.json" in md

0 commit comments

Comments
 (0)