|
6 | 6 | * A powerful CLI tool for parsing and manipulating markdown files as tree structures. |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { MarkdownTreeParser } from '../lib/markdown-parser.js'; |
10 | 9 | import fs from 'node:fs/promises'; |
11 | 10 | import path from 'node:path'; |
12 | 11 | import { fileURLToPath } from 'node:url'; |
| 12 | +import { MarkdownTreeParser } from '../lib/markdown-parser.js'; |
13 | 13 |
|
14 | 14 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
15 | 15 | const packagePath = path.join(__dirname, '..', 'package.json'); |
@@ -352,7 +352,7 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser |
352 | 352 | if (Object.keys(stats.headings.byLevel).length > 0) { |
353 | 353 | console.log(' By level:'); |
354 | 354 | for (const [level, count] of Object.entries(stats.headings.byLevel).sort( |
355 | | - ([a], [b]) => Number.parseInt(a) - Number.parseInt(b) |
| 355 | + ([a], [b]) => Number.parseInt(a, 10) - Number.parseInt(b, 10) |
356 | 356 | )) { |
357 | 357 | console.log(` Level ${level}: ${count}`); |
358 | 358 | } |
@@ -468,13 +468,13 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser |
468 | 468 | options.output = args[i + 1]; |
469 | 469 | i++; // skip next arg |
470 | 470 | } else if (arg === '--level' || arg === '-l') { |
471 | | - options.level = Number.parseInt(args[i + 1]) || 2; |
| 471 | + options.level = Number.parseInt(args[i + 1], 10) || 2; |
472 | 472 | i++; // skip next arg |
473 | 473 | } else if (arg === '--format' || arg === '-f') { |
474 | 474 | options.format = args[i + 1] || 'text'; |
475 | 475 | i++; // skip next arg |
476 | 476 | } else if (arg === '--max-level') { |
477 | | - options.maxLevel = Number.parseInt(args[i + 1]) || 3; |
| 477 | + options.maxLevel = Number.parseInt(args[i + 1], 10) || 3; |
478 | 478 | i++; // skip next arg |
479 | 479 | } else if (arg === '--recursive' || arg === '-r') { |
480 | 480 | options.recursive = true; |
@@ -517,7 +517,7 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser |
517 | 517 | console.error(MESSAGES.USAGE_EXTRACT_ALL); |
518 | 518 | process.exit(1); |
519 | 519 | } |
520 | | - const level = args[2] ? Number.parseInt(args[2]) : options.level; |
| 520 | + const level = args[2] ? Number.parseInt(args[2], 10) : options.level; |
521 | 521 | await this.extractAllSections(args[1], level, options.output); |
522 | 522 | } |
523 | 523 |
|
@@ -645,9 +645,27 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser |
645 | 645 | // Find all level 2 headings and their positions |
646 | 646 | const sections = []; |
647 | 647 | let currentSection = null; |
| 648 | + let inCodeBlock = false; |
648 | 649 |
|
649 | 650 | for (let i = 0; i < lines.length; i++) { |
650 | 651 | const line = lines[i]; |
| 652 | + const trimmed = line.trim(); |
| 653 | + |
| 654 | + // Track fenced code blocks and ignore headings within them |
| 655 | + if (trimmed.startsWith('```') || trimmed.startsWith('~~~')) { |
| 656 | + if (currentSection) { |
| 657 | + currentSection.lines.push(line); |
| 658 | + } |
| 659 | + inCodeBlock = !inCodeBlock; |
| 660 | + continue; |
| 661 | + } |
| 662 | + |
| 663 | + if (inCodeBlock) { |
| 664 | + if (currentSection) { |
| 665 | + currentSection.lines.push(line); |
| 666 | + } |
| 667 | + continue; |
| 668 | + } |
651 | 669 |
|
652 | 670 | // Check for main title (level 1) |
653 | 671 | if (line.match(/^# /)) { |
@@ -819,8 +837,20 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser |
819 | 837 | */ |
820 | 838 | adjustHeadingLevels(content, adjustment) { |
821 | 839 | const lines = content.split('\n'); |
| 840 | + let inCodeBlock = false; |
822 | 841 |
|
823 | 842 | const adjustedLines = lines.map((line) => { |
| 843 | + // Check for code block boundaries (``` or ~~~) |
| 844 | + if (line.trim().startsWith('```') || line.trim().startsWith('~~~')) { |
| 845 | + inCodeBlock = !inCodeBlock; |
| 846 | + return line; |
| 847 | + } |
| 848 | + |
| 849 | + // Skip heading adjustment if we're inside a code block |
| 850 | + if (inCodeBlock) { |
| 851 | + return line; |
| 852 | + } |
| 853 | + |
824 | 854 | const headingMatch = line.match(PATTERNS.HEADING_LEVEL_1_5); |
825 | 855 | if (headingMatch) { |
826 | 856 | const [, hashes, rest] = headingMatch; |
|
0 commit comments