Markdown Tree Parser Truncates Sections At # Comments Within Embedded Bash Code Blocks
Hi,
I'm trying to use md-tree to explode the attached file:
testInputFileForTruncationBug.md
but when it hits a # inside an embedded bash statement it truncates the section.
I would expect the whole "Level 4 Section Header Number 1" section to be in the testInputFileForTruncationBug/level-2-section-header-number-3.md file, but what actually happens is the file is truncated at the #, I think because the md-tree tool thinks it has reached a new Level 1 Section Header.
I've attached the output of the session below.
To give context this tool is being used to shard large Architecture documents while doing the BMAD process at:
https://github.com/bmadcode/BMAD-METHOD
which uses AI to create and execute large spec files.
Thanks
Steve.
stevepersonal@Steves-MacBook-Pro test-output % md-tree explode testInputFileForTruncationBug.md testInputFileForTruncationBug
📚 Exploding 3 sections from testInputFileForTruncationBug.md to testInputFileForTruncationBug:
✅ Written to testInputFileForTruncationBug/level-2-section-header-number-1.md
✅ Processing Level 2 Section Header Number 1 → level-2-section-header-number-1.md
✅ Written to testInputFileForTruncationBug/level-2-section-header-number-2.md
✅ Processing Level 2 Section Header Number 2 → level-2-section-header-number-2.md
✅ Written to testInputFileForTruncationBug/level-2-section-header-number-3.md
✅ Processing Level 2 Section Header Number 3 → level-2-section-header-number-3.md
✅ Written to testInputFileForTruncationBug/index.md
✅ Processing Table of Contents → index.md
✨ Document exploded to testInputFileForTruncationBug (4 files)
stevepersonal@Steves-MacBook-Pro test-output %
stevepersonal@Steves-MacBook-Pro test-output % cat testInputFileForTruncationBug/level-2-section-header-number-3.md
# Level 2 Section Header Number 3
blah blah blah
stevepersonal@Steves-MacBook-Pro test-output %
I'm also experiencing this issue. Here is the markdown in the file:
```cli
# Keep the 3 most recent scrapes and delete all others
bntd-plwr clean --keep 3
```
And the output comes out as:
```cli
@ksylvan - I got Claude Code to write some tests for this and a fix. I've created a pull request, which has 2 commits. The first was just because when I ran your linting code without making changes it automatically reformatted some things, and also I added an exclusion of CLAUDE.md to your .gitignore as Claude Code created that file locally. The 2nd commit is the real fix (just 2 files - test + src).
Claude also tested this manually on:
testInputFileForTruncationBug.md
by calling:
node bin/md-tree.js explode testInputFileForTruncationBug.md ./test-output
and confirmed the output in the following zip has the right sections now:
I fixed the bug in a minimal way (and also made sure that you can explode and reassemble a file to get ack the exact same file)
Fix coming.
@ksylvan thanks for sorting this out, and sorry I didn't get round to splitting the fix into multiple issues. AI definitely has a thing about over engineering things, so it's probably good you did a minimal fix 🙂