From 6c6e5c140cc0471abe3ef2c57a051c4837fd8a84 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 1 Jul 2026 23:48:28 +0000 Subject: [PATCH] feat(staging): render list bullets and title heading depth in markdown --- test_unstructured/staging/test_base.py | 13 +++++++++++++ unstructured/staging/base.py | 13 +++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/test_unstructured/staging/test_base.py b/test_unstructured/staging/test_base.py index 8f2b0c35f6..8b2f3c516b 100644 --- a/test_unstructured/staging/test_base.py +++ b/test_unstructured/staging/test_base.py @@ -592,6 +592,19 @@ def test_elements_to_md_conversion(json_filename: str, expected_md_filename: str ("element", "expected_markdown", "exclude_binary"), [ (Title("Test Title"), "# Test Title", False), + ( + Title("Second Level", metadata=ElementMetadata(category_depth=2)), + "## Second Level", + False, + ), + ( + Title("Third Level", metadata=ElementMetadata(category_depth=3)), + "### Third Level", + False, + ), + (Title("No Depth"), "# No Depth", False), + (ListItem("Buy milk"), "- Buy milk", False), + (ListItem("Another item"), "- Another item", False), (NarrativeText("This is some narrative text."), "This is some narrative text.", False), (Formula(r"\int_a^b x^2 dx"), "$$\n\\int_a^b x^2 dx\n$$", False), ( diff --git a/unstructured/staging/base.py b/unstructured/staging/base.py index 47d8c99f83..dce3fa5514 100644 --- a/unstructured/staging/base.py +++ b/unstructured/staging/base.py @@ -18,6 +18,7 @@ ElementMetadata, Formula, Image, + ListItem, Table, Title, ) @@ -278,8 +279,16 @@ def element_to_md( formula_markdown_style: str = FORMULA_MARKDOWN_AUTO, ) -> str: match element: - case Title(text=text): - return f"# {text}" + case Title(text=text, metadata=metadata): + depth = metadata.category_depth if metadata.category_depth else 1 + if not isinstance(depth, int): + try: + depth = int(depth) if isinstance(depth, (str, float)) else 1 + except (TypeError, ValueError): + depth = 1 + return f"{'#' * depth} {text}" + case ListItem(text=text): + return f"- {text}" case Formula(text=text): return _emit_formula_markdown( text,