Fix section extraction bug in extractSection method #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix section extraction bug in extractSection method
Summary
Fixed a critical bug in the
extractSectionmethod where themd-tree extractcommand was incorrectly extracting additional sections beyond the requested section. The issue was caused by usingvisit()function for tree traversal, which doesn't work correctly with array indices needed for section slicing. Replaced with directforloops overtree.childrento ensure accurate index tracking and proper section boundary detection.Files Changed
extractSectionmethod to use direct array iteration instead ofvisit()function calls. This ensures correct index tracking when finding headings and determining section boundaries.Code Changes
markdown-parser.js
Before (problematic code):
After (fixed code):
Reason for Changes
This change fixes a bug where the
md-tree extractcommand was extracting more content than intended. For example, runningmd-tree extract bmad-prd.md '6. Epic Overview'would incorrectly return sections 6, 7, and 8 instead of just section 6.The root cause was that the
visit()function traverses ALL nodes in the AST tree (including nested nodes within paragraphs, lists, etc.), but the section extraction logic needed to work with direct children indices to properly slice thetree.childrenarray.Impact of Changes
Test Plan
The changes were thoroughly tested using multiple approaches:
Reproduction Testing: Verified the original bug was fixed by testing the problematic command:
node bin/md-tree.js extract tmp/bmad-prd.md '6. Epic Overview'Edge Case Testing: Tested extraction of different sections including:
Regression Testing: Ran the full test suite to ensure no existing functionality was broken:
Multiple File Testing: Verified the fix works with different markdown files and heading structures
Additional Notes