fixed frontmatter processing, added test

This commit is contained in:
Daniel Fichtinger 2025-02-08 00:11:28 -05:00
parent 4629200510
commit 0ecad9e96a
5 changed files with 102 additions and 4 deletions

View file

@ -147,7 +147,7 @@ func BuildFile(f *File, settings *Settings) error {
if err := util.CreateParents(f.OutPath); err != nil {
return err
}
if err := BuildHtmlFile(f.FrontMatterLen, f.InPath, f.OutPath, f.Data, settings); err != nil {
if err := BuildHtmlFile(f.FrontMatterLen, f.InPath, f.OutPath, f.PageData, settings); err != nil {
return errors.Join(errors.New("Error processing file "+f.InPath), err)
} else {
return nil

View file

@ -42,9 +42,10 @@ func readFrontmatter(path string) ([]byte, int, error) {
for s.Scan() {
l := s.Text()
if l == `---` {
if i > 0 && delims == 0 {
if i == 1 && delims == 0 {
// if --- is not the first line, we
// assume the file does not contain frontmatter
// fmt.Println("Delimiter first line")
return nil, 0, nil
}
delims += 1
@ -53,6 +54,9 @@ func readFrontmatter(path string) ([]byte, int, error) {
break
}
} else {
if i == 0 {
return nil, 0, nil
}
lines = append(lines, l)
i += 1
}
@ -76,6 +80,7 @@ func readFrontmatter(path string) ([]byte, int, error) {
} else {
// not enough delimiters, don't
// treat as frontmatter
return nil, 0, errors.New("frontmatter is missing closing delimiter")
s := fmt.Sprintf("%s: frontmatter is missing closing delimiter", path)
return nil, 0, errors.New(s)
}
}

View file

@ -2,6 +2,7 @@
package builder
import (
"bytes"
"os"
"testing"
)
@ -89,3 +90,94 @@ This is the body of the document.`
t.Fatalf("Expected error for invalid frontmatter, got nil")
}
}
func TestReadFrontmatter(t *testing.T) {
tests := []struct {
name string
content string
wantErr bool
wantData []byte
wantLen int
}{
{
name: "Valid frontmatter",
content: `---
title: "Test"
author: "User"
---
Content here`,
wantErr: false,
wantData: []byte("title: \"Test\"\nauthor: \"User\"\n"),
wantLen: 2,
},
{
name: "Missing closing delimiter",
content: `---
title: "Incomplete Frontmatter"`,
wantErr: true,
},
{
name: "Frontmatter later in file",
content: `This is some content
---
title: "Not Frontmatter"
---`,
wantErr: false,
wantData: nil, // Should return nil because `---` is not the first line
wantLen: 0,
},
{
name: "Empty frontmatter",
content: `---
---`,
wantErr: true,
},
{
name: "No frontmatter",
content: `This is just a normal file.`,
wantErr: false,
wantData: nil, // Should return nil as there's no frontmatter
wantLen: 0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create a temporary file
tmpFile, err := os.CreateTemp("", "testfile-*.md")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
// Write test content
_, err = tmpFile.WriteString(tc.content)
if err != nil {
t.Fatalf("failed to write to temp file: %v", err)
}
tmpFile.Close()
// Call function under test
data, length, err := readFrontmatter(tmpFile.Name())
// Check for expected error
if tc.wantErr {
if err == nil {
t.Errorf("expected error but got none")
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// Check content
if !bytes.Equal(data, tc.wantData) {
t.Errorf("expected %q, got %q", tc.wantData, data)
}
// Check length
if length != tc.wantLen {
t.Errorf("expected length %d, got %d", tc.wantLen, length)
}
}
})
}
}

View file

@ -21,7 +21,7 @@ type ProcessMemory struct {
}
type File struct {
Data *PageData
PageData *PageData
Ext string
InPath string
OutPath string