fixed frontmatter processing, added test
This commit is contained in:
parent
4629200510
commit
0ecad9e96a
5 changed files with 102 additions and 4 deletions
|
@ -47,4 +47,5 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %s\n", err.Error())
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
}
|
}
|
||||||
|
fmt.Printf("%#v", pm)
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,7 +147,7 @@ func BuildFile(f *File, settings *Settings) error {
|
||||||
if err := util.CreateParents(f.OutPath); err != nil {
|
if err := util.CreateParents(f.OutPath); err != nil {
|
||||||
return err
|
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)
|
return errors.Join(errors.New("Error processing file "+f.InPath), err)
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -42,9 +42,10 @@ func readFrontmatter(path string) ([]byte, int, error) {
|
||||||
for s.Scan() {
|
for s.Scan() {
|
||||||
l := s.Text()
|
l := s.Text()
|
||||||
if l == `---` {
|
if l == `---` {
|
||||||
if i > 0 && delims == 0 {
|
if i == 1 && delims == 0 {
|
||||||
// if --- is not the first line, we
|
// if --- is not the first line, we
|
||||||
// assume the file does not contain frontmatter
|
// assume the file does not contain frontmatter
|
||||||
|
// fmt.Println("Delimiter first line")
|
||||||
return nil, 0, nil
|
return nil, 0, nil
|
||||||
}
|
}
|
||||||
delims += 1
|
delims += 1
|
||||||
|
@ -53,6 +54,9 @@ func readFrontmatter(path string) ([]byte, int, error) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if i == 0 {
|
||||||
|
return nil, 0, nil
|
||||||
|
}
|
||||||
lines = append(lines, l)
|
lines = append(lines, l)
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
|
@ -76,6 +80,7 @@ func readFrontmatter(path string) ([]byte, int, error) {
|
||||||
} else {
|
} else {
|
||||||
// not enough delimiters, don't
|
// not enough delimiters, don't
|
||||||
// treat as frontmatter
|
// 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
package builder
|
package builder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -89,3 +90,94 @@ This is the body of the document.`
|
||||||
t.Fatalf("Expected error for invalid frontmatter, got nil")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ type ProcessMemory struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
Data *PageData
|
PageData *PageData
|
||||||
Ext string
|
Ext string
|
||||||
InPath string
|
InPath string
|
||||||
OutPath string
|
OutPath string
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue