Files
ipal-kit/src/modules/seo/buildSiteNavigationJsonLd.ts
T
radoslaw.smolinskiandClaude Opus 5.5 c9a50abc33 SEO/GEO tools and project requests: site index, IndexNow, page JSON-LD, fixes
GEO / SEO
- collectSiteIndex: one list of public docs per locale (pages + archive
  entries) on sitemap rules; llms.txt/llms-full and MCP list_pages now include
  blog/content entries and every language. Fixes hard-coded "## Strony" heading
  and homepage not collapsing to the locale root in llms.txt.
- Markdown frontmatter: canonical url + updated date.
- Metadata: X/Twitter card, OG image width/height/alt, og:type article with
  published/modified times for entries.
- Fix: Site Settings "defaultShareImage" was never used — now the OG fallback.
- getPageJsonLd (createContentHelpers): WebPage/CollectionPage/BlogPosting,
  BreadcrumbList from the real route, FAQPage via a project mapper.
- IndexNow (opt-in ipalKit({ indexNow: true })): submit published URLs in all
  languages, key generated in Site Integrations or INDEXNOW_KEY, key route.
- sameAs profiles in Site Settings + sameAsUrls(); sitemap <image:image>.

From project reports
- Fix: package exports were missing ./env and ./fluid.css although the docs
  import them (TS2307); regression test checks every documented subpath.
- Fix: plugins run before Payload adds its default users collection, so roles
  and 2FA had nothing to attach to — ipalKit injects an auth collection when
  the project has none (project's own one wins). Audit no longer errors on it.
- createMarkdownRoute, createSubmitAction (/server, headers injected).
- formatOpeningHours (PL/EN/DE text → schema.org), used by
  buildLocalBusinessJsonLd, which also takes a schema.org `type`.
- buildNavigationJsonLd straight from the header global.
- buildCsp googleMaps: full Maps JS API domain set; values de-duplicated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 02:19:08 +02:00

104 lines
3.6 KiB
TypeScript

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}` }))
* )
* <script type="application/ld+json" ... />
*
* 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<string, unknown>
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, string> | 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 })
* <JsonLdGraph schemas={[website, org, nav]} />
*/
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<string>()
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)
}