- Forms: step and checkboxGroup field types (radio, date on), server validation for them, useBuilderForm (logic) + BuilderForm (unstyled default) + RenderForm (/rsc: rich text, bound action, public part of the form only — the Emails tab never reaches the browser) - Notifications: next / back / stepOf texts - createContentHelpers: getSiteShell, getFormTexts, getLinkOptions, resolveLink, resolveNav, resolvePageRedirect, one-line route handlers (llms, llms-full, md, mcp, health, indexnow, manifest) - /rsc: SiteProviders, createPageRoute, createNotFoundPage - resolveLink / resolveNav / homeSlugFrom, buildSectionHeaderField / buildSectionSettingsField - Dev sandbox frontend built on these imports; tests (unit + real Payload) Co-Authored-By: Claude Opus 5.5 <[email protected]>
78 lines
3.3 KiB
TypeScript
78 lines
3.3 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
|
|
}
|