blogposts: finish up

This commit is contained in:
vinchent 2024-09-24 21:58:06 +02:00
parent 48df8ace6e
commit e8b8166ae9

View File

@ -16,7 +16,7 @@ const (
titleLine = "Title: "
descriptionLine = "Description: "
tagsLine = "Tags: "
tagSeparator = ","
tagSeparator = ", "
bodySeparator = "---"
)
@ -40,15 +40,16 @@ 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(titleLine),
Description: readMetaLine(descriptionLine),
Tags: strings.Split(readMetaLine(tagsLine), tagSeparator),
Body: readBody(scanner),
}
return post, nil
}
func readBody(scanner *bufio.Scanner) string {
for scanner.Scan() {
if strings.TrimSpace(scanner.Text()) == bodySeparator {
break
@ -61,13 +62,5 @@ func newPost(postFile io.Reader) (Post, error) {
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
return strings.TrimSuffix(buf.String(), "\n")
}