sorting posts by date. Resolves #4

This commit is contained in:
Niko Abeler 2022-08-17 21:57:55 +02:00
parent 54e6bc907e
commit e4c0550205
4 changed files with 101 additions and 17 deletions

View File

@ -75,8 +75,7 @@ func RenderIndexPage(user User) (string, error) {
posts, _ := user.Posts() posts, _ := user.Posts()
postHtml := "" postHtml := ""
for _, postId := range posts { for _, post := range posts {
post, _ := user.GetPost(postId)
postHtml += "<h2><a href=\"" + post.UrlPath() + "\">" + post.Title() + "</a></h2>\n" postHtml += "<h2><a href=\"" + post.UrlPath() + "\">" + post.Title() + "</a></h2>\n"
} }

3
rss.go
View File

@ -40,8 +40,7 @@ func RenderRSSFeed(user User) (string, error) {
} }
posts, _ := user.Posts() posts, _ := user.Posts()
for _, postId := range posts { for _, post := range posts {
post, _ := user.GetPost(postId)
_, meta := post.MarkdownData() _, meta := post.MarkdownData()
rss.Channel.Items = append(rss.Channel.Items, RSSItem{ rss.Channel.Items = append(rss.Channel.Items, RSSItem{
Guid: post.FullUrl(), Guid: post.FullUrl(),

30
user.go
View File

@ -6,6 +6,7 @@ import (
"net/url" "net/url"
"os" "os"
"path" "path"
"sort"
"time" "time"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -51,17 +52,37 @@ func (user User) Name() string {
return user.name return user.name
} }
func (user User) Posts() ([]string, error) { func (user User) Posts() ([]Post, error) {
postFiles := listDir(path.Join(user.Dir(), "public")) postFiles := listDir(path.Join(user.Dir(), "public"))
posts := make([]string, 0) posts := make([]Post, 0)
for _, id := range postFiles { for _, id := range postFiles {
// if is a directory and has index.md, add to posts // if is a directory and has index.md, add to posts
if dirExists(path.Join(user.Dir(), "public", id)) { if dirExists(path.Join(user.Dir(), "public", id)) {
if fileExists(path.Join(user.Dir(), "public", id, "index.md")) { if fileExists(path.Join(user.Dir(), "public", id, "index.md")) {
posts = append(posts, id) post, _ := user.GetPost(id)
posts = append(posts, post)
} }
} }
} }
// sort posts by date
sort.Slice(posts, func(i, j int) bool {
_, markdownData_i := posts[i].MarkdownData()
_, markdownData_j := posts[j].MarkdownData()
date_i, err := time.Parse(time.RFC1123Z, markdownData_i.Date)
if err != nil {
// invalid date -> use 1970-01-01
date_i = time.Time{}
}
date_j, err := time.Parse(time.RFC1123Z, markdownData_j.Date)
if err != nil {
// invalid date -> use 1970-01-01
date_j = time.Time{}
}
return date_i.After(date_j)
})
return posts, nil return posts, nil
} }
@ -156,8 +177,7 @@ func (user User) PostAliases() (map[string]*Post, error) {
if err != nil { if err != nil {
return post_aliases, err return post_aliases, err
} }
for _, id := range posts { for _, post := range posts {
post, err := user.GetPost(id)
if err != nil { if err != nil {
return post_aliases, err return post_aliases, err
} }

View File

@ -40,7 +40,7 @@ func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) {
// Create a new post // Create a new post
user.CreateNewPost("testpost") user.CreateNewPost("testpost")
posts, _ := user.Posts() posts, _ := user.Posts()
post, _ := user.GetPost(posts[0]) post, _ := user.GetPost(posts[0].Id())
_, meta := post.MarkdownData() _, meta := post.MarkdownData()
if meta.Date == "" { if meta.Date == "" {
t.Error("Found no date. Got: " + meta.Date) t.Error("Found no date. Got: " + meta.Date)
@ -99,17 +99,21 @@ func TestCannotListUserPostsInSubdirectories(t *testing.T) {
os.WriteFile(path.Join(user.PostDir(), "foo/index.md"), []byte(content), 0644) os.WriteFile(path.Join(user.PostDir(), "foo/index.md"), []byte(content), 0644)
os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644) os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.Posts()
if !contains(posts, "foo") { postIds := []string{}
for _, p := range posts {
postIds = append(postIds, p.Id())
}
if !contains(postIds, "foo") {
t.Error("Does not contain post: foo. Found:") t.Error("Does not contain post: foo. Found:")
for _, p := range posts { for _, p := range posts {
t.Error("\t" + p) t.Error("\t" + p.Id())
} }
} }
if contains(posts, "foo/bar") { if contains(postIds, "foo/bar") {
t.Error("Invalid post found: foo/bar. Found:") t.Error("Invalid post found: foo/bar. Found:")
for _, p := range posts { for _, p := range posts {
t.Error("\t" + p) t.Error("\t" + p.Id())
} }
} }
} }
@ -131,10 +135,14 @@ func TestCannotListUserPostsWithoutIndexMd(t *testing.T) {
os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644) os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644)
posts, _ := user.Posts() posts, _ := user.Posts()
if contains(posts, "foo") { postIds := []string{}
for _, p := range posts {
postIds = append(postIds, p.Id())
}
if contains(postIds, "foo") {
t.Error("Contains invalid post: foo. Found:") t.Error("Contains invalid post: foo. Found:")
for _, p := range posts { for _, p := range posts {
t.Error("\t" + p) t.Error("\t" + p.Id())
} }
} }
} }
@ -145,7 +153,7 @@ func TestCanLoadPost(t *testing.T) {
user.CreateNewPost("testpost") user.CreateNewPost("testpost")
posts, _ := user.Posts() posts, _ := user.Posts()
post, _ := user.GetPost(posts[0]) post, _ := user.GetPost(posts[0].Id())
if post.Title() != "testpost" { if post.Title() != "testpost" {
t.Error("Wrong title, Got: " + post.Title()) t.Error("Wrong title, Got: " + post.Title())
} }
@ -164,3 +172,61 @@ func TestUserFullUrl(t *testing.T) {
t.Error("Wrong url path, Expected: " + "http://localhost:8080/user/" + user.Name() + "/" + " Got: " + user.FullUrl()) t.Error("Wrong url path, Expected: " + "http://localhost:8080/user/" + user.Name() + "/" + " Got: " + user.FullUrl())
} }
} }
func TestPostsSortedByPublishingDateLatestFirst(t *testing.T) {
user := getTestUser()
// Create a new post
post1, _ := user.CreateNewPost("testpost")
post2, _ := user.CreateNewPost("testpost2")
content := "---\n"
content += "title: Test Post\n"
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post1.ContentFile(), []byte(content), 0644)
content = "---\n"
content += "title: Test Post 2\n"
content += "date: Wed, 17 Aug 2022 20:50:06 +0000\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
if posts[0].Id() != post2.Id() {
t.Error("Wrong Id, Got: " + posts[0].Id())
}
if posts[1].Id() != post1.Id() {
t.Error("Wrong Id, Got: " + posts[1].Id())
}
}
func TestPostsSortedByPublishingDateBrokenAtBottom(t *testing.T) {
user := getTestUser()
// Create a new post
post1, _ := user.CreateNewPost("testpost")
post2, _ := user.CreateNewPost("testpost2")
content := "---\n"
content += "title: Test Post\n"
content += "date: Wed, 17 +0000\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post1.ContentFile(), []byte(content), 0644)
content = "---\n"
content += "title: Test Post 2\n"
content += "date: Wed, 17 Aug 2022 20:50:06 +0000\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
if posts[0].Id() != post2.Id() {
t.Error("Wrong Id, Got: " + posts[0].Id())
}
if posts[1].Id() != post1.Id() {
t.Error("Wrong Id, Got: " + posts[1].Id())
}
}