Add support for preserving content before first H2 header as 00-preamble.md - #5
Conversation
Co-authored-by: liamgallear <185555686+liamgallear@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR adds functionality to preserve content that appears before the first H2 header in markdown files by saving it to a special "00-preamble.md" file during the split operation.
- Modified
parseMarkdownSectionsto capture pre-H2 content as a "preamble" section - Updated split logic to create "00-preamble.md" files for content before the first H2 header
- Updated merge logic to process files starting from "00-" instead of "01-" to include preamble files
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| cmd/split.go | Implements preamble content collection and creates "00-preamble.md" files with updated filename generation logic |
| cmd/merge.go | Updates merge logic to start processing from "00-" files and updates related error messages |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| for i, section := range sections { | ||
| filename := fmt.Sprintf("%02d-%s.md", i+1, sanitizeFilename(section.title)) | ||
| var filename string | ||
| if section.title == "preamble" { | ||
| filename = "00-preamble.md" | ||
| } else { | ||
| // Adjust index for non-preamble sections | ||
| sectionNum := i + 1 | ||
| if len(sections) > 0 && sections[0].title == "preamble" { | ||
| sectionNum = i // Don't increment if first section is preamble | ||
| } | ||
| filename = fmt.Sprintf("%02d-%s.md", sectionNum, sanitizeFilename(section.title)) |
There was a problem hiding this comment.
The indexing logic is incorrect. When the first section is a preamble, non-preamble sections will have duplicate numbering. For example, if sections = [preamble, section1, section2], then section1 gets sectionNum = 1 (i=1, no increment) and section2 gets sectionNum = 2 (i=2, no increment), but this should be sectionNum = 1 for section1 and sectionNum = 2 for section2. The logic should decrement i when preamble exists: sectionNum = i for non-preamble sections when preamble is present.
See below for a potential fix:
sectionCounter := 1
for _, section := range sections {
var filename string
if section.title == "preamble" {
filename = "00-preamble.md"
} else {
filename = fmt.Sprintf("%02d-%s.md", sectionCounter, sanitizeFilename(section.title))
sectionCounter++
Co-authored-by: liamgallear <185555686+liamgallear@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 5 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| for i, section := range sections { | ||
| filename := fmt.Sprintf("%02d-%s.md", i+1, sanitizeFilename(section.title)) | ||
| var filename string | ||
| if section.title == "preamble" { | ||
| filename = "00-preamble.md" | ||
| } else { | ||
| // Adjust index for non-preamble sections | ||
| sectionNum := i + 1 | ||
| if len(sections) > 0 && sections[0].title == "preamble" { | ||
| sectionNum = i // Don't increment if first section is preamble | ||
| } | ||
| filename = fmt.Sprintf("%02d-%s.md", sectionNum, sanitizeFilename(section.title)) |
There was a problem hiding this comment.
The section numbering logic is incorrect. When the first section is a preamble, sectionNum = i will result in section 1 being numbered as 01- (correct) but section 2 will be numbered as 01- instead of 02-. The logic should be sectionNum = i to skip the preamble index adjustment.
See below for a potential fix:
sectionCount := 1
for _, section := range sections {
var filename string
if section.title == "preamble" {
filename = "00-preamble.md"
} else {
filename = fmt.Sprintf("%02d-%s.md", sectionCount, sanitizeFilename(section.title))
sectionCount++
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 5 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Problem
Previously, any content that appeared before the first H2 header (
## Section) in a markdown file was silently ignored during the split operation. This meant that important introductory content like titles, metadata, or preamble text would be lost when splitting documents.For example, given this markdown file:
The split operation would only create
01-first-section.mdand02-second-section.md, completely losing the title and introductory content.Solution
This PR implements functionality to capture and preserve pre-H2 content in a special
00-preamble.mdfile:00-preamble.md00-prefixed files correctlyKey Changes
parseMarkdownSections()to collect pre-H2 content00-preamble.mdnaming00-prefixTesting
The implementation has been thoroughly tested with various scenarios:
00-preamble.mdplus numbered section files00-preamble.mdfile01-)This change ensures that no content is lost during markdown splitting operations while maintaining full backward compatibility with existing workflows.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.