From 9056b22536973e5c2b511681f832858fa182ea72 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Mon, 10 Oct 2022 20:59:06 +0200 Subject: [PATCH] add reply-to url. #22 --- embed/post.html | 13 +++++++++++++ post.go | 10 +++++++++- renderer_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/embed/post.html b/embed/post.html index b87c1a1..36e3cc3 100644 --- a/embed/post.html +++ b/embed/post.html @@ -15,6 +15,19 @@

+ + {{ if .Post.Meta.Reply.Url }} +

+ In reply to: + {{ if .Post.Meta.Reply.Text }} + {{.Post.Meta.Reply.Text}} + {{ else }} + {{.Post.Meta.Reply.Url}} + {{ end }} + +

+ {{ end }} +
{{.Content}}
diff --git a/post.go b/post.go index f731be1..ee4d7b0 100644 --- a/post.go +++ b/post.go @@ -27,11 +27,17 @@ type Post struct { wmLock sync.Mutex } +type Reply struct { + Url string `yaml:"url"` + Text string `yaml:"text"` +} + type PostMeta struct { Title string `yaml:"title"` Aliases []string `yaml:"aliases"` Date time.Time `yaml:"date"` Draft bool `yaml:"draft"` + Reply Reply `yaml:"reply"` } func (pm PostMeta) FormattedDate() string { @@ -43,6 +49,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error { Title string `yaml:"title"` Aliases []string `yaml:"aliases"` Draft bool `yaml:"draft"` + Reply Reply `yaml:"reply"` } type S struct { Date string `yaml:"date"` @@ -60,6 +67,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error { pm.Title = t.Title pm.Aliases = t.Aliases pm.Draft = t.Draft + pm.Reply = t.Reply possibleFormats := []string{ "2006-01-02", @@ -206,7 +214,7 @@ func (post *Post) LoadMeta() error { if string(trimmedData[0:4]) == "---\n" { trimmedData = trimmedData[4:] // find --- end - end := bytes.Index(trimmedData, []byte("\n---\n")) + end := bytes.Index(trimmedData, []byte("---\n")) if end != -1 { metaData := trimmedData[:end] err := yaml.Unmarshal(metaData, &meta) diff --git a/renderer_test.go b/renderer_test.go index d98f6a1..10714d3 100644 --- a/renderer_test.go +++ b/renderer_test.go @@ -277,3 +277,49 @@ func TestAuthorNameInPost(t *testing.T) { t.Error("Author Name not included. Got: " + result) } } + +func TestRenderReplyWithoutText(t *testing.T) { + + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + + content := "---\n" + content += "title: test\n" + content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n" + content += "reply: \n" + content += " url: https://example.com/post\n" + content += "---\n" + content += "\n" + content += "Hi \n" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + result, _ := owl.RenderPost(&post) + if !strings.Contains(result, "https://example.com/post") { + t.Error("Reply url not rendered. Got: " + result) + } +} + +func TestRenderReplyWithText(t *testing.T) { + + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + + content := "---\n" + content += "title: test\n" + content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n" + content += "reply: \n" + content += " url: https://example.com/post\n" + content += " text: \"This is a reply\"\n" + content += "---\n" + content += "Hi \n" + os.WriteFile(post.ContentFile(), []byte(content), 0644) + + result, _ := owl.RenderPost(&post) + if !strings.Contains(result, "https://example.com/post") { + t.Error("Reply url not rendered. Got: " + result) + } + + if !strings.Contains(result, "This is a reply") { + t.Error("Reply text not rendered. Got: " + result) + } +}