@@ -10,21 +10,23 @@ export interface BreadcrumbItem {
1010
1111export interface BreadcrumbOptions {
1212 /**
13- * Include the root itself in the breadcrumb items array.
14- * Specify the url by passing an object instead
13+ * Include the root folders in the breadcrumb items array.
1514 *
1615 * @defaultValue false
1716 */
1817 includeRoot ?:
1918 | boolean
2019 | {
20+ /**
21+ * Specify the url of root
22+ */
2123 url : string ;
2224 } ;
2325
2426 /**
2527 * Include the page itself in the breadcrumb items array
2628 *
27- * @defaultValue true
29+ * @defaultValue false
2830 */
2931 includePage ?: boolean ;
3032
@@ -64,38 +66,45 @@ export function getBreadcrumbItemsFromPath(
6466 path : PageTree . Node [ ] ,
6567 options : BreadcrumbOptions ,
6668) : BreadcrumbItem [ ] {
67- const { includePage = true , includeSeparator = false , includeRoot } = options ;
69+ const {
70+ includePage = false ,
71+ includeSeparator = false ,
72+ includeRoot = false ,
73+ } = options ;
6874 let items : BreadcrumbItem [ ] = [ ] ;
69-
70- path . forEach ( ( item , i ) => {
71- if ( item . type === 'separator' && item . name && includeSeparator ) {
72- items . push ( {
73- name : item . name ,
74- } ) ;
75+ for ( let i = 0 ; i < path . length ; i ++ ) {
76+ const item = path [ i ] ;
77+
78+ switch ( item . type ) {
79+ case 'page' :
80+ if ( includePage )
81+ items . push ( {
82+ name : item . name ,
83+ url : item . url ,
84+ } ) ;
85+ break ;
86+ case 'folder' :
87+ if ( item . root && ! includeRoot ) {
88+ items = [ ] ;
89+ break ;
90+ }
91+
92+ // only show the index node of folders if possible
93+ if ( i === path . length - 1 || item . index !== path [ i + 1 ] ) {
94+ items . push ( {
95+ name : item . name ,
96+ url : item . index ?. url ,
97+ } ) ;
98+ }
99+ break ;
100+ case 'separator' :
101+ if ( item . name && includeSeparator )
102+ items . push ( {
103+ name : item . name ,
104+ } ) ;
105+ break ;
75106 }
76-
77- if ( item . type === 'folder' ) {
78- const next = path . at ( i + 1 ) ;
79- if ( next && item . index === next ) return ;
80-
81- if ( item . root ) {
82- items = [ ] ;
83- return ;
84- }
85-
86- items . push ( {
87- name : item . name ,
88- url : item . index ?. url ,
89- } ) ;
90- }
91-
92- if ( item . type === 'page' && includePage ) {
93- items . push ( {
94- name : item . name ,
95- url : item . url ,
96- } ) ;
97- }
98- } ) ;
107+ }
99108
100109 if ( includeRoot ) {
101110 items . unshift ( {
@@ -113,7 +122,7 @@ export function getBreadcrumbItemsFromPath(
113122 * - When the page doesn't exist, return null
114123 *
115124 * @returns The path to the target node from root
116- * @internal
125+ * @internal Don't use this on your own
117126 */
118127export function searchPath (
119128 nodes : PageTree . Node [ ] ,
0 commit comments