Repository navigation
fix(deepdoc): keep pipe tables inside markdown code fences - #17201
Conversation
extract_tables_and_remainder matched pipe tables anywhere in the document, with no notion of fenced code. a doc that documents markdown syntax lost its example: the table was appended to `tables` and deleted from the remainder, leaving a hollow fence behind and re-emitting the example as a bogus standalone table chunk. skip matches that start inside a fence, mirroring the shielding MarkdownElementExtractor already does in this module (_fenced_code_ranges / _protected_ranges). genuine tables outside fences are still extracted. adds regression tests for both cases.
📝 WalkthroughWalkthroughMarkdown table extraction now detects fenced code blocks and leaves table-like content inside them unchanged. New tests verify fenced examples remain in the remainder while genuine tables outside fences are still extracted. ChangesMarkdown table fence protection
Estimated code review effort: 3 (Moderate) | ~20 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
deepdoc/parser/markdown_parser.py (1)
32-50: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winExtend code fence protection to HTML tag replacement and HTML table extraction.
The current changes successfully protect Markdown pipe tables inside fenced code blocks. However, the exact same issue applies to HTML table extraction and HTML tag attribute stripping (lines 89-136). Without similar protection, HTML examples inside code fences will still be modified (e.g.,
<table border="1">stripped to<table>) or extracted, hollowing out the code block.Consider applying this newly introduced fence protection logic consistently to the HTML passes.
♻️ Proposed refactor to protect HTML tables and tags
Update the tag replacement pass (around line 93):
# Replace any TAGS e.g. <table ...> to <table> TAGS = ["table", "td", "tr", "th", "tbody", "thead", "div"] table_with_attributes_pattern = re.compile(rf"<(?:{'|'.join(TAGS)})[^>]*>", re.IGNORECASE) + fenced_spans_tags = [(m.start(), m.end()) for m in fence_pattern.finditer(working_text)] def replace_tag(m): + if any(start <= m.start() < end for start, end in fenced_spans_tags): + return m.group() tag_name = re.match(r"<(\w+)", m.group()).group(1) return "<{}>".format(tag_name) working_text = re.sub(table_with_attributes_pattern, replace_tag, working_text)Update the HTML table extraction pass (around line 121):
def replace_html_tables(): nonlocal working_text new_text = "" last_end = 0 + fenced_spans_html = [(m.start(), m.end()) for m in fence_pattern.finditer(working_text)] for match in html_table_pattern.finditer(working_text): + if any(start <= match.start() < end for start, end in fenced_spans_html): + continue raw_table = match.group() tables.append(raw_table)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@deepdoc/parser/markdown_parser.py` around lines 32 - 50, Extend the fence-aware filtering in replace_tables_with_rendered_html to the HTML tag attribute replacement and HTML table extraction passes around the existing HTML processing logic. Reuse the existing fence_pattern and fenced_spans computed from working_text, skip matches whose start lies within a fenced span, and preserve their original text so HTML examples remain unchanged inside code fences.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@deepdoc/parser/markdown_parser.py`:
- Around line 32-50: Extend the fence-aware filtering in
replace_tables_with_rendered_html to the HTML tag attribute replacement and HTML
table extraction passes around the existing HTML processing logic. Reuse the
existing fence_pattern and fenced_spans computed from working_text, skip matches
whose start lies within a fenced span, and preserve their original text so HTML
examples remain unchanged inside code fences.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: a8c2cc1d-20f7-4757-bbef-478f77d1f9c6
📒 Files selected for processing (2)
deepdoc/parser/markdown_parser.pytest/unit_test/deepdoc/parser/test_markdown_parser.py
review follow-up: the tag-stripping and html-table passes had the same blind spot as the pipe-table pass. an html example inside a fence had its attributes stripped (<table border="1"> -> <table>) and the table pulled out into `tables`, leaving a hollow fence behind. shield both passes with the same fence spans. html tables outside fences are still normalised and extracted exactly as before. adds a regression test.
|
Good catch — that was a real gap, applied in Confirmed the same failure on the HTML passes before fixing: for a HTML tables outside fences are unchanged — still normalised to |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
deepdoc/parser/markdown_parser.py (1)
37-40: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winMatch the fence character, not the exact opener length.
\\1requires the closing fence to have exactly the same length as the opener. Markdown allows a closing fence to be longer, so an opener of ``` followed by ```` is treated as unclosed; the\Zfallback then shields all subsequent content from extraction.Proposed fix
- r"^[ \t]{0,3}(`{3,}|~{3,})[^\n]*\n.*?(?:^[ \t]{0,3}\1[ \t]*$|\Z)", + r"^[ \t]{0,3}(?P<fence_char>`|~)(?P=fence_char){2,}[^\n]*\n" + r".*?(?:^[ \t]{0,3}(?P=fence_char){3,}[ \t]*$|\Z)",Add a regression test with a longer closing fence followed by a genuine table.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@deepdoc/parser/markdown_parser.py` around lines 37 - 40, Update the fence_pattern in the markdown parser so the closing fence matches the opener’s character while allowing a closing run longer than the opener, rather than requiring identical length. Preserve support for both backtick and tilde fences and ensure subsequent table content remains extractable; add a regression test covering a longer closing fence followed by a genuine table.
🧹 Nitpick comments (2)
test/unit_test/deepdoc/parser/test_markdown_parser.py (1)
209-220: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winExercise the HTML-table extraction branch in this regression test.
With
<table border="1">, tag preservation prevents the"<table>"fast-path from running, so this test only validates tag stripping. Add a second fenced fixture containing a bare<table>to verifyreplace_html_tablesalso skips it.[details]
+ bare_table = "```html\n<table>\n<tr><td>A</td></tr>\n</table>\n```" + remainder, tables = parser.extract_tables_and_remainder( + bare_table, separate_tables=True + ) + assert tables == [] + assert "<table>" in remainder + assert "</table>" in remainder🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/unit_test/deepdoc/parser/test_markdown_parser.py` around lines 209 - 220, Extend test_html_table_inside_code_fence_is_left_intact with a second fenced Markdown fixture containing a bare <table> element, then call extract_tables_and_remainder with separate_tables=True and assert no tables are extracted while both table tags remain in the remainder, exercising replace_html_tables’ fenced-code branch.deepdoc/parser/markdown_parser.py (1)
47-50: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd debug logging for the new fence-skipping flows.
These branches silently skip Markdown tables, HTML tags, and HTML tables. Add low-volume debug logs identifying the processing pass and match position, without logging document content. As per coding guidelines, new Python flows should add logging.
Also applies to: 96-98, 130-133
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@deepdoc/parser/markdown_parser.py` around lines 47 - 50, Add low-volume debug logging to the fence-skipping branches in the Markdown parser, including the flows around the shown check and the corresponding Markdown-table, HTML-tag, and HTML-table branches. Log the processing pass and match position without including document content, using the module’s existing logger conventions.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@deepdoc/parser/markdown_parser.py`:
- Around line 37-40: Update the fence_pattern in the markdown parser so the
closing fence matches the opener’s character while allowing a closing run longer
than the opener, rather than requiring identical length. Preserve support for
both backtick and tilde fences and ensure subsequent table content remains
extractable; add a regression test covering a longer closing fence followed by a
genuine table.
---
Nitpick comments:
In `@deepdoc/parser/markdown_parser.py`:
- Around line 47-50: Add low-volume debug logging to the fence-skipping branches
in the Markdown parser, including the flows around the shown check and the
corresponding Markdown-table, HTML-tag, and HTML-table branches. Log the
processing pass and match position without including document content, using the
module’s existing logger conventions.
In `@test/unit_test/deepdoc/parser/test_markdown_parser.py`:
- Around line 209-220: Extend test_html_table_inside_code_fence_is_left_intact
with a second fenced Markdown fixture containing a bare <table> element, then
call extract_tables_and_remainder with separate_tables=True and assert no tables
are extracted while both table tags remain in the remainder, exercising
replace_html_tables’ fenced-code branch.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6b66909d-5245-4668-90e1-8cd299854f38
📒 Files selected for processing (2)
deepdoc/parser/markdown_parser.pytest/unit_test/deepdoc/parser/test_markdown_parser.py
review follow-up: - the existing html-fence test used <table border="1">, which the "<table>" fast-path guard skips, so the html-table extraction pass (and its new fence check) was never exercised. add a bare-<table> fenced fixture that drives that pass. - add low-volume debug logs to the three fence-skip branches (markdown table, html tag, html table), position only, no document content.
|
Both applied in
|
|
Heads-up: the red So the gitee clone couldn't authenticate/reach the host — the test code never ran. The same preflight passed on my other PRs (#17062, #17064), so it looks transient. A re-run should clear it. This PR only touches |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #17201 +/- ##
==========================================
- Coverage 94.56% 90.65% -3.91%
==========================================
Files 10 10
Lines 717 717
Branches 118 118
==========================================
- Hits 678 650 -28
- Misses 25 39 +14
- Partials 14 28 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
RAGFlowMarkdownParser.extract_tables_and_remaindermatches pipe tables anywhere in the document, with no notion of fenced code. A table inside a fenced code block is therefore appended totablesand deleted from the remainder.For any doc that documents markdown syntax (or LLM-generated docs containing a
```markdownblock), the example is destroyed. Given this input:extract_tables_and_remainder(doc, separate_tables=True)currently returns:The fence is left hollow and the example resurfaces as a bogus standalone table chunk (via
rag/app/naive.py, which feeds the remainder toMarkdownElementExtractorand renders each extracted table separately).MarkdownElementExtractorin this same module already shields fenced code (_fenced_code_ranges/_protected_ranges) so delimiters never split inside a fence — table extraction simply missed the same case. This skips matches that start inside a fence; genuine tables outside fences are extracted exactly as before.Tests
Two regression tests added to
TestMarkdownTableDedup: one asserting a fenced example survives, one asserting a real table alongside a fenced example is still extracted. Both fail without the change and pass with it; the existing 12 markdown tests are unaffected.