diff --git a/post.go b/post.go index 0bad857..f91b9b4 100644 --- a/post.go +++ b/post.go @@ -21,6 +21,7 @@ type PostMeta struct { Title string `yaml:"title"` Aliases []string `yaml:"aliases"` Date string `yaml:"date"` + Draft bool `yaml:"draft"` } func (post Post) Id() string { diff --git a/post_test.go b/post_test.go index d399782..a7661b8 100644 --- a/post_test.go +++ b/post_test.go @@ -1,6 +1,7 @@ package owl_test import ( + "os" "path" "testing" ) @@ -66,3 +67,20 @@ func TestPostUrlMediaPathWithSubDir(t *testing.T) { t.Error(" Got: " + post.UrlPath()) } } + +func TestDraftInMetaData(t *testing.T) { + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + content := "---\n" + content += "title: test\n" + content += "draft: true\n" + content += "---\n" + content += "\n" + content += "Write your post here.\n" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + _, meta := post.MarkdownData() + if !meta.Draft { + t.Error("Draft should be true") + } + +} diff --git a/user.go b/user.go index e4dc75f..243b815 100644 --- a/user.go +++ b/user.go @@ -65,6 +65,14 @@ func (user User) Posts() ([]Post, error) { } } + // remove drafts + for i, post := range posts { + _, meta := post.MarkdownData() + if meta.Draft { + posts = append(posts[:i], posts[i+1:]...) + } + } + type PostWithDate struct { post Post date time.Time diff --git a/user_test.go b/user_test.go index 89645a8..c3ccb3a 100644 --- a/user_test.go +++ b/user_test.go @@ -147,6 +147,27 @@ func TestCannotListUserPostsWithoutIndexMd(t *testing.T) { } } +func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) { + // Create a new user + repo, _ := owl.CreateRepository(testRepoName()) + user, _ := repo.CreateUser(randomUserName()) + // Create a new post + post, _ := user.CreateNewPost("testpost") + content := "" + content += "---\n" + content += "title: test\n" + content += "draft: true\n" + content += "---\n" + content += "\n" + content += "Write your post here.\n" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + posts, _ := user.Posts() + if len(posts) != 0 { + t.Error("Found draft post") + } +} + func TestCanLoadPost(t *testing.T) { user := getTestUser() // Create a new post