Skip to content

Conversation

ksylvan
Copy link
Owner

@ksylvan ksylvan commented Aug 22, 2025

Fix heading detection in fenced code blocks

Summary

This PR introduces improvements to code parsing reliability by fixing issues with heading detection inside fenced code blocks, updates development dependencies to their latest versions, and makes minor code quality improvements throughout the codebase.

Related Issues

Closes #7

Files Changed

  • .vscode/settings.json: Disabled Biome linter integration to prevent conflicts with the existing ESLint setup.

  • bin/md-tree.js: Fixed critical bug where markdown headings inside code blocks were incorrectly parsed as actual document headings. Also improved parseInt calls by explicitly specifying radix parameter and reorganized imports alphabetically.

  • index.js: Reordered export statements alphabetically for better code organization.

  • package.json & package-lock.json: Updated version to 1.6.1 and upgraded development dependencies to latest versions.

  • pnpm-lock.yaml: Updated lockfile to reflect dependency changes.

  • test/test-cli.js: Added new test case to verify correct handling of hash symbols inside code blocks.

  • test/test.js: Minor import statement reordering for consistency.

Code Changes

Critical Bug Fix - Code Block Parsing

// bin/md-tree.js - Added code block tracking
let inCodeBlock = false;

for (let i = 0; i < lines.length; i++) {
  const line = lines[i];
  const trimmed = line.trim();

  // Track fenced code blocks and ignore headings within them
  if (trimmed.startsWith('```') || trimmed.startsWith('~~~')) {
    if (currentSection) {
      currentSection.lines.push(line);
    }
    inCodeBlock = !inCodeBlock;
    continue;
  }

  if (inCodeBlock) {
    if (currentSection) {
      currentSection.lines.push(line);
    }
    continue;
  }
  // ... rest of heading detection logic
}

parseInt Improvements

// Before
Number.parseInt(args[i + 1])

// After  
Number.parseInt(args[i + 1], 10)

Reason for Changes

  1. Code Block Bug Fix: Users reported that markdown documents containing code examples with bash comments (starting with #) were being incorrectly parsed, causing sections to be truncated when using the explode command. This was a critical issue affecting document integrity.

  2. parseInt Radix: Added explicit radix parameter (base 10) to all parseInt calls to follow JavaScript best practices and avoid potential parsing issues with strings that could be interpreted as octal or hexadecimal.

  3. Dependency Updates: Updated ESLint, Prettier, and Node.js types to benefit from latest bug fixes and improvements.

  4. Code Organization: Alphabetized imports and exports for better maintainability and consistency across the codebase.

Impact of Changes

  • Functionality: The explode and section extraction commands now correctly handle markdown documents containing code blocks with hash symbols, preventing data loss and ensuring document integrity.

  • Reliability: Explicit radix in parseInt calls eliminates edge cases where numeric strings could be misinterpreted.

  • Development Experience: Updated development dependencies provide better linting, formatting, and type checking capabilities.

  • Performance: No performance impact expected; changes are primarily bug fixes and code organization improvements.

Test Plan

Added comprehensive test case CLI explode handles # inside code blocks that:

  1. Creates a markdown file with headings inside code blocks
  2. Runs the explode command
  3. Verifies that the code block content is preserved intact
  4. Confirms that content after code blocks is not lost

Existing tests continue to pass, confirming backward compatibility.

Additional Notes

  • The fix applies to both the extractAllSections and adjustHeadingLevels methods to ensure consistent behavior across all CLI commands.
  • Similar patterns using triple backticks (```) and tildes (~~~) are both handled correctly.
  • This is a patch release (1.6.0 → 1.6.1) as it contains backward-compatible bug fixes only.

### CHANGES

- Add `biome.enabled` setting to VSCode config
- Reorder imports in `md-tree.js` for clarity
- Use radix in `Number.parseInt` for consistency
- Track and ignore headings in code blocks
- Adjust heading levels outside code blocks only
- Update package version to `1.6.1`
- Upgrade dev dependencies: ESLint, Prettier, etc.
- Add test for CLI handling of code blocks
@ksylvan ksylvan merged commit fb08e85 into main Aug 22, 2025
4 checks passed
@ksylvan ksylvan deleted the 0822-fix-truncation-in-code-blocks branch August 22, 2025 23:37
@halso
Copy link

halso commented Aug 23, 2025

@ksylvan thanks for fixing this. I see 1.6.1 was created on npm 11 hours ago so I would guess this fix is in that version? I'm a newbie so was wondering if you'd have a CHANGELOG or Releases file that shows which issues are fixed in each releases? Also whether something could auto generate that? Also whether you can auto link issues like this to the release it shipped in? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Markdown Tree Parser Truncates Sections At # Comments Within Embedded Bash Code Blocks
2 participants