From 93e589659df58bcf17cfc00ae4ff3b1479d244dc Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Tue, 30 Jan 2024 04:54:01 -0800 Subject: [PATCH] some code cleanup now that its working --- packages/astro-ghostcms/index.ts | 60 +++++++++++-------- packages/astro-ghostcms/package.json | 5 +- .../default-routes/open-graph/[slug].png.ts | 2 +- .../open-graph/author/[slug].png.ts | 4 +- .../default-routes/open-graph/authors.png.ts | 2 +- .../default-routes/open-graph/index.png.ts | 2 +- .../open-graph/tag/[slug].png.ts | 2 +- .../src/default-routes/open-graph/tags.png.ts | 2 +- .../astro-ghostcms/src/integrations/satori.ts | 29 +++------ .../astro-ghostcms/src/schemas/userconfig.ts | 4 ++ 10 files changed, 56 insertions(+), 56 deletions(-) diff --git a/packages/astro-ghostcms/index.ts b/packages/astro-ghostcms/index.ts index 070791ff..7d5cdbf0 100644 --- a/packages/astro-ghostcms/index.ts +++ b/packages/astro-ghostcms/index.ts @@ -53,7 +53,11 @@ const IC = { /** INTERNAL STRING */ id404: "404 Injection Disabled", /** INTERNAL STRING */ - idRSS: "RSS Injection Disabled" + idRSS: "RSS Injection Disabled", + /** INTERNAL STRING */ + satori_e: "Injecting Satori-OpenGraph Generator", + /** INTERNAL STRING */ + satori_d: "Satori Injection disabled" } /** CONTENT API ENVIRONMENT VARIABLES */ @@ -91,7 +95,8 @@ export default function GhostCMS(options: UserConfig): AstroIntegration { RTXT: GhostConfig.robotstxt, gSite: GhostConfig.ghostURL, dRSS: GhostConfig.disableRSS, - d404: GhostConfig.disable404 + d404: GhostConfig.disable404, + dOG: GhostConfig.disableSatoriOG } // Check For ENV Variables @@ -141,30 +146,33 @@ export default function GhostCMS(options: UserConfig): AstroIntegration { }); } else { if( !GCD.dCO ) { logger.info(IC.idRSS)}} - injectRoute({ - pattern: '/open-graph/[slug].png', - entrypoint: `${IC.PKG}/open-graph/[slug].png.ts` - }); - injectRoute({ - pattern: '/open-graph/index.png', - entrypoint: `${IC.PKG}/open-graph/index.png.ts` - }); - injectRoute({ - pattern: '/open-graph/authors.png', - entrypoint: `${IC.PKG}/open-graph/authors.png.ts` - }); - injectRoute({ - pattern: '/open-graph/author/[slug].png', - entrypoint: `${IC.PKG}/open-graph/author/[slug].png.ts` - }); - injectRoute({ - pattern: '/open-graph/tags.png', - entrypoint: `${IC.PKG}/open-graph/tags.png.ts` - }); - injectRoute({ - pattern: '/open-graph/tag/[slug].png', - entrypoint: `${IC.PKG}/open-graph/tag/[slug].png.ts` - }); + if ( !GCD.dOG ) { + if( !GCD.dCO ) { logger.info( IC.satori_e )} + injectRoute({ + pattern: '/open-graph/[slug].png', + entrypoint: `${IC.PKG}/open-graph/[slug].png.ts` + }); + injectRoute({ + pattern: '/open-graph/index.png', + entrypoint: `${IC.PKG}/open-graph/index.png.ts` + }); + injectRoute({ + pattern: '/open-graph/authors.png', + entrypoint: `${IC.PKG}/open-graph/authors.png.ts` + }); + injectRoute({ + pattern: '/open-graph/author/[slug].png', + entrypoint: `${IC.PKG}/open-graph/author/[slug].png.ts` + }); + injectRoute({ + pattern: '/open-graph/tags.png', + entrypoint: `${IC.PKG}/open-graph/tags.png.ts` + }); + injectRoute({ + pattern: '/open-graph/tag/[slug].png', + entrypoint: `${IC.PKG}/open-graph/tag/[slug].png.ts` + }); + } else { if( !GCD.dCO ) { logger.info( IC.satori_d )}} // THEME ROUTES if( !GCD.dCO ) { logger.info( IC.ITR )} diff --git a/packages/astro-ghostcms/package.json b/packages/astro-ghostcms/package.json index fe64582b..d6386426 100644 --- a/packages/astro-ghostcms/package.json +++ b/packages/astro-ghostcms/package.json @@ -54,11 +54,12 @@ "./rss.xml.ts": "./src/default-routes/rss.xml.ts", "./config": "./src/integrations/virtual-config.ts", "./types": "./types.ts", - "./open-graph/[slug].png.ts": "./src/default-routes/open-graph/[slug].png.ts", + "./satoriOG": "./src/integrations/satori.ts", "./open-graph/index.png.ts": "./src/default-routes/open-graph/index.png.ts", "./open-graph/authors.png.ts": "./src/default-routes/open-graph/authors.png.ts", - "./open-graph/author/[slug].png.ts": "./src/default-routes/open-graph/author/[slug].png.ts", "./open-graph/tags.png.ts": "./src/default-routes/open-graph/tags.png.ts", + "./open-graph/[slug].png.ts": "./src/default-routes/open-graph/[slug].png.ts", + "./open-graph/author/[slug].png.ts": "./src/default-routes/open-graph/author/[slug].png.ts", "./open-graph/tag/[slug].png.ts": "./src/default-routes/open-graph/tag/[slug].png.ts" }, "scripts": { diff --git a/packages/astro-ghostcms/src/default-routes/open-graph/[slug].png.ts b/packages/astro-ghostcms/src/default-routes/open-graph/[slug].png.ts index 39bc29f0..66654edb 100644 --- a/packages/astro-ghostcms/src/default-routes/open-graph/[slug].png.ts +++ b/packages/astro-ghostcms/src/default-routes/open-graph/[slug].png.ts @@ -1,5 +1,5 @@ import type { APIRoute, GetStaticPaths, GetStaticPathsItem, InferGetStaticPropsType } from "astro"; -import { satoriOG } from "../../integrations/satori.js"; +import satoriOG from "../../integrations/satori.js"; import { html } from "satori-html"; import { invariant, getAllPosts, getAllPages, getSettings } from "../../api/index.js"; diff --git a/packages/astro-ghostcms/src/default-routes/open-graph/author/[slug].png.ts b/packages/astro-ghostcms/src/default-routes/open-graph/author/[slug].png.ts index ed79a8df..6e03cb68 100644 --- a/packages/astro-ghostcms/src/default-routes/open-graph/author/[slug].png.ts +++ b/packages/astro-ghostcms/src/default-routes/open-graph/author/[slug].png.ts @@ -1,5 +1,5 @@ -import type { APIRoute, GetStaticPaths, GetStaticPathsItem, InferGetStaticParamsType, InferGetStaticPropsType } from "astro"; -import { satoriOG } from "../../../integrations/satori.js"; +import type { APIRoute, GetStaticPaths, GetStaticPathsItem, InferGetStaticPropsType } from "astro"; +import satoriOG from "../../../integrations/satori.js"; import { html } from "satori-html"; import { invariant, getAllPosts, getSettings, getAllAuthors } from "../../../api/index.js"; diff --git a/packages/astro-ghostcms/src/default-routes/open-graph/authors.png.ts b/packages/astro-ghostcms/src/default-routes/open-graph/authors.png.ts index 65438463..735f16cf 100644 --- a/packages/astro-ghostcms/src/default-routes/open-graph/authors.png.ts +++ b/packages/astro-ghostcms/src/default-routes/open-graph/authors.png.ts @@ -1,5 +1,5 @@ import type { APIRoute, GetStaticPaths, GetStaticPathsItem, InferGetStaticPropsType } from "astro"; -import { satoriOG } from "../../integrations/satori.js"; +import satoriOG from "../../integrations/satori.js"; import { html } from "satori-html"; import { invariant, getAllPosts, getAllPages, getSettings } from "../../api/index.js"; diff --git a/packages/astro-ghostcms/src/default-routes/open-graph/index.png.ts b/packages/astro-ghostcms/src/default-routes/open-graph/index.png.ts index 54091b24..3b10a5c9 100644 --- a/packages/astro-ghostcms/src/default-routes/open-graph/index.png.ts +++ b/packages/astro-ghostcms/src/default-routes/open-graph/index.png.ts @@ -1,5 +1,5 @@ import type { APIRoute, GetStaticPaths, GetStaticPathsItem, InferGetStaticPropsType } from "astro"; -import { satoriOG } from "../../integrations/satori.js"; +import satoriOG from "../../integrations/satori.js"; import { html } from "satori-html"; import { invariant, getAllPosts, getAllPages, getSettings } from "../../api/index.js"; diff --git a/packages/astro-ghostcms/src/default-routes/open-graph/tag/[slug].png.ts b/packages/astro-ghostcms/src/default-routes/open-graph/tag/[slug].png.ts index a1f7f2aa..429f7fa2 100644 --- a/packages/astro-ghostcms/src/default-routes/open-graph/tag/[slug].png.ts +++ b/packages/astro-ghostcms/src/default-routes/open-graph/tag/[slug].png.ts @@ -1,5 +1,5 @@ import type { APIRoute, GetStaticPaths, GetStaticPathsItem, InferGetStaticPropsType } from "astro"; -import { satoriOG } from "../../../integrations/satori.js"; +import satoriOG from "../../../integrations/satori.js"; import { html } from "satori-html"; import { invariant, getAllPosts, getSettings, getAllTags } from "../../../api/index.js"; diff --git a/packages/astro-ghostcms/src/default-routes/open-graph/tags.png.ts b/packages/astro-ghostcms/src/default-routes/open-graph/tags.png.ts index efe43795..ca298727 100644 --- a/packages/astro-ghostcms/src/default-routes/open-graph/tags.png.ts +++ b/packages/astro-ghostcms/src/default-routes/open-graph/tags.png.ts @@ -1,5 +1,5 @@ import type { APIRoute, GetStaticPaths, GetStaticPathsItem, InferGetStaticPropsType } from "astro"; -import { satoriOG } from "../../integrations/satori.js"; +import satoriOG from "../../integrations/satori.js"; import { html } from "satori-html"; import { invariant, getAllPosts, getAllPages, getSettings } from "../../api/index.js"; diff --git a/packages/astro-ghostcms/src/integrations/satori.ts b/packages/astro-ghostcms/src/integrations/satori.ts index 77579a26..8da00ed3 100644 --- a/packages/astro-ghostcms/src/integrations/satori.ts +++ b/packages/astro-ghostcms/src/integrations/satori.ts @@ -1,25 +1,13 @@ import { Resvg } from "@resvg/resvg-js"; import satori from "satori"; -import type { - SatoriAstroOGOptions, - ToSvgOptions, - ToImageOptions, - ToResponseOptions, -} from "../../types.js"; +import type { SatoriAstroOGOptions, ToSvgOptions, ToImageOptions, ToResponseOptions } from "../../types.js"; -export const satoriOG = ({ - width, - height, - template, -}: SatoriAstroOGOptions) => { +const satoriOG = ({ width, height, template }: SatoriAstroOGOptions) => { return { async toSvg(options: ToSvgOptions) { return await satori(template, { width, height, ...options }); }, - async toImage({ - satori: satoriOptions, - resvg: _resvgOptions, - }: ToImageOptions) { + async toImage({ satori: satoriOptions, resvg: _resvgOptions }: ToImageOptions) { const resvgOptions = typeof _resvgOptions === "function" ? _resvgOptions({ width, height }) @@ -28,15 +16,12 @@ export const satoriOG = ({ return new Resvg(await this.toSvg(satoriOptions), { fitTo: { mode: "width", value: width }, ...resvgOptions, - }) - .render() - .asPng(); + }).render().asPng(); }, async toResponse({ response: init, ...rest }: ToResponseOptions) { const image = await this.toImage(rest); - return new Response(image, { - ...init, + return new Response(image, {...init, headers: { "Content-Type": "image/png", "Content-Length": image.length.toString(), @@ -46,4 +31,6 @@ export const satoriOG = ({ }); }, }; -}; \ No newline at end of file +}; + +export default satoriOG; \ No newline at end of file diff --git a/packages/astro-ghostcms/src/schemas/userconfig.ts b/packages/astro-ghostcms/src/schemas/userconfig.ts index 7d184434..a23bdb3e 100644 --- a/packages/astro-ghostcms/src/schemas/userconfig.ts +++ b/packages/astro-ghostcms/src/schemas/userconfig.ts @@ -15,6 +15,10 @@ export const UserConfigSchema = z.object({ ghostURL: z.string().url().optional(), /** OPTIONAL - Allows the user to disable the `/rss.xml` injection */ disableRSS: z.boolean().default(false), + /** OPTIONAL - Allows the user to disable the `/open-graph/*` route injection + * @ This feature uses `satori` to generate OpenGraph Images + */ + disableSatoriOG: z.boolean().default(false), /** OPTIONAL - Allows the user to disable the `/404` injection */ disable404: z.boolean().default(false), /** OPTIONAL - Disable Route Injector