Skip to main content

Function Signature

Source: pageindex/page_index_md.py:243

Description

The md_to_tree() function generates a PageIndex tree structure from Markdown documents. It analyzes Markdown headers (#, ##, ###, etc.) to build a hierarchical structure similar to the PDF processing, but optimized for Markdown’s inherent structure. Important: This function is asynchronous and must be called with asyncio.run() or await.
Markdown files must use proper header hierarchy with # symbols. If your Markdown was converted from PDF or HTML, ensure the conversion tool preserved the original heading structure. For best results with converted documents, use PageIndex OCR.

Parameters

str
required
Path to the Markdown file. Must be a valid file path ending in .md or .markdown.
bool
default:"False"
Whether to apply tree thinning. When enabled, small nodes (below min_token_threshold) are merged with their parent nodes to simplify the tree structure.
int
default:"None"
Minimum token count for a node when thinning is enabled. Nodes with fewer tokens are merged with their parents. Only used if if_thinning=True.
str
default:"no"
Whether to generate AI summaries for each node. Valid values: "yes" or "no". For leaf nodes, creates summary field. For parent nodes, creates prefix_summary field.
int
default:"None"
Token threshold for summary generation. If a node’s text is shorter than this threshold, the full text is used instead of generating a summary. Only used if if_add_node_summary="yes".
str
default:"None"
OpenAI model to use for summary generation and token counting. Examples: "gpt-4o-2024-11-20", "gpt-4o", "gpt-4.1"
str
default:"no"
Whether to generate a one-sentence description for the entire document. Valid values: "yes" or "no". Only works if if_add_node_summary="yes".
str
default:"no"
Whether to include full text content in each node. Valid values: "yes" or "no"
str
default:"yes"
Whether to add sequential node IDs to the tree structure. Valid values: "yes" or "no"

Return Value

dict
Dictionary containing the Markdown structure:

Example Usage

Basic Usage

With Summaries and Description

With Tree Thinning

Async Context Usage

Markdown Format Requirements

Headers must follow standard Markdown syntax:
  • Use # for level 1 headers
  • Use ## for level 2 headers
  • Use ### for level 3 headers, etc.
  • Headers inside code blocks (triple backticks) are ignored
Valid Markdown:
Invalid/Problematic:

Tree Thinning Behavior

When if_thinning=True:
  1. Calculates token count for each node (including all descendants)
  2. If a node has fewer tokens than min_token_threshold:
    • Merges all child content into parent node
    • Removes child nodes from tree
    • Updates parent’s text and token count
  3. Processes from leaves to root to ensure accurate merging
Example:

Performance Notes

  • Processing is much faster than PDF (no OCR or complex parsing)
  • Summary generation is the slowest step
  • Token counting uses the specified model’s tokenizer
  • Large documents may take several minutes if summaries are enabled

See Also