feat: add opengraph and twitter previews

Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2023-05-28 23:51:48 +05:30
parent c628ee7d6a
commit 3837d07495
3 changed files with 58 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package routes package routes
import ( import (
"fmt"
"html/template" "html/template"
"time" "time"
@ -23,7 +24,22 @@ func show(c *fiber.Ctx) error {
post := e.Data.Post post := e.Data.Post
publishDate := time.UnixMilli(e.Data.Post.CreatedAt) publishDate := time.UnixMilli(e.Data.Post.CreatedAt)
p := converters.ConvertParagraphs(post.Content.BodyModel.Paragraphs) paragraphs := post.Content.BodyModel.Paragraphs
p := converters.ConvertParagraphs(paragraphs)
desc := ""
if len(paragraphs) >= 0 {
desc = paragraphs[1].Text
}
imgUrl := ""
for _, p := range paragraphs {
if p.Type == "IMG" {
imgUrl = fmt.Sprintf("https://miro.medium.com/v2/resize:fit:1200/%s", p.Metadata.ID)
break
}
}
return c.Render("show", fiber.Map { return c.Render("show", fiber.Map {
"Title": post.Title, "Title": post.Title,
@ -31,6 +47,9 @@ func show(c *fiber.Ctx) error {
"Author": post.Creator.Name, "Author": post.Creator.Name,
"PublishDate": publishDate.Format(time.DateOnly), "PublishDate": publishDate.Format(time.DateOnly),
"Paragraphs": template.HTML(p), "Paragraphs": template.HTML(p),
"Description": desc,
"Path": c.Path(),
"Image": imgUrl,
}) })
} }

View File

@ -14,6 +14,43 @@
rel="stylesheet"> rel="stylesheet">
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<meta property="og:type" content="object">
<meta property="og:title" content="{{ .Title }}">
<meta property="og:image" content="{{.Image}}">
<meta name="twitter:title" content="{{ .Title }}">
<meta itemprop="name" content="{{ .Title }}">
<meta name="application-name" content="{{ .Title }}">
<meta property="og:site_name" content="{{ .Title }}">
<meta name="theme-color" content="#ffffff">
<meta property="og:locale" content="en">
<meta name="language" content="en">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@Spherical_Kat">
<meta name="robots" content="index,follow">
<meta name="googlebot" content="index,follow">
<meta name="keywords" content="medium,blog,frontend,alternative,technology,golang,go">
<meta name="coverage" content="Worldwide">
<meta name="distribution" content="Global">
<meta name="HandheldFriendly" content="True">
<meta name="HandheldFriendly" content="True">
<meta name="apple-mobile-web-app-title" content="{{ .Title }}">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="description" content="{{.Description}}">
<meta itemprop="description" content={{.Description}}>
<meta property="og:description" content={{.Description}}>
<meta name="twitter:description" content={{.Description}}>
<meta name="url" content="https://medium.rip/{{.Path}}">
<meta name="twitter:url" content="https://medium.rip/{{.Path}}">
<meta property="og:url" content="https://medium.rip/{{.Path}}">
</head> </head>
<body class="flex flex-col h-full w-full items-center"> <body class="flex flex-col h-full w-full items-center">

View File

@ -40,19 +40,7 @@ func (i *Image) width() int64 {
} }
func (i *Image) src() string { func (i *Image) src() string {
return fmt.Sprintf("%s/%d/%d/%s", IMAGE_HOST, i.width(), i.height(), i.ID) return fmt.Sprintf("https://miro.medium.com/v2/resize:fit:1200/%s", i.ID)
}
func (i *Image) height() int64 {
if i.OriginalWidth > MAX_WIDTH {
return i.OriginalHeight * int64(i.ratio())
} else {
return i.OriginalHeight
}
}
func (i *Image) ratio() float32 {
return float32(MAX_WIDTH) / float32(i.OriginalWidth)
} }
func ConvertParagraphs(paragraphs []entities.Paragraph) string { func ConvertParagraphs(paragraphs []entities.Paragraph) string {