You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MarkdownHeaderSplitter treats headings inside longer closing fences as headers #12954
MarkdownHeaderSplitter excludes headers inside fenced code blocks only if the closing fence has exactly the same length as the opening fence. Markdown permits a closing fence of the same type with at least as many fence characters. With a longer valid closing fence, the splitter treats code comments as document headers and cuts the code block apart.
Actual: ['Intro', 'fake', 'Real']. Expected: ['Intro', 'Real']. I ran it twice with identical results. Replacing the five-backtick closer with a four-backtick closer gives the expected result. The same failure occurs with four opening tildes and five closing tildes.
Recognize longer closing fences when excluding code blocks from header matches, while keeping unrelated heading and secondary-split behavior unchanged. Regression tests should cover longer backtick and tilde closers, equal-length closers, and the real header after the block. This should not require a public API change. I did not find a matching open/closed issue or PR and can prepare a patch once the current Haystack PR queue clears.
The same fence matcher also misses an unclosed fence, which Markdown treats as continuing to the end of the document. With # Intro\nBody\n```python\n# code comment\nprint("x")\n, the splitter returns headers ['Intro', 'code comment'] instead of just ['Intro']. Reproduced twice on current main. This is another regression case for the fence-exclusion fix, not a separate issue.
One more fence variant for the same fix: CommonMark allows up to three spaces before an opening or closing fence. With # Intro\nBody\n ```python\n# fake\n ```\n## Real\nEnd\n, the splitter returns ['Intro', 'fake', 'Real']; expected ['Intro', 'Real']. The same result repeats on current main. Please cover indentation along with unequal-length and unclosed fences in the regression tests.
I'd like to pick this up. Thanks @Harsh23Kashyap for the clear report and the CommonMark reference. I reproduced the issue on main at 87dba65.
The scope is three cases, all with the same root cause in _code_block_pattern. @Harsh23Kashyap had already flagged the unclosed fence and the indentation case earlier in this thread, and I missed those comments when I first posted.
Longer closing fence: a closing fence may be longer than the opener, but it must use the same fence character. A shorter run never closes the block.
Indented fence: a fence indented by up to three spaces is still a fence. Four or more makes it an indented code block, so a # line after it remains a real header.
Unclosed fence: per CommonMark, an unclosed fence runs to the end of the document. The current pattern requires a closing fence to match, so when none exists, every # line inside the block is parsed as a header. This is likely to show up with truncated or streamed markdown.
Proposed fix: capture the fence character separately from its length, so the closing fence must repeat the opener's character at least as many times, and allow up to three spaces of indentation on either fence. An unterminated block extends to the end of the document instead of failing the match.
Tests cover each of the three cases above, plus the existing equal-length behavior as a regression guard.
There is no public API change. All three belong in one fix, and I'll open a PR shortly.
Hi @AmaedaQ, are you still working on this? If not, I have a fix ready that covers the longer, indented and unclosed fences from this thread, plus a guard so a line that starts with inline code doesn't open a block that would hide every later header. Happy to leave it with you if you're close.
Opened #13344 for this. Thanks @AmaedaQ for the write-up, it covers the same three cases, and the PR also handles a line that starts with inline code. Happy to close mine if you already have a PR coming.
Activity
Harsh23Kashyap commentedon Sep 25, 2026
The same fence matcher also misses an unclosed fence, which Markdown treats as continuing to the end of the document. With
# Intro\nBody\n```python\n# code comment\nprint("x")\n, the splitter returns headers['Intro', 'code comment']instead of just['Intro']. Reproduced twice on currentmain. This is another regression case for the fence-exclusion fix, not a separate issue.Harsh23Kashyap commentedon Sep 25, 2026
One more fence variant for the same fix: CommonMark allows up to three spaces before an opening or closing fence. With
# Intro\nBody\n ```python\n# fake\n ```\n## Real\nEnd\n, the splitter returns['Intro', 'fake', 'Real']; expected['Intro', 'Real']. The same result repeats on currentmain. Please cover indentation along with unequal-length and unclosed fences in the regression tests.AmaedaQ commentedon Oct 4, 2026
I'd like to pick this up. Thanks @Harsh23Kashyap for the clear report and the CommonMark reference. I reproduced the issue on
mainat87dba65.The scope is three cases, all with the same root cause in
_code_block_pattern. @Harsh23Kashyap had already flagged the unclosed fence and the indentation case earlier in this thread, and I missed those comments when I first posted.#line after it remains a real header.#line inside the block is parsed as a header. This is likely to show up with truncated or streamed markdown.Repro for the unclosed case:
Proposed fix: capture the fence character separately from its length, so the closing fence must repeat the opener's character at least as many times, and allow up to three spaces of indentation on either fence. An unterminated block extends to the end of the document instead of failing the match.
Tests cover each of the three cases above, plus the existing equal-length behavior as a regression guard.
There is no public API change. All three belong in one fix, and I'll open a PR shortly.
Edited to correct scope.
Vedant-Agarwal commentedon Oct 18, 2026
Hi @AmaedaQ, are you still working on this? If not, I have a fix ready that covers the longer, indented and unclosed fences from this thread, plus a guard so a line that starts with inline code doesn't open a block that would hide every later header. Happy to leave it with you if you're close.
(AI-assisted: drafted with Claude Code.)
Vedant-Agarwal commentedon Oct 20, 2026
Opened #13344 for this. Thanks @AmaedaQ for the write-up, it covers the same three cases, and the PR also handles a line that starts with inline code. Happy to close mine if you already have a PR coming.
(AI-assisted)