@@ -54,12 +54,23 @@ export interface RouteMeta {
5454 route ?: RouteItem ;
5555 /** Trail of parent routes */
5656 breadcrumbs ?: RouteItem [ ] ;
57+ /** Order in the section */
58+ order ?: number ;
5759}
5860
61+ type TravesalContext = RouteMeta & {
62+ currentIndex : number ;
63+ } ;
64+
5965export function getRouteMeta ( cleanedPath : string , routeTree : RouteItem ) {
6066 const breadcrumbs = getBreadcrumbs ( cleanedPath , routeTree ) ;
67+ const ctx : TravesalContext = {
68+ currentIndex : 0 ,
69+ } ;
70+ buildRouteMeta ( cleanedPath , routeTree , ctx ) ;
71+ const { currentIndex, ...meta } = ctx ;
6172 return {
62- ...buildRouteMeta ( cleanedPath , routeTree , { } ) ,
73+ ...meta ,
6374 breadcrumbs : breadcrumbs . length > 0 ? breadcrumbs : [ routeTree ] ,
6475 } ;
6576}
@@ -68,8 +79,10 @@ export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) {
6879function buildRouteMeta (
6980 searchPath : string ,
7081 currentRoute : RouteItem ,
71- ctx : RouteMeta
72- ) : RouteMeta {
82+ ctx : TravesalContext
83+ ) {
84+ ctx . currentIndex ++ ;
85+
7386 const { routes} = currentRoute ;
7487
7588 if ( ctx . route && ! ctx . nextRoute ) {
@@ -78,6 +91,7 @@ function buildRouteMeta(
7891
7992 if ( currentRoute . path === searchPath ) {
8093 ctx . route = currentRoute ;
94+ ctx . order = ctx . currentIndex ;
8195 // If we've found a deeper match, reset the previously stored next route.
8296 // TODO: this only works reliably if deeper matches are first in the tree.
8397 // We should revamp all of this to be more explicit.
@@ -89,14 +103,12 @@ function buildRouteMeta(
89103 }
90104
91105 if ( ! routes ) {
92- return ctx ;
106+ return ;
93107 }
94108
95109 for ( const route of routes ) {
96110 buildRouteMeta ( searchPath , route , ctx ) ;
97111 }
98-
99- return ctx ;
100112}
101113
102114// iterates the route tree from the current route to find its ancestors for breadcrumbs
0 commit comments