From ee5cd0df4e1a6bc17836e666b1e85fec8128ff0e Mon Sep 17 00:00:00 2001 From: Research Assistant Date: Sun, 7 Jun 2026 12:53:04 +0800 Subject: [PATCH] fix: render section_heading with Markdown heading prefix --- paperforge/worker/ocr_render.py | 20 +++++++++----------- tests/test_ocr_render_stabilization.py | 13 +++++++++++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/paperforge/worker/ocr_render.py b/paperforge/worker/ocr_render.py index 440ab465..423f7568 100644 --- a/paperforge/worker/ocr_render.py +++ b/paperforge/worker/ocr_render.py @@ -843,17 +843,15 @@ def render_fulltext_markdown( if role == "backmatter_boundary_heading" or role == "backmatter_heading" or role == "reference_heading": lines.append(f"## {text}") lines.append("") - elif role == "section_heading": - if text.strip().lower() in FRONTMATTER_NOISE: - continue - if _is_bogus_heading(text): - if text: - lines.append(text) - lines.append("") - else: - lines.append(f"## {text}") - lines.append("") - elif role == "subsection_heading" or role == "sub_subsection_heading": + elif role in ("subsection_heading", "sub_subsection_heading", "section_heading"): + if role == "section_heading": + if text.strip().lower() in FRONTMATTER_NOISE: + continue + if _is_bogus_heading(text): + if text: + lines.append(text) + lines.append("") + continue lines.append(f"### {text}") lines.append("") elif role in ("backmatter_body", "tail_candidate_body", "body_paragraph"): diff --git a/tests/test_ocr_render_stabilization.py b/tests/test_ocr_render_stabilization.py index f3b02b35..6f8aa6a1 100644 --- a/tests/test_ocr_render_stabilization.py +++ b/tests/test_ocr_render_stabilization.py @@ -315,7 +315,7 @@ def test_stabilize_heading_sanity_allows_valid_short_heading() -> None: table_inventory={}, ) - assert "## 1 Introduction" in md + assert "### 1 Introduction" in md def test_stabilize_heading_sanity_downgrades_verb_heavy_heading() -> None: @@ -374,7 +374,7 @@ def test_stabilize_heading_sanity_allows_verb_short_heading() -> None: table_inventory={}, ) - assert "## Results are shown" in md + assert "### Results are shown" in md def test_stabilize_reference_content_mapped() -> None: @@ -914,3 +914,12 @@ def test_normalize_ocr_math_text_greek_letter_compound_preserved() -> None: def test_normalize_ocr_math_text_display_math() -> None: from paperforge.worker.ocr_math import normalize_ocr_math_text assert normalize_ocr_math_text("$$ ... $$") == "$$...$$" + + +def test_section_heading_renders_with_prefix() -> None: + from paperforge.worker.ocr_render import render_fulltext_markdown + blocks = [ + {"paper_id": "KEY", "page": 3, "block_id": "b1", "role": "section_heading", "text": "1 Introduction", "render_default": True, "bbox": [80, 200, 500, 230], "page_width": 1200, "page_height": 1700}, + ] + md = render_fulltext_markdown(structured_blocks=blocks, resolved_metadata={}, figure_inventory={}, table_inventory={}) + assert "### 1 Introduction" in md, f"Expected ### prefix, got: {md[:200]}"