Skip to content

Commit 9abcdce

Browse files
committed
chore: fix
1 parent e1fb967 commit 9abcdce

File tree

5 files changed

+115
-5
lines changed

5 files changed

+115
-5
lines changed

packages/plugin-llms/src/plugin.ts

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import path from 'node:path';
22
import type { RsbuildPlugin } from '@rsbuild/core';
33
import 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';
56
import type {
67
Nav,
78
PageIndexInfo,
89
RouteMeta,
910
RspressPlugin,
1011
Sidebar,
12+
SidebarDivider,
13+
SidebarGroup,
14+
SidebarItem,
15+
SidebarSectionHeader,
1116
} from '@rspress/shared';
1217
import type { NavItemWithLink } from '@rspress/shared';
1318
import { 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 => {

packages/runtime/server.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { StaticRouter } from 'react-router-dom/server';
2+
export { matchPath } from 'react-router-dom';

packages/runtime/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { StaticRouter } from 'react-router-dom/server';
2+
export { matchPath } from 'react-router-dom';

packages/shared/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export * from './types';
2-
export { matchSidebar, matchNavbar } from './runtime-utils/sidebar';
2+
export {
3+
matchSidebar,
4+
matchNavbar,
5+
getSidebarDataGroup,
6+
} from './runtime-utils/sidebar';
37
export {
48
APPEARANCE_KEY,
59
DEFAULT_HIGHLIGHT_LANGUAGES,

packages/shared/src/runtime-utils/sidebar.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NavItemWithLink } from '../types/defaultTheme';
1+
import type { NavItemWithLink, NormalizedSidebar } from '../types/defaultTheme';
22
import { withBase, withoutBase } from './utils';
33
import { addTrailingSlash } from './utils';
44

@@ -29,6 +29,39 @@ export const matchSidebar = (
2929
return currentPathname.startsWith(prefixWithDot);
3030
};
3131

32+
/**
33+
* get the sidebar group for the current page
34+
* @param sidebar const { sidebar } = useLocaleSiteData();
35+
* @param currentPathname
36+
* @returns
37+
*/
38+
export const getSidebarDataGroup = (
39+
sidebar: NormalizedSidebar,
40+
currentPathname: string,
41+
base: string,
42+
): NormalizedSidebar[string] => {
43+
/**
44+
* why sort?
45+
* {
46+
* '/': [],
47+
* '/guide': [
48+
* {
49+
* text: 'Getting Started',
50+
* link: '/guide/getting-started',
51+
* }
52+
* ],
53+
* }
54+
*/
55+
const navRoutes = Object.keys(sidebar).sort((a, b) => b.length - a.length);
56+
for (const name of navRoutes) {
57+
if (matchSidebar(name, currentPathname, base)) {
58+
const sidebarGroup = sidebar[name];
59+
return sidebarGroup;
60+
}
61+
}
62+
return [];
63+
};
64+
3265
export const matchNavbar = (
3366
item: NavItemWithLink,
3467
currentPathname: string,

0 commit comments

Comments
 (0)