blogposts: read tags

This commit is contained in:
vinchent 2024-09-24 21:38:21 +02:00
parent a33d490896
commit 8228bcdce3
2 changed files with 21 additions and 6 deletions

View File

@ -19,9 +19,11 @@ import (
func TestNewBlogPosts(t *testing.T) { func TestNewBlogPosts(t *testing.T) {
const ( const (
firstBody = `Title: Post 1 firstBody = `Title: Post 1
Description: Description 1` Description: Description 1
Tags: tag1, tag2`
secondBody = `Title: Post 2 secondBody = `Title: Post 2
Description: Description 2` Description: Description 2
Tags: tag1, tag2`
) )
fs := fstest.MapFS{ fs := fstest.MapFS{
@ -41,6 +43,7 @@ Description: Description 2`
assertPost(t, posts[0], Post{ assertPost(t, posts[0], Post{
Title: "Post 1", Title: "Post 1",
Description: "Description 1", Description: "Description 1",
Tags: []string{"tag1", "tag2"},
}) })
} }

View File

@ -13,8 +13,10 @@ type Post struct {
} }
const ( const (
titleSeparator = "Title: " titleLine = "Title: "
descriptionSeparator = "Description: " descriptionLine = "Description: "
tagsLine = "Tags: "
tagSeparator = ","
) )
func getPost(fileSystem fs.FS, fileName string) (Post, error) { func getPost(fileSystem fs.FS, fileName string) (Post, error) {
@ -37,9 +39,19 @@ func newPost(postFile io.Reader) (Post, error) {
return strings.TrimPrefix(scanner.Text(), tagName) return strings.TrimPrefix(scanner.Text(), tagName)
} }
title := readMetaLine(titleLine)
description := readMetaLine(descriptionLine)
tagsOneLine := readMetaLine(tagsLine)
tags := strings.Split(tagsOneLine, ",")
for i, tag := range tags {
tags[i] = strings.TrimSpace(tag)
}
post := Post{ post := Post{
Title: readMetaLine(titleSeparator), Title: title,
Description: readMetaLine(descriptionSeparator), Description: description,
Tags: tags,
} }
return post, nil return post, nil
} }