11import path from 'node:path' ;
22import type { RsbuildPlugin } from '@rsbuild/core' ;
33import type { RouteService } from '@rspress/core' ;
4- import { removeBase } from '@rspress/shared' ;
4+ import { matchPath } from '@rspress/runtime/server' ;
5+ import { getSidebarDataGroup , removeBase } from '@rspress/shared' ;
56import type {
67 Nav ,
78 PageIndexInfo ,
89 RouteMeta ,
910 RspressPlugin ,
1011 Sidebar ,
12+ SidebarDivider ,
13+ SidebarGroup ,
14+ SidebarItem ,
15+ SidebarSectionHeader ,
1116} from '@rspress/shared' ;
1217import type { NavItemWithLink } from '@rspress/shared' ;
1318import { generateLlmsFullTxt , generateLlmsTxt } from './llmsTxt' ;
@@ -19,7 +24,7 @@ const rsbuildPluginLlms = ({
1924 routes,
2025 titleRef,
2126 descriptionRef,
22- // sidebar,
27+ sidebar,
2328 baseRef,
2429 docDirectoryRef,
2530 routeServiceRef,
@@ -88,6 +93,10 @@ const rsbuildPluginLlms = ({
8893 others . push ( pageData ) ;
8994 } ) ;
9095
96+ for ( const array of pageArray ) {
97+ organizeBySidebar ( sidebar , array , base ) ;
98+ }
99+
91100 if ( llmsTxt ) {
92101 const llmsTxtContent = generateLlmsTxt (
93102 pageArray ,
@@ -200,6 +209,61 @@ function mergeRouteMetaWithPageData(
200209 return mergedPageDataList ;
201210}
202211
212+ function flatSidebar (
213+ sidebar : (
214+ | SidebarGroup
215+ | SidebarItem
216+ | SidebarDivider
217+ | SidebarSectionHeader
218+ | string
219+ ) [ ] ,
220+ ) : string [ ] {
221+ if ( ! sidebar ) {
222+ return [ ] ;
223+ }
224+ return sidebar
225+ . flatMap ( i => {
226+ if ( typeof i === 'string' ) {
227+ return i ;
228+ }
229+ if ( 'link' in i && typeof i . link === 'string' ) {
230+ return [ i . link , ...flatSidebar ( ( i as any ) ?. items ?? [ ] ) ] ;
231+ }
232+ if ( 'items' in i && Array . isArray ( i . items ) ) {
233+ return flatSidebar ( i . items ) ;
234+ }
235+ return undefined ;
236+ } )
237+ . filter ( Boolean ) as string [ ] ;
238+ }
239+
240+ function organizeBySidebar (
241+ sidebar : Sidebar ,
242+ pages : PageIndexInfo [ ] ,
243+ base : string ,
244+ ) {
245+ if ( pages . length === 0 ) {
246+ return ;
247+ }
248+ const pageItem = pages [ 0 ] ;
249+ const currSidebar = getSidebarDataGroup (
250+ sidebar as any ,
251+ pageItem . routePath ,
252+ base ,
253+ ) ;
254+
255+ if ( currSidebar . length === 0 ) {
256+ return ;
257+ }
258+ const orderList = flatSidebar ( currSidebar ) ;
259+
260+ pages . sort ( ( a , b ) => {
261+ const aIndex = orderList . findIndex ( order => matchPath ( order , a . routePath ) ) ;
262+ const bIndex = orderList . findIndex ( order => matchPath ( order , b . routePath ) ) ;
263+ return aIndex - bIndex ;
264+ } ) ;
265+ }
266+
203267/**
204268 * A plugin for rspress to generate llms.txt, llms-full.txt, md files to let llm understand your website.
205269 */
@@ -237,7 +301,14 @@ export function pluginLlms(options: Options = {}): RspressPlugin {
237301 }
238302 } ,
239303 beforeBuild ( config ) {
240- Object . assign ( sidebar , config . themeConfig ?. sidebar ?? { } ) ;
304+ const configSidebar = config ?. themeConfig ?. locales
305+ ?. map ( i => i . sidebar )
306+ . reduce ( ( prev : Sidebar , curr ) => {
307+ Object . assign ( prev , curr ) ;
308+ return prev ;
309+ } , { } as Sidebar ) ;
310+ Object . assign ( sidebar , configSidebar ) ;
311+
241312 const configNav = config . themeConfig ?. locales
242313 ?. filter ( i => Boolean ( i . nav ) )
243314 ?. map ( i => {
0 commit comments