From bd3a0f6d6f757a1cbf014f7477dd6deb8aea4311 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Oct 2025 22:52:12 +0000 Subject: [PATCH 1/5] Initial plan From 74fa3240ccf899452b1f076f661644f4102f8073 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Oct 2025 22:57:57 +0000 Subject: [PATCH 2/5] Fix markdown title removal bug - preserve first line when no frontmatter Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> --- markdown.go | 21 +++++++++++++++++++-- markdown_test.go | 12 +++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/markdown.go b/markdown.go index 159afbb9..41ca05f3 100644 --- a/markdown.go +++ b/markdown.go @@ -20,7 +20,19 @@ func parseMarkdownFile(path string, frontmatter any) (string, error) { s := bufio.NewScanner(fh) - if s.Scan() && s.Text() == "---" { + var content bytes.Buffer + + // Check if there's a first line + if !s.Scan() { + // Empty file or only contains no content + if err := s.Err(); err != nil { + return "", fmt.Errorf("failed to scan file: %w", err) + } + return "", nil + } + + // First line exists, check if it's frontmatter delimiter + if s.Text() == "---" { var frontMatterBytes bytes.Buffer for s.Scan() { line := s.Text() @@ -36,9 +48,14 @@ func parseMarkdownFile(path string, frontmatter any) (string, error) { if err := yaml.Unmarshal(frontMatterBytes.Bytes(), frontmatter); err != nil { return "", fmt.Errorf("failed to unmarshal frontmatter: %w", err) } + } else { + // First line was not "---", so it's content, not frontmatter + // We need to include this line in the content + if _, err := content.WriteString(s.Text() + "\n"); err != nil { + return "", fmt.Errorf("failed to write content: %w", err) + } } - var content bytes.Buffer for s.Scan() { if _, err := content.WriteString(s.Text() + "\n"); err != nil { return "", fmt.Errorf("failed to write content: %w", err) diff --git a/markdown_test.go b/markdown_test.go index bc98f0ec..e5798515 100644 --- a/markdown_test.go +++ b/markdown_test.go @@ -35,7 +35,17 @@ of the markdown file. content: `This is a simple markdown file without any frontmatter. `, - wantContent: "without any frontmatter.\n", + wantContent: "This is a simple markdown file\nwithout any frontmatter.\n", + wantFrontmatter: map[string]string{}, + wantErr: false, + }, + { + name: "markdown with title as first line", + content: `# My Title + +This is the content. +`, + wantContent: "# My Title\n\nThis is the content.\n", wantFrontmatter: map[string]string{}, wantErr: false, }, From deb66cce190abc45daa0b57b26d2727f3d7fa5a0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Oct 2025 22:58:54 +0000 Subject: [PATCH 3/5] Address code review comments - improve comment grammar and variable scope Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> --- markdown.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/markdown.go b/markdown.go index 41ca05f3..ffa325e0 100644 --- a/markdown.go +++ b/markdown.go @@ -19,18 +19,18 @@ func parseMarkdownFile(path string, frontmatter any) (string, error) { defer fh.Close() s := bufio.NewScanner(fh) - - var content bytes.Buffer // Check if there's a first line if !s.Scan() { - // Empty file or only contains no content + // Empty file or file contains no content if err := s.Err(); err != nil { return "", fmt.Errorf("failed to scan file: %w", err) } return "", nil } + var content bytes.Buffer + // First line exists, check if it's frontmatter delimiter if s.Text() == "---" { var frontMatterBytes bytes.Buffer From 6a29c00037b5e33ad1b2a21e4dd155936eda124a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Oct 2025 23:05:58 +0000 Subject: [PATCH 4/5] Remove unnecessary empty file check as suggested in review Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> --- markdown.go | 50 +++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/markdown.go b/markdown.go index ffa325e0..22a731f9 100644 --- a/markdown.go +++ b/markdown.go @@ -19,40 +19,32 @@ func parseMarkdownFile(path string, frontmatter any) (string, error) { defer fh.Close() s := bufio.NewScanner(fh) - - // Check if there's a first line - if !s.Scan() { - // Empty file or file contains no content - if err := s.Err(); err != nil { - return "", fmt.Errorf("failed to scan file: %w", err) - } - return "", nil - } - + var content bytes.Buffer - // First line exists, check if it's frontmatter delimiter - if s.Text() == "---" { - var frontMatterBytes bytes.Buffer - for s.Scan() { - line := s.Text() - if line == "---" { - break + if s.Scan() { + if s.Text() == "---" { + var frontMatterBytes bytes.Buffer + for s.Scan() { + line := s.Text() + if line == "---" { + break + } + + if _, err := frontMatterBytes.WriteString(line + "\n"); err != nil { + return "", fmt.Errorf("failed to write frontmatter: %w", err) + } } - if _, err := frontMatterBytes.WriteString(line + "\n"); err != nil { - return "", fmt.Errorf("failed to write frontmatter: %w", err) + if err := yaml.Unmarshal(frontMatterBytes.Bytes(), frontmatter); err != nil { + return "", fmt.Errorf("failed to unmarshal frontmatter: %w", err) + } + } else { + // First line was not "---", so it's content, not frontmatter + // We need to include this line in the content + if _, err := content.WriteString(s.Text() + "\n"); err != nil { + return "", fmt.Errorf("failed to write content: %w", err) } - } - - if err := yaml.Unmarshal(frontMatterBytes.Bytes(), frontmatter); err != nil { - return "", fmt.Errorf("failed to unmarshal frontmatter: %w", err) - } - } else { - // First line was not "---", so it's content, not frontmatter - // We need to include this line in the content - if _, err := content.WriteString(s.Text() + "\n"); err != nil { - return "", fmt.Errorf("failed to write content: %w", err) } } From bd5bf8eb129ca05709bc685d7849ac4371f2b7a3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Oct 2025 23:12:06 +0000 Subject: [PATCH 5/5] Refactor to use single scan loop with state machine Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> --- markdown.go | 53 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/markdown.go b/markdown.go index 22a731f9..a1a2c0ba 100644 --- a/markdown.go +++ b/markdown.go @@ -21,40 +21,49 @@ func parseMarkdownFile(path string, frontmatter any) (string, error) { s := bufio.NewScanner(fh) var content bytes.Buffer + var frontMatterBytes bytes.Buffer - if s.Scan() { - if s.Text() == "---" { - var frontMatterBytes bytes.Buffer - for s.Scan() { - line := s.Text() - if line == "---" { - break + // State machine: 0 = unknown, 1 = scanning frontmatter, 2 = scanning content + state := 0 + + for s.Scan() { + line := s.Text() + + switch state { + case 0: // State unknown - first line + if line == "---" { + state = 1 // Start scanning frontmatter + } else { + state = 2 // No frontmatter, start scanning content + if _, err := content.WriteString(line + "\n"); err != nil { + return "", fmt.Errorf("failed to write content: %w", err) } - + } + case 1: // Scanning frontmatter + if line == "---" { + state = 2 // End of frontmatter, start scanning content + } else { if _, err := frontMatterBytes.WriteString(line + "\n"); err != nil { return "", fmt.Errorf("failed to write frontmatter: %w", err) } } - - if err := yaml.Unmarshal(frontMatterBytes.Bytes(), frontmatter); err != nil { - return "", fmt.Errorf("failed to unmarshal frontmatter: %w", err) - } - } else { - // First line was not "---", so it's content, not frontmatter - // We need to include this line in the content - if _, err := content.WriteString(s.Text() + "\n"); err != nil { + case 2: // Scanning content + if _, err := content.WriteString(line + "\n"); err != nil { return "", fmt.Errorf("failed to write content: %w", err) } } } - - for s.Scan() { - if _, err := content.WriteString(s.Text() + "\n"); err != nil { - return "", fmt.Errorf("failed to write content: %w", err) - } - } + if err := s.Err(); err != nil { return "", fmt.Errorf("failed to scan file: %w", err) } + + // Parse frontmatter if we collected any + if frontMatterBytes.Len() > 0 { + if err := yaml.Unmarshal(frontMatterBytes.Bytes(), frontmatter); err != nil { + return "", fmt.Errorf("failed to unmarshal frontmatter: %w", err) + } + } + return content.String(), nil }