fix(parser): skip hash symbols inside code blocks when parsing headers
Problem
The markdown parser's parseSections() and getContentUntilNextHeader() methods use a regex pattern ^(#{1,6})\s+(.+)$ to detect headers, but they don't account for fenced code blocks. Lines starting with # inside code blocks (e.g. bash comments like # install dependencies) are incorrectly treated as section headers, causing the parser to split content at those lines and produce incorrect section boundaries.
Solution
Added inCodeBlock state tracking to both parseSections() and getContentUntilNextHeader():
- Toggle the flag when encountering lines that start with triple backticks (```````)
- Skip header regex matching while inside a code block
- Preserve code block content in the output as-is
Changes
src/core/parsers/markdown-parser.ts: Added code block fence detection toparseSections()andgetContentUntilNextHeader()test/core/parsers/markdown-parser.test.ts: Added test case with a spec containing code blocks with bash comments (#lines), verifying all 3 requirements are correctly parsed
Verification
- All 14 parser tests pass
- ESLint passes with no errors
- Full test suite: all failures are pre-existing in
zsh-installer.test.ts(unrelated)
Fixes #312
Summary by CodeRabbit
- Bug Fixes
- Fixed markdown parser to correctly handle code blocks when identifying section headers. Hash symbols and markdown syntax inside code blocks are no longer incorrectly treated as actual headers. Code block delimiters are properly preserved during parsing, ensuring accurate document structure identification.
The markdown parser is fixed to properly handle code blocks by tracking block state during header detection and content collection. Hash symbols and other syntax inside fenced code blocks are no longer incorrectly parsed as markdown headers, preserving document structure and accurate requirement counts. 🎯 2 (Simple) | ⏱️ ~10 minutes 🐰 A rabbit hops through code blocks bright,
No longer fooled by hashes in the night,
Where triple backticks guard the bash and shell,
The parser now knows markdown quite well,
One flag to track, and all is right! ✨ ✏️ Tip: You can configure your own custom pre-merge checks in the settings.📝 Walkthrough
Walkthrough
Changes
Cohort / File(s)
Summary
Code Block State Tracking
src/core/parsers/markdown-parser.tsModified
parseSections() and getContentUntilNextHeader() to track in-code-block state using triple-backtick delimiters. Header detection is skipped inside code blocks; code block content and delimiters are preserved in collected output.
Code Block Header Test
test/core/parsers/markdown-parser.test.tsAdded test case validating that hash symbols inside fenced code blocks are not treated as markdown headers, confirming correct requirement parsing when code blocks contain hash-prefixed content.
Estimated code review effort
Poem
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name
Status
Explanation
Description Check
✅ Passed
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
✅ Passed
The title clearly and specifically describes the main fix: skipping hash symbols inside code blocks when parsing markdown headers.
Linked Issues check
✅ Passed
The PR implements all key objectives from issue
#312: tracks code block state, skips header detection in code blocks, and preserves code block content unchanged.
Out of Scope Changes check
✅ Passed
All changes are directly scoped to fixing the code block parsing issue: markdown parser logic updates and corresponding test case additions.
Docstring Coverage
✅ Passed
No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
[!TIP] Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Share your feedback on Discord.
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 @coderabbitai help to get the list of available commands and usage tips.
Greptile Summary
This PR fixes a bug where hash symbols (#) inside fenced code blocks were incorrectly treated as markdown headers, causing incorrect section parsing. The fix adds inCodeBlock state tracking to toggle when encountering triple backticks, skipping header regex matching while inside code blocks.
Key Changes:
- Added code block fence detection (
line.trim().startsWith('```')) toparseSections()andgetContentUntilNextHeader() - Toggle
inCodeBlockflag when encountering code fences - Skip header matching when
inCodeBlockis true - Test case verifies 3 requirements are correctly parsed despite bash comments in code blocks
Implementation Notes:
- Uses simple toggle logic (not tracking backtick count), which is sufficient for standard triple-backtick fences
parseSections()skips code fence lines;getContentUntilNextHeader()includes them in content (both behaviors are correct for their purposes)- Handles code blocks with language specifiers (e.g.,
```bash)
Suggestion:
- Consider extending the fix to
parseRequirements()method (line 184), which has similar logic checking for#symbols without code block awareness
Confidence Score: 4/5
- This PR is safe to merge with minimal risk - it fixes a clear bug with appropriate test coverage
- Score reflects a well-implemented fix that addresses the stated problem. Deducted one point due to the related issue in
parseRequirements()not being addressed, though this is a minor consistency concern rather than a blocker. The implementation is clean, the test coverage validates the fix, and the change is low-risk. - No files require special attention - both changes are straightforward and well-tested
Important Files Changed
| Filename | Overview |
|---|---|
| src/core/parsers/markdown-parser.ts | Added code block state tracking to parseSections() and getContentUntilNextHeader() to prevent hash symbols inside fenced code blocks from being incorrectly treated as headers. Implementation is clean and handles the main use case well. |
| test/core/parsers/markdown-parser.test.ts | Added comprehensive test case verifying that hash symbols inside code blocks are correctly ignored during section parsing, ensuring all 3 requirements are properly parsed. |
Last reviewed commit: f066d91
Example case that would break: The loop would incorrectly break at Additional Comments (1)
src/core/parsers/markdown-parser.ts
parseRequirements() also checks for # symbols to detect child headers but doesn't account for code blocks. Consider applying the same code block detection logic here for consistency.### Requirement: Support deployment
Description with example:
\`\`\`bash
# Install dependencies
npm install
\`\`\`
More description text here.
# Install dependencies, excluding subsequent content from the requirement text extraction.
Hi — just a gentle bump on this. Happy to make any changes if needed!
While triaging #312 I did some verification on this PR: it appears to have been superseded by #966 (merged April), which implemented the same code-fence handling with additional coverage (tilde fences, fence-length matching) — that's also why this branch now shows conflicts. The repro from #312 passes on current main, and equivalent regression tests already exist in markdown-parser.test.ts. Sharing in case it saves a rebase — deferring to maintainers on whether to close.
Thanks for putting this together. The underlying parser bug was real, and this PR was useful when it was opened.
Since then, the fix has been superseded by merged PR #966, which landed broader fenced-code handling on main, including tilde fences, fence-length handling, and regression coverage. Current main already handles the #312 repro, so merging this now would mostly add conflict churn and risk reintroducing a narrower implementation. This PR is also currently conflicting and does not have a fresh full CI signal, so we’re going to close it rather than revive it.
Thanks again for the helpful contribution.