Skip to content

Commit 51e78ea

Browse files
authored
feat(plugin-llms): support mdFiles.remarkPlugins (#2516)
1 parent 30c9410 commit 51e78ea

File tree

5 files changed

+79
-41
lines changed

5 files changed

+79
-41
lines changed

packages/plugin-llms/src/normalizeMdFile.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Root } from 'hast';
77
import remarkMdx from 'remark-mdx';
88
import remarkParse from 'remark-parse';
99
import remarkStringify from 'remark-stringify';
10-
import { type Plugin, unified } from 'unified';
10+
import { type PluggableList, type Plugin, unified } from 'unified';
1111
import { SKIP, visit } from 'unist-util-visit';
1212
import type { VFile } from 'vfile';
1313

@@ -48,8 +48,9 @@ function normalizeMdFile(
4848
base: string,
4949
mdxToMd: boolean,
5050
isMd: boolean,
51+
remarkPlugins: PluggableList,
5152
): Promise<VFile> {
52-
return unified()
53+
const compiler = unified()
5354
.use(remarkParse)
5455
.use(isMd ? noopPlugin : remarkMdx)
5556
.use(remarkFileCodeBlock, { filepath })
@@ -63,11 +64,13 @@ function normalizeMdFile(
6364
},
6465
__base: base,
6566
} satisfies Parameters<typeof remarkLink>[0])
66-
.use(remarkStringify)
67-
.process({
68-
value: content,
69-
path: filepath,
70-
});
67+
.use(remarkPlugins)
68+
.use(remarkStringify);
69+
70+
return compiler.process({
71+
value: content,
72+
path: filepath,
73+
});
7174
}
7275

7376
export { normalizeMdFile };

packages/plugin-llms/src/plugin.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const rsbuildPluginLlms = ({
4848
},
4949
mdFiles = {
5050
mdxToMd: false,
51+
remarkPlugins: [],
5152
},
5253
llmsFullTxt = {
5354
name: 'llms-full.txt',
@@ -149,8 +150,13 @@ const rsbuildPluginLlms = ({
149150
filepath,
150151
routeServiceRef.current!,
151152
baseRef.current,
152-
typeof mdFiles !== 'boolean' ? mdFiles?.mdxToMd : false,
153+
typeof mdFiles !== 'boolean'
154+
? (mdFiles?.mdxToMd ?? false)
155+
: false,
153156
isMD,
157+
typeof mdFiles !== 'boolean'
158+
? (mdFiles?.remarkPlugins ?? [])
159+
: [],
154160
)
155161
).toString();
156162
} catch (e) {
@@ -278,8 +284,15 @@ function organizeBySidebar(sidebar: Sidebar, pages: PageIndexInfo[]) {
278284
const orderList = flatSidebar(currSidebar);
279285

280286
pages.sort((a, b) => {
281-
const aIndex = orderList.findIndex(order => matchPath(order, a.routePath));
282-
const bIndex = orderList.findIndex(order => matchPath(order, b.routePath));
287+
let aIndex = orderList.findIndex(order => matchPath(order, a.routePath));
288+
// if not in sidebar, put it to last
289+
if (aIndex === -1) {
290+
aIndex = Number.MAX_SAFE_INTEGER;
291+
}
292+
let bIndex = orderList.findIndex(order => matchPath(order, b.routePath));
293+
if (bIndex === -1) {
294+
bIndex = Number.MAX_SAFE_INTEGER;
295+
}
283296
return aIndex - bIndex;
284297
});
285298
}
@@ -300,9 +313,6 @@ function getDefaultOptions(
300313
llmsFullTxt: {
301314
name: 'llms-full.txt',
302315
},
303-
include({ page }) {
304-
return page.lang === l;
305-
},
306316
};
307317
}
308318
return {

packages/plugin-llms/src/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
RouteService,
66
Sidebar,
77
} from '@rspress/core';
8+
import type { PluggableList } from 'unified';
89

910
/**
1011
* @default { name: "llms.txt" }
@@ -21,9 +22,15 @@ export interface LlmsTxt {
2122

2223
export interface MdFiles {
2324
/**
25+
* Whether to convert mdx to md.
2426
* @default false
2527
*/
26-
mdxToMd: boolean;
28+
mdxToMd?: boolean;
29+
/**
30+
* Allow users to customize remarkPlugins and edit the content of generated md files.
31+
* @default []
32+
*/
33+
remarkPlugins?: PluggableList;
2734
}
2835

2936
/**
@@ -48,8 +55,7 @@ export interface Options {
4855
llmsFullTxt?: false | LlmsFullTxt;
4956
/**
5057
* Whether to include some routes from llms.txt.
51-
* @param context
52-
* @default (context) => context.page.lang === config.lang
58+
* @default undefined
5359
*/
5460
include?: (context: { page: PageIndexInfo }) => boolean;
5561
/**

website/docs/en/plugin/official-plugins/llms.mdx

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ interface LlmsTxt {
101101
}
102102

103103
interface MdFiles {
104-
mdxToMd: boolean;
104+
mdxToMd?: boolean;
105+
remarkPlugins?: PluggableList;
105106
}
106107

107108
interface LlmsFullTxt {
@@ -233,21 +234,29 @@ Rspress is a static site generator based on Rsbuild and it can generate llms.txt
233234

234235
```ts
235236
export interface MdFiles {
236-
mdxToMd: boolean;
237+
mdxToMd?: boolean;
238+
remarkPlugins?: PluggableList;
237239
}
238240
```
239241

240-
- **Default**: `{ mdxToMd: false }`
242+
- **Default**: `{ mdxToMd: false, remarkPlugins: [] }`
241243

242-
Whether to generate a markdown file for the corresponding route, when set to `false`, the markdown file for the corresponding route will not be generated.
244+
Whether to generate a markdown file for the corresponding route. When set to `false`, the markdown file for the corresponding route will not be generated.
243245

244246
#### mdxToMd
245247

246-
- Type: `boolean`
247-
- Default: `false`
248+
- **Type**: `boolean`
249+
- **Default**: `false`
248250

249251
Whether to convert mdx content to md content. If enabled, mdx files will be converted to md files through a set of default strategies, but there may be some information loss.
250252

253+
#### remarkPlugins
254+
255+
- **Type**: `PluggableList`
256+
- **Default**: `[]`
257+
258+
You can pass in custom remark plugins to modify the Markdown content.
259+
251260
### llmsFullTxt
252261

253262
- **Type**: `false | LlmsFullTxt`
@@ -349,23 +358,24 @@ Text on the copy button, has higher priority than `textByLang`.
349358
- **Type**: `LlmsViewOptionsProps`
350359

351360
```ts
361+
type Option =
362+
| {
363+
title: string;
364+
icon?: React.ReactNode;
365+
onClick?: () => void;
366+
}
367+
| {
368+
title: string;
369+
href: string;
370+
icon?: React.ReactNode;
371+
}
372+
| 'markdownLink'
373+
| 'chatgpt'
374+
| 'claude';
375+
352376
interface LlmsViewOptionsProps
353377
extends React.ButtonHTMLAttributes<HTMLButtonElement> {
354-
options?: Array<
355-
| {
356-
title: string;
357-
icon?: React.ReactNode;
358-
onClick?: () => void;
359-
}
360-
| {
361-
title: string;
362-
icon?: React.ReactNode;
363-
href: string;
364-
}
365-
| 'markdownLink'
366-
| 'chatgpt'
367-
| 'claude'
368-
>;
378+
options?: Option[];
369379
textByLang?: Record<string, string>;
370380
text?: string;
371381
}
@@ -394,7 +404,7 @@ type Option =
394404

395405
- **Default**: `['markdownLink', 'chatgpt', 'claude']`
396406

397-
Customize the options in the dropdown menu, supports "Copy Markdown Link", [ChatGPT](https://chatgpt.com/) and [Claude](https://claude.ai) by default.
407+
Customize the options in the dropdown menu. By default, supports "Copy Markdown Link", [ChatGPT](https://chatgpt.com/) and [Claude](https://claude.ai).
398408

399409
#### textByLang
400410

website/docs/zh/plugin/official-plugins/llms.mdx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ interface LlmsTxt {
101101
}
102102

103103
interface MdFiles {
104-
mdxToMd: boolean;
104+
mdxToMd?: boolean;
105+
remarkPlugins?: PluggableList;
105106
}
106107

107108
interface LlmsFullTxt {
@@ -232,11 +233,12 @@ Rspress is a static site generator based on Rsbuild and it can generate llms.txt
232233

233234
```ts
234235
export interface MdFiles {
235-
mdxToMd: boolean;
236+
mdxToMd?: boolean;
237+
remarkPlugins?: PluggableList;
236238
}
237239
```
238240

239-
- **默认值**: `{ mdxToMd: false }`
241+
- **默认值**: `{ mdxToMd: false, remarkPlugins: [] }`
240242

241243
是否生成对应路由的 markdown 文件,当设置为 `false` 时,不会生成对应路由的 markdown 文件。
242244

@@ -247,6 +249,13 @@ export interface MdFiles {
247249

248250
是否将 mdx 内容转换为 md 内容,如果启用,会将 mdx 文件通过一组默认策略转换为 md 文件,但可能会有一定的信息丢失。
249251

252+
#### remarkPlugins
253+
254+
- **类型**: `PluggableList`
255+
- **默认值**: `[]`
256+
257+
用户可以传入自定义的 remark plugins 来对 Markdown 的内容做一些修改。
258+
250259
### llmsFullTxt
251260

252261
- **类型**: `false | LlmsFullTxt`

0 commit comments

Comments
 (0)