Skip to content

fix(splitters): don't read headers from indented code in MarkdownHeaderSplitter - #1174

Open
shlokbam wants to merge 1 commit into
dynamiq-ai:mainfrom
shlokbam:fix/markdown-header-indented-code
Open

shlokbam wants to merge 1 commit into
dynamiq-ai:mainfrom
shlokbam:fix/markdown-header-indented-code

Conversation

@shlokbam

Copy link
Copy Markdown

MarkdownHeaderSplitter strips each line before checking it for a header marker, so a line inside an indented code block that starts with # is read as a header. CommonMark allows at most three spaces of indentation before an ATX header; four or more make the line indented code. AutoSplitter already follows that rule when it decides whether a document looks like Markdown (_looks_like_markdown, ^\s{0,3}#{1,6}\s+\S).

Example:

# Setup
Install it:

    pip install tool
    # then check the version
    tool --version
## Usage
Run it.

On main this gives three chunks: the comment line becomes a new h1 ("then check the version") that replaces "Setup", the code block is split in two, and because strip_headers is on by default the comment line is removed from the chunk text. The "Usage" chunk then carries h1: "then check the version".

With this change a line indented four or more columns (tabs count to the next multiple of 4) is never treated as a header, so the example gives two chunks: "Setup" with the whole code block, and "Setup > Usage". Headers with up to three spaces of indentation still split as before.

I found this by reading the splitter and checking its output against markdown-it-py, not from a real document.

Compatibility

No change for saved workflows: no new fields or parameters. Output changes only for lines indented four or more columns that start with #, which were split as headers before. One edge case: a header written inside a list item with four or more spaces of indentation is no longer split on (the splitter has no list-item tracking, so it can't tell that case from indented code).

Verified

  • New tests in tests/unit/nodes/splitters/test_markdown_header_splitter.py: test_markdown_header_splitter_ignores_headers_inside_indented_code_block fails on main and passes with the change; test_markdown_header_splitter_allows_up_to_three_spaces_before_a_header passes on both.
  • pytest tests/unit/nodes/splitters/: 77 passed, 1 skipped (includes the AutoSplitter tests).
  • flake8 --max-line-length 120, isort (black profile, 120), pyupgrade --py310-plus and bandit pass on the changed files. I didn't run the full make prepare or the whole suite.

…erSplitter

A line indented four or more columns is indented code in CommonMark, not a header, and AutoSplitter's own markdown check already allows at most three spaces before a header. MarkdownHeaderSplitter stripped every line before matching, so a comment such as "    # then check the version" in an indented code block started a new h1 section and, with strip_headers on, was removed from the chunk text.
@shlokbam
shlokbam requested a review from a team as a code owner November 27, 2026 17:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant