Files
ipal-kit/src/modules/seo/seoPluginConfig.ts
T
Radosław SmolińskiandClaude Fable 5.2 0a4f001797 IAPL rename, scripted: preferred names in src, bin, scripts, services, tests; bin files and skill folders moved
Replaced by an explicit list of forms only (ten exported names, runtime env names, project file
names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import
paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are
not touched. package.json keeps the five old bin names beside the five new ones.
The old names stop working in this commit and come back as aliases in the next; docs follow.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 17:24:21 +01:00

115 lines
4.2 KiB
TypeScript

import type { Field, Plugin } from 'payload'
import { seoPlugin } from '@payloadcms/plugin-seo'
import type { AdminText } from '../admin-i18n/define.js'
import type { SeoOption } from './types.js'
type BuildSeoPluginArgs = {
seo: SeoOption
/** Upload collection slug for the meta image. Default `seo.uploadsCollection`, then 'media'. */
uploadsCollection?: string
}
/**
* Escape hatch from automatic title composition: whatever is typed here becomes
* the entire title — no site name, no separator. Useful on a home page, where
* composing would produce "Intecion Software | Intecion Software".
*/
const titleOverrideField: Field = {
name: 'titleOverride',
type: 'text',
admin: {
description: {
en: 'Replaces the whole title, site name included; leave empty to build it automatically.',
pl: 'Zastępuje cały tytuł razem z nazwą witryny; zostaw puste, aby utworzyć go automatycznie.',
},
},
label: { en: 'Exact page title', pl: 'Dokładny tytuł strony' },
localized: true,
}
/** Panel wording for plugin-seo's own fields (labels only; names and data unchanged). */
const SEO_FIELD_TEXTS: Record<string, { description?: AdminText; label: AdminText }> = {
description: { label: { en: 'Description', pl: 'Opis' } },
image: {
description: {
en: 'Shown when the page is shared; best under 500 KB.',
pl: 'Widoczny przy udostępnianiu strony; najlepiej poniżej 500 KB.',
},
label: { en: 'Share image', pl: 'Obraz udostępniania' },
},
overview: { label: { en: 'Overview', pl: 'Przegląd' } },
preview: { label: { en: 'Preview', pl: 'Podgląd' } },
title: { label: { en: 'Page title', pl: 'Tytuł strony' } },
}
function withSeoLabels(fields: Field[]): Field[] {
return fields.map((field) => {
const texts = 'name' in field ? SEO_FIELD_TEXTS[field.name] : undefined
if (!texts) {return field}
const admin = (field as { admin?: Record<string, unknown> }).admin
return {
...field,
...(texts.description ? { admin: { ...admin, description: texts.description } } : {}),
label: texts.label,
} as Field
})
}
/**
* Admin hint right under meta.description (MetaDescriptionCheck): empty,
* same as the site-wide description, or same as another document's.
*/
function descriptionCheckField(seo: SeoOption): Field | null {
if (seo.descriptionCheck === false) {return null}
const autoFill =
seo.autoFill !== false && (seo.autoFill?.content !== false || Boolean(seo.autoFill?.description))
return {
name: 'descriptionCheck',
type: 'ui',
admin: {
components: {
Field: {
clientProps: {
autoFill,
titleField: (seo.autoFill && seo.autoFill.title) || 'title',
...(seo.descriptionCheck ?? {}),
},
path: '@intecion/ipal-kit/admin#MetaDescriptionCheck',
},
},
},
}
}
/** Puts `extra` right after the description field (end of the group if it's missing). */
function afterDescription(fields: Field[], extra: Field | null): Field[] {
if (!extra) {return fields}
const at = fields.findIndex((f) => 'name' in f && f.name === 'description')
return at === -1 ? [...fields, extra] : [...fields.slice(0, at + 1), extra, ...fields.slice(at + 1)]
}
/**
* Configures @payloadcms/plugin-seo from IAPL's SeoOption.
*
* Adds the `meta` field group (title, description, image) to the client's
* chosen collections. tabbedUI is intentionally NOT used — it merges tabs by
* inspecting the first field, which breaks when other plugins/fields (roles,
* slug) already sit in the collection, leaving an empty SEO tab. Instead the
* SEO UI fields are injected into a controlled tab by injectSeoTabs.
*/
export function buildSeoPlugin({ seo, uploadsCollection = seo.uploadsCollection ?? 'media' }: BuildSeoPluginArgs): Plugin {
return seoPlugin({
collections: seo.collections,
fields: ({ defaultFields }) => [
...afterDescription(withSeoLabels(defaultFields), descriptionCheckField(seo)),
titleOverrideField,
...(seo.fields ?? []),
],
uploadsCollection,
...(seo.generateTitle && { generateTitle: seo.generateTitle }),
...(seo.generateDescription && { generateDescription: seo.generateDescription }),
})
}