Skip to content

Commit

Permalink
feat: add custom property preset
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherdjerred committed Aug 4, 2024
1 parent 6836154 commit 9ebff27
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ export default defineConfig({
![](assets/presets/brandedLogo.png)
### `customProperty`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";
export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.customProperty,
}),
],
});
```
![](assets/presets/customProperty.png)
### `gradients`
```diff
Expand Down
Binary file added assets/presets/customProperty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion example/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default defineConfig({
},
],
},
render: presets.blackAndWhite,
render: presets.customProperty,
}),
],
});
2 changes: 1 addition & 1 deletion src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function handlePage({ page, options, render, dir, logger }: HandlePageInpu
const html = fs.readFileSync(htmlFile).toString();
const pageDetails = extract(html);

const reactNode = await render({ ...page, ...pageDetails });
const reactNode = await render({ ...page, ...pageDetails, dir: dir });
const svg = await satori(reactNode, options);
const resvg = new Resvg(svg, {
font: {
Expand Down
32 changes: 32 additions & 0 deletions src/presets/customProperty.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { fileURLToPath } from "url";
import { sanitizeHtml } from "../extract.js";
import type { RenderFunctionInput } from "../types.js";
import { getFilePath } from "../util.js";
import * as jsdom from "jsdom";
import * as fs from "fs";

// This preset demonstrates how to extract arbitrary content from an HTML file
// and render it in an opengraph image.
export async function customProperty({ title, pathname, dir }: RenderFunctionInput): Promise<React.ReactNode> {
const htmlFile = getFilePath({ dir: fileURLToPath(dir), page: pathname });
const html = fs.readFileSync(htmlFile).toString();
const htmlDoc = new jsdom.JSDOM(sanitizeHtml(html)).window.document;

// extract the body
const body = htmlDoc.querySelector("body")?.textContent ?? "";
// truncate the body to 50 characters, add ellipsis if truncated
const bodyTruncated = body.substring(0, 50) + (body.length > 50 ? "..." : "");

const twj = (await import("tw-to-css")).twj;

return (
<div style={twj("h-full w-full flex items-start justify-start border border-blue-500 border-[12px] bg-gray-50")}>
<div style={twj("flex items-start justify-start h-full")}>
<div style={twj("flex flex-col justify-between w-full h-full")}>
<h1 style={twj("text-[80px] p-20 font-black text-left")}>{title}</h1>
<div style={twj("text-2xl pb-10 px-20 font-bold mb-0")}>{bodyTruncated}</div>
</div>
</div>
</div>
);
}
2 changes: 2 additions & 0 deletions src/presets/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { backgroundImage } from "./backgroundImage.js";
import { blackAndWhite } from "./blackAndWhite.js";
import { brandedLogo } from "./brandedLogo.js";
import { customProperty } from "./customProperty.js";
import { gradients } from "./gradients.js";
import { podcast } from "./podcast.js";
import { rauchg } from "./rauchg.js";
Expand All @@ -20,4 +21,5 @@ export const presets = {
podcast,
simpleBlog,
waveSvg,
customProperty,
};
3 changes: 2 additions & 1 deletion src/presets/renderExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ async function renderExamples() {
url: "https://example.com/3d-graphics",
type: "article",
image: "https://example.com/3d-graphics.png",
pathname: "empty",
pathname: "dist/index/",
dir: new URL("../../example", import.meta.url),
};

const options: SatoriOptions = {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface AstroBuildDoneHookInput {
/** The input arguments to a `RenderFunction` */
export type RenderFunctionInput = {
pathname: string;
dir: URL;
} & PageDetails;

/** A function that renders some page input to React */
Expand Down

0 comments on commit 9ebff27

Please sign in to comment.