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,133 @@
|
|
|
1
|
+
// src/core/image-url.ts
|
|
2
|
+
function buildOgImageUrl(route, params = {}) {
|
|
3
|
+
const routeText = route.toString();
|
|
4
|
+
const hashIndex = routeText.indexOf("#");
|
|
5
|
+
const withoutHash = hashIndex >= 0 ? routeText.slice(0, hashIndex) : routeText;
|
|
6
|
+
const hash = hashIndex >= 0 ? routeText.slice(hashIndex) : "";
|
|
7
|
+
const queryIndex = withoutHash.indexOf("?");
|
|
8
|
+
const base = queryIndex >= 0 ? withoutHash.slice(0, queryIndex) : withoutHash;
|
|
9
|
+
const existingQuery = queryIndex >= 0 ? withoutHash.slice(queryIndex + 1) : "";
|
|
10
|
+
const searchParams = new URLSearchParams(existingQuery);
|
|
11
|
+
for (const [key, value] of Object.entries(params)) {
|
|
12
|
+
if (value == null) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
searchParams.delete(key);
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
for (const item of value) {
|
|
18
|
+
searchParams.append(key, String(item));
|
|
19
|
+
}
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
searchParams.set(key, String(value));
|
|
23
|
+
}
|
|
24
|
+
const query = searchParams.toString();
|
|
25
|
+
return `${base}${query ? `?${query}` : ""}${hash}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/core/text.ts
|
|
29
|
+
function normalizeText(value, options = {}) {
|
|
30
|
+
const collapseWhitespace = options.collapseWhitespace ?? true;
|
|
31
|
+
const trimmed = value.trim();
|
|
32
|
+
const normalized = collapseWhitespace ? trimmed.replace(/\s+/g, " ") : trimmed;
|
|
33
|
+
if (typeof options.max === "number") {
|
|
34
|
+
return normalized.slice(0, options.max);
|
|
35
|
+
}
|
|
36
|
+
return normalized;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/core/params.ts
|
|
40
|
+
var DEFAULT_OG_PARAM_MAX = 512;
|
|
41
|
+
var OgParamError = class extends Error {
|
|
42
|
+
constructor(message) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.name = "OgParamError";
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
function normalizeOgParams(input, options) {
|
|
48
|
+
const searchParams = toURLSearchParams(input);
|
|
49
|
+
const output = {};
|
|
50
|
+
for (const key of Object.keys(options.defaults)) {
|
|
51
|
+
const field = options.fields?.[key];
|
|
52
|
+
const rawValue = searchParams.get(key);
|
|
53
|
+
const fallback = field?.fallback ?? options.defaults[key] ?? "";
|
|
54
|
+
const max = resolveMax(field?.max);
|
|
55
|
+
const normalizedValue = rawValue == null ? "" : normalizeText(rawValue.slice(0, max), { collapseWhitespace: field?.collapseWhitespace });
|
|
56
|
+
if (normalizedValue.length > 0) {
|
|
57
|
+
output[key] = applyMax(normalizedValue, max);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const normalizedFallback = normalizeText(fallback, {
|
|
61
|
+
collapseWhitespace: field?.collapseWhitespace,
|
|
62
|
+
max: resolveMax(field?.max)
|
|
63
|
+
});
|
|
64
|
+
if (normalizedFallback.length > 0) {
|
|
65
|
+
output[key] = normalizedFallback;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (field?.required) {
|
|
69
|
+
throw new OgParamError(`Missing required OG image field: ${key}`);
|
|
70
|
+
}
|
|
71
|
+
output[key] = "";
|
|
72
|
+
}
|
|
73
|
+
return output;
|
|
74
|
+
}
|
|
75
|
+
function applyMax(value, max) {
|
|
76
|
+
return value.slice(0, resolveMax(max));
|
|
77
|
+
}
|
|
78
|
+
function resolveMax(max) {
|
|
79
|
+
if (max === void 0) {
|
|
80
|
+
return DEFAULT_OG_PARAM_MAX;
|
|
81
|
+
}
|
|
82
|
+
return Number.isSafeInteger(max) && max > 0 ? max : DEFAULT_OG_PARAM_MAX;
|
|
83
|
+
}
|
|
84
|
+
function toURLSearchParams(input) {
|
|
85
|
+
if (input instanceof URLSearchParams) {
|
|
86
|
+
return input;
|
|
87
|
+
}
|
|
88
|
+
const params = new URLSearchParams();
|
|
89
|
+
if (isIterablePairs(input)) {
|
|
90
|
+
for (const [key, value] of input) {
|
|
91
|
+
params.append(key, value);
|
|
92
|
+
}
|
|
93
|
+
return params;
|
|
94
|
+
}
|
|
95
|
+
for (const [key, value] of Object.entries(input)) {
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
for (const item of value) {
|
|
98
|
+
params.append(key, item);
|
|
99
|
+
}
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (value != null) {
|
|
103
|
+
params.set(key, value);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return params;
|
|
107
|
+
}
|
|
108
|
+
function isIterablePairs(input) {
|
|
109
|
+
return Symbol.iterator in Object(input);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/core/sizes.ts
|
|
113
|
+
var OG_IMAGE_SIZES = {
|
|
114
|
+
og: { width: 1200, height: 630 },
|
|
115
|
+
twitter: { width: 1200, height: 675 },
|
|
116
|
+
square: { width: 1200, height: 1200 }
|
|
117
|
+
};
|
|
118
|
+
function resolveOgImageSize(size = "og") {
|
|
119
|
+
if (typeof size === "string") {
|
|
120
|
+
return OG_IMAGE_SIZES[size];
|
|
121
|
+
}
|
|
122
|
+
return size;
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
DEFAULT_OG_PARAM_MAX,
|
|
126
|
+
OG_IMAGE_SIZES,
|
|
127
|
+
OgParamError,
|
|
128
|
+
buildOgImageUrl,
|
|
129
|
+
normalizeOgParams,
|
|
130
|
+
normalizeText,
|
|
131
|
+
resolveOgImageSize
|
|
132
|
+
};
|
|
133
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/image-url.ts","../../src/core/text.ts","../../src/core/params.ts","../../src/core/sizes.ts"],"sourcesContent":["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"],"mappings":";AAKO,SAAS,gBAAgB,OAAqB,SAA2B,CAAC,GAAW;AAC1F,QAAM,YAAY,MAAM,SAAS;AACjC,QAAM,YAAY,UAAU,QAAQ,GAAG;AACvC,QAAM,cAAc,aAAa,IAAI,UAAU,MAAM,GAAG,SAAS,IAAI;AACrE,QAAM,OAAO,aAAa,IAAI,UAAU,MAAM,SAAS,IAAI;AAC3D,QAAM,aAAa,YAAY,QAAQ,GAAG;AAC1C,QAAM,OAAO,cAAc,IAAI,YAAY,MAAM,GAAG,UAAU,IAAI;AAClE,QAAM,gBAAgB,cAAc,IAAI,YAAY,MAAM,aAAa,CAAC,IAAI;AA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ACT;AAEA,SAAS,gBAAgB,OAAiE;AACxF,SAAO,OAAO,YAAY,OAAO,KAAK;AACxC;;;ACzGO,IAAM,iBAAiB;AAAA,EAC5B,IAAI,EAAE,OAAO,MAAM,QAAQ,IAAI;AAAA,EAC/B,SAAS,EAAE,OAAO,MAAM,QAAQ,IAAI;AAAA,EACpC,QAAQ,EAAE,OAAO,MAAM,QAAQ,KAAK;AACtC;AAIO,SAAS,mBAAmB,OAAyB,MAAmB;AAC7E,MAAI,OAAO,SAAS,UAAU;AAC5B,WAAO,eAAe,IAAI;AAAA,EAC5B;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type ArticleCardProps = {
|
|
4
|
+
eyebrow: string;
|
|
5
|
+
title: string;
|
|
6
|
+
description: string;
|
|
7
|
+
};
|
|
8
|
+
declare function ArticleCard({ eyebrow, title, description }: ArticleCardProps): react.JSX.Element;
|
|
9
|
+
|
|
10
|
+
type ChangelogCardProps = {
|
|
11
|
+
eyebrow: string;
|
|
12
|
+
title: string;
|
|
13
|
+
description: string;
|
|
14
|
+
};
|
|
15
|
+
declare function ChangelogCard({ eyebrow, title, description }: ChangelogCardProps): react.JSX.Element;
|
|
16
|
+
|
|
17
|
+
type ProductCardProps = {
|
|
18
|
+
eyebrow: string;
|
|
19
|
+
title: string;
|
|
20
|
+
description: string;
|
|
21
|
+
};
|
|
22
|
+
declare function ProductCard({ eyebrow, title, description }: ProductCardProps): react.JSX.Element;
|
|
23
|
+
|
|
24
|
+
export { ArticleCard, type ArticleCardProps, ChangelogCard, type ChangelogCardProps, ProductCard, type ProductCardProps };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// src/examples/cards/article-card.tsx
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
function ArticleCard({ eyebrow, title, description }) {
|
|
4
|
+
return /* @__PURE__ */ jsxs(
|
|
5
|
+
"div",
|
|
6
|
+
{
|
|
7
|
+
style: {
|
|
8
|
+
width: "100%",
|
|
9
|
+
height: "100%",
|
|
10
|
+
display: "flex",
|
|
11
|
+
flexDirection: "column",
|
|
12
|
+
justifyContent: "space-between",
|
|
13
|
+
background: "#fbfbf9",
|
|
14
|
+
color: "#111827",
|
|
15
|
+
fontFamily: "Georgia, serif",
|
|
16
|
+
padding: 74
|
|
17
|
+
},
|
|
18
|
+
children: [
|
|
19
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", justifyContent: "space-between", fontSize: 28, color: "#5b616e" }, children: [
|
|
20
|
+
/* @__PURE__ */ jsx("div", { children: eyebrow }),
|
|
21
|
+
/* @__PURE__ */ jsx("div", { children: "Open Graph" })
|
|
22
|
+
] }),
|
|
23
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 56, alignItems: "flex-end" }, children: [
|
|
24
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", flexDirection: "column", gap: 30, width: "70%" }, children: [
|
|
25
|
+
/* @__PURE__ */ jsx("div", { style: { fontSize: 82, lineHeight: 0.98, fontWeight: 700 }, children: title }),
|
|
26
|
+
/* @__PURE__ */ jsx("div", { style: { fontSize: 32, lineHeight: 1.3, color: "#4b5563" }, children: description })
|
|
27
|
+
] }),
|
|
28
|
+
/* @__PURE__ */ jsx("div", { style: { flex: 1, height: 250, borderLeft: "6px solid #111827", background: "#ece7dd" } })
|
|
29
|
+
] })
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/examples/cards/changelog-card.tsx
|
|
36
|
+
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
37
|
+
function ChangelogCard({ eyebrow, title, description }) {
|
|
38
|
+
return /* @__PURE__ */ jsxs2(
|
|
39
|
+
"div",
|
|
40
|
+
{
|
|
41
|
+
style: {
|
|
42
|
+
width: "100%",
|
|
43
|
+
height: "100%",
|
|
44
|
+
display: "flex",
|
|
45
|
+
background: "#111111",
|
|
46
|
+
color: "#f5f5f0",
|
|
47
|
+
fontFamily: "Inter, Arial, sans-serif",
|
|
48
|
+
padding: 70
|
|
49
|
+
},
|
|
50
|
+
children: [
|
|
51
|
+
/* @__PURE__ */ jsxs2("div", { style: { display: "flex", flexDirection: "column", gap: 30, width: "58%" }, children: [
|
|
52
|
+
/* @__PURE__ */ jsx2("div", { style: { fontSize: 30, color: "#78dcca", fontWeight: 700 }, children: eyebrow }),
|
|
53
|
+
/* @__PURE__ */ jsx2("div", { style: { fontSize: 78, lineHeight: 0.96, fontWeight: 800 }, children: title }),
|
|
54
|
+
/* @__PURE__ */ jsx2("div", { style: { fontSize: 32, lineHeight: 1.28, color: "#c9c9c3" }, children: description })
|
|
55
|
+
] }),
|
|
56
|
+
/* @__PURE__ */ jsx2("div", { style: { marginLeft: 58, flex: 1, display: "flex", flexDirection: "column", justifyContent: "center", gap: 18 }, children: ["Typed route factory", "Metadata helper", "Static PNG export"].map((item) => /* @__PURE__ */ jsx2(
|
|
57
|
+
"div",
|
|
58
|
+
{
|
|
59
|
+
style: {
|
|
60
|
+
display: "flex",
|
|
61
|
+
alignItems: "center",
|
|
62
|
+
height: 72,
|
|
63
|
+
border: "1px solid #343434",
|
|
64
|
+
padding: "0 26px",
|
|
65
|
+
fontSize: 26,
|
|
66
|
+
background: "#1d1d1b"
|
|
67
|
+
},
|
|
68
|
+
children: item
|
|
69
|
+
},
|
|
70
|
+
item
|
|
71
|
+
)) })
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/examples/cards/product-card.tsx
|
|
78
|
+
import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
79
|
+
function ProductCard({ eyebrow, title, description }) {
|
|
80
|
+
return /* @__PURE__ */ jsxs3(
|
|
81
|
+
"div",
|
|
82
|
+
{
|
|
83
|
+
style: {
|
|
84
|
+
width: "100%",
|
|
85
|
+
height: "100%",
|
|
86
|
+
display: "flex",
|
|
87
|
+
background: "#f7f5ef",
|
|
88
|
+
color: "#151515",
|
|
89
|
+
fontFamily: "Inter, Arial, sans-serif",
|
|
90
|
+
padding: 72
|
|
91
|
+
},
|
|
92
|
+
children: [
|
|
93
|
+
/* @__PURE__ */ jsxs3("div", { style: { display: "flex", flexDirection: "column", justifyContent: "space-between", width: "62%" }, children: [
|
|
94
|
+
/* @__PURE__ */ jsx3("div", { style: { fontSize: 30, letterSpacing: 0, color: "#2f6f62", fontWeight: 700 }, children: eyebrow }),
|
|
95
|
+
/* @__PURE__ */ jsxs3("div", { style: { display: "flex", flexDirection: "column", gap: 26 }, children: [
|
|
96
|
+
/* @__PURE__ */ jsx3("div", { style: { fontSize: 78, lineHeight: 0.95, fontWeight: 800 }, children: title }),
|
|
97
|
+
/* @__PURE__ */ jsx3("div", { style: { fontSize: 34, lineHeight: 1.25, color: "#494844" }, children: description })
|
|
98
|
+
] })
|
|
99
|
+
] }),
|
|
100
|
+
/* @__PURE__ */ jsx3(
|
|
101
|
+
"div",
|
|
102
|
+
{
|
|
103
|
+
style: {
|
|
104
|
+
marginLeft: 56,
|
|
105
|
+
flex: 1,
|
|
106
|
+
display: "flex",
|
|
107
|
+
alignItems: "center",
|
|
108
|
+
justifyContent: "center",
|
|
109
|
+
border: "2px solid #d8d2c3",
|
|
110
|
+
background: "#ffffff"
|
|
111
|
+
},
|
|
112
|
+
children: /* @__PURE__ */ jsxs3("div", { style: { display: "flex", flexDirection: "column", gap: 18, width: 250 }, children: [
|
|
113
|
+
/* @__PURE__ */ jsx3("div", { style: { height: 24, background: "#2f6f62" } }),
|
|
114
|
+
/* @__PURE__ */ jsx3("div", { style: { height: 24, background: "#a5b8ae" } }),
|
|
115
|
+
/* @__PURE__ */ jsx3("div", { style: { height: 24, background: "#d8d2c3" } }),
|
|
116
|
+
/* @__PURE__ */ jsx3("div", { style: { height: 120, border: "2px solid #151515", marginTop: 18 } })
|
|
117
|
+
] })
|
|
118
|
+
}
|
|
119
|
+
)
|
|
120
|
+
]
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
ArticleCard,
|
|
126
|
+
ChangelogCard,
|
|
127
|
+
ProductCard
|
|
128
|
+
};
|
|
129
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/examples/cards/article-card.tsx","../../../src/examples/cards/changelog-card.tsx","../../../src/examples/cards/product-card.tsx"],"sourcesContent":["export type ArticleCardProps = {\n eyebrow: string;\n title: string;\n description: string;\n};\n\nexport function ArticleCard({ eyebrow, title, description }: ArticleCardProps) {\n return (\n <div\n style={{\n width: \"100%\",\n height: \"100%\",\n display: \"flex\",\n flexDirection: \"column\",\n justifyContent: \"space-between\",\n background: \"#fbfbf9\",\n color: \"#111827\",\n fontFamily: \"Georgia, serif\",\n padding: 74,\n }}\n >\n <div style={{ display: \"flex\", justifyContent: \"space-between\", fontSize: 28, color: \"#5b616e\" }}>\n <div>{eyebrow}</div>\n <div>Open Graph</div>\n </div>\n <div style={{ display: \"flex\", gap: 56, alignItems: \"flex-end\" }}>\n <div style={{ display: \"flex\", flexDirection: \"column\", gap: 30, width: \"70%\" }}>\n <div style={{ fontSize: 82, lineHeight: 0.98, fontWeight: 700 }}>{title}</div>\n <div style={{ fontSize: 32, lineHeight: 1.3, color: \"#4b5563\" }}>{description}</div>\n </div>\n <div style={{ flex: 1, height: 250, borderLeft: \"6px solid #111827\", background: \"#ece7dd\" }} />\n </div>\n </div>\n );\n}\n","export type ChangelogCardProps = {\n eyebrow: string;\n title: string;\n description: string;\n};\n\nexport function ChangelogCard({ eyebrow, title, description }: ChangelogCardProps) {\n return (\n <div\n style={{\n width: \"100%\",\n height: \"100%\",\n display: \"flex\",\n background: \"#111111\",\n color: \"#f5f5f0\",\n fontFamily: \"Inter, Arial, sans-serif\",\n padding: 70,\n }}\n >\n <div style={{ display: \"flex\", flexDirection: \"column\", gap: 30, width: \"58%\" }}>\n <div style={{ fontSize: 30, color: \"#78dcca\", fontWeight: 700 }}>{eyebrow}</div>\n <div style={{ fontSize: 78, lineHeight: 0.96, fontWeight: 800 }}>{title}</div>\n <div style={{ fontSize: 32, lineHeight: 1.28, color: \"#c9c9c3\" }}>{description}</div>\n </div>\n <div style={{ marginLeft: 58, flex: 1, display: \"flex\", flexDirection: \"column\", justifyContent: \"center\", gap: 18 }}>\n {[\"Typed route factory\", \"Metadata helper\", \"Static PNG export\"].map((item) => (\n <div\n key={item}\n style={{\n display: \"flex\",\n alignItems: \"center\",\n height: 72,\n border: \"1px solid #343434\",\n padding: \"0 26px\",\n fontSize: 26,\n background: \"#1d1d1b\",\n }}\n >\n {item}\n </div>\n ))}\n </div>\n </div>\n );\n}\n","export type ProductCardProps = {\n eyebrow: string;\n title: string;\n description: string;\n};\n\nexport function ProductCard({ eyebrow, title, description }: ProductCardProps) {\n return (\n <div\n style={{\n width: \"100%\",\n height: \"100%\",\n display: \"flex\",\n background: \"#f7f5ef\",\n color: \"#151515\",\n fontFamily: \"Inter, Arial, sans-serif\",\n padding: 72,\n }}\n >\n <div style={{ display: \"flex\", flexDirection: \"column\", justifyContent: \"space-between\", width: \"62%\" }}>\n <div style={{ fontSize: 30, letterSpacing: 0, color: \"#2f6f62\", fontWeight: 700 }}>{eyebrow}</div>\n <div style={{ display: \"flex\", flexDirection: \"column\", gap: 26 }}>\n <div style={{ fontSize: 78, lineHeight: 0.95, fontWeight: 800 }}>{title}</div>\n <div style={{ fontSize: 34, lineHeight: 1.25, color: \"#494844\" }}>{description}</div>\n </div>\n </div>\n <div\n style={{\n marginLeft: 56,\n flex: 1,\n display: \"flex\",\n alignItems: \"center\",\n justifyContent: \"center\",\n border: \"2px solid #d8d2c3\",\n background: \"#ffffff\",\n }}\n >\n <div style={{ display: \"flex\", flexDirection: \"column\", gap: 18, width: 250 }}>\n <div style={{ height: 24, background: \"#2f6f62\" }} />\n <div style={{ height: 24, background: \"#a5b8ae\" }} />\n <div style={{ height: 24, background: \"#d8d2c3\" }} />\n <div style={{ height: 120, border: \"2px solid #151515\", marginTop: 18 }} />\n </div>\n </div>\n </div>\n );\n}\n"],"mappings":";AAqBM,SACE,KADF;AAfC,SAAS,YAAY,EAAE,SAAS,OAAO,YAAY,GAAqB;AAC7E,SACE;AAAA,IAAC;AAAA;AAAA,MACC,OAAO;AAAA,QACL,OAAO;AAAA,QACP,QAAQ;AAAA,QACR,SAAS;AAAA,QACT,eAAe;AAAA,QACf,gBAAgB;AAAA,QAChB,YAAY;AAAA,QACZ,OAAO;AAAA,QACP,YAAY;AAAA,QACZ,SAAS;AAAA,MACX;AAAA,MAEA;AAAA,6BAAC,SAAI,OAAO,EAAE,SAAS,QAAQ,gBAAgB,iBAAiB,UAAU,IAAI,OAAO,UAAU,GAC7F;AAAA,8BAAC,SAAK,mBAAQ;AAAA,UACd,oBAAC,SAAI,wBAAU;AAAA,WACjB;AAAA,QACA,qBAAC,SAAI,OAAO,EAAE,SAAS,QAAQ,KAAK,IAAI,YAAY,WAAW,GAC7D;AAAA,+BAAC,SAAI,OAAO,EAAE,SAAS,QAAQ,eAAe,UAAU,KAAK,IAAI,OAAO,MAAM,GAC5E;AAAA,gCAAC,SAAI,OAAO,EAAE,UAAU,IAAI,YAAY,MAAM,YAAY,IAAI,GAAI,iBAAM;AAAA,YACxE,oBAAC,SAAI,OAAO,EAAE,UAAU,IAAI,YAAY,KAAK,OAAO,UAAU,GAAI,uBAAY;AAAA,aAChF;AAAA,UACA,oBAAC,SAAI,OAAO,EAAE,MAAM,GAAG,QAAQ,KAAK,YAAY,qBAAqB,YAAY,UAAU,GAAG;AAAA,WAChG;AAAA;AAAA;AAAA,EACF;AAEJ;;;ACfM,SACE,OAAAA,MADF,QAAAC,aAAA;AAbC,SAAS,cAAc,EAAE,SAAS,OAAO,YAAY,GAAuB;AACjF,SACE,gBAAAA;AAAA,IAAC;AAAA;AAAA,MACC,O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ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { c as OG_IMAGE_SIZES, d as OgField, a as OgFields, e as OgImageSize, O as OgImageSizeInput, f as OgImageSizeName, b as OgImageUrlParams, g as buildOgImageUrl, r as resolveOgImageSize } from './sizes-Bn3i71Bz.js';
|
|
2
|
+
export { DEFAULT_OG_PARAM_MAX, NormalizeOgParamsOptions, NormalizeTextOptions, OgParamError, OgSearchParamsInput, normalizeOgParams, normalizeText } from './core/index.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// src/core/image-url.ts
|
|
2
|
+
function buildOgImageUrl(route, params = {}) {
|
|
3
|
+
const routeText = route.toString();
|
|
4
|
+
const hashIndex = routeText.indexOf("#");
|
|
5
|
+
const withoutHash = hashIndex >= 0 ? routeText.slice(0, hashIndex) : routeText;
|
|
6
|
+
const hash = hashIndex >= 0 ? routeText.slice(hashIndex) : "";
|
|
7
|
+
const queryIndex = withoutHash.indexOf("?");
|
|
8
|
+
const base = queryIndex >= 0 ? withoutHash.slice(0, queryIndex) : withoutHash;
|
|
9
|
+
const existingQuery = queryIndex >= 0 ? withoutHash.slice(queryIndex + 1) : "";
|
|
10
|
+
const searchParams = new URLSearchParams(existingQuery);
|
|
11
|
+
for (const [key, value] of Object.entries(params)) {
|
|
12
|
+
if (value == null) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
searchParams.delete(key);
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
for (const item of value) {
|
|
18
|
+
searchParams.append(key, String(item));
|
|
19
|
+
}
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
searchParams.set(key, String(value));
|
|
23
|
+
}
|
|
24
|
+
const query = searchParams.toString();
|
|
25
|
+
return `${base}${query ? `?${query}` : ""}${hash}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/core/text.ts
|
|
29
|
+
function normalizeText(value, options = {}) {
|
|
30
|
+
const collapseWhitespace = options.collapseWhitespace ?? true;
|
|
31
|
+
const trimmed = value.trim();
|
|
32
|
+
const normalized = collapseWhitespace ? trimmed.replace(/\s+/g, " ") : trimmed;
|
|
33
|
+
if (typeof options.max === "number") {
|
|
34
|
+
return normalized.slice(0, options.max);
|
|
35
|
+
}
|
|
36
|
+
return normalized;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/core/params.ts
|
|
40
|
+
var DEFAULT_OG_PARAM_MAX = 512;
|
|
41
|
+
var OgParamError = class extends Error {
|
|
42
|
+
constructor(message) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.name = "OgParamError";
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
function normalizeOgParams(input, options) {
|
|
48
|
+
const searchParams = toURLSearchParams(input);
|
|
49
|
+
const output = {};
|
|
50
|
+
for (const key of Object.keys(options.defaults)) {
|
|
51
|
+
const field = options.fields?.[key];
|
|
52
|
+
const rawValue = searchParams.get(key);
|
|
53
|
+
const fallback = field?.fallback ?? options.defaults[key] ?? "";
|
|
54
|
+
const max = resolveMax(field?.max);
|
|
55
|
+
const normalizedValue = rawValue == null ? "" : normalizeText(rawValue.slice(0, max), { collapseWhitespace: field?.collapseWhitespace });
|
|
56
|
+
if (normalizedValue.length > 0) {
|
|
57
|
+
output[key] = applyMax(normalizedValue, max);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const normalizedFallback = normalizeText(fallback, {
|
|
61
|
+
collapseWhitespace: field?.collapseWhitespace,
|
|
62
|
+
max: resolveMax(field?.max)
|
|
63
|
+
});
|
|
64
|
+
if (normalizedFallback.length > 0) {
|
|
65
|
+
output[key] = normalizedFallback;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (field?.required) {
|
|
69
|
+
throw new OgParamError(`Missing required OG image field: ${key}`);
|
|
70
|
+
}
|
|
71
|
+
output[key] = "";
|
|
72
|
+
}
|
|
73
|
+
return output;
|
|
74
|
+
}
|
|
75
|
+
function applyMax(value, max) {
|
|
76
|
+
return value.slice(0, resolveMax(max));
|
|
77
|
+
}
|
|
78
|
+
function resolveMax(max) {
|
|
79
|
+
if (max === void 0) {
|
|
80
|
+
return DEFAULT_OG_PARAM_MAX;
|
|
81
|
+
}
|
|
82
|
+
return Number.isSafeInteger(max) && max > 0 ? max : DEFAULT_OG_PARAM_MAX;
|
|
83
|
+
}
|
|
84
|
+
function toURLSearchParams(input) {
|
|
85
|
+
if (input instanceof URLSearchParams) {
|
|
86
|
+
return input;
|
|
87
|
+
}
|
|
88
|
+
const params = new URLSearchParams();
|
|
89
|
+
if (isIterablePairs(input)) {
|
|
90
|
+
for (const [key, value] of input) {
|
|
91
|
+
params.append(key, value);
|
|
92
|
+
}
|
|
93
|
+
return params;
|
|
94
|
+
}
|
|
95
|
+
for (const [key, value] of Object.entries(input)) {
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
for (const item of value) {
|
|
98
|
+
params.append(key, item);
|
|
99
|
+
}
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (value != null) {
|
|
103
|
+
params.set(key, value);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return params;
|
|
107
|
+
}
|
|
108
|
+
function isIterablePairs(input) {
|
|
109
|
+
return Symbol.iterator in Object(input);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/core/sizes.ts
|
|
113
|
+
var OG_IMAGE_SIZES = {
|
|
114
|
+
og: { width: 1200, height: 630 },
|
|
115
|
+
twitter: { width: 1200, height: 675 },
|
|
116
|
+
square: { width: 1200, height: 1200 }
|
|
117
|
+
};
|
|
118
|
+
function resolveOgImageSize(size = "og") {
|
|
119
|
+
if (typeof size === "string") {
|
|
120
|
+
return OG_IMAGE_SIZES[size];
|
|
121
|
+
}
|
|
122
|
+
return size;
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
DEFAULT_OG_PARAM_MAX,
|
|
126
|
+
OG_IMAGE_SIZES,
|
|
127
|
+
OgParamError,
|
|
128
|
+
buildOgImageUrl,
|
|
129
|
+
normalizeOgParams,
|
|
130
|
+
normalizeText,
|
|
131
|
+
resolveOgImageSize
|
|
132
|
+
};
|
|
133
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/image-url.ts","../src/core/text.ts","../src/core/params.ts","../src/core/sizes.ts"],"sourcesContent":["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"],"mappings":";AAKO,SAAS,gBAAgB,OAAqB,SAA2B,CAAC,GAAW;AAC1F,QAAM,YAAY,MAAM,SAAS;AACjC,QAAM,YAAY,UAAU,QAAQ,GAAG;AACvC,QAAM,cAAc,aAAa,IAAI,UAAU,MAAM,GAAG,SAAS,IAAI;AACrE,QAAM,OAAO,aAAa,IAAI,UAAU,MAAM,SAAS,IAAI;AAC3D,QAAM,aAAa,YAAY,QAAQ,GAAG;AAC1C,QAAM,OAAO,cAAc,IAAI,YAAY,MAAM,GAAG,UAAU,IAAI;AAClE,QAAM,gBAAgB,cAAc,IAAI,YAAY,MAAM,aAAa,CAAC,IAAI;AAC5E,QAAM,eAAe,IAAI,gBAAgB,aAAa;AAEtD,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,MAAM,GAAG;AACjD,QAAI,SAAS,MAAM;AACjB;AAAA,IACF;AAEA,iBAAa,OAAO,GAAG;AAEvB,QAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,iBAAW,QAAQ,OAAO;AACxB,qBAAa,OA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K;AACtC;AAIO,SAAS,mBAAmB,OAAyB,MAAmB;AAC7E,MAAI,OAAO,SAAS,UAAU;AAC5B,WAAO,eAAe,IAAI;AAAA,EAC5B;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ReactElement } from 'react';
|
|
2
|
+
import { ImageResponse } from 'next/og.js';
|
|
3
|
+
import { O as OgImageSizeInput, a as OgFields, b as OgImageUrlParams } from '../sizes-Bn3i71Bz.js';
|
|
4
|
+
import { Metadata } from 'next';
|
|
5
|
+
|
|
6
|
+
declare const DEFAULT_CACHE_CONTROL = "public, max-age=0, s-maxage=86400, stale-while-revalidate=604800";
|
|
7
|
+
type CreateOgRouteOptions<T extends Record<string, string>> = {
|
|
8
|
+
size?: OgImageSizeInput | undefined;
|
|
9
|
+
defaults: T;
|
|
10
|
+
fields?: OgFields<T> | undefined;
|
|
11
|
+
headers?: HeadersInit | undefined;
|
|
12
|
+
render: (props: T) => ReactElement;
|
|
13
|
+
};
|
|
14
|
+
type OgRouteRequest = Request;
|
|
15
|
+
declare function createOgRoute<T extends Record<string, string>>(options: CreateOgRouteOptions<T>): (request: OgRouteRequest) => ImageResponse | Response;
|
|
16
|
+
|
|
17
|
+
type CreateSocialMetadataOptions = {
|
|
18
|
+
siteUrl: string;
|
|
19
|
+
siteName: string;
|
|
20
|
+
twitter?: string | undefined;
|
|
21
|
+
imageRoute: string;
|
|
22
|
+
defaultImage?: string | undefined;
|
|
23
|
+
includeDefaultImageFallback?: boolean | undefined;
|
|
24
|
+
imageSize?: OgImageSizeInput | undefined;
|
|
25
|
+
defaultImageSize?: OgImageSizeInput | undefined;
|
|
26
|
+
};
|
|
27
|
+
type WebsiteMetadataInput = {
|
|
28
|
+
title: string;
|
|
29
|
+
description: string;
|
|
30
|
+
path?: string | undefined;
|
|
31
|
+
image?: OgImageUrlParams | undefined;
|
|
32
|
+
imageAlt?: string | undefined;
|
|
33
|
+
};
|
|
34
|
+
type ArticleMetadataInput = WebsiteMetadataInput & {
|
|
35
|
+
publishedTime?: string | undefined;
|
|
36
|
+
modifiedTime?: string | undefined;
|
|
37
|
+
authors?: string[] | undefined;
|
|
38
|
+
};
|
|
39
|
+
declare function createSocialMetadata(options: CreateSocialMetadataOptions): {
|
|
40
|
+
website(input: WebsiteMetadataInput): Metadata;
|
|
41
|
+
article(input: ArticleMetadataInput): Metadata;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { type ArticleMetadataInput, type CreateOgRouteOptions, type CreateSocialMetadataOptions, DEFAULT_CACHE_CONTROL, type OgRouteRequest, type WebsiteMetadataInput, createOgRoute, createSocialMetadata };
|