From 7a857e857f6b123416a01c3dc20a92a45af7b5ce Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sat, 13 Aug 2022 11:26:17 +0200 Subject: [PATCH 1/7] integration of alias redirect in other handlers. #2 --- cmd/owl-web/aliases_test.go | 121 ++++++++++++++++++++++++++++++++++++ cmd/owl-web/handler.go | 18 ++---- user.go | 5 ++ 3 files changed, 132 insertions(+), 12 deletions(-) diff --git a/cmd/owl-web/aliases_test.go b/cmd/owl-web/aliases_test.go index d6e4b5f..5d94d16 100644 --- a/cmd/owl-web/aliases_test.go +++ b/cmd/owl-web/aliases_test.go @@ -76,3 +76,124 @@ func TestNoRedirectOnNonExistingAliases(t *testing.T) { } } + +func TestNoRedirectIfValidPostUrl(t *testing.T) { + repo := getTestRepo() + user, _ := repo.CreateUser("test-1") + post, _ := user.CreateNewPost("post-1") + post2, _ := user.CreateNewPost("post-2") + + content := "---\n" + content += "title: Test\n" + content += "aliases: \n" + content += " - " + post2.UrlPath() + "\n" + content += "---\n" + content += "This is a test" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + // Create Request and Response + req, err := http.NewRequest("GET", post2.UrlPath(), nil) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + router := main.Router(&repo) + router.ServeHTTP(rr, req) + + // Check the status code is what we expect. + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", + status, http.StatusOK) + } + +} + +func TestRedirectIfInvalidPostUrl(t *testing.T) { + repo := getTestRepo() + user, _ := repo.CreateUser("test-1") + post, _ := user.CreateNewPost("post-1") + + content := "---\n" + content += "title: Test\n" + content += "aliases: \n" + content += " - " + user.UrlPath() + "posts/not-a-real-post/" + "\n" + content += "---\n" + content += "This is a test" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + // Create Request and Response + req, err := http.NewRequest("GET", user.UrlPath()+"posts/not-a-real-post/", nil) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + router := main.Router(&repo) + router.ServeHTTP(rr, req) + + // Check the status code is what we expect. + if status := rr.Code; status != http.StatusMovedPermanently { + t.Errorf("handler returned wrong status code: got %v want %v", + status, http.StatusMovedPermanently) + } + +} + +func TestRedirectIfInvalidUserUrl(t *testing.T) { + repo := getTestRepo() + user, _ := repo.CreateUser("test-1") + post, _ := user.CreateNewPost("post-1") + + content := "---\n" + content += "title: Test\n" + content += "aliases: \n" + content += " - /user/not-real/ \n" + content += "---\n" + content += "This is a test" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + // Create Request and Response + req, err := http.NewRequest("GET", "/user/not-real/", nil) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + router := main.Router(&repo) + router.ServeHTTP(rr, req) + + // Check the status code is what we expect. + if status := rr.Code; status != http.StatusMovedPermanently { + t.Errorf("handler returned wrong status code: got %v want %v", + status, http.StatusMovedPermanently) + } + +} + +func TestRedirectIfInvalidMediaUrl(t *testing.T) { + repo := getTestRepo() + user, _ := repo.CreateUser("test-1") + post, _ := user.CreateNewPost("post-1") + + content := "---\n" + content += "title: Test\n" + content += "aliases: \n" + content += " - " + post.UrlMediaPath("not-real") + "\n" + content += "---\n" + content += "This is a test" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + // Create Request and Response + req, err := http.NewRequest("GET", post.UrlMediaPath("not-real"), nil) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + router := main.Router(&repo) + router.ServeHTTP(rr, req) + + // Check the status code is what we expect. + if status := rr.Code; status != http.StatusMovedPermanently { + t.Errorf("handler returned wrong status code: got %v want %v", + status, http.StatusMovedPermanently) + } + +} diff --git a/cmd/owl-web/handler.go b/cmd/owl-web/handler.go index 8de8ade..a446401 100644 --- a/cmd/owl-web/handler.go +++ b/cmd/owl-web/handler.go @@ -41,8 +41,7 @@ func userIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Requ user, err := getUserFromRepo(repo, ps) if err != nil { println("Error getting user: ", err.Error()) - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("User not found")) + notFoundHandler(repo)(w, r) return } html, err := owl.RenderIndexPage(user) @@ -64,15 +63,13 @@ func postHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, user, err := getUserFromRepo(repo, ps) if err != nil { println("Error getting user: ", err.Error()) - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("User not found")) + notFoundHandler(repo)(w, r) return } post, err := user.GetPost(postId) if err != nil { println("Error getting post: ", err.Error()) - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("Post not found")) + notFoundHandler(repo)(w, r) return } html, err := owl.RenderPost(post) @@ -96,22 +93,19 @@ func postMediaHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Requ user, err := getUserFromRepo(repo, ps) if err != nil { println("Error getting user: ", err.Error()) - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("User not found")) + notFoundHandler(repo)(w, r) return } post, err := user.GetPost(postId) if err != nil { println("Error getting post: ", err.Error()) - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("Post not found")) + notFoundHandler(repo)(w, r) return } filepath = path.Join(post.MediaDir(), filepath) if _, err := os.Stat(filepath); err != nil { println("Error getting file: ", err.Error()) - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("File not found")) + notFoundHandler(repo)(w, r) return } http.ServeFile(w, r, filepath) diff --git a/user.go b/user.go index 0998304..82fe15f 100644 --- a/user.go +++ b/user.go @@ -60,6 +60,11 @@ func (user User) Posts() ([]string, error) { } func (user User) GetPost(id string) (Post, error) { + // check if posts index.md exists + if !fileExists(path.Join(user.Dir(), "public", id, "index.md")) { + return Post{}, fmt.Errorf("post %s does not exist", id) + } + post := Post{user: &user, id: id} _, metaData := post.MarkdownData() title := metaData["title"] From 1b99eaa016e449a0104bd328dfae2e35aab5b8e0 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sat, 13 Aug 2022 15:32:26 +0200 Subject: [PATCH 2/7] WIP RSS renderer. #3 --- cmd/owl-web/handler.go | 20 +++++++ cmd/owl-web/main.go | 2 + .../{repo_base.html => repo/base.html} | 0 embed/initial/repo/config.yml | 1 + repository.go | 35 ++++++++++- rss.go | 60 +++++++++++++++++++ rss_test.go | 49 +++++++++++++++ 7 files changed, 164 insertions(+), 3 deletions(-) rename embed/initial/{repo_base.html => repo/base.html} (100%) create mode 100644 embed/initial/repo/config.yml create mode 100644 rss.go create mode 100644 rss_test.go diff --git a/cmd/owl-web/handler.go b/cmd/owl-web/handler.go index a446401..58e5566 100644 --- a/cmd/owl-web/handler.go +++ b/cmd/owl-web/handler.go @@ -56,6 +56,26 @@ func userIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Requ } } +func userRSSHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) { + return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + user, err := getUserFromRepo(repo, ps) + if err != nil { + println("Error getting user: ", err.Error()) + notFoundHandler(repo)(w, r) + return + } + html, err := owl.RenderRSSFeed(user) + if err != nil { + println("Error rendering index page: ", err.Error()) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Internal server error")) + return + } + println("Rendering index page for user", user.Name()) + w.Write([]byte(html)) + } +} + func postHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { postId := ps.ByName("post") diff --git a/cmd/owl-web/main.go b/cmd/owl-web/main.go index aee9828..a14970c 100644 --- a/cmd/owl-web/main.go +++ b/cmd/owl-web/main.go @@ -14,6 +14,7 @@ func Router(repo *owl.Repository) http.Handler { router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir())) router.GET("/", repoIndexHandler(repo)) router.GET("/user/:user/", userIndexHandler(repo)) + router.GET("/user/:user/index.xml", userRSSHandler(repo)) router.GET("/user/:user/posts/:post/", postHandler(repo)) router.GET("/user/:user/posts/:post/media/*filepath", postMediaHandler(repo)) router.NotFound = http.HandlerFunc(notFoundHandler(repo)) @@ -24,6 +25,7 @@ func SingleUserRouter(repo *owl.Repository) http.Handler { router := httprouter.New() router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir())) router.GET("/", userIndexHandler(repo)) + router.GET("/index.xml", userRSSHandler(repo)) router.GET("/posts/:post/", postHandler(repo)) router.GET("/posts/:post/media/*filepath", postMediaHandler(repo)) return router diff --git a/embed/initial/repo_base.html b/embed/initial/repo/base.html similarity index 100% rename from embed/initial/repo_base.html rename to embed/initial/repo/base.html diff --git a/embed/initial/repo/config.yml b/embed/initial/repo/config.yml new file mode 100644 index 0000000..78122e0 --- /dev/null +++ b/embed/initial/repo/config.yml @@ -0,0 +1 @@ +domain: "http://localhost:8080" \ No newline at end of file diff --git a/repository.go b/repository.go index e4fae33..529af30 100644 --- a/repository.go +++ b/repository.go @@ -25,6 +25,10 @@ type Repository struct { active_user string } +type RepoConfig struct { + Domain string `yaml:"domain"` +} + func CreateRepository(name string) (Repository, error) { newRepo := Repository{name: name} // check if repository already exists @@ -46,9 +50,15 @@ func CreateRepository(name string) (Repository, error) { os.WriteFile(newRepo.StaticDir()+"/"+file.Name(), src_data, 0644) } - // copy repo_base.html to base.html - src_data, _ := static_files.ReadFile("embed/initial/repo_base.html") - os.WriteFile(newRepo.Dir()+"/base.html", src_data, 0644) + // copy repo/ to newRepo.Dir() + init_files, _ := static_files.ReadDir("embed/initial/repo") + for _, file := range init_files { + if file.IsDir() { + continue + } + src_data, _ := static_files.ReadFile("embed/initial/repo/" + file.Name()) + os.WriteFile(newRepo.Dir()+"/"+file.Name(), src_data, 0644) + } return newRepo, nil } @@ -103,6 +113,11 @@ func (repo Repository) UserUrlPath(user User) string { return "/user/" + user.name + "/" } +func (repo Repository) FullUserUrl(user User) string { + config, _ := repo.Config() + return config.Domain + repo.UserUrlPath(user) +} + func (repo Repository) Template() (string, error) { // load base.html path := path.Join(repo.Dir(), "base.html") @@ -179,3 +194,17 @@ func (repo Repository) PostAliases() (map[string]*Post, error) { } return aliases, nil } + +func (repo Repository) Config() (RepoConfig, error) { + config_path := path.Join(repo.Dir(), "config.yml") + config_data, err := ioutil.ReadFile(config_path) + if err != nil { + return RepoConfig{}, err + } + var meta RepoConfig + err = yaml.Unmarshal(config_data, &meta) + if err != nil { + return RepoConfig{}, err + } + return meta, nil +} diff --git a/rss.go b/rss.go new file mode 100644 index 0000000..948dbf5 --- /dev/null +++ b/rss.go @@ -0,0 +1,60 @@ +package owl + +import ( + "bytes" + "encoding/xml" +) + +type RSS struct { + XMLName xml.Name `xml:"rss"` + Version string `xml:"version,attr"` + Channel RSSChannel `xml:"channel"` +} + +type RSSChannel struct { + Title string `xml:"title"` + Link string `xml:"link"` + Description string `xml:"description"` + Items []RSSItem `xml:"item"` +} + +type RSSItem struct { + Title string `xml:"title"` + Link string `xml:"link"` + Description string `xml:"description"` + PubDate string `xml:"pubDate"` +} + +func RenderRSSFeed(user User) (string, error) { + + config, _ := user.Config() + + rss := RSS{ + Version: "2.0", + Channel: RSSChannel{ + Title: config.Title, + Link: user.repo.FullUserUrl(user), + Description: config.SubTitle, + Items: make([]RSSItem, 0), + }, + } + + // posts, _ := user.Posts() + // for _, post := range posts { + // rss.Channel.Items = append(rss.Channel.Items, RSSItem{ + // Title: post.Title(), + // Link: post.Link(), + // Description: post.Description(), + // PubDate: post.PubDate(), + // }) + // } + + buf := new(bytes.Buffer) + err := xml.NewEncoder(buf).Encode(rss) + if err != nil { + return "", err + } + + return xml.Header + buf.String(), nil + +} diff --git a/rss_test.go b/rss_test.go new file mode 100644 index 0000000..b273288 --- /dev/null +++ b/rss_test.go @@ -0,0 +1,49 @@ +package owl_test + +import ( + "h4kor/owl-blogs" + "strings" + "testing" +) + +func TestRenderRSSFeedMeta(t *testing.T) { + user := getTestUser() + user.SetConfig(owl.UserConfig{ + Title: "Test Title", + SubTitle: "Test SubTitle", + }) + res, err := owl.RenderRSSFeed(user) + if err != nil { + t.Error("Error rendering RSS feed: " + err.Error()) + return + } + if !strings.Contains(res, "") { + t.Error("xml version not rendered. Got: " + res) + } + if !strings.Contains(res, "") { + t.Error("rss version not rendered. Got: " + res) + } + +} + +func TestRenderRSSFeedUserData(t *testing.T) { + user := getTestUser() + user.SetConfig(owl.UserConfig{ + Title: "Test Title", + SubTitle: "Test SubTitle", + }) + res, err := owl.RenderRSSFeed(user) + if err != nil { + t.Error("Error rendering RSS feed: " + err.Error()) + return + } + if !strings.Contains(res, "Test Title") { + t.Error("Title not rendered. Got: " + res) + } + if !strings.Contains(res, "Test SubTitle") { + t.Error("SubTitle not rendered. Got: " + res) + } + if !strings.Contains(res, "http://localhost:8080/user/") { + t.Error("SubTitle not rendered. Got: " + res) + } +} From e93808ac88b56bc5c553fba907265850e5277254 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sat, 13 Aug 2022 18:47:27 +0200 Subject: [PATCH 3/7] items in rss feed. #3 --- post.go | 4 ++++ post_test.go | 11 +++++++++++ repository.go | 4 ++-- rss.go | 34 ++++++++++++++++++++-------------- rss_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ user.go | 6 ++++++ user_test.go | 7 +++++++ 7 files changed, 101 insertions(+), 16 deletions(-) diff --git a/post.go b/post.go index c7ae3b8..9666039 100644 --- a/post.go +++ b/post.go @@ -33,6 +33,10 @@ func (post Post) UrlPath() string { return post.user.UrlPath() + "posts/" + post.id + "/" } +func (post Post) FullUrl() string { + return post.user.FullUrl() + "posts/" + post.id + "/" +} + func (post Post) UrlMediaPath(filename string) string { return post.UrlPath() + "media/" + filename } diff --git a/post_test.go b/post_test.go index efd8412..d399782 100644 --- a/post_test.go +++ b/post_test.go @@ -34,6 +34,17 @@ func TestPostUrlPath(t *testing.T) { } } +func TestPostFullUrl(t *testing.T) { + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + expected := "http://localhost:8080/user/" + user.Name() + "/posts/" + post.Id() + "/" + if !(post.FullUrl() == expected) { + t.Error("Wrong url path") + t.Error("Expected: " + expected) + t.Error(" Got: " + post.FullUrl()) + } +} + func TestPostUrlMediaPath(t *testing.T) { user := getTestUser() post, _ := user.CreateNewPost("testpost") diff --git a/repository.go b/repository.go index 529af30..201ee74 100644 --- a/repository.go +++ b/repository.go @@ -113,9 +113,9 @@ func (repo Repository) UserUrlPath(user User) string { return "/user/" + user.name + "/" } -func (repo Repository) FullUserUrl(user User) string { +func (repo Repository) FullUrl() string { config, _ := repo.Config() - return config.Domain + repo.UserUrlPath(user) + return config.Domain } func (repo Repository) Template() (string, error) { diff --git a/rss.go b/rss.go index 948dbf5..e975350 100644 --- a/rss.go +++ b/rss.go @@ -19,10 +19,10 @@ type RSSChannel struct { } type RSSItem struct { - Title string `xml:"title"` - Link string `xml:"link"` - Description string `xml:"description"` - PubDate string `xml:"pubDate"` + Guid string `xml:"guid"` + Title string `xml:"title"` + Link string `xml:"link"` + PubDate string `xml:"pubDate"` } func RenderRSSFeed(user User) (string, error) { @@ -33,21 +33,27 @@ func RenderRSSFeed(user User) (string, error) { Version: "2.0", Channel: RSSChannel{ Title: config.Title, - Link: user.repo.FullUserUrl(user), + Link: user.FullUrl(), Description: config.SubTitle, Items: make([]RSSItem, 0), }, } - // posts, _ := user.Posts() - // for _, post := range posts { - // rss.Channel.Items = append(rss.Channel.Items, RSSItem{ - // Title: post.Title(), - // Link: post.Link(), - // Description: post.Description(), - // PubDate: post.PubDate(), - // }) - // } + posts, _ := user.Posts() + for _, postId := range posts { + post, _ := user.GetPost(postId) + _, meta := post.MarkdownData() + date, ok := meta["date"] + if !ok { + date = "" + } + rss.Channel.Items = append(rss.Channel.Items, RSSItem{ + Guid: postId, + Title: post.Title(), + Link: post.FullUrl(), + PubDate: date.(string), + }) + } buf := new(bytes.Buffer) err := xml.NewEncoder(buf).Encode(rss) diff --git a/rss_test.go b/rss_test.go index b273288..6073cef 100644 --- a/rss_test.go +++ b/rss_test.go @@ -2,6 +2,7 @@ package owl_test import ( "h4kor/owl-blogs" + "os" "strings" "testing" ) @@ -47,3 +48,53 @@ func TestRenderRSSFeedUserData(t *testing.T) { t.Error("SubTitle not rendered. Got: " + res) } } + +func TestRenderRSSFeedPostData(t *testing.T) { + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + + content := "---\n" + content += "title: Test Post\n" + content += "date: 2015-01-01\n" + content += "---\n" + content += "This is a test" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + res, err := owl.RenderRSSFeed(user) + if err != nil { + t.Error("Error rendering RSS feed: " + err.Error()) + return + } + if !strings.Contains(res, "Test Post") { + t.Error("Title not rendered. Got: " + res) + } + if !strings.Contains(res, post.FullUrl()) { + t.Error("SubTitle not rendered. Got: " + res) + } + if !strings.Contains(res, "2015-01-01") { + t.Error("Date not rendered. Got: " + res) + } +} + +func TestRenderRSSFeedPostDataWithoutDate(t *testing.T) { + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + + content := "---\n" + content += "title: Test Post\n" + content += "---\n" + content += "This is a test" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + res, err := owl.RenderRSSFeed(user) + if err != nil { + t.Error("Error rendering RSS feed: " + err.Error()) + return + } + if !strings.Contains(res, "Test Post") { + t.Error("Title not rendered. Got: " + res) + } + if !strings.Contains(res, post.FullUrl()) { + t.Error("SubTitle not rendered. Got: " + res) + } +} diff --git a/user.go b/user.go index 82fe15f..8b2131b 100644 --- a/user.go +++ b/user.go @@ -3,6 +3,7 @@ package owl import ( "fmt" "io/ioutil" + "net/url" "os" "path" "time" @@ -29,6 +30,11 @@ func (user User) UrlPath() string { return user.repo.UserUrlPath(user) } +func (user User) FullUrl() string { + url, _ := url.JoinPath(user.repo.FullUrl(), user.UrlPath()) + return url +} + func (user User) PostDir() string { return path.Join(user.Dir(), "public") } diff --git a/user_test.go b/user_test.go index e45e01d..3b372c9 100644 --- a/user_test.go +++ b/user_test.go @@ -145,3 +145,10 @@ func TestUserUrlPath(t *testing.T) { t.Error("Wrong url path, Expected: " + "/user/" + user.Name() + "/" + " Got: " + user.UrlPath()) } } + +func TestUserFullUrl(t *testing.T) { + user := getTestUser() + if !(user.FullUrl() == "http://localhost:8080/user/"+user.Name()+"/") { + t.Error("Wrong url path, Expected: " + "http://localhost:8080/user/" + user.Name() + "/" + " Got: " + user.FullUrl()) + } +} From bed274cbf64f6f005befefcf1ab4163002cfd0e2 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sat, 13 Aug 2022 19:07:10 +0200 Subject: [PATCH 4/7] Include Date in RSS Feed #3 + Parse metadata as struct --- post.go | 42 ++++++++++++++++++++++++++++-------------- rss.go | 6 +----- user.go | 3 ++- user_test.go | 12 ++++++++++++ 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/post.go b/post.go index 9666039..0bad857 100644 --- a/post.go +++ b/post.go @@ -6,9 +6,9 @@ import ( "path" "github.com/yuin/goldmark" - meta "github.com/yuin/goldmark-meta" "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/parser" + "gopkg.in/yaml.v2" ) type Post struct { @@ -17,6 +17,12 @@ type Post struct { title string } +type PostMeta struct { + Title string `yaml:"title"` + Aliases []string `yaml:"aliases"` + Date string `yaml:"date"` +} + func (post Post) Id() string { return post.id } @@ -55,11 +61,27 @@ func (post Post) Content() []byte { return data } -func (post Post) MarkdownData() (bytes.Buffer, map[string]interface{}) { +func (post Post) MarkdownData() (bytes.Buffer, PostMeta) { data := post.Content() + + // get yaml metadata block + meta := PostMeta{} + trimmedData := bytes.TrimSpace(data) + // check first line is --- + if string(trimmedData[0:4]) == "---\n" { + trimmedData = trimmedData[4:] + // find --- end + end := bytes.Index(trimmedData, []byte("\n---\n")) + if end != -1 { + metaData := trimmedData[:end] + yaml.Unmarshal(metaData, &meta) + data = trimmedData[end+5:] + } + } + markdown := goldmark.New( goldmark.WithExtensions( - meta.Meta, + // meta.Meta, extension.GFM, ), ) @@ -68,21 +90,13 @@ func (post Post) MarkdownData() (bytes.Buffer, map[string]interface{}) { if err := markdown.Convert(data, &buf, parser.WithContext(context)); err != nil { panic(err) } - metaData := meta.Get(context) + // metaData := meta.Get(context) - return buf, metaData + return buf, meta } func (post Post) Aliases() []string { _, metaData := post.MarkdownData() - if metaData["aliases"] != nil { - alias_data := metaData["aliases"].([]interface{}) - aliases := make([]string, 0) - for _, alias := range alias_data { - aliases = append(aliases, alias.(string)) - } - return aliases - } - return []string{} + return metaData.Aliases } diff --git a/rss.go b/rss.go index e975350..39d13e5 100644 --- a/rss.go +++ b/rss.go @@ -43,15 +43,11 @@ func RenderRSSFeed(user User) (string, error) { for _, postId := range posts { post, _ := user.GetPost(postId) _, meta := post.MarkdownData() - date, ok := meta["date"] - if !ok { - date = "" - } rss.Channel.Items = append(rss.Channel.Items, RSSItem{ Guid: postId, Title: post.Title(), Link: post.FullUrl(), - PubDate: date.(string), + PubDate: meta.Date, }) } diff --git a/user.go b/user.go index 8b2131b..47905ae 100644 --- a/user.go +++ b/user.go @@ -73,7 +73,7 @@ func (user User) GetPost(id string) (Post, error) { post := Post{user: &user, id: id} _, metaData := post.MarkdownData() - title := metaData["title"] + title := metaData.Title post.title = fmt.Sprint(title) return post, nil @@ -100,6 +100,7 @@ func (user User) CreateNewPost(title string) (Post, error) { initial_content := "" initial_content += "---\n" initial_content += "title: " + title + "\n" + initial_content += "date: " + time.Now().UTC().Format(time.RFC3339) + "\n" initial_content += "---\n" initial_content += "\n" initial_content += "Write your post here.\n" diff --git a/user_test.go b/user_test.go index 3b372c9..7cc21ec 100644 --- a/user_test.go +++ b/user_test.go @@ -35,6 +35,18 @@ func TestCreateNewPostCreatesMediaDir(t *testing.T) { } } +func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) { + user := getTestUser() + // Create a new post + user.CreateNewPost("testpost") + posts, _ := user.Posts() + post, _ := user.GetPost(posts[0]) + _, meta := post.MarkdownData() + if meta.Date == "" { + t.Error("Found no date. Got: " + meta.Date) + } +} + func TestCreateNewPostMultipleCalls(t *testing.T) { // Create a new user repo, _ := owl.CreateRepository(testRepoName()) From e15268092a5e165295f1d6fb4ddde82fbff94ec6 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sat, 13 Aug 2022 19:08:20 +0200 Subject: [PATCH 5/7] RFC 822 default date --- user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user.go b/user.go index 47905ae..6f1b5fc 100644 --- a/user.go +++ b/user.go @@ -100,7 +100,7 @@ func (user User) CreateNewPost(title string) (Post, error) { initial_content := "" initial_content += "---\n" initial_content += "title: " + title + "\n" - initial_content += "date: " + time.Now().UTC().Format(time.RFC3339) + "\n" + initial_content += "date: " + time.Now().UTC().Format(time.RFC822) + "\n" initial_content += "---\n" initial_content += "\n" initial_content += "Write your post here.\n" From c89bca3fc75f888471f3b71858b45dd7ee152b96 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sat, 13 Aug 2022 19:15:34 +0200 Subject: [PATCH 6/7] four digit year #3 + README update --- README.md | 1 + user.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f5ef6ca..ceb591f 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ Posts are Markdown files with a mandatory metadata head. ``` --- title: My new Post +date: 13 Aug 22 17:07 UTC aliases: - /my/new/post - /old_blog_path/ diff --git a/user.go b/user.go index 6f1b5fc..b664786 100644 --- a/user.go +++ b/user.go @@ -100,7 +100,7 @@ func (user User) CreateNewPost(title string) (Post, error) { initial_content := "" initial_content += "---\n" initial_content += "title: " + title + "\n" - initial_content += "date: " + time.Now().UTC().Format(time.RFC822) + "\n" + initial_content += "date: " + time.Now().UTC().Format("02 Jan 2006 15:04 MST") + "\n" initial_content += "---\n" initial_content += "\n" initial_content += "Write your post here.\n" From bd5f69486a2e238d5ba89982f3ae3fb87dc6688e Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Mon, 15 Aug 2022 07:31:19 +0200 Subject: [PATCH 7/7] date adjustment --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ceb591f..6219cf1 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Posts are Markdown files with a mandatory metadata head. ``` --- title: My new Post -date: 13 Aug 22 17:07 UTC +date: 13 Aug 2022 17:07 UTC aliases: - /my/new/post - /old_blog_path/