From dbfd1a4280bb477e004310037e60b8deca492efb Mon Sep 17 00:00:00 2001 From: Ben Schlegel Date: Thu, 7 Mar 2024 19:31:54 +0100 Subject: [PATCH] feat: pass `fileData` to image generator --- quartz/components/Head.tsx | 5 +++-- quartz/util/imageHelper.ts | 9 ++++++++- quartz/util/socialImage.tsx | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/quartz/components/Head.tsx b/quartz/components/Head.tsx index 6c87d6c07..0372b8b35 100644 --- a/quartz/components/Head.tsx +++ b/quartz/components/Head.tsx @@ -14,13 +14,13 @@ import { unescapeHTML } from "../util/escape" * @param opts options for generating image */ async function generateSocialImage( - { cfg, description, fileName, fontsPromise, title }: ImageOptions, + { cfg, description, fileName, fontsPromise, title, fileData }: ImageOptions, userOpts: SocialImageOptions, ) { const fonts = await fontsPromise // JSX that will be used to generate satori svg - const imageElement = userOpts.imageStructure(cfg, userOpts, title, description, fonts) + const imageElement = userOpts.imageStructure(cfg, userOpts, title, description, fonts, fileData) const svg = await satori(imageElement, { width: userOpts.width, @@ -101,6 +101,7 @@ export default (() => { fileExt: extension, fontsPromise, cfg, + fileData }, fullOptions, ) diff --git a/quartz/util/imageHelper.ts b/quartz/util/imageHelper.ts index 0f8f15bf4..1f437010b 100644 --- a/quartz/util/imageHelper.ts +++ b/quartz/util/imageHelper.ts @@ -1,6 +1,7 @@ -import satori, { FontWeight, SatoriOptions } from "satori/wasm" +import { FontWeight, SatoriOptions } from "satori/wasm" import { GlobalConfiguration } from "../cfg" import { JSXInternal } from "preact/src/jsx" +import { QuartzPluginData } from "../plugins/vfile" /** * Get an array of `FontOptions` (for satori) given google font names @@ -70,6 +71,7 @@ export type SocialImageOptions = { * @param title title of current page * @param description description of current page * @param fonts global font that can be used for styling + * @param fileData full fileData of current page * @returns prepared jsx to be used for generating image */ imageStructure: ( @@ -78,6 +80,7 @@ export type SocialImageOptions = { title: string, description: string, fonts: SatoriOptions["fonts"], + fileData: QuartzPluginData ) => JSXInternal.Element } @@ -112,4 +115,8 @@ export type ImageOptions = { * `GlobalConfiguration` of quartz (used for theme/typography) */ cfg: GlobalConfiguration + /** + * full file data of current page + */ + fileData: QuartzPluginData } diff --git a/quartz/util/socialImage.tsx b/quartz/util/socialImage.tsx index 2eede2c99..213945433 100644 --- a/quartz/util/socialImage.tsx +++ b/quartz/util/socialImage.tsx @@ -1,6 +1,7 @@ import { SatoriOptions } from "satori/wasm" import { GlobalConfiguration } from "../cfg" import { SocialImageOptions, UserOpts } from "./imageHelper" +import { QuartzPluginData } from "../plugins/vfile" export const defaultImage: SocialImageOptions["imageStructure"] = ( cfg: GlobalConfiguration, @@ -8,6 +9,7 @@ export const defaultImage: SocialImageOptions["imageStructure"] = ( title: string, description: string, fonts: SatoriOptions["fonts"], + fileData: QuartzPluginData ) => { // How many characters are allowed before switching to smaller font const fontBreakPoint = 22