feat: breadcrumbs component
- <SeoBreadcrumbs> (UI-SPEC L1): the resolve answer's trail as nav > ol with server-rendered links and the last item as aria-current text; nothing with fewer than two items, with page meta seo.breadcrumbs false or with the option seo.breadcrumbs false; props items, label, hideCurrent, locale - one trail per page: the first rendered instance claims it (seoClaimBreadcrumbs, kept per path) - seoSitePath: same-origin CMS URLs become paths, non-http(s) URLs are never links; useSeoLabels takes an optional locale - L1 default styles with CSS-generated separators
This commit is contained in:
27
app/utils/breadcrumbClaim.ts
Normal file
27
app/utils/breadcrumbClaim.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
// One visible breadcrumb trail per page (UI-SPEC L1): an app may place
|
||||
// <SeoBreadcrumbs> in its layout while a layer's page renders one too; the
|
||||
// first instance that renders in a request claims the page and later ones
|
||||
// render nothing. The claim is kept per path, so a client navigation hands
|
||||
// it out again (the layout instance, set up first, claims first). Pure, so
|
||||
// `node --test` drives it; names start with `seo` (app/utils is
|
||||
// auto-imported).
|
||||
|
||||
export interface SeoBreadcrumbClaim {
|
||||
path: string
|
||||
id: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether instance `id` owns the trail of `path`, and the claim to keep:
|
||||
* no claim or one for another path is taken over; the claim of this path
|
||||
* belongs to the instance that made it.
|
||||
*/
|
||||
export function seoClaimBreadcrumbs(state: SeoBreadcrumbClaim | null | undefined, path: string, id: string): { state: SeoBreadcrumbClaim; owns: boolean } {
|
||||
if (!state || state.path !== path) return { state: { path, id }, owns: true }
|
||||
return { state, owns: state.id === id }
|
||||
}
|
||||
|
||||
/** Releases the claim of `id` (an instance that stops rendering its trail). */
|
||||
export function seoReleaseBreadcrumbs(state: SeoBreadcrumbClaim | null | undefined, id: string): SeoBreadcrumbClaim | null {
|
||||
return state && state.id === id ? null : (state ?? null)
|
||||
}
|
||||
26
app/utils/sitePath.ts
Normal file
26
app/utils/sitePath.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// Links to the site's own pages from absolute CMS URLs: a URL on the site
|
||||
// URL's origin (or on the request origin) becomes its path, so it renders
|
||||
// as a NuxtLink; another http(s) URL stays as it is, and anything else
|
||||
// (javascript:, data: …) becomes '' so it is never rendered as a link.
|
||||
// Pure; names start with `seo` because app/utils is auto-imported.
|
||||
|
||||
export function seoSitePath(url: string, ...origins: Array<string | undefined | null>): string {
|
||||
if (typeof url !== 'string' || url === '') return ''
|
||||
if (url.startsWith('/') && !url.startsWith('//')) return url
|
||||
let parsed: URL
|
||||
try {
|
||||
parsed = new URL(url)
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') return ''
|
||||
for (const o of origins) {
|
||||
if (!o) continue
|
||||
try {
|
||||
if (new URL(o).origin === parsed.origin) return `${parsed.pathname}${parsed.search}` || '/'
|
||||
} catch {
|
||||
// not a URL
|
||||
}
|
||||
}
|
||||
return url
|
||||
}
|
||||
Reference in New Issue
Block a user