Skip to content

Add support for preserving content before first H2 header as 00-preamble.md - #5

Merged
liamgallear merged 4 commits into
mainfrom
copilot/fix-e18b0d08-1a70-4fa3-816e-9ef3bf3f82c8
Sep 24, 2025
Merged

Add support for preserving content before first H2 header as 00-preamble.md#5
liamgallear merged 4 commits into
mainfrom
copilot/fix-e18b0d08-1a70-4fa3-816e-9ef3bf3f82c8

Conversation

Copilot AI commented Sep 24, 2025

Copy link
Copy Markdown
Contributor

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:

# My Document Title

This is important introductory content that provides context.
It includes metadata and overview information.

## First Section
Content for first section...

## Second Section  
Content for second section...

The split operation would only create 01-first-section.md and 02-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.md file:

  • Content preservation: All content before the first H2 header is now saved as 00-preamble.md
  • Smart filtering: Empty or whitespace-only preambles are ignored to avoid creating unnecessary files
  • Backward compatibility: Files without preamble content work exactly as before
  • Updated merge support: The merge command now handles 00- prefixed files correctly
  • Round-trip integrity: Split → merge operations preserve the complete original document structure

Key Changes

  1. Enhanced parsing logic in parseMarkdownSections() to collect pre-H2 content
  2. Updated file creation to handle preamble sections with 00-preamble.md naming
  3. Extended merge functionality to process files starting from 00- prefix
  4. Updated documentation and help text to reflect the new capability

Testing

The implementation has been thoroughly tested with various scenarios:

  • Documents with preamble + H2 sections → Creates 00-preamble.md plus numbered section files
  • Documents with only preamble content → Creates single 00-preamble.md file
  • Documents starting directly with H2 → Works unchanged (files start with 01-)
  • Empty or whitespace-only files → Proper error handling
  • Complex documents with subsections → All content preserved correctly
  • Full round-trip (split → merge) → Original document structure maintained

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.

Co-authored-by: liamgallear <185555686+liamgallear@users.noreply.github.com>
@liamgallear
liamgallear requested a review from Copilot September 24, 2025 09:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 parseMarkdownSections to 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.

Comment thread cmd/split.go Outdated
Comment on lines +60 to +70
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))

Copilot AI Sep 24, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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++

Copilot uses AI. Check for mistakes.
Comment thread cmd/split.go Outdated
Co-authored-by: liamgallear <185555686+liamgallear@users.noreply.github.com>
Copilot AI changed the title [WIP] Need to add in functionality that will save any lines that may come before the first H2 header into it's own split file that is prefixed with 00- Add support for preserving content before first H2 header as 00-preamble.md Sep 24, 2025
Copilot AI requested a review from liamgallear September 24, 2025 09:57
@liamgallear
liamgallear requested a review from Copilot September 24, 2025 10:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/split.go Outdated
Comment on lines +60 to +71
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))

Copilot AI Sep 24, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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++

Copilot uses AI. Check for mistakes.
Comment thread cmd/split.go
@liamgallear
liamgallear requested a review from Copilot September 24, 2025 10:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/split.go
Comment thread cmd/split.go
@liamgallear
liamgallear marked this pull request as ready for review September 24, 2025 11:01
@liamgallear
liamgallear merged commit 185aaca into main Sep 24, 2025
@liamgallear
liamgallear deleted the copilot/fix-e18b0d08-1a70-4fa3-816e-9ef3bf3f82c8 branch September 24, 2025 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants