added some OpenGraph meta tags

This commit is contained in:
Niko Abeler 2022-10-19 21:14:31 +02:00
parent 5abd9f4b4b
commit 7667190a9c
4 changed files with 84 additions and 19 deletions

View File

@ -6,6 +6,19 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }} - {{ .User.Config.Title }}</title> <title>{{ .Title }} - {{ .User.Config.Title }}</title>
<meta property="og:title" content="{{ .Title }}" />
{{ if .Description }}
<meta name="description" content="{{ .Description }}">
<meta property="og:description" content="{{ .Description }}" />
{{ end }}
{{ if .Type }}
<meta property="og:type" content="{{ .Type }}" />
{{ end }}
{{ if .SelfUrl }}
<meta property="og:url" content="{{ .SelfUrl }}" />
{{ end }}
<link rel="stylesheet" href="/static/pico.min.css"> <link rel="stylesheet" href="/static/pico.min.css">
<link rel="webmention" href="{{ .User.WebmentionUrl }}"> <link rel="webmention" href="{{ .User.WebmentionUrl }}">
<style> <style>

View File

@ -33,6 +33,7 @@ type Reply struct {
type PostMeta struct { type PostMeta struct {
Title string `yaml:"title"` Title string `yaml:"title"`
Description string `yaml:"description"`
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"`
@ -46,6 +47,7 @@ func (pm PostMeta) FormattedDate() string {
func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error { func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error {
type T struct { type T struct {
Title string `yaml:"title"` Title string `yaml:"title"`
Description string `yaml:"description"`
Aliases []string `yaml:"aliases"` Aliases []string `yaml:"aliases"`
Draft bool `yaml:"draft"` Draft bool `yaml:"draft"`
Reply Reply `yaml:"reply"` Reply Reply `yaml:"reply"`
@ -64,6 +66,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error {
} }
pm.Title = t.Title pm.Title = t.Title
pm.Description = t.Description
pm.Aliases = t.Aliases pm.Aliases = t.Aliases
pm.Draft = t.Draft pm.Draft = t.Draft
pm.Reply = t.Reply pm.Reply = t.Reply

View File

@ -8,7 +8,10 @@ import (
type PageContent struct { type PageContent struct {
Title string Title string
Description string
Content template.HTML Content template.HTML
Type string
SelfUrl string
} }
type PostRenderData struct { type PostRenderData struct {
@ -47,11 +50,17 @@ func renderIntoBaseTemplate(user User, data PageContent) (string, error) {
full_data := struct { full_data := struct {
Title string Title string
Description string
Content template.HTML Content template.HTML
Type string
SelfUrl string
User User User User
}{ }{
Title: data.Title, Title: data.Title,
Description: data.Description,
Content: data.Content, Content: data.Content,
Type: data.Type,
SelfUrl: data.SelfUrl,
User: user, User: user,
} }
@ -79,7 +88,10 @@ func RenderPost(post *Post) (string, error) {
return renderIntoBaseTemplate(*post.user, PageContent{ return renderIntoBaseTemplate(*post.user, PageContent{
Title: post.Title(), Title: post.Title(),
Description: post.Meta().Description,
Content: template.HTML(postHtml), Content: template.HTML(postHtml),
Type: "article",
SelfUrl: post.FullUrl(),
}) })
} }

View File

@ -323,3 +323,40 @@ func TestRenderReplyWithText(t *testing.T) {
t.Error("Reply text not rendered. Got: " + result) t.Error("Reply text not rendered. Got: " + result)
} }
} }
func TestOpenGraphTags(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
content := "---\n"
content += "title: The Rock\n"
content += "description: Dwayne Johnson\n"
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
content += "---\n"
content += "\n"
content += "Hi \n"
err := os.WriteFile(post.ContentFile(), []byte(content), 0644)
if err != nil {
t.Error(err)
}
post, _ = user.GetPost(post.Id())
result, _ := owl.RenderPost(post)
if !strings.Contains(result, "<meta property=\"og:title\" content=\"The Rock\" />") {
t.Error("incorrent og:title . Got: " + result)
}
if !strings.Contains(result, "<meta property=\"og:description\" content=\"Dwayne Johnson\" />") {
t.Error("incorrent og:description . Got: " + result)
}
if !strings.Contains(result, "<meta property=\"og:type\" content=\"article\" />") {
t.Error("incorrent og:type . Got: " + result)
}
if !strings.Contains(result, "<meta property=\"og:url\" content=\""+post.FullUrl()+"\" />") {
t.Error("incorrent og:url . Got: " + result)
}
}