Skip to content

Commit d787ef7

Browse files
committed
fix: adjust heading levels when exploding and assembling markdown sections
## CHANGES - Make section headings level 1 when exploding - Decrement subsection headings by one level - Increment heading levels back when assembling - Fix heading regex to handle levels 1-5 - Preserve original document heading structure - Update package version to 1.3.2
1 parent 7ec274b commit d787ef7

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

bin/md-tree.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,20 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
535535
for (const section of sections) {
536536
const headingText = section.headingText;
537537

538-
// Keep the heading at level 2 for proper document structure
539-
const sectionLines = [`## ${headingText}`, ...section.lines];
540-
const sectionContent = sectionLines.join('\n');
538+
// Make main section heading Level 1, and decrement all subsection headings by one level
539+
const decrementedLines = section.lines.map((line) => {
540+
// Check if line is a heading (starts with #)
541+
const headingMatch = line.match(/^(#{2,6})(\s+.*)$/);
542+
if (headingMatch) {
543+
const [, hashes, rest] = headingMatch;
544+
// Remove one # to decrease the level (level 3 becomes level 2, etc.)
545+
return hashes.slice(1) + rest;
546+
}
547+
return line;
548+
});
549+
550+
const sectionLines = [`# ${headingText}`, ...decrementedLines];
551+
const adjustedContent = sectionLines.join('\n');
541552

542553
// Generate filename without numbered prefix
543554
const filename = `${this.sanitizeFilename(headingText)}.md`;
@@ -548,7 +559,7 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
548559
headingText,
549560
});
550561

551-
await this.writeFile(outputPath, sectionContent);
562+
await this.writeFile(outputPath, adjustedContent);
552563
console.log(`✅ ${headingText}${filename}`);
553564
}
554565

@@ -798,10 +809,14 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
798809
try {
799810
const sectionContent = await this.readFile(filePath);
800811

801-
// Add the section content directly - exploded files already have correct heading levels
812+
// Increment all heading levels back up to match original document structure
813+
const adjustedContent =
814+
this.incrementHeadingLevelsInText(sectionContent);
815+
816+
// Add the section content:
802817
// - After main title: blank line then content (original has blank line after title)
803818
// - Between sections: direct concatenation (original has no spacing between sections)
804-
assembledContent += '\n' + sectionContent;
819+
assembledContent += '\n' + adjustedContent;
805820
} catch {
806821
console.error(
807822
`⚠️ Warning: Could not read ${sectionFile.filename}, skipping...`
@@ -818,16 +833,12 @@ For more information, visit: https://github.com/ksylvan/markdown-tree-parser
818833

819834
const adjustedLines = lines.map((line) => {
820835
// Check if line is a heading (starts with #)
821-
const headingMatch = line.match(/^(#{1,6})(\s+.*)$/);
836+
const headingMatch = line.match(/^(#{1,5})(\s+.*)$/);
822837
if (headingMatch) {
823838
const [, hashes, rest] = headingMatch;
824-
// Remove one # to increase the level (level 2 becomes level 1, etc.)
825-
// Only do this for levels 2-6 to avoid going below level 1
826-
if (hashes.length > 1) {
827-
return hashes.slice(1) + rest;
828-
}
829-
// If it's already level 1, keep it at level 1 (can't go higher)
830-
return line;
839+
// Add one more # to increment the level (level 1 becomes level 2, etc.)
840+
// Only do this for levels 1-5 to avoid going beyond level 6
841+
return '#' + hashes + rest;
831842
}
832843
return line;
833844
});

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.3.1",
3+
"version": "1.3.2",
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",

0 commit comments

Comments
 (0)