Skip to content

Commit a583684

Browse files
committed
feat: add heading level normalization for split markdown sections
## CHANGES - Add `decrementHeadingLevels` helper method - Adjust heading levels when splitting sections - Make split sections start at h1 - Deep clone trees to avoid mutations - Update tests for new heading levels - Bump version to 1.2.1
1 parent f2c2d37 commit a583684

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

bin/md-tree.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,10 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
472472
for (let i = 0; i < sections.length; i++) {
473473
const section = sections[i];
474474
const headingText = section.headingText;
475-
const markdown = await this.parser.stringify(section.tree);
475+
476+
// Decrement heading levels by 1 so the section starts at level 1
477+
const adjustedTree = this.decrementHeadingLevels(section.tree);
478+
const markdown = await this.parser.stringify(adjustedTree);
476479

477480
// Generate filename without numbered prefix
478481
const filename = `${this.sanitizeFilename(headingText)}.md`;
@@ -574,6 +577,30 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
574577

575578
return null;
576579
}
580+
581+
// Helper method to decrement all heading levels in a tree by 1
582+
decrementHeadingLevels(tree) {
583+
if (!tree || !tree.children) return tree;
584+
585+
// Create a deep copy to avoid modifying the original tree
586+
const clonedTree = JSON.parse(JSON.stringify(tree));
587+
588+
const decrementNode = (node) => {
589+
if (node.type === 'heading' && node.depth > 1) {
590+
node.depth = node.depth - 1;
591+
}
592+
593+
if (node.children) {
594+
node.children.forEach(decrementNode);
595+
}
596+
};
597+
598+
if (clonedTree.children) {
599+
clonedTree.children.forEach(decrementNode);
600+
}
601+
602+
return clonedTree;
603+
}
577604
}
578605

579606
// Run CLI

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kayvan/markdown-tree-parser",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "A powerful JavaScript library and CLI tool for parsing and manipulating markdown files as tree structures using the remark/unified ecosystem",
55
"type": "module",
66
"main": "index.js",

test/test-cli.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,31 +243,31 @@ async function runTests() {
243243
const introPath = path.join(outputDir, 'introduction.md');
244244
const introContent = await fs.readFile(introPath, 'utf-8');
245245
assert(
246-
introContent.includes('## Introduction'),
246+
introContent.includes('# Introduction'),
247247
'Introduction file should have section heading'
248248
);
249249
assert(
250250
introContent.includes('This is the introduction section'),
251251
'Should have section content'
252252
);
253253
assert(
254-
introContent.includes('### Background'),
254+
introContent.includes('## Background'),
255255
'Should include subsections'
256256
);
257257

258258
// Check installation.md
259259
const installPath = path.join(outputDir, 'installation.md');
260260
const installContent = await fs.readFile(installPath, 'utf-8');
261261
assert(
262-
installContent.includes('## Installation'),
262+
installContent.includes('# Installation'),
263263
'Installation file should have section heading'
264264
);
265265
assert(
266266
installContent.includes('Instructions for installation'),
267267
'Should have section content'
268268
);
269269
assert(
270-
installContent.includes('### Prerequisites'),
270+
installContent.includes('## Prerequisites'),
271271
'Should include subsections'
272272
);
273273
assert(

0 commit comments

Comments
 (0)