Skip to content

Commit 2598036

Browse files
Copilotalexec
andauthored
Fix markdown title removal in parseMarkdownFile (#21)
* Initial plan * Fix markdown title removal bug - preserve first line when no frontmatter Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> * Address code review comments - improve comment grammar and variable scope Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> * Remove unnecessary empty file check as suggested in review Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> * Refactor to use single scan loop with state machine Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> Co-authored-by: Alex Collins <alexec@users.noreply.github.com>
1 parent 55bc26d commit 2598036

2 files changed

Lines changed: 48 additions & 20 deletions

File tree

markdown.go

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,50 @@ func parseMarkdownFile(path string, frontmatter any) (string, error) {
2020

2121
s := bufio.NewScanner(fh)
2222

23-
if s.Scan() && s.Text() == "---" {
24-
var frontMatterBytes bytes.Buffer
25-
for s.Scan() {
26-
line := s.Text()
23+
var content bytes.Buffer
24+
var frontMatterBytes bytes.Buffer
25+
26+
// State machine: 0 = unknown, 1 = scanning frontmatter, 2 = scanning content
27+
state := 0
28+
29+
for s.Scan() {
30+
line := s.Text()
31+
32+
switch state {
33+
case 0: // State unknown - first line
2734
if line == "---" {
28-
break
35+
state = 1 // Start scanning frontmatter
36+
} else {
37+
state = 2 // No frontmatter, start scanning content
38+
if _, err := content.WriteString(line + "\n"); err != nil {
39+
return "", fmt.Errorf("failed to write content: %w", err)
40+
}
2941
}
30-
31-
if _, err := frontMatterBytes.WriteString(line + "\n"); err != nil {
32-
return "", fmt.Errorf("failed to write frontmatter: %w", err)
42+
case 1: // Scanning frontmatter
43+
if line == "---" {
44+
state = 2 // End of frontmatter, start scanning content
45+
} else {
46+
if _, err := frontMatterBytes.WriteString(line + "\n"); err != nil {
47+
return "", fmt.Errorf("failed to write frontmatter: %w", err)
48+
}
49+
}
50+
case 2: // Scanning content
51+
if _, err := content.WriteString(line + "\n"); err != nil {
52+
return "", fmt.Errorf("failed to write content: %w", err)
3353
}
34-
}
35-
36-
if err := yaml.Unmarshal(frontMatterBytes.Bytes(), frontmatter); err != nil {
37-
return "", fmt.Errorf("failed to unmarshal frontmatter: %w", err)
38-
}
39-
}
40-
41-
var content bytes.Buffer
42-
for s.Scan() {
43-
if _, err := content.WriteString(s.Text() + "\n"); err != nil {
44-
return "", fmt.Errorf("failed to write content: %w", err)
4554
}
4655
}
56+
4757
if err := s.Err(); err != nil {
4858
return "", fmt.Errorf("failed to scan file: %w", err)
4959
}
60+
61+
// Parse frontmatter if we collected any
62+
if frontMatterBytes.Len() > 0 {
63+
if err := yaml.Unmarshal(frontMatterBytes.Bytes(), frontmatter); err != nil {
64+
return "", fmt.Errorf("failed to unmarshal frontmatter: %w", err)
65+
}
66+
}
67+
5068
return content.String(), nil
5169
}

markdown_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ of the markdown file.
3535
content: `This is a simple markdown file
3636
without any frontmatter.
3737
`,
38-
wantContent: "without any frontmatter.\n",
38+
wantContent: "This is a simple markdown file\nwithout any frontmatter.\n",
39+
wantFrontmatter: map[string]string{},
40+
wantErr: false,
41+
},
42+
{
43+
name: "markdown with title as first line",
44+
content: `# My Title
45+
46+
This is the content.
47+
`,
48+
wantContent: "# My Title\n\nThis is the content.\n",
3949
wantFrontmatter: map[string]string{},
4050
wantErr: false,
4151
},

0 commit comments

Comments
 (0)