-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
311 lines (290 loc) · 8.53 KB
/
index.ts
File metadata and controls
311 lines (290 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import path from 'node:path'
import {
addTrailingSlash,
type RspressPlugin,
type UserConfig,
} from '@rspress/shared'
import { APIS_ROUTES } from '../../shared/index.js'
import { combineWalkResult } from './utils.js'
import { walk } from './walk.js'
export type * from './walk.js'
export type * from './type.js'
// Scan all the directories and files in the work directory(such as `docs`), and then generate the nav and sidebar configuration according to the directory structure.
// We will do as follows:
// 1. scan the directory structure, and extract all the `_meta.json` files.
// The `_meta.json` files will have two types:
// - For the `_meta.json` directly in the work directory, it will be used as the nav configuration.
// - For the `_meta.json` in the subdirectory, it will be used as the sidebar configuration.
// First, for the `_meta.json` directly in the work directory, the json content will have the following structure(see `NavMeta`):
// ```ts
// export type NavMeta = NavItem[];
// export type NavItem = NavItemWithLink | NavItemWithChildren;
// export type NavItemWithLink = {
// text: string;
// link: string;
// activeMatch?: string;
// position?: 'left' | 'right';
// };
// export type NavItemChildren = {
// text: string;
// items: NavItemWithLink[];
// };
// ```
// The `NavItemWithLink` will be used as the nav item, and the `NavItemChildren` will be used as the nav item with children.
// Second, for the `_meta.json` in the subdirectory, the json content will have the following structure(see `SideMeta`):
// ```ts
// export type SideMetaItem =
// | string
// | {
// type: 'file' | 'directory';
// name: string;
// // Use the h1 title as the sidebar title by default
// label?: string;
// collapsible?: boolean;
// collapsed?: boolean;
// };
// export type SideMeta = SideMetaItem[];
// ```
// The `SideMetaItem` can be the following types:
// - string: the file name, such as `home.md`, can also remove the extension name, such as `home`.
// - object: you can specify the `type` and `name` of the file or directory, and the `label` of the sidebar item, and whether the sidebar item is `collapsible` and `collapsed`.
// 2. generate the nav and sidebar configuration according to the `_meta.json` files.
// As you can see, every `_meta.json` file will have a corresponding file or directory, so we can get the link info from the file or directory(relative to the work directory).So we can generate the nav and sidebar config based on link info and _meta.json content.
// For nav config, we don't need to do anything, just use the `_meta.json` content as the nav config.
// For sidebar config, we need to transform every item of the array config:
// - string: we transform the string to a object with `text` and `link` property.Such as:
// ```ts
// {
// text: 'home',
// link: '/home'
// }
// ```
// - object: in this case, we will take account of the `type` property:
// - file: we will transform the object to a object with `text` and `link` property.Such as:
// ```ts
// {
// text: 'home',
// link: '/home'
// }
// ```
// - directory: we will transform the object to a object with `text` and `items` property.
// There is the following file structure:
// ```
// docs
// ├── guide
// │ ├── home.md
// │ ├── advanced
// │ │ └── plugin.md
// │ └── _meta.json
// └── _meta.json
// ```
// The `_meta.json` in the work directory has the following content:
// ```json
// [
// {
// "text": "guide",
// "link": "/guide"
// }
// ]
// ```
// The `_meta.json` in the `guide` directory has the following content:
// ```json
// [
// 'home',
// {
// "type": "directory",
// "name": "advanced",
// "label": "advanced",
// "collapsible": true,
// "collapsed": false
// }
// ]
// ```
// The `home.md` has the following content:
// ```md
// # home
// ```
// The `_meta.json` in the `advanced` directory has the following content:
// ```json
// [
// {
// "type": "file",
// "name": "plugin",
// "label": "plugin",
// "collapsible": true,
// "collapsed": false
// }
// ]
// ```
// The `plugin.md` has the following content:
// ```md
// # plugin
// ```
// The generated nav config will be:
// ```json
// [
// {
// "text": "guide",
// "link": "/guide"
// }
// ]
// ```
// The generated sidebar config will be:
// ```json
// [
// {
// "text": "home",
// "link": "/guide/home"
// },
// {
// "text": "advanced",
// "items": [
// {
// "text": "plugin",
// "link": "/guide/advanced/plugin"
// }
// ]
// }
// ]
// ```
function processLocales(
langs: string[],
versions: string[],
root: string,
defaultLang: string,
defaultVersion: string,
extensions: string[],
onlyIncludeRoutes?: string[],
excludeRoutes?: string[],
collapsed?: boolean,
) {
return Promise.all(
langs.map(async (lang) => {
const walks = versions.length
? await Promise.all(
versions.map((version) => {
const routePrefix = addTrailingSlash(
`${version === defaultVersion ? '' : `/${version}`}${
lang === defaultLang ? '' : `/${lang}`
}`,
)
return walk(
path.join(root, version, lang),
routePrefix,
root,
extensions,
onlyIncludeRoutes,
excludeRoutes,
collapsed,
)
}),
)
: [
await walk(
path.join(root, lang),
addTrailingSlash(lang === defaultLang ? '' : `/${lang}`),
root,
extensions,
onlyIncludeRoutes,
excludeRoutes,
collapsed,
),
]
return combineWalkResult(walks, versions)
}),
)
}
const defaultExtensions = ['.mdx', '.md', '.tsx', '.jsx', '.ts', '.js']
export interface AutoSidebarPluginOptions {
ignore?: boolean
export?: boolean
collapsed?: boolean
}
export const autoSidebar = async (
config: UserConfig,
{ ignore, export: export_ }: AutoSidebarPluginOptions,
) => {
const onlyIncludeRoutes = (ignore && config.onlyIncludeRoutes) || []
const excludeRoutes = (ignore && config.internalRoutes) || []
const route = (config.route ??= {})
if (export_ || excludeRoutes.length) {
const exclude = (route.exclude ??= [])
exclude.push(...excludeRoutes)
// only exclude apis routes for sidebar but not site data to avoid dead links
if (export_) {
excludeRoutes.push(...APIS_ROUTES)
}
}
config.themeConfig ??= {}
config.themeConfig.locales ??= config.locales || []
const root = config.root!
const langs = config.themeConfig.locales.map((locale) => locale.lang)
const hasLocales = langs.length > 0
const versions = config.multiVersion?.versions || []
const defaultLang = config.lang || ''
const { default: defaultVersion = '' } = config.multiVersion || {}
const { extensions = defaultExtensions } = route
const collapsed = config.sidebar?.collapsed
if (hasLocales) {
const metaInfo = await processLocales(
langs,
versions,
root,
defaultLang,
defaultVersion,
extensions,
onlyIncludeRoutes,
excludeRoutes,
collapsed,
)
config.themeConfig.locales = config.themeConfig.locales.map(
(item, index) => ({
...item,
...metaInfo[index],
}),
)
} else {
const walks = versions.length
? await Promise.all(
versions.map((version) => {
const routePrefix = addTrailingSlash(
version === defaultVersion ? '' : `/${version}`,
)
return walk(
path.join(root, version),
routePrefix,
config.root!,
extensions,
onlyIncludeRoutes,
excludeRoutes,
collapsed,
)
}),
)
: [
await walk(
root,
'/',
root,
extensions,
onlyIncludeRoutes,
excludeRoutes,
collapsed,
),
]
const combined = combineWalkResult(walks, versions)
config.themeConfig = { ...config.themeConfig, ...combined }
}
return config
}
export const autoSidebarPlugin = (
options: AutoSidebarPluginOptions = {},
): RspressPlugin => {
return {
name: 'doom-auto-sidebar',
async config(config, utils) {
utils.removePlugin('auto-nav-sidebar')
return autoSidebar(config, options)
},
}
}