@ogify/core 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +350 -0
- package/dist/index.d.mts +400 -0
- package/dist/index.d.ts +400 -0
- package/dist/index.js +492 -0
- package/dist/index.mjs +481 -0
- package/package.json +39 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import { FontWeight, FontStyle } from 'satori';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type definitions for the OGify core library.
|
|
5
|
+
*
|
|
6
|
+
* This module provides TypeScript types and interfaces for:
|
|
7
|
+
* - Font configuration and loading
|
|
8
|
+
* - Template definition and parameters
|
|
9
|
+
* - Template handler configuration
|
|
10
|
+
* - Emoji providers
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Supported emoji providers for rendering emoji characters in OG images.
|
|
15
|
+
*
|
|
16
|
+
* Each provider offers a different visual style:
|
|
17
|
+
* - `twemoji`: Twitter's emoji set (colorful, rounded)
|
|
18
|
+
* - `fluent`: Microsoft's Fluent emoji (3D style with color)
|
|
19
|
+
* - `fluentFlat`: Microsoft's Fluent emoji (flat 2D style)
|
|
20
|
+
* - `noto`: Google's Noto Color Emoji (current standard)
|
|
21
|
+
* - `blobmoji`: Google's blob-style emoji (deprecated but still available)
|
|
22
|
+
* - `openmoji`: Open-source emoji with outlined style
|
|
23
|
+
*/
|
|
24
|
+
type OgEmojiProvider = 'twemoji' | 'fluent' | 'fluentFlat' | 'noto' | 'blobmoji' | 'openmoji';
|
|
25
|
+
/**
|
|
26
|
+
* Supported font file formats.
|
|
27
|
+
*
|
|
28
|
+
* - `woff`: Web Open Font Format (modern, compressed)
|
|
29
|
+
* - `ttf`: TrueType Font (legacy, larger file size)
|
|
30
|
+
*/
|
|
31
|
+
type OgFontFormat = 'woff' | 'ttf';
|
|
32
|
+
/**
|
|
33
|
+
* Configuration for a font used in OG image templates.
|
|
34
|
+
*
|
|
35
|
+
* Fonts can be loaded from three sources (in priority order):
|
|
36
|
+
* 1. Pre-loaded binary data (via `data` property)
|
|
37
|
+
* 2. Remote URL (via `url` property)
|
|
38
|
+
* 3. Google Fonts API (automatic detection based on `name`)
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* // Load from Google Fonts (automatic)
|
|
42
|
+
* const font1: OgFontConfig = {
|
|
43
|
+
* name: 'Inter',
|
|
44
|
+
* weight: 400,
|
|
45
|
+
* style: 'normal'
|
|
46
|
+
* };
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* // Load from custom URL
|
|
50
|
+
* const font2: OgFontConfig = {
|
|
51
|
+
* name: 'CustomFont',
|
|
52
|
+
* url: 'https://example.com/font.woff2',
|
|
53
|
+
* weight: 700
|
|
54
|
+
* };
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Load from pre-loaded data
|
|
58
|
+
* const font3: OgFontConfig = {
|
|
59
|
+
* name: 'MyFont',
|
|
60
|
+
* data: fontBuffer,
|
|
61
|
+
* weight: 400
|
|
62
|
+
* };
|
|
63
|
+
*/
|
|
64
|
+
type OgFontConfig = {
|
|
65
|
+
/** The font family name (e.g., 'Inter', 'Roboto', 'Merriweather') */
|
|
66
|
+
name: string;
|
|
67
|
+
/** Font weight (100-900, default: 400) */
|
|
68
|
+
weight?: FontWeight;
|
|
69
|
+
/** Font style ('normal' or 'italic', default: 'normal') */
|
|
70
|
+
style?: FontStyle;
|
|
71
|
+
/** URL to the font file (for custom/self-hosted fonts) */
|
|
72
|
+
url?: string;
|
|
73
|
+
/** Pre-loaded font binary data (ArrayBuffer or Buffer) */
|
|
74
|
+
data?: Buffer | ArrayBuffer;
|
|
75
|
+
/** Font file format (default: 'woff') */
|
|
76
|
+
format?: OgFontFormat;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Props passed to template HTML rendering functions.
|
|
80
|
+
*
|
|
81
|
+
* Contains user-provided parameters and optional dimension overrides.
|
|
82
|
+
* Templates use these props to generate dynamic HTML content.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* const html = (props: OgTemplateOptions) => `
|
|
86
|
+
* <div style="width: ${props.width}px; height: ${props.height}px">
|
|
87
|
+
* <h1>${props.params.title}</h1>
|
|
88
|
+
* <p>${props.params.description}</p>
|
|
89
|
+
* </div>
|
|
90
|
+
* `;
|
|
91
|
+
*/
|
|
92
|
+
type OgTemplateOptions = {
|
|
93
|
+
/** User-provided parameters that populate the template (e.g., title, description, author) */
|
|
94
|
+
params: OgTemplateParams;
|
|
95
|
+
/** Optional custom width in pixels (default: 1200) */
|
|
96
|
+
width?: number;
|
|
97
|
+
/** Optional custom height in pixels (default: 630) */
|
|
98
|
+
height?: number;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Key-value pairs representing dynamic template parameters.
|
|
102
|
+
*
|
|
103
|
+
* Parameters are the data that gets injected into templates to create
|
|
104
|
+
* personalized OG images. Values can be:
|
|
105
|
+
* - Strings: Text content (titles, descriptions, names)
|
|
106
|
+
* - Numbers: Counts, dates, metrics
|
|
107
|
+
* - Booleans: Flags, toggles, states
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* const params: OgTemplateParams = {
|
|
111
|
+
* title: 'My Blog Post',
|
|
112
|
+
* author: 'John Doe',
|
|
113
|
+
* views: 1234,
|
|
114
|
+
* published: true
|
|
115
|
+
* };
|
|
116
|
+
*/
|
|
117
|
+
type OgTemplateParams = Record<string, string | number | boolean>;
|
|
118
|
+
/**
|
|
119
|
+
* Complete definition of an Open Graph image template.
|
|
120
|
+
*
|
|
121
|
+
* A template is a reusable blueprint for generating OG images.
|
|
122
|
+
* It defines:
|
|
123
|
+
* - How to render HTML from parameters
|
|
124
|
+
* - What fonts to use
|
|
125
|
+
* - Metadata for identification
|
|
126
|
+
*/
|
|
127
|
+
type OgTemplate = {
|
|
128
|
+
/** Unique identifier for this template (e.g., 'blog-post', 'product-card') */
|
|
129
|
+
id: string;
|
|
130
|
+
/** Human-readable name for display purposes */
|
|
131
|
+
name: string;
|
|
132
|
+
/** Brief description of what this template is used for */
|
|
133
|
+
description: string;
|
|
134
|
+
/**
|
|
135
|
+
* Function that generates HTML markup from template parameters.
|
|
136
|
+
*
|
|
137
|
+
* The HTML should use inline styles (Tailwind-like utilities are supported).
|
|
138
|
+
* Flexbox and basic CSS properties are supported by Satori.
|
|
139
|
+
*/
|
|
140
|
+
renderer: (props: OgTemplateOptions) => string;
|
|
141
|
+
/**
|
|
142
|
+
* Custom fonts to use in this template.
|
|
143
|
+
*
|
|
144
|
+
* Fonts are loaded in the order specified and should cover
|
|
145
|
+
* all characters used in the template.
|
|
146
|
+
*/
|
|
147
|
+
fonts: OgFontConfig[];
|
|
148
|
+
/**
|
|
149
|
+
* Optional emoji provider to use in this template.
|
|
150
|
+
*
|
|
151
|
+
* If not specified, defaults to 'noto'.
|
|
152
|
+
*/
|
|
153
|
+
emojiProvider?: OgEmojiProvider;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Configuration for the TemplateRenderer class.
|
|
157
|
+
*
|
|
158
|
+
* Defines available templates, global defaults, and lifecycle hooks.
|
|
159
|
+
* The handler manages a registry of templates and provides a unified
|
|
160
|
+
* interface for rendering them to images.
|
|
161
|
+
*/
|
|
162
|
+
type OgTemplateRenderer = {
|
|
163
|
+
/** Array of template definitions to register */
|
|
164
|
+
templates: OgTemplate[];
|
|
165
|
+
/**
|
|
166
|
+
* Default parameter values applied to all templates.
|
|
167
|
+
*
|
|
168
|
+
* These values are merged with user-provided parameters,
|
|
169
|
+
* with user values taking precedence.
|
|
170
|
+
*/
|
|
171
|
+
defaultParams?: OgTemplateParams;
|
|
172
|
+
/**
|
|
173
|
+
* Hook called before rendering.
|
|
174
|
+
*
|
|
175
|
+
* Useful for:
|
|
176
|
+
* - Logging and analytics
|
|
177
|
+
* - Parameter validation
|
|
178
|
+
* - Authentication checks
|
|
179
|
+
* - Rate limiting
|
|
180
|
+
*/
|
|
181
|
+
beforeRender?: (templateId: string, params: OgTemplateParams) => void | Promise<void>;
|
|
182
|
+
/**
|
|
183
|
+
* Hook called after rendering.
|
|
184
|
+
*
|
|
185
|
+
* Useful for:
|
|
186
|
+
* - Caching generated images
|
|
187
|
+
* - Cleanup operations
|
|
188
|
+
* - Sending notifications
|
|
189
|
+
* - Updating metrics
|
|
190
|
+
*/
|
|
191
|
+
afterRender?: (templateId: string, params: OgTemplateParams, imageBuffer: Buffer) => void | Promise<void>;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Template management and validation utilities.
|
|
196
|
+
*
|
|
197
|
+
* This module provides:
|
|
198
|
+
* - Template definition and validation
|
|
199
|
+
* - Template registry and lookup
|
|
200
|
+
* - Template rendering orchestration
|
|
201
|
+
*
|
|
202
|
+
* Templates are the core building blocks of the OGify library.
|
|
203
|
+
* They define how to convert parameters into OG images.
|
|
204
|
+
*/
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Validates that a template configuration has all required fields.
|
|
208
|
+
*
|
|
209
|
+
* This function performs structural validation to ensure the template
|
|
210
|
+
* has all necessary properties. It does NOT validate:
|
|
211
|
+
* - Renderer function output
|
|
212
|
+
* - Font availability
|
|
213
|
+
*
|
|
214
|
+
* Those validations happen at render time.
|
|
215
|
+
*
|
|
216
|
+
* @param config - The template configuration to validate
|
|
217
|
+
* @returns true if validation passes
|
|
218
|
+
* @throws Error if any required field is missing or invalid
|
|
219
|
+
*/
|
|
220
|
+
declare function validateTemplate(config: OgTemplate): boolean;
|
|
221
|
+
/**
|
|
222
|
+
* Defines and validates a new OG template.
|
|
223
|
+
*
|
|
224
|
+
* This is the primary way to create templates for use with the TemplateRenderer.
|
|
225
|
+
* It validates the template configuration and returns it for registration.
|
|
226
|
+
*
|
|
227
|
+
* **Benefits of using defineTemplate:**
|
|
228
|
+
* - Early validation catches configuration errors
|
|
229
|
+
* - Type safety ensures correct template structure
|
|
230
|
+
* - Clear intent in code (self-documenting)
|
|
231
|
+
*
|
|
232
|
+
* @param config - Complete template configuration including id, name, and renderer function
|
|
233
|
+
* @returns The validated template configuration
|
|
234
|
+
* @throws Error if validation fails
|
|
235
|
+
*/
|
|
236
|
+
declare function defineTemplate(config: OgTemplate): OgTemplate;
|
|
237
|
+
/**
|
|
238
|
+
* Template Handler class for managing multiple templates and rendering them to images.
|
|
239
|
+
*/
|
|
240
|
+
declare class TemplateRenderer {
|
|
241
|
+
/** Configuration including global settings and lifecycle hooks */
|
|
242
|
+
private config;
|
|
243
|
+
/**
|
|
244
|
+
* Internal registry mapping template IDs to template definitions.
|
|
245
|
+
*
|
|
246
|
+
* Using a Map provides:
|
|
247
|
+
* - O(1) lookup performance
|
|
248
|
+
* - Guaranteed insertion order
|
|
249
|
+
* - Better memory efficiency than objects
|
|
250
|
+
*/
|
|
251
|
+
private templates;
|
|
252
|
+
/**
|
|
253
|
+
* Creates a new TemplateRenderer instance.
|
|
254
|
+
*
|
|
255
|
+
* @param config - Handler configuration with templates and global settings
|
|
256
|
+
*/
|
|
257
|
+
constructor(config: OgTemplateRenderer);
|
|
258
|
+
/**
|
|
259
|
+
* Registers multiple templates into the internal registry.
|
|
260
|
+
*
|
|
261
|
+
* Templates are indexed by their ID for fast lookup.
|
|
262
|
+
* If a template with the same ID already exists, it will be overwritten.
|
|
263
|
+
*
|
|
264
|
+
* @param templates - Array of template definitions to register
|
|
265
|
+
*/
|
|
266
|
+
private registerTemplates;
|
|
267
|
+
/**
|
|
268
|
+
* Retrieves a template by its unique ID.
|
|
269
|
+
*
|
|
270
|
+
* This is useful for:
|
|
271
|
+
* - Checking if a template exists before rendering
|
|
272
|
+
* - Inspecting template configuration
|
|
273
|
+
* - Building template selection UIs
|
|
274
|
+
*
|
|
275
|
+
* @param id - The template ID to look up
|
|
276
|
+
* @returns The template definition, or undefined if not found
|
|
277
|
+
*/
|
|
278
|
+
getTemplate(id: string): OgTemplate | undefined;
|
|
279
|
+
/**
|
|
280
|
+
* Gets all registered template IDs.
|
|
281
|
+
*
|
|
282
|
+
* Useful for:
|
|
283
|
+
* - Building template selection dropdowns
|
|
284
|
+
* - Listing available templates in documentation
|
|
285
|
+
* - Debugging template registration
|
|
286
|
+
*
|
|
287
|
+
* @returns Array of template IDs
|
|
288
|
+
*/
|
|
289
|
+
getTemplateIds(): string[];
|
|
290
|
+
/**
|
|
291
|
+
* Renders a template to a PNG image buffer.
|
|
292
|
+
*
|
|
293
|
+
* This is the main method for generating OG images. It:
|
|
294
|
+
* 1. Looks up the template by ID
|
|
295
|
+
* 2. Merges default params with user params
|
|
296
|
+
* 3. Calls lifecycle hooks (if configured)
|
|
297
|
+
* 4. Delegates to the core rendering engine
|
|
298
|
+
*
|
|
299
|
+
* **Parameter Merging:**
|
|
300
|
+
* User-provided parameters take precedence over default parameters.
|
|
301
|
+
*
|
|
302
|
+
* **Custom Dimensions:**
|
|
303
|
+
* You can override the default 1200x630 dimensions by providing custom width/height.
|
|
304
|
+
* This is useful for platform-specific requirements (e.g., Twitter, Instagram).
|
|
305
|
+
*
|
|
306
|
+
* **Error Handling:**
|
|
307
|
+
* - Throws if template is not found
|
|
308
|
+
* - Throws if rendering fails (font loading, HTML generation, etc.)
|
|
309
|
+
*
|
|
310
|
+
* @param templateId - ID of the template to render
|
|
311
|
+
* @param params - Parameters to pass to the template
|
|
312
|
+
* @param options - Optional rendering options
|
|
313
|
+
* @param options.width - Custom image width in pixels (default: 1200)
|
|
314
|
+
* @param options.height - Custom image height in pixels (default: 630)
|
|
315
|
+
* @returns Promise resolving to a PNG image buffer
|
|
316
|
+
* @throws Error if template is not found or rendering fails
|
|
317
|
+
*/
|
|
318
|
+
renderToImage(templateId: string, params: OgTemplateParams, options?: {
|
|
319
|
+
width: number;
|
|
320
|
+
height: number;
|
|
321
|
+
}): Promise<Buffer>;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Factory function to create a new TemplateRenderer instance.
|
|
325
|
+
*
|
|
326
|
+
* This is a convenience wrapper around the TemplateRenderer constructor.
|
|
327
|
+
* It provides a more functional API style for those who prefer it.
|
|
328
|
+
*
|
|
329
|
+
* @param config - Handler configuration with templates and settings
|
|
330
|
+
* @returns A new TemplateRenderer instance
|
|
331
|
+
*/
|
|
332
|
+
declare function createTemplateRenderer(config: OgTemplateRenderer): TemplateRenderer;
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* OG image rendering engine.
|
|
336
|
+
*
|
|
337
|
+
* This module provides the core rendering pipeline that converts
|
|
338
|
+
* HTML templates into PNG images suitable for Open Graph meta tags.
|
|
339
|
+
*
|
|
340
|
+
* The rendering process uses:
|
|
341
|
+
* - Satori: Converts HTML/CSS to SVG
|
|
342
|
+
* - Resvg: Converts SVG to PNG with high quality
|
|
343
|
+
*/
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Renders an OG template to a PNG image buffer.
|
|
347
|
+
*
|
|
348
|
+
* **Rendering Pipeline:**
|
|
349
|
+
*
|
|
350
|
+
* 1. **Font Loading**: Load all fonts specified in the template
|
|
351
|
+
* 2. **HTML Generation**: Execute the template's renderer function with parameters
|
|
352
|
+
* 3. **HTML to Element Tree**: Convert HTML string to React-like element tree
|
|
353
|
+
* 4. **SVG Rendering**: Render element tree to SVG using Satori
|
|
354
|
+
* 5. **PNG Conversion**: Convert SVG to PNG using Resvg
|
|
355
|
+
*
|
|
356
|
+
* @param template - The template definition to render
|
|
357
|
+
* @param params - Dynamic parameters to populate the template
|
|
358
|
+
* @param options - Optional rendering options
|
|
359
|
+
* @param options.width - Custom image width in pixels (default: 1200)
|
|
360
|
+
* @param options.height - Custom image height in pixels (default: 630)
|
|
361
|
+
* @returns Promise resolving to a PNG image buffer
|
|
362
|
+
*
|
|
363
|
+
* @throws Will throw if:
|
|
364
|
+
* - Font loading fails
|
|
365
|
+
* - HTML generation fails
|
|
366
|
+
* - SVG rendering fails
|
|
367
|
+
* - PNG conversion fails
|
|
368
|
+
*/
|
|
369
|
+
declare function renderTemplate(template: OgTemplate, params: OgTemplateParams, options?: {
|
|
370
|
+
width: number;
|
|
371
|
+
height: number;
|
|
372
|
+
}): Promise<Buffer>;
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Network utility for fetching remote resources with caching.
|
|
376
|
+
*
|
|
377
|
+
* This module provides HTTP fetching functionality for loading font files
|
|
378
|
+
* and other binary assets from remote URLs, with built-in caching to avoid
|
|
379
|
+
* redundant network requests for the same resources.
|
|
380
|
+
*/
|
|
381
|
+
/**
|
|
382
|
+
* Fetches a font file from a URL and returns it as an ArrayBuffer, with caching.
|
|
383
|
+
*
|
|
384
|
+
* This function is used to download font files from CDNs (like Google Fonts)
|
|
385
|
+
* so they can be embedded in OG images during server-side rendering.
|
|
386
|
+
* @param url - The URL of the font file to download
|
|
387
|
+
* @returns Promise resolving to the font data as an ArrayBuffer
|
|
388
|
+
*
|
|
389
|
+
* @throws Will throw if the network request fails or the URL is invalid
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* // First call - downloads from network
|
|
393
|
+
* const fontData1 = await loadFontFromUrl('https://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfAZ9hiA.woff2');
|
|
394
|
+
*
|
|
395
|
+
* // Second call - returns cached data (no network request)
|
|
396
|
+
* const fontData2 = await loadFontFromUrl('https://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfAZ9hiA.woff2');
|
|
397
|
+
*/
|
|
398
|
+
declare const loadFontFromUrl: (url: string) => Promise<ArrayBuffer>;
|
|
399
|
+
|
|
400
|
+
export { type OgEmojiProvider, type OgFontConfig, type OgFontFormat, type OgTemplate, type OgTemplateOptions, type OgTemplateParams, type OgTemplateRenderer, TemplateRenderer, createTemplateRenderer, defineTemplate, loadFontFromUrl, renderTemplate, validateTemplate };
|