Skip to content

Fix: markdown fenced code block extraction - #15630

Merged
KevinHuSh merged 3 commits into
infiniflow:mainfrom
bitloi:fix/15482-preserve-markdown-fences
Jun 4, 2026
Merged

KevinHuSh merged 3 commits into
infiniflow:mainfrom
bitloi:fix/15482-preserve-markdown-fences

Conversation

@bitloi

@bitloi bitloi commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • preserve fenced code blocks when delimiter-based extraction is used
  • support both backtick and tilde fences
  • respect fence length so longer outer fences can contain shorter inner fences
  • keep delimiter splitting unchanged outside fenced blocks

Refs #15482

Type of change

  • Bug Fix (non-breaking change which fixes an issue)

Testing

  • ruff check deepdoc/parser/markdown_parser.py test/unit_test/deepdoc/parser/test_markdown_parser.py
  • python3 run_tests.py -t test/unit_test/deepdoc/parser/test_markdown_parser.py

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Jun 4, 2026
@coderabbitai

coderabbitai Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The 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.

Changes

Markdown fence handling robustness

Layer / File(s) Summary
Fence detection helpers
deepdoc/parser/markdown_parser.py
Adds logging and internal methods to detect fence markers and compute fenced-code ranges (_get_fence_marker, _is_closing_fence, _get_fence_block_ranges).
Delimiter-aware extraction helper
deepdoc/parser/markdown_parser.py
Introduces _extract_delimited_elements() to split on a delimiter while skipping matches inside fenced ranges and optionally emit start_line/end_line metadata.
Main dispatch and code block extraction
deepdoc/parser/markdown_parser.py
extract_elements() delegates delimiter splitting to the helper; dispatch uses _get_fence_marker() to detect code blocks; _extract_code_block() matches closing fences by character and length.
Text block termination refinements
deepdoc/parser/markdown_parser.py
_extract_text_block() now treats fence markers on the current or next line as paragraph boundaries.
Test coverage for fence handling
test/unit_test/deepdoc/parser/test_markdown_parser.py
Adds fixture and TestMarkdownElementExtractorFences with five tests covering custom delimiter behavior inside/outside fences, tilde-fence defaults, and nested fence preservation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • JinHai-CN

Poem

🐰 I peek through ticks and tildes, so sly,
Skipping splitters where the code fences lie.
Nesting kept tidy, lengths noticed right,
I hop through the parser and fix what’s not tight.
Hooray for clean tokens under moonlight!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description covers all required template sections: it clearly explains the problem being solved, specifies the type of change as a bug fix, and includes testing instructions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title 'Fix: markdown fenced code block extraction' clearly describes the main change—fixing how fenced code blocks are handled during Markdown extraction, which is the central focus of the changeset.

✏️ 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@yingfeng
yingfeng requested a review from KevinHuSh June 4, 2026 04:15
@yingfeng yingfeng added the ci Continue Integration label Jun 4, 2026
@yingfeng
yingfeng marked this pull request as draft June 4, 2026 04:16
@yingfeng
yingfeng marked this pull request as ready for review June 4, 2026 04:16

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 597ac1e and e9c3b63.

📒 Files selected for processing (2)
  • deepdoc/parser/markdown_parser.py
  • test/unit_test/deepdoc/parser/test_markdown_parser.py

Comment thread deepdoc/parser/markdown_parser.py Resolved
@codecov

codecov Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (67c3e73) to head (9be9d20).
⚠️ Report is 7 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 4, 2026
@KevinHuSh KevinHuSh changed the title Fix markdown fenced code block extraction Fix: markdown fenced code block extraction Jun 4, 2026
@KevinHuSh
KevinHuSh merged commit 01a5598 into infiniflow:main Jun 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants