import type { Field } from 'payload' import type { AdminText } from '../admin-i18n/index.js' import type { AiBotRule, AiBotsPolicy, RobotsOptions } from './buildRobots.js' /** * Site settings → Search engines: what editors set for search engines and AI * assistants without a deploy — Search Console / Bing verification codes, the * AI crawler policy for robots.txt, and a note for llms.txt. Empty fields * change nothing: verification tags appear only once a code is pasted, and the * robots policy stays the one set in code until an editor picks another. */ export const SEARCH_VERIFICATION_FIELD = 'searchVerification' export const AI_CRAWLERS_FIELD = 'aiCrawlers' export const LLMS_INTRO_FIELD = 'llmsIntro' /** Next `Metadata.verification` — site ownership tags for search consoles. */ export type VerificationMetadata = { google?: string other?: Record yandex?: string } /** * Accepts the bare code or the whole tag an editor copies from Search Console * (``) and keeps only * the code. Anything that isn't a plain token is dropped — the value ends up * in every page's . */ export function normalizeVerificationToken(raw: unknown): string { if (typeof raw !== 'string') {return ''} let value = raw.trim() const content = /content\s*=\s*["']([^"']*)["']/i.exec(value) if (content) {value = content[1].trim()} value = value.replace(/^["']|["']$/g, '') return /^[\w\-.:+/=]{1,200}$/.test(value) ? value : '' } const tokenField = (name: string, product: string, description: AdminText): Field => ({ name, type: 'text', admin: { description }, hooks: { beforeChange: [({ value }) => normalizeVerificationToken(value) || null] }, // A product name reads the same in every language. label: { en: product, pl: product }, }) const AI_POLICY_OPTIONS: { label: AdminText; value: string }[] = [ { label: { en: 'Default setting', pl: 'Ustawienie domyślne' }, value: 'default' }, { label: { en: 'Allow all, including AI training', pl: 'Zezwól na wszystko, także trening AI' }, value: 'allow' }, { label: { en: 'Allow assistants, block training', pl: 'Zezwól asystentom, blokuj trening' }, value: 'block-training' }, { label: { en: 'Block all AI services', pl: 'Blokuj wszystkie usługi AI' }, value: 'disallow' }, ] /** * The "Search engines" tab fields. Not localized, except the llms.txt note. * Order (admin-ux §7): the note (content) first, then verification codes and * the AI access policy with its per-bot rules. */ export const searchSettingsFields: Field[] = [ { name: LLMS_INTRO_FIELD, type: 'textarea', admin: { description: { en: 'Key facts AI assistants may quote: what you do, where, for whom and since when.', pl: 'Najważniejsze fakty, które asystenci AI mogą cytować: czym zajmuje się firma, gdzie, dla kogo i od kiedy.', }, }, label: { en: 'Site summary (llms.txt)', pl: 'Opis witryny (llms.txt)' }, localized: true, }, { name: SEARCH_VERIFICATION_FIELD, type: 'group', admin: { description: { en: 'Paste the code or the whole meta tag from each tool; leave empty if you verify by DNS.', pl: 'Wklej kod lub cały znacznik meta z narzędzia; zostaw puste przy weryfikacji przez DNS.', }, }, fields: [ tokenField('google', 'Google Search Console', { en: 'Find it under Settings, Ownership verification, HTML tag.', pl: 'Kod znajduje się w: Ustawienia, Weryfikacja własności, Tag HTML.', }), tokenField('bing', 'Bing Webmaster Tools', { en: 'Find it under Add site, HTML Meta Tag; Bing also feeds ChatGPT Search and Copilot.', pl: 'Kod znajduje się w: Dodaj witrynę, Tag meta HTML; z Bing korzystają też ChatGPT Search i Copilot.', }), tokenField('yandex', 'Yandex Webmaster', { en: 'Find it under Site verification, Meta tag.', pl: 'Kod znajduje się w: Weryfikacja witryny, Tag meta.', }), ], label: { en: 'Site verification', pl: 'Weryfikacja witryny' }, }, { name: AI_CRAWLERS_FIELD, type: 'group', admin: { description: { en: 'Which AI services may read the site; blocking assistants removes it from AI answers.', pl: 'Które usługi AI mogą czytać witrynę; zablokowanie asystentów usuwa ją z odpowiedzi AI.', }, }, fields: [ { name: 'policy', type: 'select', defaultValue: 'default', label: { en: 'Access', pl: 'Dostęp' }, options: AI_POLICY_OPTIONS.map((o) => ({ ...o })), }, { name: 'rules', type: 'array', admin: { description: { en: 'Exceptions for single bots by name, e.g. GPTBot; they override the setting above.', pl: 'Wyjątki dla pojedynczych botów według nazwy, np. GPTBot; mają pierwszeństwo przed ustawieniem powyżej.', }, initCollapsed: true, }, fields: [ { type: 'row', fields: [ { name: 'userAgent', type: 'text', admin: { placeholder: 'GPTBot' }, label: { en: 'Bot name', pl: 'Nazwa bota' }, required: true, }, { name: 'access', type: 'select', defaultValue: 'disallow', label: { en: 'Access', pl: 'Dostęp' }, options: [ { label: { en: 'Allow', pl: 'Zezwól' }, value: 'allow' }, { label: { en: 'Block', pl: 'Blokuj' }, value: 'disallow' }, ], required: true, }, ], }, ], label: { en: 'Bot rules', pl: 'Reguły dla botów' }, labels: { plural: { en: 'Rules', pl: 'Reguły' }, singular: { en: 'Rule', pl: 'Reguła' } }, }, ], label: { en: 'AI assistants', pl: 'Asystenci AI' }, }, ] type SettingsLike = null | Record | undefined /** Site Settings → verification codes as Next `Metadata.verification`; undefined when none is set. */ export function verificationFrom(settings: SettingsLike): undefined | VerificationMetadata { const group = settings?.[SEARCH_VERIFICATION_FIELD] as null | Record | undefined if (!group || typeof group !== 'object') {return undefined} const google = normalizeVerificationToken(group.google) const bing = normalizeVerificationToken(group.bing) const yandex = normalizeVerificationToken(group.yandex) if (!google && !bing && !yandex) {return undefined} return { ...(google ? { google } : {}), ...(yandex ? { yandex } : {}), ...(bing ? { other: { 'msvalidate.01': bing } } : {}), } } const POLICIES = new Set(['allow', 'block-training', 'disallow']) /** * Site Settings → AI crawlers as buildRobots options. Only what the editor * changed: policy 'default' keeps the option from code, no rules adds none. */ export function robotsOptionsFrom(settings: SettingsLike): Pick { const group = settings?.[AI_CRAWLERS_FIELD] as { policy?: unknown; rules?: unknown } | null | undefined if (!group || typeof group !== 'object') {return {}} const policy = typeof group.policy === 'string' && POLICIES.has(group.policy) ? (group.policy as AiBotsPolicy) : undefined const rules: AiBotRule[] = Array.isArray(group.rules) ? group.rules.flatMap((r: unknown) => { const row = r as { access?: unknown; userAgent?: unknown } | null // A user agent is one token — no spaces, colons or newlines that could // inject other robots.txt lines. const ua = typeof row?.userAgent === 'string' ? row.userAgent.trim() : '' if (!/^[\w\-.*/]{1,100}$/.test(ua)) {return []} return [{ access: row?.access === 'allow' ? ('allow' as const) : ('disallow' as const), userAgent: ua }] }) : [] return { ...(policy ? { aiBots: policy } : {}), ...(rules.length ? { aiBotRules: rules } : {}), } } /** Site Settings → llms.txt note in the locale it was read in. Empty string when unset. */ export function llmsIntroFrom(settings: SettingsLike): string { const v = settings?.[LLMS_INTRO_FIELD] return typeof v === 'string' ? v.trim() : '' }