Files
ipal-kit/src/modules/seo/buildSummaryField.ts
T
Radosław SmolińskiandClaude Opus 5.5 6b46bc0c14 Admin panel in English and Polish, reworded per docs/dev/admin-ux.md
- admin-i18n module: AdminText { en, pl }, component texts under the ipal
  namespace (project translations win), en + pl admin languages when the
  project sets none (project's supportedLanguages wins)
- every plugin label, description, option, tab and group as { en, pl };
  explicit labels instead of auto-labels; sentence case; glossary terms
- visible renames (slugs, names, data unchanged): Cookie banner, Form
  messages, Privacy group, Search engines, Maintenance, Spam protection,
  E-mail sending, stats nav link Overview (hidden without access)
- Polish visitor defaults by page locale: cookie banner, form messages,
  validators, lightbox, accessibility widget; {field} uses the label
- fixes: e-mail status banner used Turnstile wording; developer words and
  env var names out of editor messages; stats numbers/dates in the admin
  language; test e-mail no longer says ipal-kit
- test/adminUx.test.ts: length limits, en + pl present, no developer words

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 15:46:25 +01:00

91 lines
3.3 KiB
TypeScript

import type { Field, FieldHook, TextareaField } from 'payload'
import type { AdminText } from '../admin-i18n/define.js'
import { blocksToMarkdown } from '../content/blocksToMarkdown.js'
import { mdToPlain, truncate } from './plainText.js'
import { isAutoFillSkipped } from './skipAutoFill.js'
type BuildSummaryFieldArgs = {
/** Auto-fill from page content when the editor leaves it empty. Default true. */
autoFill?: boolean
/** Help text under the field: a string or `{ en, pl }`. */
description?: AdminText | string
/** Sidebar label: a string or `{ en, pl }`. Default "AI summary". */
label?: AdminText | string
/** Block layout field to derive the summary from. Default 'layout'. */
layoutField?: string
/** Target length for the auto-generated summary. Default 300. */
maxLength?: number
/** Field name. Default 'aiSummary'. */
name?: string
/** Extend/override the generated textarea (position, access…). */
overrides?: Partial<TextareaField>
}
/**
* A localized `aiSummary` textarea — a concise, human-written (or auto-derived)
* description of the page, richer than the SEO meta description. It's the
* preferred description for AI-facing surfaces: pass `summaryField: 'aiSummary'`
* to buildLlmsTxt and searchContent and they'll use it over meta.description.
*
* With `autoFill` (default), an empty summary is filled on save from the page's
* own content (blocks → plain text, trimmed to ~maxLength). Editors can always
* overwrite it — auto-fill only runs when the field is blank, never clobbers a
* manual summary. Derived from the page's OWN content, so no hard-coded data.
*
* // collections/Pages.ts
* import { buildSummaryField } from '@intecion/ipal-kit'
* fields: [ /* … *\/ buildSummaryField() ]
*
* // then, richer AI descriptions:
* buildLlmsTxt({ …, summaryField: 'aiSummary' })
* createSearchRoute({ …, summaryField: 'aiSummary' })
*/
export function buildSummaryField(args: BuildSummaryFieldArgs = {}): Field {
const {
name = 'aiSummary',
autoFill = true,
description = {
en: 'Short summary for AI assistants; leave empty to create it from the page content.',
pl: 'Krótkie podsumowanie dla asystentów AI; zostaw puste, aby utworzyć je z treści strony.',
},
label = { en: 'AI summary', pl: 'Podsumowanie dla AI' },
layoutField = 'layout',
maxLength = 300,
overrides = {},
} = args
const autofillHook: FieldHook = ({ context, data, value }) => {
if (isAutoFillSkipped(context)) {return value}
if (typeof value === 'string' && value.trim()) {return value} // never clobber a manual summary
if (!data || typeof data !== 'object') {return value}
const layout = (data as Record<string, unknown>)[layoutField]
const md = blocksToMarkdown(layout)
if (!md) {return value}
const plain = mdToPlain(md)
return plain ? truncate(plain, maxLength) : value
}
const { admin: adminOverrides, hooks: hooksOverrides, ...restOverrides } = overrides
return {
name,
type: 'textarea',
admin: {
description,
position: 'sidebar',
...adminOverrides,
},
hooks: {
...hooksOverrides,
...(autoFill
? { beforeChange: [autofillHook, ...(hooksOverrides?.beforeChange ?? [])] }
: {}),
},
label,
localized: true,
...restOverrides,
} as TextareaField
}