@@ -2,6 +2,7 @@ import { withoutTrailingSlash } from 'ufo'
2
2
import type { RouteMeta } from 'vue-router'
3
3
import type { MaybeRefOrGetter } from 'vue'
4
4
import type { BreadcrumbLink } from '@nuxt/ui/dist/runtime/types'
5
+ import { defu } from 'defu'
5
6
import { pathBreadcrumbSegments } from '../../pure/breadcrumbs'
6
7
import {
7
8
computed ,
@@ -15,8 +16,33 @@ import {
15
16
} from '#imports'
16
17
17
18
export interface BreadcrumbProps {
19
+ /**
20
+ * Generate the breadcrumbs based on a different path than the current route.
21
+ */
18
22
path ?: MaybeRefOrGetter < string >
23
+ /**
24
+ * The id of the breadcrumb list. It's recommended to provide a unique
25
+ * id when adding multiple breadcrumb lists to the same page.
26
+ */
19
27
id ?: string
28
+ /**
29
+ * Append additional breadcrumb items to the end of the list. This is applied
30
+ * after the `overrides` option.
31
+ */
32
+ append ?: BreadcrumbItemProps [ ]
33
+ /**
34
+ * Prepend additional breadcrumb items to the start of the list. This is applied
35
+ * after the `overrides` option.
36
+ */
37
+ prepend ?: BreadcrumbItemProps [ ]
38
+ /**
39
+ * Override any of the breadcrumb items based on the index.
40
+ */
41
+ overrides ?: ( BreadcrumbItemProps | false | undefined ) [ ]
42
+ /**
43
+ * Should the schema.org breadcrumb be generated.
44
+ * @default true
45
+ */
20
46
schemaOrg ?: boolean
21
47
/**
22
48
* The Aria Label for the breadcrumbs.
@@ -83,10 +109,26 @@ export function useBreadcrumbItems(options: BreadcrumbProps = {}) {
83
109
rootNode = `/${ i18n . defaultLocale . value } `
84
110
}
85
111
const current = withoutQuery ( withoutTrailingSlash ( toValue ( options . path || useRoute ( ) . path ) || rootNode ) )
86
- return pathBreadcrumbSegments ( current , rootNode )
87
- . map ( path => ( {
88
- to : path ,
89
- } ) as BreadcrumbItemProps )
112
+ // apply overrides
113
+ const overrides = options . overrides || [ ]
114
+ const segments = pathBreadcrumbSegments ( current , rootNode )
115
+ . map ( ( path , index ) => {
116
+ let item = < BreadcrumbItemProps > {
117
+ to : path ,
118
+ }
119
+ if ( typeof overrides [ index ] !== 'undefined' ) {
120
+ if ( overrides [ index ] === false )
121
+ return false
122
+ item = defu ( overrides [ index ] as any as BreadcrumbItemProps , item )
123
+ }
124
+ return item
125
+ } )
126
+ // apply prepends and appends
127
+ if ( options . prepend )
128
+ segments . unshift ( ...options . prepend )
129
+ if ( options . append )
130
+ segments . push ( ...options . append )
131
+ return ( segments . filter ( Boolean ) as BreadcrumbItemProps [ ] )
90
132
. map ( ( item ) => {
91
133
const route = routes . find ( r => withoutTrailingSlash ( r . path ) === withoutTrailingSlash ( item . to ) )
92
134
const routeMeta = ( route ?. meta || { } ) as RouteMeta & { title ?: string , breadcrumbLabel : string }
0 commit comments