import type { I18nConfig } from '../i18n/index.js'
import type { LinkValue } from '../links/resolveLinkHref.js'
import { resolveLinkHref } from '../links/resolveLinkHref.js'
type NavItem = {
/** Visible label (e.g. 'Usługi'). */
name: string
/** Absolute URL (e.g. 'https://example.com/pl/uslugi'). */
url: string
}
/**
* Builds SiteNavigationElement JSON-LD (schema.org) from the main navigation —
* declares the site's primary nav as structured data. A weaker sitelinks signal
* than WebSite/breadcrumbs, but cheap: it tells Google which pages are the main
* navigation targets.
*
* Feed it the SAME nav items the header renders (from the panel/nav global), so
* the structured data matches the visible menu — not a separate hardcoded list.
*
* import { buildSiteNavigationJsonLd } from '@intecion/ipal-kit'
* const jsonLd = buildSiteNavigationJsonLd(
* navItems.map(i => ({ name: i.label, url: `${base}${i.href}` }))
* )
*
*
* Emit once (site-wide, root layout). Data from the nav source, never hardcoded.
* Returns null for empty nav.
*/
export function buildSiteNavigationJsonLd(items: NavItem[]) {
if (!items || items.length === 0) {return null}
return {
'@context': 'https://schema.org',
'@type': 'ItemList',
itemListElement: items.map((item, index) => ({
name: item.name,
'@type': 'SiteNavigationElement',
position: index + 1,
url: item.url,
})),
}
}
type NavSource = Record
type BuildNavigationJsonLdArgs = {
/** Absolute origin, e.g. 'https://acme.pl'. */
baseUrl: string
config: I18nConfig
/** Child items. Default `item.subItems`. Return [] to list top level only. */
getChildren?: (item: NavSource) => unknown
/** Link of an item. Default `item.link` (ipal-nav template, buildLinkField). */
getLink?: (item: NavSource) => LinkValue | null | undefined
/** Homepage slug(s) so the home link collapses to /{locale}. */
homeSlug?: Record | string
/** Menu items straight from the header global (depth ≥ 1, so page links are populated). */
items: null | readonly unknown[] | undefined
locale: string
}
/**
* SiteNavigationElement list straight from the project's header global —
* resolves page links per locale (resolveLinkHref), keeps only links on this
* site (external and file links are skipped), de-duplicates, and labels each
* item with the link label from the panel.
*
* const nav = buildNavigationJsonLd({ items: header.navItems, baseUrl, locale, config: i18nConfig, homeSlug })
*
*/
export function buildNavigationJsonLd({
baseUrl,
config,
getChildren = (item) => item.subItems,
getLink = (item) => item.link as LinkValue | undefined,
homeSlug,
items,
locale,
}: BuildNavigationJsonLdArgs) {
const origin = baseUrl.replace(/\/$/, '')
const out: NavItem[] = []
const seen = new Set()
const walk = (list: unknown) => {
if (!Array.isArray(list)) {return}
for (const raw of list) {
if (!raw || typeof raw !== 'object') {continue}
const item = raw as NavSource
const link = getLink(item)
if (link && link.type !== 'external' && link.type !== 'file') {
const href = resolveLinkHref(link, { config, homeSlug, locale })
const name = typeof link.label === 'string' ? link.label.trim() : ''
if (href && name && href.startsWith('/') && !seen.has(href)) {
seen.add(href)
out.push({ name, url: `${origin}${href}` })
}
}
walk(getChildren(item))
}
}
walk(items)
return buildSiteNavigationJsonLd(out)
}