[Bug]: MarkdownNodeParser closes a code block on a shorter backtick fence
Bug Description
MarkdownNodeParser treats three backticks inside a block opened with four backticks as a closing fence. A subsequent # line in the code becomes a section heading, while the real heading after the closing fence is included in the wrong node.
The shorter fence should remain code content. CommonMark §4.5 requires a closing fence to use the same character and contain at least as many characters as the opening fence. The expected output is two nodes: the complete Section A, including its code block, followed by Section B.
The parser currently toggles a boolean whenever a line starts with three backticks after indentation is removed. It does not retain the opening fence length. A local patch tracks that length, preserving the current indentation handling and leaving tilde support unchanged. Three regression cases failed before the fix; all 19 selected parser tests, including the 13 Markdown cases, passed afterward.
The core source type check passes for 479 files with Python 3.12.3 and frozen package dependencies. A broader package test run returned 1,483 passed, 22 failed, 22 skipped, and 6 xfailed. Each of the 22 failures reports missing NLTK stopwords data; the resource downloader is blocked by its network security check in this environment. I am not claiming a clean full-suite result.
Related PR #22123 is closed without merging. Its diff handles fence characters but does not track the opening length. This report concerns the shorter-backtick case.
Would a fix limited to tracking the opening backtick length, with these regression tests, be appropriate? I can submit the local patch once the scope is confirmed.
AI assistance: Codex assisted with the investigation, local patch, regression tests, test execution, and this report.
Version
llama-index-core 0.14.25 from source, unmodified main commit 169e450aa54d26ed3e0536b1a0f1ea537871aa5d; Python 3.12.3, macOS arm64. The version is the package metadata of this source checkout, not a separate verification of a PyPI release.
Steps to Reproduce
Run this against the unmodified commit above. It uses no model or external service.
from llama_index.core.node_parser.file.markdown import MarkdownNodeParser
from llama_index.core.schema import Document, MetadataMode
text = "\n".join([
"# Section A",
"````markdown",
"```",
"# code, not a heading",
"````",
"# Section B",
"text",
])
nodes = MarkdownNodeParser().get_nodes_from_documents([Document(text=text)])
for node in nodes:
print(repr(node.get_content(metadata_mode=MetadataMode.NONE)))Expected:
'# Section A\n````markdown\n```\n# code, not a heading\n````'
'# Section B\ntext'Both the incorrect and expected outputs contain two nodes, so checking the node count alone misses the problem.
Relevant Logs/Tracebacks
There is no exception. The unmodified parser prints:
'# Section A\n````markdown\n```'
'# code, not a heading\n````\n# Section B\ntext'Source: run-llama/llama_index