add reply-to url. #22

This commit is contained in:
Niko Abeler 2022-10-10 20:59:06 +02:00
parent 4cc69ff257
commit 9056b22536
3 changed files with 68 additions and 1 deletions

View File

@ -15,6 +15,19 @@
</hgroup> </hgroup>
<hr> <hr>
<br> <br>
{{ if .Post.Meta.Reply.Url }}
<p style="font-style: italic;filter: opacity(80%);">
In reply to: <a class="u-in-reply-to h-cite" rel="in-reply-to" href="{{.Post.Meta.Reply.Url}}">
{{ if .Post.Meta.Reply.Text }}
{{.Post.Meta.Reply.Text}}
{{ else }}
{{.Post.Meta.Reply.Url}}
{{ end }}
</a>
</p>
{{ end }}
<div class="e-content"> <div class="e-content">
{{.Content}} {{.Content}}
</div> </div>

10
post.go
View File

@ -27,11 +27,17 @@ type Post struct {
wmLock sync.Mutex wmLock sync.Mutex
} }
type Reply struct {
Url string `yaml:"url"`
Text string `yaml:"text"`
}
type PostMeta struct { type PostMeta struct {
Title string `yaml:"title"` Title string `yaml:"title"`
Aliases []string `yaml:"aliases"` Aliases []string `yaml:"aliases"`
Date time.Time `yaml:"date"` Date time.Time `yaml:"date"`
Draft bool `yaml:"draft"` Draft bool `yaml:"draft"`
Reply Reply `yaml:"reply"`
} }
func (pm PostMeta) FormattedDate() string { func (pm PostMeta) FormattedDate() string {
@ -43,6 +49,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error {
Title string `yaml:"title"` Title string `yaml:"title"`
Aliases []string `yaml:"aliases"` Aliases []string `yaml:"aliases"`
Draft bool `yaml:"draft"` Draft bool `yaml:"draft"`
Reply Reply `yaml:"reply"`
} }
type S struct { type S struct {
Date string `yaml:"date"` Date string `yaml:"date"`
@ -60,6 +67,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error {
pm.Title = t.Title pm.Title = t.Title
pm.Aliases = t.Aliases pm.Aliases = t.Aliases
pm.Draft = t.Draft pm.Draft = t.Draft
pm.Reply = t.Reply
possibleFormats := []string{ possibleFormats := []string{
"2006-01-02", "2006-01-02",
@ -206,7 +214,7 @@ func (post *Post) LoadMeta() error {
if string(trimmedData[0:4]) == "---\n" { if string(trimmedData[0:4]) == "---\n" {
trimmedData = trimmedData[4:] trimmedData = trimmedData[4:]
// find --- end // find --- end
end := bytes.Index(trimmedData, []byte("\n---\n")) end := bytes.Index(trimmedData, []byte("---\n"))
if end != -1 { if end != -1 {
metaData := trimmedData[:end] metaData := trimmedData[:end]
err := yaml.Unmarshal(metaData, &meta) err := yaml.Unmarshal(metaData, &meta)

View File

@ -277,3 +277,49 @@ func TestAuthorNameInPost(t *testing.T) {
t.Error("Author Name not included. Got: " + result) 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)
}
}