From 3837d074952db510c15af04e8ddefe2b259fa3cf Mon Sep 17 00:00:00 2001 From: Sphericalkat Date: Sun, 28 May 2023 23:51:48 +0530 Subject: [PATCH] feat: add opengraph and twitter previews Signed-off-by: Sphericalkat --- api/routes/show.go | 21 ++++++++++++++- frontend/show.html | 37 +++++++++++++++++++++++++++ pkg/converters/paragraph_converter.go | 14 +--------- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/api/routes/show.go b/api/routes/show.go index fb9a8b2..f55c37b 100644 --- a/api/routes/show.go +++ b/api/routes/show.go @@ -1,6 +1,7 @@ package routes import ( + "fmt" "html/template" "time" @@ -23,7 +24,22 @@ func show(c *fiber.Ctx) error { post := e.Data.Post 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 { "Title": post.Title, @@ -31,6 +47,9 @@ func show(c *fiber.Ctx) error { "Author": post.Creator.Name, "PublishDate": publishDate.Format(time.DateOnly), "Paragraphs": template.HTML(p), + "Description": desc, + "Path": c.Path(), + "Image": imgUrl, }) } diff --git a/frontend/show.html b/frontend/show.html index 0acac0c..77df924 100644 --- a/frontend/show.html +++ b/frontend/show.html @@ -14,6 +14,43 @@ rel="stylesheet"> {{ .Title }} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pkg/converters/paragraph_converter.go b/pkg/converters/paragraph_converter.go index de6be6a..7433769 100644 --- a/pkg/converters/paragraph_converter.go +++ b/pkg/converters/paragraph_converter.go @@ -40,19 +40,7 @@ func (i *Image) width() int64 { } func (i *Image) src() string { - return fmt.Sprintf("%s/%d/%d/%s", IMAGE_HOST, i.width(), i.height(), 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) + return fmt.Sprintf("https://miro.medium.com/v2/resize:fit:1200/%s", i.ID) } func ConvertParagraphs(paragraphs []entities.Paragraph) string {