og-route-kit 0.0.0-stage → 0.1.0
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/CHANGELOG.md +43 -0
- package/LICENSE +21 -0
- package/README.md +265 -2
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +195 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/core/index.d.ts +21 -0
- package/dist/core/index.js +133 -0
- package/dist/core/index.js.map +1 -0
- package/dist/examples/cards/index.d.ts +24 -0
- package/dist/examples/cards/index.js +129 -0
- package/dist/examples/cards/index.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +133 -0
- package/dist/index.js.map +1 -0
- package/dist/next/index.d.ts +44 -0
- package/dist/next/index.js +270 -0
- package/dist/next/index.js.map +1 -0
- package/dist/sizes-Bn3i71Bz.d.ts +34 -0
- package/package.json +92 -4
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// src/next/create-og-route.tsx
|
|
2
|
+
import { ImageResponse } from "next/og.js";
|
|
3
|
+
|
|
4
|
+
// src/core/image-url.ts
|
|
5
|
+
function buildOgImageUrl(route, params = {}) {
|
|
6
|
+
const routeText = route.toString();
|
|
7
|
+
const hashIndex = routeText.indexOf("#");
|
|
8
|
+
const withoutHash = hashIndex >= 0 ? routeText.slice(0, hashIndex) : routeText;
|
|
9
|
+
const hash = hashIndex >= 0 ? routeText.slice(hashIndex) : "";
|
|
10
|
+
const queryIndex = withoutHash.indexOf("?");
|
|
11
|
+
const base = queryIndex >= 0 ? withoutHash.slice(0, queryIndex) : withoutHash;
|
|
12
|
+
const existingQuery = queryIndex >= 0 ? withoutHash.slice(queryIndex + 1) : "";
|
|
13
|
+
const searchParams = new URLSearchParams(existingQuery);
|
|
14
|
+
for (const [key, value] of Object.entries(params)) {
|
|
15
|
+
if (value == null) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
searchParams.delete(key);
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
for (const item of value) {
|
|
21
|
+
searchParams.append(key, String(item));
|
|
22
|
+
}
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
searchParams.set(key, String(value));
|
|
26
|
+
}
|
|
27
|
+
const query = searchParams.toString();
|
|
28
|
+
return `${base}${query ? `?${query}` : ""}${hash}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/core/text.ts
|
|
32
|
+
function normalizeText(value, options = {}) {
|
|
33
|
+
const collapseWhitespace = options.collapseWhitespace ?? true;
|
|
34
|
+
const trimmed = value.trim();
|
|
35
|
+
const normalized = collapseWhitespace ? trimmed.replace(/\s+/g, " ") : trimmed;
|
|
36
|
+
if (typeof options.max === "number") {
|
|
37
|
+
return normalized.slice(0, options.max);
|
|
38
|
+
}
|
|
39
|
+
return normalized;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/core/params.ts
|
|
43
|
+
var DEFAULT_OG_PARAM_MAX = 512;
|
|
44
|
+
var OgParamError = class extends Error {
|
|
45
|
+
constructor(message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = "OgParamError";
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
function normalizeOgParams(input, options) {
|
|
51
|
+
const searchParams = toURLSearchParams(input);
|
|
52
|
+
const output = {};
|
|
53
|
+
for (const key of Object.keys(options.defaults)) {
|
|
54
|
+
const field = options.fields?.[key];
|
|
55
|
+
const rawValue = searchParams.get(key);
|
|
56
|
+
const fallback = field?.fallback ?? options.defaults[key] ?? "";
|
|
57
|
+
const max = resolveMax(field?.max);
|
|
58
|
+
const normalizedValue = rawValue == null ? "" : normalizeText(rawValue.slice(0, max), { collapseWhitespace: field?.collapseWhitespace });
|
|
59
|
+
if (normalizedValue.length > 0) {
|
|
60
|
+
output[key] = applyMax(normalizedValue, max);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const normalizedFallback = normalizeText(fallback, {
|
|
64
|
+
collapseWhitespace: field?.collapseWhitespace,
|
|
65
|
+
max: resolveMax(field?.max)
|
|
66
|
+
});
|
|
67
|
+
if (normalizedFallback.length > 0) {
|
|
68
|
+
output[key] = normalizedFallback;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (field?.required) {
|
|
72
|
+
throw new OgParamError(`Missing required OG image field: ${key}`);
|
|
73
|
+
}
|
|
74
|
+
output[key] = "";
|
|
75
|
+
}
|
|
76
|
+
return output;
|
|
77
|
+
}
|
|
78
|
+
function applyMax(value, max) {
|
|
79
|
+
return value.slice(0, resolveMax(max));
|
|
80
|
+
}
|
|
81
|
+
function resolveMax(max) {
|
|
82
|
+
if (max === void 0) {
|
|
83
|
+
return DEFAULT_OG_PARAM_MAX;
|
|
84
|
+
}
|
|
85
|
+
return Number.isSafeInteger(max) && max > 0 ? max : DEFAULT_OG_PARAM_MAX;
|
|
86
|
+
}
|
|
87
|
+
function toURLSearchParams(input) {
|
|
88
|
+
if (input instanceof URLSearchParams) {
|
|
89
|
+
return input;
|
|
90
|
+
}
|
|
91
|
+
const params = new URLSearchParams();
|
|
92
|
+
if (isIterablePairs(input)) {
|
|
93
|
+
for (const [key, value] of input) {
|
|
94
|
+
params.append(key, value);
|
|
95
|
+
}
|
|
96
|
+
return params;
|
|
97
|
+
}
|
|
98
|
+
for (const [key, value] of Object.entries(input)) {
|
|
99
|
+
if (Array.isArray(value)) {
|
|
100
|
+
for (const item of value) {
|
|
101
|
+
params.append(key, item);
|
|
102
|
+
}
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (value != null) {
|
|
106
|
+
params.set(key, value);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return params;
|
|
110
|
+
}
|
|
111
|
+
function isIterablePairs(input) {
|
|
112
|
+
return Symbol.iterator in Object(input);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/core/sizes.ts
|
|
116
|
+
var OG_IMAGE_SIZES = {
|
|
117
|
+
og: { width: 1200, height: 630 },
|
|
118
|
+
twitter: { width: 1200, height: 675 },
|
|
119
|
+
square: { width: 1200, height: 1200 }
|
|
120
|
+
};
|
|
121
|
+
function resolveOgImageSize(size = "og") {
|
|
122
|
+
if (typeof size === "string") {
|
|
123
|
+
return OG_IMAGE_SIZES[size];
|
|
124
|
+
}
|
|
125
|
+
return size;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/next/create-og-route.tsx
|
|
129
|
+
var DEFAULT_CACHE_CONTROL = "public, max-age=0, s-maxage=86400, stale-while-revalidate=604800";
|
|
130
|
+
function createOgRoute(options) {
|
|
131
|
+
return function GET(request) {
|
|
132
|
+
const url = new URL(request.url);
|
|
133
|
+
const size = resolveOgImageSize(options.size);
|
|
134
|
+
let props;
|
|
135
|
+
try {
|
|
136
|
+
props = normalizeOgParams(url.searchParams, {
|
|
137
|
+
defaults: options.defaults,
|
|
138
|
+
fields: options.fields
|
|
139
|
+
});
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (error instanceof OgParamError) {
|
|
142
|
+
return new Response(`Bad request: ${error.message}`, {
|
|
143
|
+
status: 400,
|
|
144
|
+
headers: {
|
|
145
|
+
"Cache-Control": "no-store",
|
|
146
|
+
"Content-Type": "text/plain; charset=utf-8"
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
return new ImageResponse(options.render(props), {
|
|
153
|
+
...size,
|
|
154
|
+
headers: {
|
|
155
|
+
"Cache-Control": DEFAULT_CACHE_CONTROL,
|
|
156
|
+
"X-Content-Type-Options": "nosniff",
|
|
157
|
+
...headersToRecord(options.headers)
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function headersToRecord(headers) {
|
|
163
|
+
if (!headers) {
|
|
164
|
+
return {};
|
|
165
|
+
}
|
|
166
|
+
if (headers instanceof Headers) {
|
|
167
|
+
return Object.fromEntries(headers.entries());
|
|
168
|
+
}
|
|
169
|
+
if (Array.isArray(headers)) {
|
|
170
|
+
return Object.fromEntries(headers);
|
|
171
|
+
}
|
|
172
|
+
return headers;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/next/create-social-metadata.ts
|
|
176
|
+
function createSocialMetadata(options) {
|
|
177
|
+
const siteUrl = normalizeOrigin(options.siteUrl);
|
|
178
|
+
return {
|
|
179
|
+
website(input) {
|
|
180
|
+
return buildMetadata("website", siteUrl, options, input);
|
|
181
|
+
},
|
|
182
|
+
article(input) {
|
|
183
|
+
return buildMetadata("article", siteUrl, options, input);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function buildMetadata(type, siteUrl, options, input) {
|
|
188
|
+
const pageUrl = absoluteUrl(siteUrl, input.path ?? "/");
|
|
189
|
+
const images = resolveSocialImages(siteUrl, options, input);
|
|
190
|
+
const title = input.title;
|
|
191
|
+
const description = input.description;
|
|
192
|
+
return {
|
|
193
|
+
title,
|
|
194
|
+
description,
|
|
195
|
+
metadataBase: new URL(siteUrl),
|
|
196
|
+
alternates: {
|
|
197
|
+
canonical: pageUrl
|
|
198
|
+
},
|
|
199
|
+
openGraph: {
|
|
200
|
+
type,
|
|
201
|
+
url: pageUrl,
|
|
202
|
+
title,
|
|
203
|
+
description,
|
|
204
|
+
siteName: options.siteName,
|
|
205
|
+
images,
|
|
206
|
+
...type === "article" ? articleFields(input) : {}
|
|
207
|
+
},
|
|
208
|
+
twitter: {
|
|
209
|
+
card: "summary_large_image",
|
|
210
|
+
site: options.twitter,
|
|
211
|
+
title,
|
|
212
|
+
description,
|
|
213
|
+
images: images.map((image) => image.url)
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function resolveSocialImages(siteUrl, options, input) {
|
|
218
|
+
const imageSize = resolveOgImageSize(options.imageSize ?? "og");
|
|
219
|
+
const fallbackSize = resolveOgImageSize(options.defaultImageSize ?? options.imageSize ?? "og");
|
|
220
|
+
const alt = input.imageAlt ?? input.title;
|
|
221
|
+
const defaultImage = options.defaultImage ? {
|
|
222
|
+
url: absoluteUrl(siteUrl, options.defaultImage),
|
|
223
|
+
alt,
|
|
224
|
+
...fallbackSize
|
|
225
|
+
} : void 0;
|
|
226
|
+
if (input.image) {
|
|
227
|
+
const url = absoluteUrl(siteUrl, buildOgImageUrl(options.imageRoute, input.image));
|
|
228
|
+
const routeImage = {
|
|
229
|
+
url,
|
|
230
|
+
alt,
|
|
231
|
+
...imageSize
|
|
232
|
+
};
|
|
233
|
+
return options.includeDefaultImageFallback && defaultImage ? [routeImage, defaultImage] : [routeImage];
|
|
234
|
+
}
|
|
235
|
+
if (defaultImage) {
|
|
236
|
+
return [defaultImage];
|
|
237
|
+
}
|
|
238
|
+
return [
|
|
239
|
+
{
|
|
240
|
+
url: absoluteUrl(
|
|
241
|
+
siteUrl,
|
|
242
|
+
buildOgImageUrl(options.imageRoute, { title: input.title, description: input.description })
|
|
243
|
+
),
|
|
244
|
+
alt,
|
|
245
|
+
...imageSize
|
|
246
|
+
}
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
function articleFields(input) {
|
|
250
|
+
return {
|
|
251
|
+
...input.publishedTime ? { publishedTime: input.publishedTime } : {},
|
|
252
|
+
...input.modifiedTime ? { modifiedTime: input.modifiedTime } : {},
|
|
253
|
+
...input.authors ? { authors: input.authors } : {}
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function normalizeOrigin(value) {
|
|
257
|
+
return value.replace(/\/+$/, "");
|
|
258
|
+
}
|
|
259
|
+
function absoluteUrl(origin, path) {
|
|
260
|
+
if (/^https?:\/\//i.test(path)) {
|
|
261
|
+
return path;
|
|
262
|
+
}
|
|
263
|
+
return `${origin}${path.startsWith("/") ? path : `/${path}`}`;
|
|
264
|
+
}
|
|
265
|
+
export {
|
|
266
|
+
DEFAULT_CACHE_CONTROL,
|
|
267
|
+
createOgRoute,
|
|
268
|
+
createSocialMetadata
|
|
269
|
+
};
|
|
270
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/next/create-og-route.tsx","../../src/core/image-url.ts","../../src/core/text.ts","../../src/core/params.ts","../../src/core/sizes.ts","../../src/next/create-social-metadata.ts"],"sourcesContent":["import type { ReactElement } from \"react\";\nimport { ImageResponse } from \"next/og.js\";\n\nimport { normalizeOgParams, OgParamError, resolveOgImageSize } from \"../core/index\";\nimport type { OgFields, OgImageSizeInput } from \"../core/index\";\n\nexport const DEFAULT_CACHE_CONTROL =\n \"public, max-age=0, s-maxage=86400, stale-while-revalidate=604800\";\n\nexport type CreateOgRouteOptions<T extends Record<string, string>> = {\n size?: OgImageSizeInput | undefined;\n defaults: T;\n fields?: OgFields<T> | undefined;\n headers?: HeadersInit | undefined;\n render: (props: T) => ReactElement;\n};\n\nexport type OgRouteRequest = Request;\n\nexport function createOgRoute<T extends Record<string, string>>(options: CreateOgRouteOptions<T>) {\n return function GET(request: OgRouteRequest): ImageResponse | Response {\n const url = new URL(request.url);\n const size = resolveOgImageSize(options.size);\n\n let props: T;\n try {\n props = normalizeOgParams(url.searchParams, {\n defaults: options.defaults,\n fields: options.fields,\n });\n } catch (error) {\n if (error instanceof OgParamError) {\n return new Response(`Bad request: ${error.message}`, {\n status: 400,\n headers: {\n \"Cache-Control\": \"no-store\",\n \"Content-Type\": \"text/plain; charset=utf-8\",\n },\n });\n }\n throw error;\n }\n\n return new ImageResponse(options.render(props), {\n ...size,\n headers: {\n \"Cache-Control\": DEFAULT_CACHE_CONTROL,\n \"X-Content-Type-Options\": \"nosniff\",\n ...headersToRecord(options.headers),\n },\n });\n };\n}\n\nfunction headersToRecord(headers: HeadersInit | undefined): Record<string, string> {\n if (!headers) {\n return {};\n }\n\n if (headers instanceof Headers) {\n return Object.fromEntries(headers.entries());\n }\n\n if (Array.isArray(headers)) {\n return Object.fromEntries(headers);\n }\n\n return headers;\n}\n","export type OgImageUrlParams = Record<\n string,\n string | number | boolean | Array<string | number | boolean> | null | undefined\n>;\n\nexport function buildOgImageUrl(route: string | URL, params: OgImageUrlParams = {}): string {\n const routeText = route.toString();\n const hashIndex = routeText.indexOf(\"#\");\n const withoutHash = hashIndex >= 0 ? routeText.slice(0, hashIndex) : routeText;\n const hash = hashIndex >= 0 ? routeText.slice(hashIndex) : \"\";\n const queryIndex = withoutHash.indexOf(\"?\");\n const base = queryIndex >= 0 ? withoutHash.slice(0, queryIndex) : withoutHash;\n const existingQuery = queryIndex >= 0 ? withoutHash.slice(queryIndex + 1) : \"\";\n const searchParams = new URLSearchParams(existingQuery);\n\n for (const [key, value] of Object.entries(params)) {\n if (value == null) {\n continue;\n }\n\n searchParams.delete(key);\n\n if (Array.isArray(value)) {\n for (const item of value) {\n searchParams.append(key, String(item));\n }\n continue;\n }\n\n searchParams.set(key, String(value));\n }\n\n const query = searchParams.toString();\n\n return `${base}${query ? `?${query}` : \"\"}${hash}`;\n}\n","export type NormalizeTextOptions = {\n collapseWhitespace?: boolean | undefined;\n max?: number | undefined;\n};\n\nexport function normalizeText(value: string, options: NormalizeTextOptions = {}): string {\n const collapseWhitespace = options.collapseWhitespace ?? true;\n const trimmed = value.trim();\n const normalized = collapseWhitespace ? trimmed.replace(/\\s+/g, \" \") : trimmed;\n\n if (typeof options.max === \"number\") {\n return normalized.slice(0, options.max);\n }\n\n return normalized;\n}\n","import type { OgFields } from \"./fields\";\nimport { normalizeText } from \"./text\";\n\nexport type OgSearchParamsInput =\n | URLSearchParams\n | Iterable<[string, string]>\n | Record<string, string | string[] | null | undefined>;\n\nexport type NormalizeOgParamsOptions<T extends Record<string, string>> = {\n defaults: T;\n fields?: OgFields<T> | undefined;\n};\n\n// Caps untrusted query input that reaches the renderer when a field omits an\n// explicit max, so an unconfigured field can't accept multi-megabyte values.\n// Override per field with `fields[key].max`.\nexport const DEFAULT_OG_PARAM_MAX = 512;\n\nexport class OgParamError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"OgParamError\";\n }\n}\n\nexport function normalizeOgParams<T extends Record<string, string>>(\n input: OgSearchParamsInput,\n options: NormalizeOgParamsOptions<T>,\n): T {\n const searchParams = toURLSearchParams(input);\n const output: Record<string, string> = {};\n\n for (const key of Object.keys(options.defaults) as Array<keyof T & string>) {\n const field = options.fields?.[key];\n const rawValue = searchParams.get(key);\n const fallback = field?.fallback ?? options.defaults[key] ?? \"\";\n const max = resolveMax(field?.max);\n const normalizedValue =\n rawValue == null\n ? \"\"\n : normalizeText(rawValue.slice(0, max), { collapseWhitespace: field?.collapseWhitespace });\n\n if (normalizedValue.length > 0) {\n output[key] = applyMax(normalizedValue, max);\n continue;\n }\n\n const normalizedFallback = normalizeText(fallback, {\n collapseWhitespace: field?.collapseWhitespace,\n max: resolveMax(field?.max),\n });\n\n if (normalizedFallback.length > 0) {\n output[key] = normalizedFallback;\n continue;\n }\n\n if (field?.required) {\n throw new OgParamError(`Missing required OG image field: ${key}`);\n }\n\n output[key] = \"\";\n }\n\n return output as T;\n}\n\nfunction applyMax(value: string, max: number | undefined): string {\n return value.slice(0, resolveMax(max));\n}\n\nfunction resolveMax(max: number | undefined): number {\n if (max === undefined) {\n return DEFAULT_OG_PARAM_MAX;\n }\n\n return Number.isSafeInteger(max) && max > 0 ? max : DEFAULT_OG_PARAM_MAX;\n}\n\nfunction toURLSearchParams(input: OgSearchParamsInput): URLSearchParams {\n if (input instanceof URLSearchParams) {\n return input;\n }\n\n const params = new URLSearchParams();\n\n if (isIterablePairs(input)) {\n for (const [key, value] of input) {\n params.append(key, value);\n }\n\n return params;\n }\n\n for (const [key, value] of Object.entries(input)) {\n if (Array.isArray(value)) {\n for (const item of value) {\n params.append(key, item);\n }\n continue;\n }\n\n if (value != null) {\n params.set(key, value);\n }\n }\n\n return params;\n}\n\nfunction isIterablePairs(input: OgSearchParamsInput): input is Iterable<[string, string]> {\n return Symbol.iterator in Object(input);\n}\n","export type OgImageSize = {\n width: number;\n height: number;\n};\n\nexport type OgImageSizeName = \"og\" | \"twitter\" | \"square\";\n\nexport const OG_IMAGE_SIZES = {\n og: { width: 1200, height: 630 },\n twitter: { width: 1200, height: 675 },\n square: { width: 1200, height: 1200 },\n} as const satisfies Record<OgImageSizeName, OgImageSize>;\n\nexport type OgImageSizeInput = OgImageSize | OgImageSizeName;\n\nexport function resolveOgImageSize(size: OgImageSizeInput = \"og\"): OgImageSize {\n if (typeof size === \"string\") {\n return OG_IMAGE_SIZES[size];\n }\n\n return size;\n}\n","import type { Metadata } from \"next\";\n\nimport { buildOgImageUrl, resolveOgImageSize } from \"../core/index\";\nimport type { OgImageSizeInput, OgImageUrlParams } from \"../core/index\";\n\nexport type CreateSocialMetadataOptions = {\n siteUrl: string;\n siteName: string;\n twitter?: string | undefined;\n imageRoute: string;\n defaultImage?: string | undefined;\n includeDefaultImageFallback?: boolean | undefined;\n imageSize?: OgImageSizeInput | undefined;\n defaultImageSize?: OgImageSizeInput | undefined;\n};\n\nexport type WebsiteMetadataInput = {\n title: string;\n description: string;\n path?: string | undefined;\n image?: OgImageUrlParams | undefined;\n imageAlt?: string | undefined;\n};\n\nexport type ArticleMetadataInput = WebsiteMetadataInput & {\n publishedTime?: string | undefined;\n modifiedTime?: string | undefined;\n authors?: string[] | undefined;\n};\n\nexport function createSocialMetadata(options: CreateSocialMetadataOptions) {\n const siteUrl = normalizeOrigin(options.siteUrl);\n\n return {\n website(input: WebsiteMetadataInput): Metadata {\n return buildMetadata(\"website\", siteUrl, options, input);\n },\n article(input: ArticleMetadataInput): Metadata {\n return buildMetadata(\"article\", siteUrl, options, input);\n },\n };\n}\n\nfunction buildMetadata(\n type: \"website\" | \"article\",\n siteUrl: string,\n options: CreateSocialMetadataOptions,\n input: WebsiteMetadataInput | ArticleMetadataInput,\n): Metadata {\n const pageUrl = absoluteUrl(siteUrl, input.path ?? \"/\");\n const images = resolveSocialImages(siteUrl, options, input);\n const title = input.title;\n const description = input.description;\n\n return {\n title,\n description,\n metadataBase: new URL(siteUrl),\n alternates: {\n canonical: pageUrl,\n },\n openGraph: {\n type,\n url: pageUrl,\n title,\n description,\n siteName: options.siteName,\n images,\n ...(type === \"article\" ? articleFields(input as ArticleMetadataInput) : {}),\n },\n twitter: {\n card: \"summary_large_image\",\n site: options.twitter,\n title,\n description,\n images: images.map((image) => image.url),\n },\n };\n}\n\ntype SocialImageMetadata = {\n url: string;\n alt: string;\n width: number;\n height: number;\n};\n\nfunction resolveSocialImages(\n siteUrl: string,\n options: CreateSocialMetadataOptions,\n input: WebsiteMetadataInput,\n): SocialImageMetadata[] {\n const imageSize = resolveOgImageSize(options.imageSize ?? \"og\");\n const fallbackSize = resolveOgImageSize(options.defaultImageSize ?? options.imageSize ?? \"og\");\n const alt = input.imageAlt ?? input.title;\n const defaultImage = options.defaultImage\n ? {\n url: absoluteUrl(siteUrl, options.defaultImage),\n alt,\n ...fallbackSize,\n }\n : undefined;\n\n if (input.image) {\n const url = absoluteUrl(siteUrl, buildOgImageUrl(options.imageRoute, input.image));\n const routeImage = {\n url,\n alt,\n ...imageSize,\n };\n\n return options.includeDefaultImageFallback && defaultImage\n ? [routeImage, defaultImage]\n : [routeImage];\n }\n\n if (defaultImage) {\n return [defaultImage];\n }\n\n return [\n {\n url: absoluteUrl(\n siteUrl,\n buildOgImageUrl(options.imageRoute, { title: input.title, description: input.description }),\n ),\n alt,\n ...imageSize,\n },\n ];\n}\n\nfunction articleFields(input: ArticleMetadataInput): Record<string, unknown> {\n return {\n ...(input.publishedTime ? { publishedTime: input.publishedTime } : {}),\n ...(input.modifiedTime ? { modifiedTime: input.modifiedTime } : {}),\n ...(input.authors ? { authors: input.authors } : {}),\n };\n}\n\nfunction normalizeOrigin(value: string): string {\n return value.replace(/\\/+$/, \"\");\n}\n\nfunction absoluteUrl(origin: string, path: string): string {\n if (/^https?:\\/\\//i.test(path)) {\n return path;\n }\n\n return `${origin}${path.startsWith(\"/\") ? path : `/${path}`}`;\n}\n"],"mappings":";AACA,SAAS,qBAAqB;;;ACIvB,SAAS,gBAAgB,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,SAAS,gBAAgB,OAAuB;AAC9C,SAAO,MAAM,QAAQ,QAAQ,EAAE;AACjC;AAEA,SAAS,YAAY,QAAgB,MAAsB;AACzD,MAAI,gBAAgB,KAAK,IAAI,GAAG;AAC9B,WAAO;AAAA,EACT;AAEA,SAAO,GAAG,MAAM,GAAG,KAAK,WAAW,GAAG,IAAI,OAAO,IAAI,IAAI,EAAE;AAC7D;","names":[]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type OgField = {
|
|
2
|
+
required?: boolean;
|
|
3
|
+
max?: number;
|
|
4
|
+
fallback?: string;
|
|
5
|
+
collapseWhitespace?: boolean;
|
|
6
|
+
};
|
|
7
|
+
type OgFields<T extends Record<string, string>> = Partial<Record<keyof T, OgField>>;
|
|
8
|
+
|
|
9
|
+
type OgImageUrlParams = Record<string, string | number | boolean | Array<string | number | boolean> | null | undefined>;
|
|
10
|
+
declare function buildOgImageUrl(route: string | URL, params?: OgImageUrlParams): string;
|
|
11
|
+
|
|
12
|
+
type OgImageSize = {
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
};
|
|
16
|
+
type OgImageSizeName = "og" | "twitter" | "square";
|
|
17
|
+
declare const OG_IMAGE_SIZES: {
|
|
18
|
+
readonly og: {
|
|
19
|
+
readonly width: 1200;
|
|
20
|
+
readonly height: 630;
|
|
21
|
+
};
|
|
22
|
+
readonly twitter: {
|
|
23
|
+
readonly width: 1200;
|
|
24
|
+
readonly height: 675;
|
|
25
|
+
};
|
|
26
|
+
readonly square: {
|
|
27
|
+
readonly width: 1200;
|
|
28
|
+
readonly height: 1200;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
type OgImageSizeInput = OgImageSize | OgImageSizeName;
|
|
32
|
+
declare function resolveOgImageSize(size?: OgImageSizeInput): OgImageSize;
|
|
33
|
+
|
|
34
|
+
export { type OgImageSizeInput as O, type OgFields as a, type OgImageUrlParams as b, OG_IMAGE_SIZES as c, type OgField as d, type OgImageSize as e, type OgImageSizeName as f, buildOgImageUrl as g, resolveOgImageSize as r };
|
package/package.json
CHANGED
|
@@ -1,6 +1,94 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "og-route-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Code-rendered Open Graph image routes for Next.js.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://pixelbrew-studio.github.io/og-route-kit/",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/pixelbrew-studio/og-route-kit/issues"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/pixelbrew-studio/og-route-kit.git"
|
|
14
|
+
},
|
|
15
|
+
"packageManager": "pnpm@10.33.4",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=20"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public",
|
|
21
|
+
"provenance": true
|
|
22
|
+
},
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"keywords": [
|
|
27
|
+
"opengraph",
|
|
28
|
+
"og-image",
|
|
29
|
+
"nextjs",
|
|
30
|
+
"next-og",
|
|
31
|
+
"image-response",
|
|
32
|
+
"social-image"
|
|
33
|
+
],
|
|
34
|
+
"bin": {
|
|
35
|
+
"og-route-kit": "dist/cli/index.js"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"CHANGELOG.md",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"types": "./dist/index.d.ts",
|
|
46
|
+
"import": "./dist/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./core": {
|
|
49
|
+
"types": "./dist/core/index.d.ts",
|
|
50
|
+
"import": "./dist/core/index.js"
|
|
51
|
+
},
|
|
52
|
+
"./next": {
|
|
53
|
+
"types": "./dist/next/index.d.ts",
|
|
54
|
+
"import": "./dist/next/index.js"
|
|
55
|
+
},
|
|
56
|
+
"./examples/cards": {
|
|
57
|
+
"types": "./dist/examples/cards/index.d.ts",
|
|
58
|
+
"import": "./dist/examples/cards/index.js"
|
|
59
|
+
},
|
|
60
|
+
"./package.json": "./package.json"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "tsup",
|
|
64
|
+
"ci": "pnpm typecheck && pnpm test && pnpm build && pnpm example:build && pnpm pack --dry-run",
|
|
65
|
+
"example:build": "pnpm --filter og-route-kit-next-app-example build",
|
|
66
|
+
"lint": "tsc --noEmit",
|
|
67
|
+
"pack:check": "pnpm pack --dry-run",
|
|
68
|
+
"prepare": "tsup",
|
|
69
|
+
"prepack": "tsup",
|
|
70
|
+
"test": "vitest run",
|
|
71
|
+
"typecheck": "tsc --noEmit",
|
|
72
|
+
"verify:local": "bash scripts/verify-local.sh"
|
|
73
|
+
},
|
|
74
|
+
"peerDependencies": {
|
|
75
|
+
"next": ">=14.0.0",
|
|
76
|
+
"react": ">=18.2.0"
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"@types/node": "^26.0.1",
|
|
80
|
+
"@types/react": "^19.3.3",
|
|
81
|
+
"next": "^16.5.0",
|
|
82
|
+
"react": "^19.3.1",
|
|
83
|
+
"react-dom": "^19.3.1",
|
|
84
|
+
"tsup": "^8.5.0",
|
|
85
|
+
"typescript": "^6.0.3",
|
|
86
|
+
"vitest": "^4.1.9"
|
|
87
|
+
},
|
|
88
|
+
"pnpm": {
|
|
89
|
+
"overrides": {
|
|
90
|
+
"esbuild@<0.28.1": "0.28.1",
|
|
91
|
+
"postcss@<8.5.10": ">=8.5.10"
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|