Skip to content

Commit b73de56

Browse files
committed
Generate og:images
1 parent 9d454b7 commit b73de56

File tree

8 files changed

+69
-3
lines changed

8 files changed

+69
-3
lines changed

components/Layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const Layout = (props: Props) =>
1919
${props.description && html`
2020
<meta name="description" content="${props.description}">
2121
`}
22-
<meta property="og:image" content="/assets/og.png">
22+
<meta property="og:image" content="./og.png">
2323
<script type="module" src="/scripts.js"></script>
2424
<script
2525
data-goatcounter="https://mastrojs.goatcounter.com/count"

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"lineWidth": 100
1111
},
1212
"imports": {
13+
"@mastrojs/og-image": "jsr:@mastrojs/og-image@^0.0.4",
1314
"@mastrojs/feed": "jsr:@mastrojs/feed@^0.0.1",
1415
"@mastrojs/markdown": "jsr:@mastrojs/[email protected]",
1516
"@mastrojs/mastro": "jsr:@mastrojs/[email protected]",

deno.lock

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helpers/Roboto-Bold.ttf

143 KB
Binary file not shown.

helpers/ogChef.png

9.08 KB
Loading

routes/[...slug].server.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const { GET, getStaticPaths } = serveMarkdownFolder({
3030

3131
return htmlToResponse(
3232
Layout({
33-
title: meta.metaTitle || (meta.title ? `${meta.title} | Mastro ${part?.label}` : "Mastro"),
33+
title: meta.metaTitle || (meta.title ? `${meta.title} | Mastro ${pageTitlePrefix(pathname)}` : "Mastro"),
3434
description: meta.description,
3535
children: html`
3636
${Sidebar(sidebar, part, pathname)}
@@ -94,6 +94,18 @@ export const { GET, getStaticPaths } = serveMarkdownFolder({
9494
);
9595
});
9696

97+
export const pageTitlePrefix = (path: string) => {
98+
const segments = path.split("/");
99+
if (segments.length > 2) {
100+
const part = segments[1];
101+
return part
102+
? (part[0].toUpperCase() + part.slice(1))
103+
: "";
104+
} else {
105+
return "";
106+
}
107+
}
108+
97109
const getPrevNext = (contents: SidebarItem[] | undefined, pathname: string) => {
98110
if (!contents) {
99111
return {};

routes/[...slug]/og.png.server.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { serveMarkdownFolder } from "@mastrojs/markdown";
2+
import { readFile } from "@mastrojs/mastro";
3+
import { renderImage } from "@mastrojs/og-image";
4+
import { pageTitlePrefix } from "../[...slug].server.ts";
5+
6+
const fontFile = await readFile("./helpers/Roboto-Bold.ttf");
7+
const chefIcon = await readFile("./helpers/ogChef.png");
8+
9+
export const GET = (req: Request) => {
10+
const url = new URL(req.url);
11+
url.pathname = url.pathname.slice(0, -6); // slice off "og.png"
12+
return mdFolder.GET(new Request(url));
13+
};
14+
15+
export const getStaticPaths = async () => {
16+
const paths = await mdFolder.getStaticPaths();
17+
return paths.map((path) => path + "og.png");
18+
};
19+
20+
const mdFolder = serveMarkdownFolder({ folder: "data" }, ({ meta }, req) => {
21+
const { pathname } = new URL(req.url);
22+
const prefix = pageTitlePrefix(pathname);
23+
const text = "Mastro\n\n" + (prefix ? `${prefix}: ` : "") + meta.title;
24+
return renderImage(text, {
25+
fontFile,
26+
background: (ctx, canvas) => {
27+
ctx.fillStyle = "white";
28+
ctx.fillRect(0, 0, 1200, 600);
29+
// deno-lint-ignore no-explicit-any
30+
ctx.drawImage(canvas.decodeImage(chefIcon) as any, 335, 100);
31+
},
32+
});
33+
});

routes/assets/og.png

-71.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)