47 lines
1.7 KiB
JavaScript
47 lines
1.7 KiB
JavaScript
/**
|
|
* 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) {
|
|
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".
|
|
*
|
|
* 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 = {}) {
|
|
const titleField = mapping.title ?? 'title';
|
|
const descriptionField = mapping.description;
|
|
return ({ data })=>{
|
|
if (!data) {
|
|
return data;
|
|
}
|
|
// plugin-seo stores meta as a group under `meta`
|
|
const meta = data.meta ?? {};
|
|
// Fill meta.title from the document title when empty
|
|
if (!readString(meta.title)) {
|
|
const sourceTitle = readString(data[titleField]);
|
|
if (sourceTitle) {
|
|
meta.title = sourceTitle;
|
|
}
|
|
}
|
|
// Fill meta.description from a configured field when empty
|
|
if (descriptionField && !readString(meta.description)) {
|
|
const sourceDescription = readString(data[descriptionField]);
|
|
if (sourceDescription) {
|
|
meta.description = sourceDescription;
|
|
}
|
|
}
|
|
data.meta = meta;
|
|
return data;
|
|
};
|
|
}
|
|
|
|
//# sourceMappingURL=autoFillMeta.js.map
|