From 8228bcdce3b48911aa09f7956a5a0c9354596836 Mon Sep 17 00:00:00 2001 From: vinchent Date: Tue, 24 Sep 2024 21:38:21 +0200 Subject: [PATCH] blogposts: read tags --- blogposts/blogposts_test.go | 7 +++++-- blogposts/post.go | 20 ++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/blogposts/blogposts_test.go b/blogposts/blogposts_test.go index 0e11a7a..e10723f 100644 --- a/blogposts/blogposts_test.go +++ b/blogposts/blogposts_test.go @@ -19,9 +19,11 @@ import ( func TestNewBlogPosts(t *testing.T) { const ( firstBody = `Title: Post 1 -Description: Description 1` +Description: Description 1 +Tags: tag1, tag2` secondBody = `Title: Post 2 -Description: Description 2` +Description: Description 2 +Tags: tag1, tag2` ) fs := fstest.MapFS{ @@ -41,6 +43,7 @@ Description: Description 2` assertPost(t, posts[0], Post{ Title: "Post 1", Description: "Description 1", + Tags: []string{"tag1", "tag2"}, }) } diff --git a/blogposts/post.go b/blogposts/post.go index 6989ec7..c2eff2a 100644 --- a/blogposts/post.go +++ b/blogposts/post.go @@ -13,8 +13,10 @@ type Post struct { } const ( - titleSeparator = "Title: " - descriptionSeparator = "Description: " + titleLine = "Title: " + descriptionLine = "Description: " + tagsLine = "Tags: " + tagSeparator = "," ) 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) } + 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{ - Title: readMetaLine(titleSeparator), - Description: readMetaLine(descriptionSeparator), + Title: title, + Description: description, + Tags: tags, } return post, nil }