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 name="viewport" content="width=device-width, initial-scale=1.0">
<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="webmention" href="{{ .User.WebmentionUrl }}">
<style>

21
post.go
View File

@ -32,11 +32,12 @@ type Reply struct {
}
type PostMeta struct {
Title string `yaml:"title"`
Aliases []string `yaml:"aliases"`
Date time.Time `yaml:"date"`
Draft bool `yaml:"draft"`
Reply Reply `yaml:"reply"`
Title string `yaml:"title"`
Description string `yaml:"description"`
Aliases []string `yaml:"aliases"`
Date time.Time `yaml:"date"`
Draft bool `yaml:"draft"`
Reply Reply `yaml:"reply"`
}
func (pm PostMeta) FormattedDate() string {
@ -45,10 +46,11 @@ func (pm PostMeta) FormattedDate() string {
func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error {
type T struct {
Title string `yaml:"title"`
Aliases []string `yaml:"aliases"`
Draft bool `yaml:"draft"`
Reply Reply `yaml:"reply"`
Title string `yaml:"title"`
Description string `yaml:"description"`
Aliases []string `yaml:"aliases"`
Draft bool `yaml:"draft"`
Reply Reply `yaml:"reply"`
}
type S struct {
Date string `yaml:"date"`
@ -64,6 +66,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
pm.Title = t.Title
pm.Description = t.Description
pm.Aliases = t.Aliases
pm.Draft = t.Draft
pm.Reply = t.Reply

View File

@ -7,8 +7,11 @@ import (
)
type PageContent struct {
Title string
Content template.HTML
Title string
Description string
Content template.HTML
Type string
SelfUrl string
}
type PostRenderData struct {
@ -46,13 +49,19 @@ func renderIntoBaseTemplate(user User, data PageContent) (string, error) {
}
full_data := struct {
Title string
Content template.HTML
User User
Title string
Description string
Content template.HTML
Type string
SelfUrl string
User User
}{
Title: data.Title,
Content: data.Content,
User: user,
Title: data.Title,
Description: data.Description,
Content: data.Content,
Type: data.Type,
SelfUrl: data.SelfUrl,
User: user,
}
var html bytes.Buffer
@ -78,8 +87,11 @@ func RenderPost(post *Post) (string, error) {
}
return renderIntoBaseTemplate(*post.user, PageContent{
Title: post.Title(),
Content: template.HTML(postHtml),
Title: post.Title(),
Description: post.Meta().Description,
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)
}
}
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)
}
}