Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,12 @@ def extract_elements(
type="code",
element=line.lstrip("```"),
)
elif currentElement is not None and currentElement.type == "text":
currentElement.element += "\n" + line
Comment on lines -173 to -174
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this eliminated?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This elif branch was the root cause of the bug. When encountering an opening backtick fence (```), if there was already a text element being accumulated, this branch would append the fence line to the text element instead of starting a new code block. By removing it, opening fences now correctly fall through to the else branch, which saves the current element and starts a new code element. This is what allows code blocks to be properly extracted rather than being swallowed into surrounding text.

else:
# Start of a new code block
if currentElement is not None:
elements.append(currentElement)
currentElement = Element(
id=f"id_{len(elements)}", type="text", element=line
id=f"id_{len(elements)}", type="code", element=""
)
elif currentElement is not None and currentElement.type == "code":
currentElement.element += "\n" + line
Expand Down Expand Up @@ -266,10 +265,10 @@ def extract_elements(
element=element.element,
)
else:
# if the element is not a table, keep it as to text
# if the element is not a table, keep its original type
elements[idx] = Element(
id=f"id_{node_id}_{idx}" if node_id else f"id_{idx}",
type="text",
type=element.type,
element=element.element,
)

Expand Down
42 changes: 42 additions & 0 deletions llama-index-core/tests/node_parser/test_markdown_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -2755,3 +2755,45 @@ def test_extract_html_table():
assert test_document.text[nodes[3].start_char_idx : nodes[3].end_char_idx] == table2
assert type(nodes[4]) is TextNode
assert test_document.text[nodes[4].start_char_idx : nodes[4].end_char_idx] == table2


def test_code_block_extraction() -> None:
"""Test that code blocks are properly extracted as code elements."""
node_parser = MarkdownElementNodeParser(llm=MockLLM())

document = """my cool file
```
my cool code block
```
some text after"""

result = node_parser.extract_elements(document)

# Should have 3 elements: text, code, text
assert len(result) == 3
assert result[0].type == "text"
assert "my cool file" in result[0].element
assert result[1].type == "code"
assert "my cool code block" in result[1].element
assert result[2].type == "text"
assert "some text after" in result[2].element


def test_code_block_with_language() -> None:
"""Test code block with language identifier."""
node_parser = MarkdownElementNodeParser(llm=MockLLM())

document = """intro text
```python
def hello():
pass
```
outro text"""

result = node_parser.extract_elements(document)

assert len(result) == 3
assert result[0].type == "text"
assert result[1].type == "code"
assert "def hello():" in result[1].element
assert result[2].type == "text"