From 44b38555e51643c8b52d5642037e9c3a33758b36 Mon Sep 17 00:00:00 2001 From: Sohum Mendon Date: Sun, 13 Oct 2024 18:01:18 -0700 Subject: [PATCH] feat(head): support opengraph customizations This commit adds the ability to modify attributes of the default OpenGraph image by changes to the quartz.layout.ts file. Attributes include the MIME type, dimensions, image path, and alt text. An example of how this would be configured is present in the existing layout file. --- quartz.layout.ts | 10 +++++++++- quartz/components/Head.tsx | 23 ++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/quartz.layout.ts b/quartz.layout.ts index 81203ec2d..d2a85dc82 100644 --- a/quartz.layout.ts +++ b/quartz.layout.ts @@ -6,7 +6,15 @@ export const sharedPageComponents: SharedLayout = { head: Component.Head({ favicons: [ { path: "static/icon.png", size: "200x200", mime: "image/png" } - ] + ], + + openGraph: { + path: "static/og-image.png", + mime: "image/png", + width: "1200", + height: "675", + alt: "Quartz logo" + } }), header: [], afterBody: [], diff --git a/quartz/components/Head.tsx b/quartz/components/Head.tsx index aa0194fe9..2c7414fd1 100644 --- a/quartz/components/Head.tsx +++ b/quartz/components/Head.tsx @@ -10,8 +10,17 @@ interface Favicon { mime?: string } +interface OpenGraphImage { + path: string + alt?: string + mime?: string + width?: string + height?: string +} + interface Options { favicons?: Favicon[] + openGraph?: OpenGraphImage } export default ((opts?: Options) => { @@ -27,8 +36,6 @@ export default ((opts?: Options) => { const path = url.pathname as FullSlug const baseDir = fileData.slug === "404" ? path : pathToRoot(fileData.slug!) - const ogImagePath = `https://${cfg.baseUrl}/static/og-image.png` - const icons = opts?.favicons ?? [] return ( @@ -45,9 +52,15 @@ export default ((opts?: Options) => { - {cfg.baseUrl && } - - + {cfg.baseUrl && opts?.openGraph && ( + <> + + {opts.openGraph.mime && ()} + {opts.openGraph.width && ()} + {opts.openGraph.height && ()} + {opts.openGraph.alt && ()} + + )} {icons.map(({ path, size, mime }) => ( ))}