import { DEFAULT_LOCALE, ENABLED_LOCALES, alternatePathForLocale, getLocaleHreflang, getLocaleOgLocale, isLocale, stripLocaleFromPath, type Locale, } from '@anglefeint/site-i18n/config'; export function resolveCurrentLocale(pathname: string): string { const pathParts = pathname.split('/').filter(Boolean); return pathParts.length > 0 && isLocale(pathParts[0]) ? pathParts[0] : DEFAULT_LOCALE; } function resolveAbsoluteHref(href: string, siteURL: URL): string { return new URL(href, siteURL).toString(); } export function buildHeadLocaleState( pathname: string, siteURL: URL, localeHrefs?: Partial>, seoLocaleHrefs?: Partial> ) { const currentLocale = resolveCurrentLocale(pathname); const localeSubpath = stripLocaleFromPath(pathname, currentLocale); // An explicit SEO map is authoritative, including an empty map. Menu fallbacks // must never manufacture translations that the route did not supply. const alternateLocales = seoLocaleHrefs === undefined ? ENABLED_LOCALES : ENABLED_LOCALES.filter((locale) => Boolean(seoLocaleHrefs[locale])); const alternatePaths = alternateLocales.map((locale) => ({ locale, hreflang: getLocaleHreflang(locale), href: resolveAbsoluteHref( seoLocaleHrefs === undefined ? (localeHrefs?.[locale] ?? alternatePathForLocale(locale, localeSubpath)) : seoLocaleHrefs[locale]!, siteURL ), })); const xDefaultPath = seoLocaleHrefs === undefined ? (localeHrefs?.[DEFAULT_LOCALE] ?? alternatePathForLocale(DEFAULT_LOCALE, localeSubpath)) : alternateLocales.includes(DEFAULT_LOCALE) ? seoLocaleHrefs[DEFAULT_LOCALE] : undefined; return { currentLocale, alternatePaths, xDefaultHref: xDefaultPath ? resolveAbsoluteHref(xDefaultPath, siteURL) : undefined, ogLocale: getLocaleOgLocale(currentLocale), ogLocaleAlternates: alternateLocales .filter((locale) => locale !== currentLocale) .map((locale) => getLocaleOgLocale(locale)) .filter(Boolean), }; } export function buildJsonLdSchemas({ inLanguage, siteTitle, siteAuthor, siteURL, title, description, imageURL, canonicalURL, isArticle, author, authorURL, publishedTime, modifiedTime, tags, schema, }: { inLanguage?: string; siteTitle: string; siteAuthor: string; siteURL: URL; title: string; description: string; imageURL: string; canonicalURL: URL; isArticle: boolean; author: string; authorURL?: string; publishedTime?: Date; modifiedTime?: Date; tags?: string[]; schema?: Record | Record[]; }) { const websiteSchema = { inLanguage, '@context': 'https://schema.org', '@type': 'WebSite', name: siteTitle, url: siteURL.toString(), description, publisher: { '@type': 'Person', name: siteAuthor, }, }; const personSchema = { '@context': 'https://schema.org', '@type': 'Person', name: siteAuthor, url: siteURL.toString(), }; const articleSchema = isArticle ? { '@context': 'https://schema.org', '@type': 'BlogPosting', inLanguage, headline: title, description, image: [imageURL], author: { '@type': 'Person', name: author, ...(authorURL ? { url: authorURL } : {}), }, publisher: { '@type': 'Person', name: siteAuthor, }, mainEntityOfPage: canonicalURL.toString(), datePublished: publishedTime?.toISOString(), dateModified: (modifiedTime ?? publishedTime)?.toISOString(), keywords: tags?.length ? tags.join(', ') : undefined, } : undefined; const extraSchemas = schema ? (Array.isArray(schema) ? schema : [schema]) : []; return [websiteSchema, personSchema, articleSchema, ...extraSchemas].filter(Boolean); }