Skip to content

Commit

Permalink
feat: support for async template
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
JulianCataldo committed May 29, 2024
1 parent 4980e93 commit be04b8c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "og-images-generator",
"version": "0.1.1",
"version": "0.2.1",
"description": "Generate OG images from a static folder and / or a middleware.\nExtract metadata from HTML pages. No headless browser involved.\nComes as a CLI, API or plugins.",
"keywords": [
"og-images",
Expand Down
2 changes: 1 addition & 1 deletion src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function renderOgImage(userConfig, page) {
const pageOrDefaults = page || { path: '', meta: { tags: {}, jsonLds: [] } };
const templateOptions = { page: pageOrDefaults };

const template = userConfig.template(templateOptions);
const template = await Promise.resolve(userConfig.template(templateOptions));

/**
* `decodeHTML` is a hack because for now, we can't use `unsafeHTML` with
Expand Down
40 changes: 40 additions & 0 deletions test/__fixtures__/og-images-async.config.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { html, styled, OG_SIZE, FONTS } from 'og-images-generator';

/** @type {import('og-images-generator').PathsOptions} (Optional) */
export const paths = {
// DEFAULTS:
base: './dist',
out: './dist/og',
json: './dist/og/index.json',
};

const myInlineStyle1 = styled.div`
display: flex;
`;

const nestedTemplate1 = html`<span>My Website</span>`;

async function iAmABlocker() {
const stuff = await new Promise((resolve) =>
setTimeout(() => resolve('ok then'), 10),
);

return stuff;
}

/** @type {import('og-images-generator').Template} */
export const template = async ({ page }) =>
html` <!-- Contrived example -->
<div style=${myInlineStyle1}>
${page.meta?.tags?.['og:title'] ?? 'Untitled'} <br />
${page.meta?.tags?.['og:description'] ?? 'No description'}
<!-- -->
${nestedTemplate1}
<em>Nice</em>
<strong>Weather, ${await iAmABlocker()}</strong>
</div>`;

/** @type {import('og-images-generator').RenderOptions} */
export const renderOptions = {
satori: { fonts: [await FONTS.sourceSans()], ...OG_SIZE },
};
23 changes: 23 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import assert from 'node:assert';

import * as api from '../src/api.js';
import { hash } from './_utils.js';
// import { writeFile } from 'node:fs/promises';

const options = {
base: process.cwd() + '/test/__fixtures__/pages',
Expand Down Expand Up @@ -47,3 +48,25 @@ test('Generate single image', async (t) => {

assert.equal(hash(image), 'ad6a8e499e03a59a1c29e490e7d3f424');
});

test('Generate single image with async. template', async (t) => {
const config = await api.loadUserConfig(
process.cwd() + '/test/__fixtures__/og-images-async.config.fixture.js',
);

const image = await api.renderOgImage(config, {
path: '/nested/page',
meta: {
tags: { 'og:title': 'hello', 'og:description': 'there' },
jsonLds: [],
},
});

// NOTE: For quick visual tests
// await writeFile(
// process.cwd() + '/test/__fixtures__/.test-outputs/img-test.png',
// image,
// );

assert.equal(hash(image), '62b3609ee5fd7ed967ae24a6682ccdbe');
});

0 comments on commit be04b8c

Please sign in to comment.