← Back to Pull requests

fix(md): recognize ATX headings indented by up to three spaces #4755

Open
unknown opened this pull request with 269 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 15, 2026 06:51 UTC
Author

Summary

partition_md left a heading as plain text when it was indented by one to three spaces, for example " ## Chapter 1". CommonMark allows this indentation. The heading came out as UncategorizedText with the literal ## in it, so no Title element was created and the following elements got the wrong parent_id (and no category_depth). This is the case reported in #3952, where the sample string is built with \ line continuations that add a leading space to each line.

Root cause: Python-Markdown's HashHeaderProcessor only matches # in the first column. The fix adds a small internal extension that registers a subclass of that processor. Its regex allows [ ]{0,3} before the #. Four or more spaces still give an indented code block. Fenced code is stashed by the preprocessor before block parsing, so it is not affected. The extension is always added after the user's extensions list.

Fixes #3952

Validation

  • New tests in test_unstructured/partition/test_md.py:

  • - indented headings (1 and 3 spaces) become Title elements with the right category_depth and parent_id chain (fails on main, passes now);
    - a 4-space-indented # ... line is still code, not a heading.
  • .venv/bin/python -m pytest test_unstructured/partition/test_md.py -> 43 passed

  • ruff check and ruff format --check on the changed files: clean.

  • I did not run the full suite (limited disk on my machine).
  • Repro from the issue now gives Title(Chapter 1) -> Title(Section) -> body text, each with the expected parent.

    <!-- This is an auto-generated description by cubic. -->
    <a href="https://cubic.dev/pr/Unstructured-IO/unstructured/pull/4755?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
    <!-- End of auto-generated description by cubic. -->

    Diff: 269 lines (+201 / -68) across 3 files Reviewer: chrikrah Review duration: 2944m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.39) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 269 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.335
    Real PR Conversation Timeline
    1 comments
    "Review active on #4755"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.4% (MEDIUM) · Higher = larger blast radius
    39.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21% · Safe to merge
    21%