Mostly perfectionist import/key sorting and curly braces from --fix, plus: - spread instead of .apply() in the Meta Pixel stub - non-capturing group in the locale-code pattern - React 19 use(Context) / <Context value> in TurnstileProvider - email regex rewritten without super-linear backtracking (covered by a test) - console.warn/error allowed (deliberate server diagnostics) - test/tsconfig.json so the project service and tsc see test files Co-Authored-By: Claude Opus 5.5 <[email protected]>
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
/** Which part comes first in a composed title. */
|
||
export type TitleOrder = 'page-first' | 'site-first'
|
||
|
||
type ComposeTitleArgs = {
|
||
/** Defaults to 'page-first' — the page title is what a visitor scans for. */
|
||
order?: TitleOrder
|
||
/** Page-specific title, e.g. 'About Us'. */
|
||
pageTitle?: null | string
|
||
/** Separator between page title and site name. Defaults to ' | '. */
|
||
separator?: string
|
||
/** Site name, e.g. 'Acme Inc'. */
|
||
siteName?: null | string
|
||
}
|
||
|
||
/**
|
||
* Composes a full document title from a page title and the site name.
|
||
*
|
||
* - both, page-first: "About Us | Acme Inc"
|
||
* - both, site-first: "Acme Inc | About Us"
|
||
* - page only: "About Us"
|
||
* - site only: "Acme Inc"
|
||
* - neither: ""
|
||
*
|
||
* Pure function — no dependency on Payload or request state.
|
||
*/
|
||
export function composeTitle({
|
||
order = 'page-first',
|
||
pageTitle,
|
||
separator = ' | ',
|
||
siteName,
|
||
}: ComposeTitleArgs): string {
|
||
const page = pageTitle?.trim()
|
||
const site = siteName?.trim()
|
||
|
||
if (page && site) {
|
||
// Dedup: if the page title already ends with the site name (an editor wrote
|
||
// "Budowa auta – RCC" into meta.title), don't append it again → avoid
|
||
// "Budowa auta – RCC – RCC". Match the site name at the end, with or without
|
||
// the separator before it.
|
||
const sep = separator.trim()
|
||
if (page === site || page.endsWith(`${sep} ${site}`) || page.endsWith(` ${site}`)) {
|
||
return page
|
||
}
|
||
return order === 'site-first' ? `${site}${separator}${page}` : `${page}${separator}${site}`
|
||
}
|
||
return page || site || ''
|
||
}
|