import type { CollectionBeforeChangeHook } from 'payload' import { blocksToMarkdown } from '../content/blocksToMarkdown.js' import { describeFromMarkdown } from './plainText.js' import { isAutoFillSkipped } from './skipAutoFill.js' /** * Maps document fields to SEO meta fields for auto-fill. * Defaults match the Payload website template (title → meta.title). */ export type AutoFillMapping = { /** * Block layout field to derive meta.description from when it's empty and the * `description` field gives nothing. Default 'layout'; false turns it off. */ content?: false | string /** Document field used to fill meta.description when empty. Tried before `content`. */ description?: string /** Target length of a description derived from `content`. Default 155. */ descriptionLength?: number /** Document field used to fill meta.title when empty. Default: 'title'. */ title?: string } /** * Reads a possibly-localized field value into a plain string. * With localized fields at this hook stage the value is the active-locale * string, so we just coerce defensively. */ function readString(value: unknown): string | undefined { if (typeof value === 'string' && value.trim()) {return value.trim()} return undefined } /** * Builds a beforeChange hook that fills empty SEO meta fields from document * content. Only fills when the meta field is blank — never overwrites what an * editor typed. This runs server-side on every create/update, so editors get * sensible meta without clicking "auto-generate". * * Every page gets its own description: from the `description` field when one * is mapped, otherwise from the page's own blocks. Without it, pages fall back * to the one site-wide description, and Google — seeing the same text on every * page — ignores it and quotes the footer instead. * * The plugin doesn't know the client's collection shape, so the field mapping * is configurable; defaults follow the website template. */ export function buildAutoFillMetaHook(mapping: AutoFillMapping = {}): CollectionBeforeChangeHook { const titleField = mapping.title ?? 'title' const descriptionField = mapping.description const contentField = mapping.content === undefined ? 'layout' : mapping.content const descriptionLength = mapping.descriptionLength ?? 155 return ({ context, data, originalDoc }) => { if (!data || isAutoFillSkipped(context)) {return data} // plugin-seo stores meta as a group under `meta` const meta = (data.meta as Record | undefined) ?? {} // A partial update (REST PATCH, payload.update with a few fields) carries // only what changed — read the rest from the stored document, so a saved // description isn't treated as empty and replaced. const stored = (originalDoc?.meta as Record | undefined) ?? {} const current = (key: string) => (key in meta ? meta[key] : stored[key]) const source = (field: string) => (field in data ? data[field] : originalDoc?.[field]) // Fill meta.title from the document title when empty if (!readString(current('title'))) { const sourceTitle = readString(source(titleField)) if (sourceTitle) {meta.title = sourceTitle} } // Fill meta.description from a configured field, else from the page content if (!readString(current('description'))) { const sourceDescription = (descriptionField && readString(source(descriptionField))) || (contentField && describeFromMarkdown(blocksToMarkdown(source(contentField)), descriptionLength)) || undefined if (sourceDescription) {meta.description = sourceDescription} } if (data.meta !== undefined || Object.keys(meta).length) {data.meta = meta} return data } }