Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
"[markdown]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"files.trimTrailingWhitespace": false
}
},
"biome.enabled": false
}
40 changes: 35 additions & 5 deletions bin/md-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* A powerful CLI tool for parsing and manipulating markdown files as tree structures.
*/

import { MarkdownTreeParser } from '../lib/markdown-parser.js';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { MarkdownTreeParser } from '../lib/markdown-parser.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const packagePath = path.join(__dirname, '..', 'package.json');
Expand Down Expand Up @@ -352,7 +352,7 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
if (Object.keys(stats.headings.byLevel).length > 0) {
console.log(' By level:');
for (const [level, count] of Object.entries(stats.headings.byLevel).sort(
([a], [b]) => Number.parseInt(a) - Number.parseInt(b)
([a], [b]) => Number.parseInt(a, 10) - Number.parseInt(b, 10)
)) {
console.log(` Level ${level}: ${count}`);
}
Expand Down Expand Up @@ -468,13 +468,13 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
options.output = args[i + 1];
i++; // skip next arg
} else if (arg === '--level' || arg === '-l') {
options.level = Number.parseInt(args[i + 1]) || 2;
options.level = Number.parseInt(args[i + 1], 10) || 2;
i++; // skip next arg
} else if (arg === '--format' || arg === '-f') {
options.format = args[i + 1] || 'text';
i++; // skip next arg
} else if (arg === '--max-level') {
options.maxLevel = Number.parseInt(args[i + 1]) || 3;
options.maxLevel = Number.parseInt(args[i + 1], 10) || 3;
i++; // skip next arg
} else if (arg === '--recursive' || arg === '-r') {
options.recursive = true;
Expand Down Expand Up @@ -517,7 +517,7 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
console.error(MESSAGES.USAGE_EXTRACT_ALL);
process.exit(1);
}
const level = args[2] ? Number.parseInt(args[2]) : options.level;
const level = args[2] ? Number.parseInt(args[2], 10) : options.level;
await this.extractAllSections(args[1], level, options.output);
}

Expand Down Expand Up @@ -645,9 +645,27 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
// Find all level 2 headings and their positions
const sections = [];
let currentSection = null;
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;
}

// Check for main title (level 1)
if (line.match(/^# /)) {
Expand Down Expand Up @@ -819,8 +837,20 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
*/
adjustHeadingLevels(content, adjustment) {
const lines = content.split('\n');
let inCodeBlock = false;

const adjustedLines = lines.map((line) => {
// Check for code block boundaries (``` or ~~~)
if (line.trim().startsWith('```') || line.trim().startsWith('~~~')) {
inCodeBlock = !inCodeBlock;
return line;
}

// Skip heading adjustment if we're inside a code block
if (inCodeBlock) {
return line;
}

const headingMatch = line.match(PATTERNS.HEADING_LEVEL_1_5);
if (headingMatch) {
const [, hashes, rest] = headingMatch;
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default MarkdownTreeParser;

// Export additional utilities that might be useful
export { unified } from 'unified';
export { visit } from 'unist-util-visit';
export { selectAll, select } from 'unist-util-select';
export { find } from 'unist-util-find';
export { select, selectAll } from 'unist-util-select';
export { visit } from 'unist-util-visit';

/**
* Convenience function to create a new parser instance
Expand Down
111 changes: 49 additions & 62 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kayvan/markdown-tree-parser",
"version": "1.6.0",
"version": "1.6.1",
"description": "A powerful JavaScript library and CLI tool for parsing and manipulating markdown files as tree structures using the remark/unified ecosystem",
"type": "module",
"main": "index.js",
Expand Down Expand Up @@ -34,10 +34,10 @@
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@eslint/js": "^9.29.0",
"@types/node": "^24.0.3",
"eslint": "^9.29.0",
"prettier": "^3.5.3"
"@eslint/js": "^9.34.0",
"@types/node": "^24.3.0",
"eslint": "^9.34.0",
"prettier": "^3.6.2"
},
"engines": {
"node": ">=16.0.0"
Expand Down
Loading