Skip to content

Commit 7b41cb1

Browse files
Add Main og Image
1 parent 0d83b76 commit 7b41cb1

File tree

5 files changed

+202
-173
lines changed

5 files changed

+202
-173
lines changed

src/pages/[date]/[title]/og.webp.js

Lines changed: 11 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,14 @@
1-
import satori from "satori"
2-
import fs from "fs/promises"
3-
import path from "path"
4-
import sharp from "sharp"
5-
6-
const [bgImg, logo, regularFont, boldFont] = await Promise.all([
7-
fs.readFile(path.resolve("./src/assets/og/graph-paper.svg"), "base64"),
8-
fs.readFile(path.resolve("./src/assets/og/logo.png"), "base64"),
9-
fs.readFile(path.resolve("./src/assets/og/Barlow-Regular.ttf")),
10-
fs.readFile(path.resolve("./src/assets/og/Barlow-Bold.ttf")),
11-
])
12-
13-
const satoriOptions = {
14-
width: 1200,
15-
height: 630,
16-
fonts: [
17-
{
18-
name: "Barlow",
19-
data: regularFont,
20-
weight: 400,
21-
style: "normal",
22-
},
23-
{
24-
name: "Barlow",
25-
data: boldFont,
26-
weight: 600,
27-
style: "normal",
28-
},
29-
],
30-
}
31-
32-
export async function get({ props: { post } }) {
33-
const { title, tags, date } = post
34-
35-
const svg = await satori(
36-
{
37-
type: "div",
38-
props: {
39-
style: {
40-
display: "flex",
41-
flexDirection: "column",
42-
justifyContent: "center",
43-
alignItems: "center",
44-
width: "100%",
45-
height: "100%",
46-
padding: "3rem",
47-
background: "#111",
48-
boxSizing: "border-box",
49-
position: "relative",
50-
gap: "1rem",
51-
color: "#F0F0F0",
52-
fontFamily: "Barlow",
53-
},
54-
children: [
55-
{
56-
type: "div",
57-
props: {
58-
style: {
59-
backgroundImage: `url("data:image/svg+xml;base64,${bgImg}")`,
60-
position: "absolute",
61-
inset: 0,
62-
width: "100%",
63-
height: "100%",
64-
maskImage:
65-
"radial-gradient(60% 60%, hsl(0, 0%, 0%, .80), transparent)",
66-
},
67-
},
68-
},
69-
{
70-
type: "div",
71-
props: {
72-
style: {
73-
display: "flex",
74-
gap: "1rem",
75-
},
76-
children: tags.map(tag => ({
77-
type: "div",
78-
props: {
79-
style: {
80-
border: "1px solid #CCC",
81-
color: "#CCC",
82-
padding: "0.5rem 1rem",
83-
borderRadius: "999px",
84-
fontSize: "1.25rem",
85-
background: "#111",
86-
},
87-
children: tag,
88-
},
89-
})),
90-
},
91-
},
92-
{
93-
type: "h1",
94-
props: {
95-
style: {
96-
textAlign: "center",
97-
fontSize: "5.5rem",
98-
fontWeight: "bold",
99-
margin: 0,
100-
lineHeight: "1",
101-
textWrap: "balance",
102-
},
103-
children: title,
104-
},
105-
},
106-
{
107-
type: "div",
108-
props: {
109-
style: {
110-
display: "flex",
111-
alignItems: "center",
112-
gap: "1rem",
113-
},
114-
children: [
115-
{
116-
type: "img",
117-
props: {
118-
src: `data:image/png;base64,${logo}`,
119-
style: {
120-
width: "5rem",
121-
},
122-
},
123-
},
124-
{
125-
type: "div",
126-
props: {
127-
style: {
128-
display: "flex",
129-
flexDirection: "column",
130-
},
131-
children: [
132-
{
133-
type: "div",
134-
props: {
135-
style: {
136-
fontSize: "2rem",
137-
color: "#CCC",
138-
},
139-
children: "Web Dev Simplified",
140-
},
141-
},
142-
{
143-
type: "div",
144-
props: {
145-
style: {
146-
fontSize: "1.5rem",
147-
color: "#AAA",
148-
},
149-
children: Intl.DateTimeFormat("en-US", {
150-
dateStyle: "medium",
151-
}).format(new Date(date)),
152-
},
153-
},
154-
],
155-
},
156-
},
157-
],
158-
},
159-
},
160-
],
161-
},
162-
},
163-
satoriOptions,
164-
)
165-
166-
const webp = await sharp(Buffer.from(svg)).webp({ quality: 100 }).toBuffer()
167-
168-
return new Response(webp, { headers: { "Content-Type": "image/webp" } })
1+
import { generateOpenGraphImage } from "src/utils/generateOpenGraphImage"
2+
3+
export function get({ props: { post } }) {
4+
return generateOpenGraphImage({
5+
title: post.title,
6+
tags: post.tags ?? [],
7+
secondaryText: post.author || "Web Dev Simplified",
8+
tertiaryText: Intl.DateTimeFormat("en-US", {
9+
dateStyle: "medium",
10+
}).format(new Date(post.updatedDate ?? post.date)),
11+
})
16912
}
17013

17114
export async function getStaticPaths() {

src/pages/index.astro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "../styles/blog.css"
55
import BaseHead from "../components/BaseHead.astro"
66
import BlogList from "/src/components/BlogList.jsx"
77
import BaseTopOfBody from "src/components/BaseTopOfBody.astro"
8+
import path from "path"
89
910
interface MarkdownFrontmatter {
1011
date: number
@@ -19,8 +20,7 @@ interface MarkdownFrontmatter {
1920
// It will run during the build, but never in the browser.
2021
// All variables are available to use in the HTML template below.
2122
let title = "Web Dev Simplified Blog"
22-
// TODO: Update description
23-
let description = "Web Dev Simplified Blog"
23+
let description = "In depth articles with interactive examples about every web dev topic."
2424
let permalink = "https://blog.webdevsimplified.com"
2525
2626
// Data Fetching: List all Markdown posts in the repo.
@@ -43,13 +43,15 @@ const allPosts = allMarkdownPosts
4343
return bDate - aDate
4444
})
4545
46+
const url = Astro.request.url
47+
4648
// Full Astro Component Syntax:
4749
// https://docs.astro.build/core-concepts/astro-components/
4850
---
4951

5052
<html lang="en">
5153
<head>
52-
<BaseHead {title} {description} {permalink} />
54+
<BaseHead {title} {description} {permalink} image={path.join(url, "og.webp")} />
5355

5456
<style>
5557
.content {

src/pages/og.webp.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { generateOpenGraphImage } from "src/utils/generateOpenGraphImage"
2+
3+
export function get() {
4+
const count = Object.values(
5+
import.meta.glob("./**/*.mdx", { eager: true }),
6+
).length
7+
8+
return generateOpenGraphImage({
9+
title: "Web Dev Simplified Blog",
10+
tags: ["TypeScript", "React", "CSS", "Next.js", "Technical Explainers"],
11+
secondaryText: "No Stack to Full Stack",
12+
tertiaryText: `${count} Articles`,
13+
})
14+
}

src/pages/rss.xml.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export function get(context) {
1010

1111
return rss({
1212
title: "Web Dev Simplified Blog",
13-
// TODO: Update description
14-
description: "Web Dev Simplified Blog",
13+
description:
14+
"In depth articles with interactive examples about every web dev topic.",
1515
customData: `<language>en-us</language>`,
1616
site: context.site,
1717
items: allPosts.map(item => ({

0 commit comments

Comments
 (0)