Repository navigation
Fix: markdown fenced code block extraction - #15630
Conversation
📝 WalkthroughWalkthroughThe markdown parser now detects backtick and tilde fenced code blocks of variable lengths, centralizes delimiter-aware extraction that skips delimiters inside fenced regions, updates code/text block extraction to use fence-aware logic, and adds unit tests validating nested and delimiter interactions. ChangesMarkdown fence handling robustness
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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.
Actionable comments posted: 2
🤖 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.
Inline comments:
In `@deepdoc/parser/markdown_parser.py`:
- Around line 194-214: In _extract_delimited_elements, add lightweight debug
logging (using the module/class logger at DEBUG level) that reports the number
of fenced ranges returned by _fenced_code_ranges (log protected_ranges length)
and logs each time a delimiter match is skipped because it falls inside a
protected range (include match.start(), match.group() or delimiters snippet, and
the protected range tuple); also optionally log when you append sections via
_append_delimited_section (e.g., start/end indices and include_meta flag) to
help trace extraction flow. Ensure logs are non-blocking, at DEBUG level, and
reference the existing local variables protected_ranges, match, and last_end so
callers can correlate parser behavior with input.
In `@test/unit_test/deepdoc/parser/test_markdown_parser.py`:
- Around line 24-30: The module-level stubbing of the markdown package (creating
markdown_stub, setting markdown_stub.markdown and writing to
sys.modules["markdown"]) leaks global state across tests; refactor this into a
pytest fixture that uses monkeypatch to insert the fake module per-test and to
restore sys.modules afterwards, move any import/use of markdown/markdown_parser
into that fixture or into the test body so the stub is only active for the test,
and repeat the same change for the similar block currently at lines 32-39;
reference the symbols markdown_stub, sys.modules["markdown"], and the test
module test_markdown_parser.py when making the change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 42bf1915-3f72-48a9-9fea-bd180b13a26f
📒 Files selected for processing (2)
deepdoc/parser/markdown_parser.pytest/unit_test/deepdoc/parser/test_markdown_parser.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15630 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
What problem does this PR solve?
Markdown extraction currently applies custom delimiters before respecting fenced code blocks. When a delimiter such as a newline is configured, fenced code can be split into separate chunks, and longer outer fences can be closed incorrectly by shorter nested fences.
This PR keeps the fix intentionally narrow for the Markdown chunking discussion in #15482:
Refs #15482
Type of change
Testing
ruff check deepdoc/parser/markdown_parser.py test/unit_test/deepdoc/parser/test_markdown_parser.pypython3 run_tests.py -t test/unit_test/deepdoc/parser/test_markdown_parser.py