106 lines
4.6 KiB
TypeScript
106 lines
4.6 KiB
TypeScript
import type { LinkValue, ResolveLinkHrefOptions } from './resolveLinkHref.js'
|
|
|
|
import { resolveLinkHref } from './resolveLinkHref.js'
|
|
|
|
/**
|
|
* Everything an <a>/<Link> needs for a panel link — the project keeps the
|
|
* element and the classes (Button variants), the plugin decides the rest:
|
|
* href, new tab (files always open in one), rel, and whether it's internal
|
|
* (render next/link) or not (plain <a>).
|
|
*/
|
|
export type ResolvedLink = {
|
|
/** `link.appearance` from the panel (primary / secondary / link) — map it to your Button classes. */
|
|
appearance?: string
|
|
href: string
|
|
/** Internal page → next/link; external URL / file / mailto → <a>. */
|
|
internal: boolean
|
|
label: string
|
|
newTab: boolean
|
|
/** Spread on the element: target + rel for a new tab, nothing otherwise. */
|
|
targetProps: { rel: string; target: '_blank' } | Record<string, never>
|
|
}
|
|
|
|
/** A panel link → ResolvedLink, or null when it leads nowhere in this locale (no page, no file, unsafe URL). */
|
|
export function resolveLink(link: LinkValue | null | undefined, options: ResolveLinkHrefOptions): null | ResolvedLink {
|
|
const href = resolveLinkHref(link, options)
|
|
if (!link || !href) {return null}
|
|
const newTab = Boolean(link.newTab) || link.type === 'file'
|
|
return {
|
|
...(link.appearance ? { appearance: link.appearance } : {}),
|
|
href,
|
|
internal: (link.type ?? 'internal') === 'internal',
|
|
label: link.label ?? '',
|
|
newTab,
|
|
targetProps: newTab ? { rel: 'noopener noreferrer', target: '_blank' } : {},
|
|
}
|
|
}
|
|
|
|
export type NavItem = {
|
|
/** Nested items (dropdown), already resolved. */
|
|
children: NavItem[]
|
|
} & ResolvedLink
|
|
|
|
export type ResolveNavOptions = {
|
|
/** Field with the nested items inside an item. Default 'subItems'. */
|
|
childrenField?: string
|
|
/** Field with the link inside an item. Default 'link'. */
|
|
linkField?: string
|
|
} & ResolveLinkHrefOptions
|
|
|
|
/**
|
|
* Menu items from a Header/Footer global → plain, serializable items (safe to
|
|
* pass to a client MobileNav). Items whose link leads nowhere in this locale
|
|
* — a deleted or untranslated page — are dropped instead of rendering "#".
|
|
*
|
|
* const items = resolveNav(header.navItems, { locale, config: i18nConfig, homeSlug })
|
|
* // [{ href, label, newTab, internal, targetProps, children: [...] }]
|
|
*
|
|
* Any depth; the item shape is configurable (linkField, childrenField).
|
|
*/
|
|
export function resolveNav(items: unknown, options: ResolveNavOptions): NavItem[] {
|
|
const { childrenField = 'subItems', linkField = 'link', ...linkOptions } = options
|
|
if (!Array.isArray(items)) {return []}
|
|
return items.flatMap((item) => {
|
|
if (!item || typeof item !== 'object') {return []}
|
|
const record = item as Record<string, unknown>
|
|
const link = resolveLink(record[linkField] as LinkValue, linkOptions)
|
|
if (!link || !link.label) {return []}
|
|
return [{ ...link, children: resolveNav(record[childrenField], options) }]
|
|
})
|
|
}
|
|
|
|
/** Homepage slug from SiteSettings (`homepage` relationship, populated) — for links that collapse to /{locale}. */
|
|
export function homeSlugFrom(settings: unknown, field = 'homepage'): string | undefined {
|
|
const home = settings && typeof settings === 'object' ? (settings as Record<string, unknown>)[field] : undefined
|
|
const slug = home && typeof home === 'object' ? (home as { slug?: unknown }).slug : undefined
|
|
return typeof slug === 'string' && slug ? slug : undefined
|
|
}
|
|
|
|
export type SystemLink = { role: string } & ResolvedLink
|
|
|
|
/**
|
|
* Links to the pages assigned in Site Settings → System Pages (privacy policy,
|
|
* cookie policy, terms…) — href AND label from the panel: the label is the
|
|
* page's own title in this locale, so nothing is typed in code and a renamed
|
|
* or translated page updates the footer by itself. Unassigned roles are skipped.
|
|
*
|
|
* resolveSystemLinks(settings, { locale, config, homeSlug }) // privacy, cookies, terms
|
|
* resolveSystemLinks(settings, { …, roles: ['termsOfService'] })
|
|
*
|
|
* `settings` = SiteSettings read with depth ≥ 1 (lib/content getSettings is depth 2).
|
|
*/
|
|
export function resolveSystemLinks(
|
|
settings: unknown,
|
|
{ roles = ['privacyPolicy', 'cookiePolicy', 'termsOfService'], ...options }: { roles?: readonly string[] } & ResolveLinkHrefOptions,
|
|
): SystemLink[] {
|
|
if (!settings || typeof settings !== 'object') {return []}
|
|
const record = settings as Record<string, unknown>
|
|
return roles.flatMap((role) => {
|
|
const page = record[role]
|
|
if (!page || typeof page !== 'object') {return []}
|
|
const title = (page as { title?: unknown }).title
|
|
const link = resolveLink({ type: 'internal', label: typeof title === 'string' ? title : '', page }, options)
|
|
return link && link.label ? [{ ...link, role }] : []
|
|
})
|
|
}
|