generated from forge42dev/open-source-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadcrumb.ts
31 lines (31 loc) · 1.04 KB
/
breadcrumb.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* This helper is used to generate an breadcrumb ld+json structured data object.
*
*
* Find more information about the breadcrumb schema here:
*
*
* https://developers.google.com/search/docs/appearance/structured-data/breadcrumb
* @param fullUrl string representing the current page (e.g. https://localhost:3000/blog/2021/01/01/article)
* @param names optional string array representing the names of the breadcrumb items, matched 1:1 with the path
* @returns BreadcrumbLdJson object to be used in head via json-ld script tag
*/
export const breadcrumbs = (fullUrl: string, names?: string[]) => {
const url = new URL(fullUrl)
const domain = url.origin
const path = url.pathname.split("/").filter(Boolean)
const breadcrumb = path.map((_, index) => {
const pathToOpen = path.slice(0, index + 1).join("/")
return {
"@type": "ListItem",
position: index + 1,
name: names?.[index] || _,
item: `${domain}/${pathToOpen}`,
}
})
return {
"@context": "https://schema.org",
"@type": "BreadcrumbList",
itemListElement: breadcrumb,
}
}