11"""Markdown renderers for PageIndex tree structures."""
22from __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
1622def _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
0 commit comments