Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,20 @@ func parseMarkdownFile(path string, frontmatter any) (string, error) {
defer fh.Close()

s := bufio.NewScanner(fh)

if s.Scan() && s.Text() == "---" {

// Check if there's a first line
if !s.Scan() {
// Empty file or file contains no content
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.

@copilot This will never happen in practice. Don't bother checking.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the empty file check. The logic now uses nested if statements to properly handle the case when s.Scan() returns true but the text is not "---". (6a29c00)

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()
Expand All @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down