|
| 1 | +/* eslint-disable jsx-a11y/alt-text */ |
| 2 | + |
| 3 | +import { ImageResponse } from "next/og"; |
| 4 | +// import { notFound } from "next/navigation"; |
| 5 | +import path from "path"; |
| 6 | +import fs from "fs/promises"; |
| 7 | +import glob from "fast-glob"; |
| 8 | +import { getFrontMatter } from "../../../lib/helpers/posts"; |
| 9 | + |
| 10 | +export const contentType = "image/png"; |
| 11 | +export const size = { |
| 12 | + // https://developers.facebook.com/docs/sharing/webmasters/images/ |
| 13 | + width: 1200, |
| 14 | + height: 630, |
| 15 | +}; |
| 16 | + |
| 17 | +const Image = async ({ params }: { params: { slug: string } }) => { |
| 18 | + const { slug } = params; |
| 19 | + |
| 20 | + // check for invalid slug |
| 21 | + // const validSlugs = await getPostSlugs(); |
| 22 | + // if (!validSlugs.includes(slug)) { |
| 23 | + // notFound(); |
| 24 | + // } |
| 25 | + |
| 26 | + // search for an opengraph-image.* file next to the note's index.mdx and load it |
| 27 | + const imagePath = await glob(`notes/${slug}/opengraph-image.(png|jpg|jpeg)`); |
| 28 | + let imageSrc = null; |
| 29 | + if (imagePath.length > 0) { |
| 30 | + const imageData = await fs.readFile(path.join(process.cwd(), imagePath[0])); |
| 31 | + imageSrc = Uint8Array.from(imageData).buffer; |
| 32 | + } |
| 33 | + |
| 34 | + // load the author avatar |
| 35 | + const avatarData = await fs.readFile(path.join(process.cwd(), "app", "me.jpg")); |
| 36 | + const avatarSrc = Uint8Array.from(avatarData).buffer; |
| 37 | + |
| 38 | + // load the Geist font from its npm package |
| 39 | + const geistFont = await fs.readFile( |
| 40 | + path.join(process.cwd(), "node_modules/geist/dist/fonts/geist-sans/Geist-SemiBold.ttf") |
| 41 | + ); |
| 42 | + |
| 43 | + // load the note's front matter |
| 44 | + const { title } = await getFrontMatter(slug); |
| 45 | + |
| 46 | + return new ImageResponse( |
| 47 | + ( |
| 48 | + <div |
| 49 | + style={{ |
| 50 | + display: "flex", |
| 51 | + height: "100%", |
| 52 | + width: "100%", |
| 53 | + flexDirection: "column", |
| 54 | + fontFamily: "Geist", |
| 55 | + fontSize: 20, |
| 56 | + fontWeight: 600, |
| 57 | + letterSpacing: -0.5, |
| 58 | + background: "linear-gradient(0deg, hsla(197, 14%, 57%, 1) 0%, hsla(192, 17%, 94%, 1) 100%)", |
| 59 | + }} |
| 60 | + > |
| 61 | + {imageSrc && ( |
| 62 | + <div |
| 63 | + style={{ |
| 64 | + display: "flex", |
| 65 | + alignItems: "center", |
| 66 | + justifyContent: "center", |
| 67 | + width: "100%", |
| 68 | + height: "100%", |
| 69 | + }} |
| 70 | + > |
| 71 | + <img |
| 72 | + // @ts-expect-error |
| 73 | + src={imageSrc} |
| 74 | + style={{ objectFit: "cover", width: "100%", height: "100%" }} |
| 75 | + /> |
| 76 | + </div> |
| 77 | + )} |
| 78 | + <div |
| 79 | + style={{ |
| 80 | + display: "flex", |
| 81 | + alignItems: "center", |
| 82 | + position: "absolute", |
| 83 | + left: 42, |
| 84 | + top: 42, |
| 85 | + }} |
| 86 | + > |
| 87 | + <img |
| 88 | + // @ts-expect-error |
| 89 | + src={avatarSrc} |
| 90 | + style={{ width: 64, height: 64, borderRadius: "100%" }} |
| 91 | + /> |
| 92 | + </div> |
| 93 | + <div |
| 94 | + style={{ |
| 95 | + display: "flex", |
| 96 | + flexWrap: "wrap", |
| 97 | + position: "absolute", |
| 98 | + left: 0, |
| 99 | + bottom: 42, |
| 100 | + padding: "12px 20px", |
| 101 | + margin: "0 42px", |
| 102 | + width: "auto", |
| 103 | + maxWidth: "100%", |
| 104 | + backgroundColor: "rgba(16, 16, 16, 0.85)", |
| 105 | + fontSize: 40, |
| 106 | + lineHeight: 1.4, |
| 107 | + color: "#fefefe", |
| 108 | + }} |
| 109 | + > |
| 110 | + {title} |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + ), |
| 114 | + { |
| 115 | + ...size, |
| 116 | + fonts: [ |
| 117 | + { |
| 118 | + name: "Geist", |
| 119 | + data: geistFont, |
| 120 | + style: "normal", |
| 121 | + weight: 700, |
| 122 | + }, |
| 123 | + ], |
| 124 | + } |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export default Image; |
0 commit comments