Files
ipal-kit/src/modules/seo/titleSeparator.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

92 lines
3.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { BasePayload } from 'payload'
/**
* Site Settings → Title separator: stored value → the character in the title.
*
* Stored values are names, not the characters: select values become GraphQL
* enum names, and '|', '–', '·' are not valid ones ('-' and '/' both turn into
* '_'), which made /api/graphql answer 500 for every site up to 1.14.4. Those
* releases stored the character itself; it is still understood everywhere.
*/
export const TITLE_SEPARATORS = {
endash: '–',
hyphen: '-',
middot: '·',
pipe: '|',
slash: '/',
} as const
export type TitleSeparatorName = keyof typeof TITLE_SEPARATORS
const NAME_OF_CHAR: Record<string, TitleSeparatorName> = Object.fromEntries(
Object.entries(TITLE_SEPARATORS).map(([name, char]) => [char, name as TitleSeparatorName]),
)
/**
* Stored value → separator character. Names map to their character, a legacy
* stored character is returned as is, and any other value (a project's own
* option) is used literally. Empty → undefined.
*/
export function titleSeparatorChar(value: unknown): string | undefined {
if (typeof value !== 'string' || !value.trim()) {return undefined}
return (TITLE_SEPARATORS as Record<string, string>)[value] ?? value
}
/**
* Legacy stored character → its name; everything else unchanged. The field's
* afterRead/beforeChange hooks use it, so documents saved before the rename open
* and save without "invalid selection" before anyone touches them.
*/
export function titleSeparatorName(value: unknown): unknown {
return typeof value === 'string' && NAME_OF_CHAR[value] ? NAME_OF_CHAR[value] : value
}
type MongoGlobals = {
collection?: {
updateOne: (filter: Record<string, unknown>, update: Record<string, unknown>) => Promise<{ modifiedCount?: number }>
}
}
/**
* MongoDB: rewrites a legacy stored character to its name, once per start (a
* no-op once done). SQL adapters change the column in a migration — see
* TITLE_SEPARATOR_SQL and docs/seo.md.
*/
export async function migrateTitleSeparator(payload: BasePayload, settingsSlug = 'site-settings'): Promise<void> {
if (payload.db.name !== 'mongoose') {return}
const globals = (payload.db as unknown as { globals?: MongoGlobals }).globals
if (!globals?.collection) {return}
try {
for (const [char, name] of Object.entries(NAME_OF_CHAR)) {
const result = await globals.collection.updateOne({ globalType: settingsSlug, titleSeparator: char }, { $set: { titleSeparator: name } })
if (result.modifiedCount) {
payload.logger.info(`[iapl] Site Settings: title separator '${char}' stored as '${name}' (GraphQL-safe value).`)
}
}
} catch (error) {
payload.logger.error({ err: error, msg: '[iapl] Site Settings: could not migrate the title separator' })
}
}
/**
* SQL that rewrites legacy separator characters to names (`direction: 'up'`) or
* back (`'down'`), for a Postgres or SQLite migration. Postgres: the generated
* migration turns the column into text, recreates the enum and casts back —
* insert this right after `SET DATA TYPE text;`, or the cast fails on '|':
*
* ALTER TABLE "site_settings" ALTER COLUMN "title_separator" SET DATA TYPE text;
* ${sql.raw(titleSeparatorSql())}
*
* Table/column default to Payload's names for the plugin's Site Settings.
*/
export function titleSeparatorSql({
column = 'title_separator',
direction = 'up',
table = 'site_settings',
}: { column?: string; direction?: 'down' | 'up'; table?: string } = {}): string {
const pairs = Object.entries(NAME_OF_CHAR).map(([char, name]) => (direction === 'up' ? [char, name] : [name, char]))
const cases = pairs.map(([from, to]) => `WHEN '${from}' THEN '${to}'`).join(' ')
const from = pairs.map(([f]) => `'${f}'`).join(', ')
return `UPDATE "${table}" SET "${column}" = CASE "${column}" ${cases} ELSE "${column}" END WHERE "${column}" IN (${from});`
}