|
1 | 1 | import type { Collection } from '@nuxt/content' |
2 | 2 | import { z } from 'zod' |
3 | 3 |
|
4 | | -export const ogImageSchema = z.object({ |
5 | | - url: z.string().optional(), |
6 | | - component: z.string().optional(), |
7 | | - props: z.record(z.string(), z.any()), |
8 | | -}).optional() |
| 4 | +function buildOgImageObjectSchema(_z: typeof z) { |
| 5 | + return _z.object({ |
| 6 | + url: _z.string().optional(), |
| 7 | + component: _z.string().optional(), |
| 8 | + props: _z.record(_z.string(), _z.any()), |
| 9 | + }).optional() |
| 10 | +} |
| 11 | + |
| 12 | +const ogImageObjectSchema = buildOgImageObjectSchema(z) |
| 13 | + |
| 14 | +function withEditorHidden<T extends z.ZodTypeAny>(s: T): T { |
| 15 | + // .editor() is patched onto ZodType by @nuxt/content at runtime |
| 16 | + if (typeof (s as any).editor === 'function') |
| 17 | + return (s as any).editor({ hidden: true }) |
| 18 | + return s |
| 19 | +} |
| 20 | + |
| 21 | +export interface DefineOgImageSchemaOptions { |
| 22 | + /** |
| 23 | + * Pass the `z` instance from `@nuxt/content` to ensure `.editor({ hidden: true })` works |
| 24 | + * across Zod versions. When omitted, the bundled `z` is used (`.editor()` applied if available). |
| 25 | + */ |
| 26 | + z?: typeof z |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Define the ogImage schema field for a Nuxt Content collection. |
| 31 | + * |
| 32 | + * @example |
| 33 | + * defineCollection({ |
| 34 | + * type: 'page', |
| 35 | + * source: '**', |
| 36 | + * schema: z.object({ |
| 37 | + * ogImage: defineOgImageSchema() |
| 38 | + * }) |
| 39 | + * }) |
| 40 | + */ |
| 41 | +export function defineOgImageSchema(options?: DefineOgImageSchemaOptions) { |
| 42 | + const s = options?.z ? buildOgImageObjectSchema(options.z) : ogImageObjectSchema |
| 43 | + return withEditorHidden(s) |
| 44 | +} |
| 45 | + |
| 46 | +// Legacy exports |
| 47 | +export const ogImageSchema = ogImageObjectSchema |
9 | 48 |
|
10 | 49 | export const schema = z.object({ |
11 | | - ogImage: ogImageSchema, |
| 50 | + ogImage: withEditorHidden(ogImageObjectSchema), |
12 | 51 | }) |
13 | 52 |
|
| 53 | +/** @deprecated Use `defineOgImageSchema()` in your collection schema instead. See https://nuxtseo.com/og-image/integrations/content */ |
14 | 54 | export function asOgImageCollection<T>(collection: Collection<T>): Collection<T> { |
| 55 | + console.warn('[og-image] `asOgImageCollection()` is deprecated. Use `defineOgImageSchema()` in your collection schema instead. See https://nuxtseo.com/og-image/integrations/content') |
15 | 56 | if (collection.type === 'page') { |
16 | 57 | // @ts-expect-error untyped |
17 | 58 | collection.schema = collection.schema ? schema.extend(collection.schema.shape) : schema |
|
0 commit comments