blogposts: read body

This commit is contained in:
vinchent 2024-09-24 21:51:09 +02:00
parent 8228bcdce3
commit 48df8ace6e
2 changed files with 28 additions and 3 deletions

View File

@ -20,10 +20,17 @@ func TestNewBlogPosts(t *testing.T) {
const (
firstBody = `Title: Post 1
Description: Description 1
Tags: tag1, tag2`
Tags: tdd, go
---
Hello
World`
secondBody = `Title: Post 2
Description: Description 2
Tags: tag1, tag2`
Tags: rust, borrow-checker
---
B
L
M`
)
fs := fstest.MapFS{
@ -43,7 +50,9 @@ Tags: tag1, tag2`
assertPost(t, posts[0], Post{
Title: "Post 1",
Description: "Description 1",
Tags: []string{"tag1", "tag2"},
Tags: []string{"tdd", "go"},
Body: `Hello
World`,
})
}

View File

@ -17,6 +17,7 @@ const (
descriptionLine = "Description: "
tagsLine = "Tags: "
tagSeparator = ","
bodySeparator = "---"
)
func getPost(fileSystem fs.FS, fileName string) (Post, error) {
@ -48,10 +49,25 @@ func newPost(postFile io.Reader) (Post, error) {
tags[i] = strings.TrimSpace(tag)
}
for scanner.Scan() {
if strings.TrimSpace(scanner.Text()) == bodySeparator {
break
}
}
// The rest is the body
var buf strings.Builder
for scanner.Scan() {
buf.Write(scanner.Bytes())
buf.Write([]byte{'\n'})
}
body := strings.TrimSuffix(buf.String(), "\n")
post := Post{
Title: title,
Description: description,
Tags: tags,
Body: body,
}
return post, nil
}