@rspress/core 1.39.3 → 1.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1006 -902
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +7 -7
- package/dist/loader.js +102 -104
- package/dist/loader.js.map +1 -1
- package/dist/runtime/App.js +9 -9
- package/dist/runtime/clientEntry.js +5 -5
- package/dist/runtime/ssrEntry.js +1 -1
- package/index.html +12 -14
- package/package.json +42 -41
- package/src/runtime/App.tsx +8 -8
- package/src/runtime/clientEntry.tsx +4 -4
- package/src/runtime/ssrEntry.tsx +2 -2
package/dist/index.js
CHANGED
|
@@ -66,7 +66,7 @@ var require_package = __commonJS({
|
|
|
66
66
|
"package.json"(exports, module) {
|
|
67
67
|
module.exports = {
|
|
68
68
|
name: "@rspress/core",
|
|
69
|
-
version: "1.
|
|
69
|
+
version: "1.40.0",
|
|
70
70
|
description: "The Rspress Documentation Framework",
|
|
71
71
|
bugs: "https://github.com/web-infra-dev/rspress/issues",
|
|
72
72
|
repository: {
|
|
@@ -75,11 +75,16 @@ var require_package = __commonJS({
|
|
|
75
75
|
directory: "packages/cli/core"
|
|
76
76
|
},
|
|
77
77
|
license: "MIT",
|
|
78
|
+
sideEffects: [
|
|
79
|
+
"*.css",
|
|
80
|
+
"*.less",
|
|
81
|
+
"*.sass",
|
|
82
|
+
"*.scss",
|
|
83
|
+
"**/virtual-global-styles.js",
|
|
84
|
+
"virtual-global-styles",
|
|
85
|
+
"./src/theme-default/styles/index.ts"
|
|
86
|
+
],
|
|
78
87
|
type: "module",
|
|
79
|
-
"jsnext:source": "./src/index.ts",
|
|
80
|
-
types: "./dist/index.d.ts",
|
|
81
|
-
main: "./dist/index.js",
|
|
82
|
-
module: "./dist/node/index.js",
|
|
83
88
|
exports: {
|
|
84
89
|
".": {
|
|
85
90
|
types: "./dist/index.d.ts",
|
|
@@ -95,24 +100,35 @@ var require_package = __commonJS({
|
|
|
95
100
|
default: "./runtime.ts"
|
|
96
101
|
}
|
|
97
102
|
},
|
|
103
|
+
main: "./dist/index.js",
|
|
104
|
+
module: "./dist/node/index.js",
|
|
105
|
+
types: "./dist/index.d.ts",
|
|
106
|
+
files: [
|
|
107
|
+
"dist",
|
|
108
|
+
"src/runtime",
|
|
109
|
+
"src/theme-default",
|
|
110
|
+
"src/shared",
|
|
111
|
+
"index.html",
|
|
112
|
+
"runtime.ts",
|
|
113
|
+
"theme.ts",
|
|
114
|
+
"loader.cjs",
|
|
115
|
+
"compiled"
|
|
116
|
+
],
|
|
98
117
|
scripts: {
|
|
99
|
-
dev: "modern build -w",
|
|
100
118
|
build: "modern build",
|
|
119
|
+
dev: "modern build -w",
|
|
101
120
|
reset: "rimraf ./**/node_modules",
|
|
102
121
|
test: "vitest run --passWithNoTests"
|
|
103
122
|
},
|
|
104
|
-
engines: {
|
|
105
|
-
node: ">=14.17.6"
|
|
106
|
-
},
|
|
107
123
|
dependencies: {
|
|
108
124
|
"@mdx-js/loader": "2.3.0",
|
|
109
125
|
"@mdx-js/mdx": "2.3.0",
|
|
110
126
|
"@mdx-js/react": "2.3.0",
|
|
111
|
-
"@rsbuild/core": "~1.1.
|
|
127
|
+
"@rsbuild/core": "~1.1.13",
|
|
112
128
|
"@rsbuild/plugin-less": "~1.1.0",
|
|
113
129
|
"@rsbuild/plugin-react": "~1.1.0",
|
|
114
130
|
"@rsbuild/plugin-sass": "~1.1.2",
|
|
115
|
-
"@rspress/mdx-rs": "0.6.
|
|
131
|
+
"@rspress/mdx-rs": "0.6.6",
|
|
116
132
|
"@rspress/plugin-auto-nav-sidebar": "workspace:*",
|
|
117
133
|
"@rspress/plugin-container-syntax": "workspace:*",
|
|
118
134
|
"@rspress/plugin-last-updated": "workspace:*",
|
|
@@ -128,6 +144,7 @@ var require_package = __commonJS({
|
|
|
128
144
|
htmr: "^1.0.2",
|
|
129
145
|
"lodash-es": "^4.17.21",
|
|
130
146
|
"mdast-util-mdxjs-esm": "^1.3.1",
|
|
147
|
+
memfs: "^4.17.0",
|
|
131
148
|
react: "^18.3.1",
|
|
132
149
|
"react-dom": "^18.3.1",
|
|
133
150
|
"react-helmet-async": "^1.3.0",
|
|
@@ -142,13 +159,13 @@ var require_package = __commonJS({
|
|
|
142
159
|
"unist-util-visit-children": "^3.0.0"
|
|
143
160
|
},
|
|
144
161
|
devDependencies: {
|
|
145
|
-
"@modern-js/plugin-tailwindcss": "2.63.
|
|
146
|
-
"@modern-js/tsconfig": "2.63.
|
|
162
|
+
"@modern-js/plugin-tailwindcss": "2.63.6",
|
|
163
|
+
"@modern-js/tsconfig": "2.63.6",
|
|
147
164
|
"@types/hast": "3.0.4",
|
|
148
165
|
"@types/html-to-text": "^9.0.4",
|
|
149
166
|
"@types/jest": "~29.5.14",
|
|
150
167
|
"@types/lodash-es": "^4.17.12",
|
|
151
|
-
"@types/mdast": "^
|
|
168
|
+
"@types/mdast": "^3.0.15",
|
|
152
169
|
"@types/node": "^18.11.17",
|
|
153
170
|
"@types/react": "^18.3.18",
|
|
154
171
|
"@types/react-dom": "^18.3.5",
|
|
@@ -161,43 +178,219 @@ var require_package = __commonJS({
|
|
|
161
178
|
typescript: "^5.5.3",
|
|
162
179
|
vitest: "2.1.8"
|
|
163
180
|
},
|
|
164
|
-
|
|
165
|
-
"
|
|
166
|
-
|
|
167
|
-
"*.sass",
|
|
168
|
-
"*.scss",
|
|
169
|
-
"**/virtual-global-styles.js",
|
|
170
|
-
"virtual-global-styles",
|
|
171
|
-
"./src/theme-default/styles/index.ts"
|
|
172
|
-
],
|
|
173
|
-
files: [
|
|
174
|
-
"dist",
|
|
175
|
-
"src/runtime",
|
|
176
|
-
"src/theme-default",
|
|
177
|
-
"src/shared",
|
|
178
|
-
"index.html",
|
|
179
|
-
"runtime.ts",
|
|
180
|
-
"theme.ts",
|
|
181
|
-
"loader.cjs",
|
|
182
|
-
"compiled"
|
|
183
|
-
],
|
|
181
|
+
engines: {
|
|
182
|
+
node: ">=14.17.6"
|
|
183
|
+
},
|
|
184
184
|
publishConfig: {
|
|
185
185
|
access: "public",
|
|
186
186
|
provenance: true,
|
|
187
187
|
registry: "https://registry.npmjs.org/"
|
|
188
|
-
}
|
|
188
|
+
},
|
|
189
|
+
"jsnext:source": "./src/index.ts"
|
|
189
190
|
};
|
|
190
191
|
}
|
|
191
192
|
});
|
|
192
193
|
|
|
194
|
+
// src/node/PluginDriver.ts
|
|
195
|
+
import { pluginContainerSyntax } from "@rspress/plugin-container-syntax";
|
|
196
|
+
import { isDevDebugMode } from "@rspress/shared";
|
|
197
|
+
var _config, _plugins, _isProd;
|
|
198
|
+
var PluginDriver = class {
|
|
199
|
+
constructor(config, isProd) {
|
|
200
|
+
__privateAdd(this, _config, void 0);
|
|
201
|
+
__privateAdd(this, _plugins, void 0);
|
|
202
|
+
__privateAdd(this, _isProd, void 0);
|
|
203
|
+
__privateSet(this, _config, config);
|
|
204
|
+
__privateSet(this, _isProd, isProd);
|
|
205
|
+
__privateSet(this, _plugins, []);
|
|
206
|
+
}
|
|
207
|
+
// The init function is used to initialize the doc plugins and will execute before the build process.
|
|
208
|
+
async init() {
|
|
209
|
+
this.clearPlugins();
|
|
210
|
+
const config = __privateGet(this, _config);
|
|
211
|
+
const themeConfig = config?.themeConfig || {};
|
|
212
|
+
const enableLastUpdated = themeConfig?.lastUpdated || themeConfig?.locales?.some((locale) => locale.lastUpdated);
|
|
213
|
+
const mediumZoomConfig = config?.mediumZoom ?? true;
|
|
214
|
+
const haveNavSidebarConfig = themeConfig.nav || themeConfig.sidebar || themeConfig.locales?.[0]?.nav || themeConfig.locales?.[0]?.sidebar;
|
|
215
|
+
if (enableLastUpdated) {
|
|
216
|
+
const { pluginLastUpdated } = await import("@rspress/plugin-last-updated");
|
|
217
|
+
this.addPlugin(pluginLastUpdated());
|
|
218
|
+
}
|
|
219
|
+
if (mediumZoomConfig) {
|
|
220
|
+
const { pluginMediumZoom } = await import("@rspress/plugin-medium-zoom");
|
|
221
|
+
this.addPlugin(
|
|
222
|
+
pluginMediumZoom(
|
|
223
|
+
typeof mediumZoomConfig === "object" ? mediumZoomConfig : void 0
|
|
224
|
+
)
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
this.addPlugin(pluginContainerSyntax());
|
|
228
|
+
if (isDevDebugMode()) {
|
|
229
|
+
const SourceBuildPlugin = await import(
|
|
230
|
+
// @ts-expect-error need moduleResolution: Node16, NodeNext or Bundler to get type declarations work
|
|
231
|
+
"@rspress/theme-default/node/source-build-plugin.js"
|
|
232
|
+
).then(
|
|
233
|
+
(r) => r.SourceBuildPlugin,
|
|
234
|
+
() => null
|
|
235
|
+
);
|
|
236
|
+
if (SourceBuildPlugin) {
|
|
237
|
+
this.addPlugin(SourceBuildPlugin());
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
(config.plugins || []).forEach((plugin) => {
|
|
241
|
+
this.addPlugin(plugin);
|
|
242
|
+
});
|
|
243
|
+
if (!haveNavSidebarConfig) {
|
|
244
|
+
const { pluginAutoNavSidebar } = await import("@rspress/plugin-auto-nav-sidebar");
|
|
245
|
+
this.addPlugin(pluginAutoNavSidebar());
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
addPlugin(plugin) {
|
|
249
|
+
const existedIndex = __privateGet(this, _plugins).findIndex(
|
|
250
|
+
(item) => item.name === plugin.name
|
|
251
|
+
);
|
|
252
|
+
if (existedIndex !== -1) {
|
|
253
|
+
throw new Error(`The plugin "${plugin.name}" has been registered`);
|
|
254
|
+
}
|
|
255
|
+
__privateGet(this, _plugins).push(plugin);
|
|
256
|
+
}
|
|
257
|
+
getPlugins() {
|
|
258
|
+
return __privateGet(this, _plugins);
|
|
259
|
+
}
|
|
260
|
+
clearPlugins() {
|
|
261
|
+
__privateSet(this, _plugins, []);
|
|
262
|
+
}
|
|
263
|
+
removePlugin(pluginName) {
|
|
264
|
+
const index = __privateGet(this, _plugins).findIndex((item) => item.name === pluginName);
|
|
265
|
+
if (index !== -1) {
|
|
266
|
+
__privateGet(this, _plugins).splice(index, 1);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
async modifyConfig() {
|
|
270
|
+
let config = __privateGet(this, _config);
|
|
271
|
+
for (let i = 0; i < __privateGet(this, _plugins).length; i++) {
|
|
272
|
+
const plugin = __privateGet(this, _plugins)[i];
|
|
273
|
+
if (typeof plugin.config === "function") {
|
|
274
|
+
config = await plugin.config(
|
|
275
|
+
config || {},
|
|
276
|
+
{
|
|
277
|
+
addPlugin: this.addPlugin.bind(this),
|
|
278
|
+
removePlugin: (pluginName) => {
|
|
279
|
+
const index = __privateGet(this, _plugins).findIndex(
|
|
280
|
+
(item) => item.name === pluginName
|
|
281
|
+
);
|
|
282
|
+
this.removePlugin(pluginName);
|
|
283
|
+
if (index <= i && index > 0) {
|
|
284
|
+
i--;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
__privateGet(this, _isProd)
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
__privateSet(this, _config, config);
|
|
293
|
+
return __privateGet(this, _config);
|
|
294
|
+
}
|
|
295
|
+
async beforeBuild() {
|
|
296
|
+
return this._runParallelAsyncHook(
|
|
297
|
+
"beforeBuild",
|
|
298
|
+
__privateGet(this, _config) || {},
|
|
299
|
+
__privateGet(this, _isProd)
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
async afterBuild() {
|
|
303
|
+
return this._runParallelAsyncHook(
|
|
304
|
+
"afterBuild",
|
|
305
|
+
__privateGet(this, _config) || {},
|
|
306
|
+
__privateGet(this, _isProd)
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
async modifySearchIndexData(pages) {
|
|
310
|
+
return this._runParallelAsyncHook(
|
|
311
|
+
"modifySearchIndexData",
|
|
312
|
+
pages,
|
|
313
|
+
__privateGet(this, _isProd)
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
async extendPageData(pageData) {
|
|
317
|
+
return this._runParallelAsyncHook("extendPageData", pageData, __privateGet(this, _isProd));
|
|
318
|
+
}
|
|
319
|
+
async addPages() {
|
|
320
|
+
const result = await this._runParallelAsyncHook(
|
|
321
|
+
"addPages",
|
|
322
|
+
__privateGet(this, _config) || {},
|
|
323
|
+
__privateGet(this, _isProd)
|
|
324
|
+
);
|
|
325
|
+
return result.flat();
|
|
326
|
+
}
|
|
327
|
+
async routeGenerated(routes) {
|
|
328
|
+
return this._runParallelAsyncHook("routeGenerated", routes);
|
|
329
|
+
}
|
|
330
|
+
async addRuntimeModules() {
|
|
331
|
+
const result = await this._runParallelAsyncHook(
|
|
332
|
+
"addRuntimeModules",
|
|
333
|
+
__privateGet(this, _config) || {},
|
|
334
|
+
__privateGet(this, _isProd)
|
|
335
|
+
);
|
|
336
|
+
return result.reduce((prev, current) => {
|
|
337
|
+
return {
|
|
338
|
+
...prev,
|
|
339
|
+
...current
|
|
340
|
+
};
|
|
341
|
+
}, {});
|
|
342
|
+
}
|
|
343
|
+
async addSSGRoutes() {
|
|
344
|
+
const result = await this._runParallelAsyncHook(
|
|
345
|
+
"addSSGRoutes",
|
|
346
|
+
__privateGet(this, _config) || {},
|
|
347
|
+
__privateGet(this, _isProd)
|
|
348
|
+
);
|
|
349
|
+
return result.flat();
|
|
350
|
+
}
|
|
351
|
+
globalUIComponents() {
|
|
352
|
+
const result = __privateGet(this, _plugins).map((plugin) => {
|
|
353
|
+
return plugin.globalUIComponents || [];
|
|
354
|
+
});
|
|
355
|
+
return result.flat();
|
|
356
|
+
}
|
|
357
|
+
globalStyles() {
|
|
358
|
+
return __privateGet(this, _plugins).filter((plugin) => typeof plugin.globalStyles === "string").map((plugin) => {
|
|
359
|
+
return plugin.globalStyles;
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
_runParallelAsyncHook(hookName, ...args) {
|
|
363
|
+
return Promise.all(
|
|
364
|
+
__privateGet(this, _plugins).filter((plugin) => typeof plugin[hookName] === "function").map((plugin) => {
|
|
365
|
+
return plugin[hookName](...args);
|
|
366
|
+
})
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
_runSerialAsyncHook(hookName, ...args) {
|
|
370
|
+
return __privateGet(this, _plugins).reduce(async (prev, plugin) => {
|
|
371
|
+
if (typeof plugin[hookName] === "function") {
|
|
372
|
+
await prev;
|
|
373
|
+
return plugin[hookName](...args);
|
|
374
|
+
}
|
|
375
|
+
return prev;
|
|
376
|
+
}, Promise.resolve());
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
_config = new WeakMap();
|
|
380
|
+
_plugins = new WeakMap();
|
|
381
|
+
_isProd = new WeakMap();
|
|
382
|
+
|
|
193
383
|
// src/node/initRsbuild.ts
|
|
194
384
|
import path11 from "path";
|
|
385
|
+
import { PLUGIN_LESS_NAME, pluginLess } from "@rsbuild/plugin-less";
|
|
386
|
+
import { PLUGIN_REACT_NAME, pluginReact } from "@rsbuild/plugin-react";
|
|
387
|
+
import { PLUGIN_SASS_NAME, pluginSass } from "@rsbuild/plugin-sass";
|
|
195
388
|
import {
|
|
196
|
-
removeLeadingSlash as removeLeadingSlash2,
|
|
197
389
|
MDX_REGEXP as MDX_REGEXP3,
|
|
198
390
|
RSPRESS_TEMP_DIR as RSPRESS_TEMP_DIR2,
|
|
199
|
-
|
|
200
|
-
|
|
391
|
+
isDebugMode,
|
|
392
|
+
removeLeadingSlash as removeLeadingSlash2,
|
|
393
|
+
removeTrailingSlash
|
|
201
394
|
} from "@rspress/shared";
|
|
202
395
|
import fs8 from "@rspress/shared/fs-extra";
|
|
203
396
|
|
|
@@ -242,28 +435,14 @@ var TEMP_DIR = path.join(
|
|
|
242
435
|
RSPRESS_TEMP_DIR
|
|
243
436
|
);
|
|
244
437
|
|
|
245
|
-
// src/node/
|
|
246
|
-
import
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
["virtual-routes" /* RouteForClient */]: routeService2.generateRoutesCode(false)
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
return {
|
|
257
|
-
["virtual-routes" /* RouteForClient */]: routeService2.generateRoutesCode(false),
|
|
258
|
-
["virtual-routes-ssr" /* RouteForSSR */]: routeService2.generateRoutesCode(true)
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// src/node/runtimeModule/siteData/index.ts
|
|
263
|
-
import path6 from "path";
|
|
264
|
-
import fs4 from "@rspress/shared/fs-extra";
|
|
265
|
-
import { groupBy } from "lodash-es";
|
|
266
|
-
import { SEARCH_INDEX_NAME } from "@rspress/shared";
|
|
438
|
+
// src/node/route/RouteService.ts
|
|
439
|
+
import path5 from "path";
|
|
440
|
+
import {
|
|
441
|
+
addLeadingSlash,
|
|
442
|
+
addTrailingSlash,
|
|
443
|
+
withBase
|
|
444
|
+
} from "@rspress/shared";
|
|
445
|
+
import fs3 from "@rspress/shared/fs-extra";
|
|
267
446
|
|
|
268
447
|
// src/node/utils/normalizePath.ts
|
|
269
448
|
import os from "os";
|
|
@@ -292,8 +471,8 @@ function createHash(str) {
|
|
|
292
471
|
// src/node/utils/detectReactVersion.ts
|
|
293
472
|
import path3 from "path";
|
|
294
473
|
import fs from "@rspress/shared/fs-extra";
|
|
295
|
-
import enhancedResolve from "enhanced-resolve";
|
|
296
474
|
import { logger } from "@rspress/shared/logger";
|
|
475
|
+
import enhancedResolve from "enhanced-resolve";
|
|
297
476
|
var { CachedInputFileSystem, ResolverFactory } = enhancedResolve;
|
|
298
477
|
var DEFAULT_REACT_VERSION = 18;
|
|
299
478
|
async function detectReactVersion() {
|
|
@@ -353,10 +532,10 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
353
532
|
|
|
354
533
|
// src/node/utils/flattenMdxContent.ts
|
|
355
534
|
import path4 from "path";
|
|
535
|
+
import { createProcessor } from "@mdx-js/mdx";
|
|
536
|
+
import { MDX_REGEXP } from "@rspress/shared";
|
|
356
537
|
import fs2 from "@rspress/shared/fs-extra";
|
|
357
538
|
import enhancedResolve2 from "enhanced-resolve";
|
|
358
|
-
import { MDX_REGEXP } from "@rspress/shared";
|
|
359
|
-
import { createProcessor } from "@mdx-js/mdx";
|
|
360
539
|
var resolver;
|
|
361
540
|
var startFlatten = false;
|
|
362
541
|
var processor = createProcessor();
|
|
@@ -451,217 +630,587 @@ function applyReplaceRules(code = "", replaceRules = []) {
|
|
|
451
630
|
return result;
|
|
452
631
|
}
|
|
453
632
|
|
|
454
|
-
// src/node/
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
633
|
+
// src/node/route/RouteService.ts
|
|
634
|
+
var DEFAULT_PAGE_EXTENSIONS = ["js", "jsx", "ts", "tsx", "md", "mdx"];
|
|
635
|
+
var normalizeRoutePath = (routePath, base, lang, version, langs, versions, extensions = DEFAULT_PAGE_EXTENSIONS) => {
|
|
636
|
+
const hasTrailSlash = routePath.endsWith("/");
|
|
637
|
+
let versionPart = "";
|
|
638
|
+
let langPart = "";
|
|
639
|
+
let purePathPart = "";
|
|
640
|
+
const parts = routePath.split("/").filter(Boolean);
|
|
641
|
+
if (version) {
|
|
642
|
+
const versionToMatch = parts[0];
|
|
643
|
+
if (versions.includes(versionToMatch)) {
|
|
644
|
+
if (versionToMatch !== version) {
|
|
645
|
+
versionPart = versionToMatch;
|
|
646
|
+
}
|
|
647
|
+
parts.shift();
|
|
469
648
|
}
|
|
470
649
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const defaultIndexInfo = {
|
|
477
|
-
id: index,
|
|
478
|
-
title: "",
|
|
479
|
-
content: "",
|
|
480
|
-
_html: "",
|
|
481
|
-
routePath: route.routePath,
|
|
482
|
-
lang: route.lang,
|
|
483
|
-
toc: [],
|
|
484
|
-
domain,
|
|
485
|
-
frontmatter: {},
|
|
486
|
-
version: route.version,
|
|
487
|
-
_filepath: route.absolutePath,
|
|
488
|
-
_relativePath: path5.relative(root, route.absolutePath)
|
|
489
|
-
};
|
|
490
|
-
if (!MDX_REGEXP2.test(route.absolutePath)) {
|
|
491
|
-
return defaultIndexInfo;
|
|
492
|
-
}
|
|
493
|
-
let content = await fs3.readFile(route.absolutePath, "utf8");
|
|
494
|
-
const { frontmatter, content: strippedFrontMatter } = loadFrontMatter(
|
|
495
|
-
content,
|
|
496
|
-
route.absolutePath,
|
|
497
|
-
root
|
|
498
|
-
);
|
|
499
|
-
applyReplaceRulesToNestedObject(frontmatter, replaceRules);
|
|
500
|
-
const { flattenContent } = await flattenMdxContent(
|
|
501
|
-
applyReplaceRules(strippedFrontMatter, replaceRules),
|
|
502
|
-
route.absolutePath,
|
|
503
|
-
alias
|
|
504
|
-
);
|
|
505
|
-
content = flattenContent.replace(importStatementRegex, "");
|
|
506
|
-
const {
|
|
507
|
-
html,
|
|
508
|
-
title,
|
|
509
|
-
toc: rawToc,
|
|
510
|
-
languages
|
|
511
|
-
} = await compile({
|
|
512
|
-
value: content,
|
|
513
|
-
filepath: route.absolutePath,
|
|
514
|
-
development: process.env.NODE_ENV !== "production",
|
|
515
|
-
root
|
|
516
|
-
});
|
|
517
|
-
if (!title?.length && !frontmatter && !frontmatter.title?.length) {
|
|
518
|
-
return null;
|
|
519
|
-
}
|
|
520
|
-
content = htmlToText(String(html), {
|
|
521
|
-
wordwrap: 80,
|
|
522
|
-
selectors: [
|
|
523
|
-
{
|
|
524
|
-
selector: "a",
|
|
525
|
-
options: {
|
|
526
|
-
ignoreHref: true
|
|
527
|
-
}
|
|
528
|
-
},
|
|
529
|
-
{
|
|
530
|
-
selector: "img",
|
|
531
|
-
format: "skip"
|
|
532
|
-
},
|
|
533
|
-
{
|
|
534
|
-
// Skip code blocks
|
|
535
|
-
selector: "pre > code",
|
|
536
|
-
format: "skip"
|
|
537
|
-
},
|
|
538
|
-
...["h1", "h2", "h3", "h4", "h5", "h6"].map((tag) => ({
|
|
539
|
-
selector: tag,
|
|
540
|
-
options: {
|
|
541
|
-
uppercase: false
|
|
542
|
-
}
|
|
543
|
-
}))
|
|
544
|
-
],
|
|
545
|
-
tables: true,
|
|
546
|
-
longWordSplit: {
|
|
547
|
-
forceWrapOnLimit: true
|
|
548
|
-
}
|
|
549
|
-
});
|
|
550
|
-
if (content.startsWith(title)) {
|
|
551
|
-
content = content.slice(title.length);
|
|
650
|
+
if (lang) {
|
|
651
|
+
const langToMatch = parts[0];
|
|
652
|
+
if (langs.includes(langToMatch)) {
|
|
653
|
+
if (langToMatch !== lang) {
|
|
654
|
+
langPart = langToMatch;
|
|
552
655
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
`, position + 1);
|
|
563
|
-
if (position === -1) {
|
|
564
|
-
break;
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
return {
|
|
569
|
-
...item,
|
|
570
|
-
charIndex: content.indexOf(`
|
|
571
|
-
${item.text}#
|
|
572
|
-
|
|
573
|
-
`, position + 1)
|
|
574
|
-
};
|
|
575
|
-
});
|
|
576
|
-
return {
|
|
577
|
-
...defaultIndexInfo,
|
|
578
|
-
title: frontmatter.title || title,
|
|
579
|
-
toc,
|
|
580
|
-
// Stripped frontmatter content
|
|
581
|
-
content,
|
|
582
|
-
_html: html,
|
|
583
|
-
frontmatter: {
|
|
584
|
-
...frontmatter,
|
|
585
|
-
__content: void 0
|
|
586
|
-
}
|
|
587
|
-
};
|
|
588
|
-
})
|
|
589
|
-
);
|
|
590
|
-
return pageData.filter(Boolean);
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
// src/node/runtimeModule/siteData/normalizeThemeConfig.ts
|
|
594
|
-
import {
|
|
595
|
-
withBase,
|
|
596
|
-
addLeadingSlash,
|
|
597
|
-
isExternalUrl,
|
|
598
|
-
withoutBase,
|
|
599
|
-
slash as slash2
|
|
600
|
-
} from "@rspress/shared";
|
|
601
|
-
|
|
602
|
-
// src/node/runtimeModule/i18n.ts
|
|
603
|
-
import { join } from "path";
|
|
604
|
-
var DEFAULT_I18N_SOURCE = join(process.cwd(), "i18n.json");
|
|
605
|
-
function getI18nData(docConfig) {
|
|
606
|
-
const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig;
|
|
607
|
-
try {
|
|
608
|
-
delete __require.cache[i18nSourcePath];
|
|
609
|
-
const i18nSource = __require(i18nSourcePath);
|
|
610
|
-
return i18nSource;
|
|
611
|
-
} catch (e) {
|
|
612
|
-
return {};
|
|
656
|
+
parts.shift();
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
purePathPart = parts.join("/");
|
|
660
|
+
let normalizedRoutePath = addLeadingSlash(
|
|
661
|
+
[versionPart, langPart, purePathPart].filter(Boolean).join("/")
|
|
662
|
+
).replace(new RegExp(`\\.(${extensions.join("|")})$`), "").replace(/\.html$/, "").replace(/\/index$/, "/");
|
|
663
|
+
if (hasTrailSlash) {
|
|
664
|
+
normalizedRoutePath = addTrailingSlash(normalizedRoutePath);
|
|
613
665
|
}
|
|
614
|
-
}
|
|
615
|
-
function i18nVMPlugin(context) {
|
|
616
|
-
const { config } = context;
|
|
617
|
-
const i18nData = getI18nData(config);
|
|
618
666
|
return {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
// src/node/runtimeModule/siteData/normalizeThemeConfig.ts
|
|
624
|
-
function normalizeThemeConfig(docConfig, pages = []) {
|
|
625
|
-
const {
|
|
626
|
-
locales: siteLocales,
|
|
627
|
-
base = "",
|
|
628
|
-
lang,
|
|
629
|
-
replaceRules = [],
|
|
630
|
-
multiVersion
|
|
631
|
-
} = docConfig;
|
|
632
|
-
const { versions = [] } = multiVersion || {};
|
|
633
|
-
const hasMultiVersion = versions.length > 0;
|
|
634
|
-
docConfig.themeConfig = docConfig.themeConfig || {};
|
|
635
|
-
const { themeConfig } = docConfig;
|
|
636
|
-
const locales = siteLocales ?? (themeConfig?.locales || []);
|
|
637
|
-
const i18nTextData = getI18nData(docConfig);
|
|
638
|
-
const normalizeLinkPrefix = (link = "", currentLang = "") => {
|
|
639
|
-
const normalizedLink = slash2(link);
|
|
640
|
-
if (!currentLang || !link || withoutBase(normalizedLink, base).startsWith(`/${currentLang}`) || isExternalUrl(normalizedLink) || // In multi version case, we have got the complete link prefix in `plugin-auto-nav-sidebar` and does not need to add the lang prefix
|
|
641
|
-
hasMultiVersion) {
|
|
642
|
-
return normalizedLink;
|
|
643
|
-
}
|
|
644
|
-
return lang === currentLang ? normalizedLink : `/${currentLang}${addLeadingSlash(normalizedLink)}`;
|
|
645
|
-
};
|
|
646
|
-
const getI18nText = (key = "", currentLang = "") => {
|
|
647
|
-
const text = i18nTextData[key]?.[currentLang];
|
|
648
|
-
return text || key;
|
|
667
|
+
routePath: withBase(normalizedRoutePath, base),
|
|
668
|
+
lang: langPart || lang,
|
|
669
|
+
version: versionPart || version
|
|
649
670
|
};
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
671
|
+
};
|
|
672
|
+
var _scanDir, _defaultLang, _defaultVersion, _extensions, _langs, _versions, _include, _exclude, _base, _tempDir, _pluginDriver, _writeTempFile, writeTempFile_fn, _generateRouteInfo, generateRouteInfo_fn;
|
|
673
|
+
var RouteService = class {
|
|
674
|
+
constructor(scanDir, userConfig, tempDir, pluginDriver) {
|
|
675
|
+
__privateAdd(this, _writeTempFile);
|
|
676
|
+
__privateAdd(this, _generateRouteInfo);
|
|
677
|
+
__publicField(this, "routeData", /* @__PURE__ */ new Map());
|
|
678
|
+
__privateAdd(this, _scanDir, void 0);
|
|
679
|
+
__privateAdd(this, _defaultLang, void 0);
|
|
680
|
+
__privateAdd(this, _defaultVersion, "");
|
|
681
|
+
__privateAdd(this, _extensions, []);
|
|
682
|
+
__privateAdd(this, _langs, []);
|
|
683
|
+
__privateAdd(this, _versions, []);
|
|
684
|
+
__privateAdd(this, _include, []);
|
|
685
|
+
__privateAdd(this, _exclude, []);
|
|
686
|
+
__privateAdd(this, _base, "");
|
|
687
|
+
__privateAdd(this, _tempDir, "");
|
|
688
|
+
__privateAdd(this, _pluginDriver, void 0);
|
|
689
|
+
const routeOptions = userConfig?.route || {};
|
|
690
|
+
__privateSet(this, _scanDir, scanDir);
|
|
691
|
+
__privateSet(this, _extensions, routeOptions.extensions || DEFAULT_PAGE_EXTENSIONS);
|
|
692
|
+
__privateSet(this, _include, routeOptions.include || []);
|
|
693
|
+
__privateSet(this, _exclude, routeOptions.exclude || []);
|
|
694
|
+
__privateSet(this, _defaultLang, userConfig?.lang || "");
|
|
695
|
+
__privateSet(this, _langs, (userConfig?.locales ?? userConfig?.themeConfig?.locales ?? []).map((item) => item.lang));
|
|
696
|
+
__privateSet(this, _base, userConfig?.base || "");
|
|
697
|
+
__privateSet(this, _tempDir, tempDir);
|
|
698
|
+
__privateSet(this, _pluginDriver, pluginDriver);
|
|
699
|
+
if (userConfig.multiVersion) {
|
|
700
|
+
__privateSet(this, _defaultVersion, userConfig.multiVersion.default || "");
|
|
701
|
+
__privateSet(this, _versions, userConfig.multiVersion.versions || []);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
async init() {
|
|
705
|
+
const globby = (await import(
|
|
706
|
+
// @ts-expect-error
|
|
707
|
+
// eslint-disable-next-line node/file-extension-in-import
|
|
708
|
+
"../compiled/globby/index.js"
|
|
709
|
+
)).default;
|
|
710
|
+
const files = (await globby([`**/*.{${__privateGet(this, _extensions).join(",")}}`, ...__privateGet(this, _include)], {
|
|
711
|
+
cwd: __privateGet(this, _scanDir),
|
|
712
|
+
absolute: true,
|
|
713
|
+
ignore: [
|
|
714
|
+
...__privateGet(this, _exclude),
|
|
715
|
+
"**/node_modules/**",
|
|
716
|
+
"**/.eslintrc.js",
|
|
717
|
+
"**/.nx-cache/**",
|
|
718
|
+
`./${PUBLIC_DIR}/**`
|
|
719
|
+
]
|
|
720
|
+
})).sort();
|
|
721
|
+
files.forEach((filePath) => {
|
|
722
|
+
const fileRelativePath = normalizePath(
|
|
723
|
+
path5.relative(__privateGet(this, _scanDir), filePath)
|
|
724
|
+
);
|
|
725
|
+
const { routePath, lang, version } = this.normalizeRoutePath(fileRelativePath);
|
|
726
|
+
const absolutePath = path5.join(__privateGet(this, _scanDir), fileRelativePath);
|
|
727
|
+
const routeInfo = {
|
|
728
|
+
routePath,
|
|
729
|
+
absolutePath: normalizePath(absolutePath),
|
|
730
|
+
relativePath: fileRelativePath,
|
|
731
|
+
pageName: getPageKey(fileRelativePath),
|
|
732
|
+
lang,
|
|
733
|
+
version
|
|
734
|
+
};
|
|
735
|
+
this.addRoute(routeInfo);
|
|
736
|
+
});
|
|
737
|
+
const externalPages = await __privateGet(this, _pluginDriver).addPages();
|
|
738
|
+
await Promise.all(
|
|
739
|
+
externalPages.map(async (route, index) => {
|
|
740
|
+
const { routePath, content, filepath } = route;
|
|
741
|
+
if (filepath) {
|
|
742
|
+
const routeInfo = __privateMethod(this, _generateRouteInfo, generateRouteInfo_fn).call(this, routePath, filepath);
|
|
743
|
+
this.addRoute(routeInfo);
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
if (content) {
|
|
747
|
+
const filepath2 = await __privateMethod(this, _writeTempFile, writeTempFile_fn).call(this, index, content);
|
|
748
|
+
const routeInfo = __privateMethod(this, _generateRouteInfo, generateRouteInfo_fn).call(this, routePath, filepath2);
|
|
749
|
+
this.addRoute(routeInfo);
|
|
750
|
+
}
|
|
751
|
+
})
|
|
752
|
+
);
|
|
753
|
+
await __privateGet(this, _pluginDriver).routeGenerated(this.getRoutes());
|
|
754
|
+
}
|
|
755
|
+
addRoute(routeInfo) {
|
|
756
|
+
const { routePath, absolutePath } = routeInfo;
|
|
757
|
+
if (this.routeData.has(routePath)) {
|
|
758
|
+
const preRouteExtIndex = __privateGet(this, _extensions).indexOf(
|
|
759
|
+
path5.extname(this.routeData.get(routePath).absolutePath).slice(1)
|
|
760
|
+
);
|
|
761
|
+
const currRouteExtIndex = __privateGet(this, _extensions).indexOf(
|
|
762
|
+
path5.extname(absolutePath).slice(1)
|
|
763
|
+
);
|
|
764
|
+
if (currRouteExtIndex !== -1 && (currRouteExtIndex < preRouteExtIndex || preRouteExtIndex === -1)) {
|
|
765
|
+
this.routeData.set(routePath, routeInfo);
|
|
766
|
+
}
|
|
767
|
+
} else {
|
|
768
|
+
this.routeData.set(routePath, routeInfo);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
removeRoute(filePath) {
|
|
772
|
+
const fileRelativePath = path5.relative(__privateGet(this, _scanDir), filePath);
|
|
773
|
+
const { routePath } = this.normalizeRoutePath(fileRelativePath);
|
|
774
|
+
this.routeData.delete(routePath);
|
|
775
|
+
}
|
|
776
|
+
getRoutes() {
|
|
777
|
+
return Array.from(this.routeData.values());
|
|
778
|
+
}
|
|
779
|
+
isExistRoute(routePath) {
|
|
780
|
+
const { routePath: normalizedRoute } = this.normalizeRoutePath(routePath);
|
|
781
|
+
return this.routeData.get(normalizedRoute);
|
|
782
|
+
}
|
|
783
|
+
isEmpty() {
|
|
784
|
+
return this.routeData.size === 0;
|
|
785
|
+
}
|
|
786
|
+
generateRoutesCode(isStaticImport = false) {
|
|
787
|
+
return this.generateRoutesCodeByRouteMeta(this.getRoutes(), isStaticImport);
|
|
788
|
+
}
|
|
789
|
+
generateRoutesCodeByRouteMeta(routeMeta, isStaticImport) {
|
|
790
|
+
return `
|
|
791
|
+
import React from 'react';
|
|
792
|
+
import { lazyWithPreload } from "react-lazy-with-preload";
|
|
793
|
+
${routeMeta.map((route, index) => {
|
|
794
|
+
return isStaticImport ? `import * as Route${index} from '${route.absolutePath}';` : `const Route${index} = lazyWithPreload(() => import('${route.absolutePath}'))`;
|
|
795
|
+
}).join("\n")}
|
|
796
|
+
export const routes = [
|
|
797
|
+
${routeMeta.map((route, index) => {
|
|
798
|
+
const preload = isStaticImport ? `() => Route${index}` : `async () => {
|
|
799
|
+
await Route${index}.preload();
|
|
800
|
+
return import("${route.absolutePath}");
|
|
801
|
+
}`;
|
|
802
|
+
const component = isStaticImport ? `Route${index}.default` : `Route${index}`;
|
|
803
|
+
return `{ path: '${route.routePath}', element: React.createElement(${component}), filePath: '${route.relativePath}', preload: ${preload}, lang: '${route.lang}', version: '${route.version}' }`;
|
|
804
|
+
}).join(",\n")}
|
|
805
|
+
];
|
|
806
|
+
`;
|
|
807
|
+
}
|
|
808
|
+
normalizeRoutePath(routePath) {
|
|
809
|
+
return normalizeRoutePath(
|
|
810
|
+
routePath,
|
|
811
|
+
__privateGet(this, _base),
|
|
812
|
+
__privateGet(this, _defaultLang),
|
|
813
|
+
__privateGet(this, _defaultVersion),
|
|
814
|
+
__privateGet(this, _langs),
|
|
815
|
+
__privateGet(this, _versions),
|
|
816
|
+
__privateGet(this, _extensions)
|
|
817
|
+
);
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
_scanDir = new WeakMap();
|
|
821
|
+
_defaultLang = new WeakMap();
|
|
822
|
+
_defaultVersion = new WeakMap();
|
|
823
|
+
_extensions = new WeakMap();
|
|
824
|
+
_langs = new WeakMap();
|
|
825
|
+
_versions = new WeakMap();
|
|
826
|
+
_include = new WeakMap();
|
|
827
|
+
_exclude = new WeakMap();
|
|
828
|
+
_base = new WeakMap();
|
|
829
|
+
_tempDir = new WeakMap();
|
|
830
|
+
_pluginDriver = new WeakMap();
|
|
831
|
+
_writeTempFile = new WeakSet();
|
|
832
|
+
writeTempFile_fn = async function(index, content) {
|
|
833
|
+
const tempFilePath = path5.join(__privateGet(this, _tempDir), `temp-${index}.mdx`);
|
|
834
|
+
await fs3.writeFile(tempFilePath, content);
|
|
835
|
+
return tempFilePath;
|
|
836
|
+
};
|
|
837
|
+
_generateRouteInfo = new WeakSet();
|
|
838
|
+
generateRouteInfo_fn = function(routePath, filepath) {
|
|
839
|
+
const {
|
|
840
|
+
routePath: normalizedPath,
|
|
841
|
+
lang,
|
|
842
|
+
version
|
|
843
|
+
} = this.normalizeRoutePath(routePath);
|
|
844
|
+
return {
|
|
845
|
+
routePath: normalizedPath,
|
|
846
|
+
absolutePath: normalizePath(filepath),
|
|
847
|
+
relativePath: normalizePath(path5.relative(__privateGet(this, _scanDir), filepath)),
|
|
848
|
+
pageName: getPageKey(routePath),
|
|
849
|
+
lang,
|
|
850
|
+
version
|
|
851
|
+
};
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
// src/node/route/init.ts
|
|
855
|
+
var routeService = null;
|
|
856
|
+
async function initRouteService(options) {
|
|
857
|
+
const { scanDir, config, runtimeTempDir, pluginDriver } = options;
|
|
858
|
+
routeService = new RouteService(
|
|
859
|
+
scanDir,
|
|
860
|
+
config,
|
|
861
|
+
runtimeTempDir,
|
|
862
|
+
pluginDriver
|
|
863
|
+
);
|
|
864
|
+
await routeService.init();
|
|
865
|
+
return routeService;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
// src/node/runtimeModule/index.ts
|
|
869
|
+
import { RspackVirtualModulePlugin } from "rspack-plugin-virtual-module";
|
|
870
|
+
|
|
871
|
+
// src/node/runtimeModule/globalStyles.ts
|
|
872
|
+
import fs4 from "@rspress/shared/fs-extra";
|
|
873
|
+
async function globalStylesVMPlugin(context) {
|
|
874
|
+
const { config, pluginDriver } = context;
|
|
875
|
+
const globalStylesByPlugins = pluginDriver.globalStyles();
|
|
876
|
+
if (config.globalStyles) {
|
|
877
|
+
const source = config.globalStyles;
|
|
878
|
+
const styleContent = await fs4.readFile(source, "utf-8");
|
|
879
|
+
const patchedStyleContent = styleContent.replace(/--modern-/g, "--rp-").replace(/\.modern-doc/g, ".rspress").replace(/\.modern-/g, ".rspress-");
|
|
880
|
+
await fs4.writeFile(source, patchedStyleContent, "utf-8");
|
|
881
|
+
}
|
|
882
|
+
const moduleContent = (await Promise.all(
|
|
883
|
+
[config?.globalStyles || "", ...globalStylesByPlugins].filter((source) => source.length > 0).map(async (source) => {
|
|
884
|
+
return `import ${JSON.stringify(source)};`;
|
|
885
|
+
})
|
|
886
|
+
)).join("");
|
|
887
|
+
return {
|
|
888
|
+
["virtual-global-styles" /* GlobalStyles */]: moduleContent
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
// src/node/runtimeModule/globalUIComponents.ts
|
|
893
|
+
async function globalUIComponentsVMPlugin(context) {
|
|
894
|
+
const { config, pluginDriver } = context;
|
|
895
|
+
let index = 0;
|
|
896
|
+
const globalUIComponentsByPlugins = pluginDriver.globalUIComponents();
|
|
897
|
+
const globalComponents = [
|
|
898
|
+
...config?.globalUIComponents || [],
|
|
899
|
+
...globalUIComponentsByPlugins
|
|
900
|
+
];
|
|
901
|
+
const moduleContent = globalComponents.map((source) => {
|
|
902
|
+
const name = `Comp_${index}`;
|
|
903
|
+
if (Array.isArray(source)) {
|
|
904
|
+
return `import ${name} from ${JSON.stringify(source[0])};
|
|
905
|
+
const Props_${index++} = ${JSON.stringify(source[1])};
|
|
906
|
+
`;
|
|
907
|
+
}
|
|
908
|
+
index++;
|
|
909
|
+
return `import ${name} from ${JSON.stringify(source)};
|
|
910
|
+
`;
|
|
911
|
+
}).concat(
|
|
912
|
+
`export default [${Array.from({ length: index }, (_, i) => {
|
|
913
|
+
if (Array.isArray(globalComponents[i])) {
|
|
914
|
+
return `[${`Comp_${i}`}, Props_${i}]`;
|
|
915
|
+
}
|
|
916
|
+
return `Comp_${i}`;
|
|
917
|
+
}).join(", ")}];`
|
|
918
|
+
).join("");
|
|
919
|
+
return {
|
|
920
|
+
["virtual-global-components" /* GlobalComponents */]: moduleContent
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// src/node/runtimeModule/i18n.ts
|
|
925
|
+
import { join } from "path";
|
|
926
|
+
var DEFAULT_I18N_SOURCE = join(process.cwd(), "i18n.json");
|
|
927
|
+
function getI18nData(docConfig) {
|
|
928
|
+
const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig;
|
|
929
|
+
try {
|
|
930
|
+
delete __require.cache[i18nSourcePath];
|
|
931
|
+
const i18nSource = __require(i18nSourcePath);
|
|
932
|
+
return i18nSource;
|
|
933
|
+
} catch (e) {
|
|
934
|
+
return {};
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
function i18nVMPlugin(context) {
|
|
938
|
+
const { config } = context;
|
|
939
|
+
const i18nData = getI18nData(config);
|
|
940
|
+
return {
|
|
941
|
+
["virtual-i18n-text" /* I18nText */]: `export default ${JSON.stringify(i18nData, null, 2)}`
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
// src/node/runtimeModule/routeData.ts
|
|
946
|
+
async function routeVMPlugin(context) {
|
|
947
|
+
const { routeService: routeService2, isSSR } = context;
|
|
948
|
+
if (!isSSR) {
|
|
949
|
+
return {
|
|
950
|
+
["virtual-routes" /* RouteForClient */]: routeService2.generateRoutesCode(false)
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
return {
|
|
954
|
+
["virtual-routes" /* RouteForClient */]: routeService2.generateRoutesCode(false),
|
|
955
|
+
["virtual-routes-ssr" /* RouteForSSR */]: routeService2.generateRoutesCode(true)
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
// src/node/runtimeModule/searchHooks.ts
|
|
960
|
+
import path6 from "path";
|
|
961
|
+
async function searchHookVMPlugin(context) {
|
|
962
|
+
const { config } = context;
|
|
963
|
+
let content = "export const onSearch = () => {};";
|
|
964
|
+
let extname = "";
|
|
965
|
+
if (typeof config.search === "object" && typeof config.search.searchHooks === "string") {
|
|
966
|
+
content = `export * from '${config.search.searchHooks}'`;
|
|
967
|
+
extname = path6.extname(config.search.searchHooks);
|
|
968
|
+
}
|
|
969
|
+
return {
|
|
970
|
+
[`${"virtual-search-hooks" /* SearchHooks */}${extname}`]: content
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
// src/node/runtimeModule/siteData/index.ts
|
|
975
|
+
import path8 from "path";
|
|
976
|
+
import { SEARCH_INDEX_NAME } from "@rspress/shared";
|
|
977
|
+
import fs6 from "@rspress/shared/fs-extra";
|
|
978
|
+
import { groupBy } from "lodash-es";
|
|
979
|
+
|
|
980
|
+
// src/node/runtimeModule/siteData/extractPageData.ts
|
|
981
|
+
import path7 from "path";
|
|
982
|
+
import { compile } from "@rspress/mdx-rs";
|
|
983
|
+
import {
|
|
984
|
+
MDX_REGEXP as MDX_REGEXP2
|
|
985
|
+
} from "@rspress/shared";
|
|
986
|
+
import fs5 from "@rspress/shared/fs-extra";
|
|
987
|
+
import { loadFrontMatter } from "@rspress/shared/node-utils";
|
|
988
|
+
import { htmlToText } from "html-to-text";
|
|
989
|
+
function applyReplaceRulesToNestedObject(obj, replaceRules) {
|
|
990
|
+
for (const key in obj) {
|
|
991
|
+
if (typeof obj[key] === "string") {
|
|
992
|
+
obj[key] = applyReplaceRules(obj[key], replaceRules);
|
|
993
|
+
} else if (typeof obj[key] === "object") {
|
|
994
|
+
obj[key] = applyReplaceRulesToNestedObject(obj[key], replaceRules);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return obj;
|
|
998
|
+
}
|
|
999
|
+
async function extractPageData(replaceRules, alias, domain, root, routeService2, highlighterLangs) {
|
|
1000
|
+
const pageData = await Promise.all(
|
|
1001
|
+
routeService2.getRoutes().map(async (route, index) => {
|
|
1002
|
+
const defaultIndexInfo = {
|
|
1003
|
+
id: index,
|
|
1004
|
+
title: "",
|
|
1005
|
+
content: "",
|
|
1006
|
+
_html: "",
|
|
1007
|
+
routePath: route.routePath,
|
|
1008
|
+
lang: route.lang,
|
|
1009
|
+
toc: [],
|
|
1010
|
+
domain,
|
|
1011
|
+
frontmatter: {},
|
|
1012
|
+
version: route.version,
|
|
1013
|
+
_filepath: route.absolutePath,
|
|
1014
|
+
_relativePath: path7.relative(root, route.absolutePath)
|
|
1015
|
+
};
|
|
1016
|
+
if (!MDX_REGEXP2.test(route.absolutePath)) {
|
|
1017
|
+
return defaultIndexInfo;
|
|
1018
|
+
}
|
|
1019
|
+
let content = await fs5.readFile(route.absolutePath, "utf8");
|
|
1020
|
+
const { frontmatter, content: strippedFrontMatter } = loadFrontMatter(
|
|
1021
|
+
content,
|
|
1022
|
+
route.absolutePath,
|
|
1023
|
+
root
|
|
1024
|
+
);
|
|
1025
|
+
applyReplaceRulesToNestedObject(frontmatter, replaceRules);
|
|
1026
|
+
const { flattenContent } = await flattenMdxContent(
|
|
1027
|
+
applyReplaceRules(strippedFrontMatter, replaceRules),
|
|
1028
|
+
route.absolutePath,
|
|
1029
|
+
alias
|
|
1030
|
+
);
|
|
1031
|
+
content = flattenContent.replace(importStatementRegex, "");
|
|
1032
|
+
const {
|
|
1033
|
+
html,
|
|
1034
|
+
title,
|
|
1035
|
+
toc: rawToc,
|
|
1036
|
+
languages
|
|
1037
|
+
} = await compile({
|
|
1038
|
+
value: content,
|
|
1039
|
+
filepath: route.absolutePath,
|
|
1040
|
+
development: process.env.NODE_ENV !== "production",
|
|
1041
|
+
root
|
|
1042
|
+
});
|
|
1043
|
+
if (!title?.length && !frontmatter && !frontmatter.title?.length) {
|
|
1044
|
+
return null;
|
|
1045
|
+
}
|
|
1046
|
+
content = htmlToText(String(html), {
|
|
1047
|
+
wordwrap: 80,
|
|
1048
|
+
selectors: [
|
|
1049
|
+
{
|
|
1050
|
+
selector: "a",
|
|
1051
|
+
options: {
|
|
1052
|
+
ignoreHref: true
|
|
1053
|
+
}
|
|
1054
|
+
},
|
|
1055
|
+
{
|
|
1056
|
+
selector: "img",
|
|
1057
|
+
format: "skip"
|
|
1058
|
+
},
|
|
1059
|
+
{
|
|
1060
|
+
// Skip code blocks
|
|
1061
|
+
selector: "pre > code",
|
|
1062
|
+
format: "skip"
|
|
1063
|
+
},
|
|
1064
|
+
...["h1", "h2", "h3", "h4", "h5", "h6"].map((tag) => ({
|
|
1065
|
+
selector: tag,
|
|
1066
|
+
options: {
|
|
1067
|
+
uppercase: false
|
|
1068
|
+
}
|
|
1069
|
+
}))
|
|
1070
|
+
],
|
|
1071
|
+
tables: true,
|
|
1072
|
+
longWordSplit: {
|
|
1073
|
+
forceWrapOnLimit: true
|
|
1074
|
+
}
|
|
1075
|
+
});
|
|
1076
|
+
if (content.startsWith(title)) {
|
|
1077
|
+
content = content.slice(title.length);
|
|
1078
|
+
}
|
|
1079
|
+
languages.forEach((lang) => highlighterLangs.add(lang));
|
|
1080
|
+
const toc = rawToc.map((item) => {
|
|
1081
|
+
const match = item.id.match(/-(\d+)$/);
|
|
1082
|
+
let position = -1;
|
|
1083
|
+
if (match) {
|
|
1084
|
+
for (let i = 0; i < Number(match[1]); i++) {
|
|
1085
|
+
position = content.indexOf(`
|
|
1086
|
+
${item.text}#
|
|
1087
|
+
|
|
1088
|
+
`, position + 1);
|
|
1089
|
+
if (position === -1) {
|
|
1090
|
+
break;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
return {
|
|
1095
|
+
...item,
|
|
1096
|
+
charIndex: content.indexOf(`
|
|
1097
|
+
${item.text}#
|
|
1098
|
+
|
|
1099
|
+
`, position + 1)
|
|
1100
|
+
};
|
|
1101
|
+
});
|
|
1102
|
+
return {
|
|
1103
|
+
...defaultIndexInfo,
|
|
1104
|
+
title: frontmatter.title || title,
|
|
1105
|
+
toc,
|
|
1106
|
+
// Stripped frontmatter content
|
|
1107
|
+
content,
|
|
1108
|
+
_html: html,
|
|
1109
|
+
frontmatter: {
|
|
1110
|
+
...frontmatter,
|
|
1111
|
+
__content: void 0
|
|
1112
|
+
}
|
|
1113
|
+
};
|
|
1114
|
+
})
|
|
1115
|
+
);
|
|
1116
|
+
return pageData.filter(Boolean);
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
// src/node/runtimeModule/siteData/highlightLanguages.ts
|
|
1120
|
+
import { DEFAULT_HIGHLIGHT_LANGUAGES } from "@rspress/shared";
|
|
1121
|
+
import chalk from "@rspress/shared/chalk";
|
|
1122
|
+
var supportedLanguages;
|
|
1123
|
+
function handleHighlightLanguages(highlightLanguages, defaultLanguages) {
|
|
1124
|
+
const aliases = {};
|
|
1125
|
+
if (highlightLanguages.size) {
|
|
1126
|
+
if (!supportedLanguages) {
|
|
1127
|
+
const langs = __require("react-syntax-highlighter/dist/cjs/languages/prism/supported-languages").default;
|
|
1128
|
+
supportedLanguages = new Set(langs);
|
|
1129
|
+
}
|
|
1130
|
+
let useDeprecatedTypeWarning = true;
|
|
1131
|
+
const names = {};
|
|
1132
|
+
[...DEFAULT_HIGHLIGHT_LANGUAGES, ...defaultLanguages].forEach((lang) => {
|
|
1133
|
+
if (Array.isArray(lang)) {
|
|
1134
|
+
const [alias, name] = lang;
|
|
1135
|
+
names[alias] = name;
|
|
1136
|
+
} else if (useDeprecatedTypeWarning) {
|
|
1137
|
+
console.log(
|
|
1138
|
+
`${chalk.yellowBright(
|
|
1139
|
+
"warning"
|
|
1140
|
+
)} Automatic import is supported. \`highlightLanguages\` is now used only as alias, and string types will be ignored.
|
|
1141
|
+
`
|
|
1142
|
+
);
|
|
1143
|
+
useDeprecatedTypeWarning = false;
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
1146
|
+
[...highlightLanguages.values()].forEach((lang) => {
|
|
1147
|
+
const name = names[lang];
|
|
1148
|
+
if (name && supportedLanguages.has(name)) {
|
|
1149
|
+
const temp = aliases[name] || (aliases[name] = []);
|
|
1150
|
+
if (!temp.includes(lang)) {
|
|
1151
|
+
temp.push(lang);
|
|
1152
|
+
}
|
|
1153
|
+
highlightLanguages.delete(lang);
|
|
1154
|
+
highlightLanguages.add(name);
|
|
1155
|
+
return;
|
|
1156
|
+
}
|
|
1157
|
+
if (!supportedLanguages.has(lang)) {
|
|
1158
|
+
highlightLanguages.delete(lang);
|
|
1159
|
+
}
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
1162
|
+
return aliases;
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
// src/node/runtimeModule/siteData/normalizeThemeConfig.ts
|
|
1166
|
+
import {
|
|
1167
|
+
addLeadingSlash as addLeadingSlash2,
|
|
1168
|
+
isExternalUrl,
|
|
1169
|
+
slash as slash2,
|
|
1170
|
+
withBase as withBase2,
|
|
1171
|
+
withoutBase
|
|
1172
|
+
} from "@rspress/shared";
|
|
1173
|
+
function normalizeThemeConfig(docConfig, pages = []) {
|
|
1174
|
+
const {
|
|
1175
|
+
locales: siteLocales,
|
|
1176
|
+
base = "",
|
|
1177
|
+
lang,
|
|
1178
|
+
replaceRules = [],
|
|
1179
|
+
multiVersion
|
|
1180
|
+
} = docConfig;
|
|
1181
|
+
const { versions = [] } = multiVersion || {};
|
|
1182
|
+
const hasMultiVersion = versions.length > 0;
|
|
1183
|
+
docConfig.themeConfig = docConfig.themeConfig || {};
|
|
1184
|
+
const { themeConfig } = docConfig;
|
|
1185
|
+
const locales = siteLocales ?? (themeConfig?.locales || []);
|
|
1186
|
+
const i18nTextData = getI18nData(docConfig);
|
|
1187
|
+
const normalizeLinkPrefix = (link = "", currentLang = "") => {
|
|
1188
|
+
const normalizedLink = slash2(link);
|
|
1189
|
+
if (!currentLang || !link || withoutBase(normalizedLink, base).startsWith(`/${currentLang}`) || isExternalUrl(normalizedLink) || // In multi version case, we have got the complete link prefix in `plugin-auto-nav-sidebar` and does not need to add the lang prefix
|
|
1190
|
+
hasMultiVersion) {
|
|
1191
|
+
return normalizedLink;
|
|
1192
|
+
}
|
|
1193
|
+
return lang === currentLang ? normalizedLink : `/${currentLang}${addLeadingSlash2(normalizedLink)}`;
|
|
1194
|
+
};
|
|
1195
|
+
const getI18nText = (key = "", currentLang = "") => {
|
|
1196
|
+
const text = i18nTextData[key]?.[currentLang];
|
|
1197
|
+
return text || key;
|
|
1198
|
+
};
|
|
1199
|
+
const normalizeSidebar = (sidebar, currentLang = "") => {
|
|
1200
|
+
const normalizedSidebar = {};
|
|
1201
|
+
if (!sidebar) {
|
|
1202
|
+
return {};
|
|
1203
|
+
}
|
|
1204
|
+
const normalizeSidebarItem = (item) => {
|
|
1205
|
+
if (typeof item === "object" && "dividerType" in item) {
|
|
1206
|
+
return item;
|
|
1207
|
+
}
|
|
1208
|
+
if (typeof item === "object" && "sectionHeaderText" in item) {
|
|
1209
|
+
item.sectionHeaderText = applyReplaceRules(
|
|
1210
|
+
getI18nText(item.sectionHeaderText, currentLang),
|
|
1211
|
+
replaceRules
|
|
1212
|
+
);
|
|
1213
|
+
return item;
|
|
665
1214
|
}
|
|
666
1215
|
if (typeof item === "object" && "items" in item) {
|
|
667
1216
|
return {
|
|
@@ -682,7 +1231,7 @@ function normalizeThemeConfig(docConfig, pages = []) {
|
|
|
682
1231
|
if (typeof item === "string") {
|
|
683
1232
|
const normalizedItem = normalizeLinkPrefix(item, currentLang);
|
|
684
1233
|
const page = pages.find(
|
|
685
|
-
(page2) => page2.routePath ===
|
|
1234
|
+
(page2) => page2.routePath === withBase2(normalizedItem, base)
|
|
686
1235
|
);
|
|
687
1236
|
return {
|
|
688
1237
|
text: applyReplaceRules(page?.title || "", replaceRules),
|
|
@@ -777,52 +1326,6 @@ function normalizeThemeConfig(docConfig, pages = []) {
|
|
|
777
1326
|
return themeConfig;
|
|
778
1327
|
}
|
|
779
1328
|
|
|
780
|
-
// src/node/runtimeModule/siteData/highlightLanguages.ts
|
|
781
|
-
import chalk from "@rspress/shared/chalk";
|
|
782
|
-
import { DEFAULT_HIGHLIGHT_LANGUAGES } from "@rspress/shared";
|
|
783
|
-
var supportedLanguages;
|
|
784
|
-
function handleHighlightLanguages(highlightLanguages, defaultLanguages) {
|
|
785
|
-
const aliases = {};
|
|
786
|
-
if (highlightLanguages.size) {
|
|
787
|
-
if (!supportedLanguages) {
|
|
788
|
-
const langs = __require("react-syntax-highlighter/dist/cjs/languages/prism/supported-languages").default;
|
|
789
|
-
supportedLanguages = new Set(langs);
|
|
790
|
-
}
|
|
791
|
-
let useDeprecatedTypeWarning = true;
|
|
792
|
-
const names = {};
|
|
793
|
-
[...DEFAULT_HIGHLIGHT_LANGUAGES, ...defaultLanguages].forEach((lang) => {
|
|
794
|
-
if (Array.isArray(lang)) {
|
|
795
|
-
const [alias, name] = lang;
|
|
796
|
-
names[alias] = name;
|
|
797
|
-
} else if (useDeprecatedTypeWarning) {
|
|
798
|
-
console.log(
|
|
799
|
-
`${chalk.yellowBright(
|
|
800
|
-
"warning"
|
|
801
|
-
)} Automatic import is supported. \`highlightLanguages\` is now used only as alias, and string types will be ignored.
|
|
802
|
-
`
|
|
803
|
-
);
|
|
804
|
-
useDeprecatedTypeWarning = false;
|
|
805
|
-
}
|
|
806
|
-
});
|
|
807
|
-
[...highlightLanguages.values()].forEach((lang) => {
|
|
808
|
-
const name = names[lang];
|
|
809
|
-
if (name && supportedLanguages.has(name)) {
|
|
810
|
-
const temp = aliases[name] || (aliases[name] = []);
|
|
811
|
-
if (!temp.includes(lang)) {
|
|
812
|
-
temp.push(lang);
|
|
813
|
-
}
|
|
814
|
-
highlightLanguages.delete(lang);
|
|
815
|
-
highlightLanguages.add(name);
|
|
816
|
-
return;
|
|
817
|
-
}
|
|
818
|
-
if (!supportedLanguages.has(lang)) {
|
|
819
|
-
highlightLanguages.delete(lang);
|
|
820
|
-
}
|
|
821
|
-
});
|
|
822
|
-
}
|
|
823
|
-
return aliases;
|
|
824
|
-
}
|
|
825
|
-
|
|
826
1329
|
// src/node/runtimeModule/siteData/index.ts
|
|
827
1330
|
function deletePrivateField(obj) {
|
|
828
1331
|
if (typeof obj !== "object" || obj === null) {
|
|
@@ -876,9 +1379,9 @@ async function siteDataVMPlugin(context) {
|
|
|
876
1379
|
const [version, lang] = group.split("###");
|
|
877
1380
|
const indexVersion = version ? `.${version.replace(".", "_")}` : "";
|
|
878
1381
|
const indexLang = lang ? `.${lang}` : "";
|
|
879
|
-
await
|
|
880
|
-
await
|
|
881
|
-
|
|
1382
|
+
await fs6.ensureDir(TEMP_DIR);
|
|
1383
|
+
await fs6.writeFile(
|
|
1384
|
+
path8.join(
|
|
882
1385
|
TEMP_DIR,
|
|
883
1386
|
`${SEARCH_INDEX_NAME}${indexVersion}${indexLang}.${indexHash}.json`
|
|
884
1387
|
),
|
|
@@ -954,74 +1457,6 @@ async function siteDataVMPlugin(context) {
|
|
|
954
1457
|
};
|
|
955
1458
|
}
|
|
956
1459
|
|
|
957
|
-
// src/node/runtimeModule/globalUIComponents.ts
|
|
958
|
-
async function globalUIComponentsVMPlugin(context) {
|
|
959
|
-
const { config, pluginDriver } = context;
|
|
960
|
-
let index = 0;
|
|
961
|
-
const globalUIComponentsByPlugins = pluginDriver.globalUIComponents();
|
|
962
|
-
const globalComponents = [
|
|
963
|
-
...config?.globalUIComponents || [],
|
|
964
|
-
...globalUIComponentsByPlugins
|
|
965
|
-
];
|
|
966
|
-
const moduleContent = globalComponents.map((source) => {
|
|
967
|
-
const name = `Comp_${index}`;
|
|
968
|
-
if (Array.isArray(source)) {
|
|
969
|
-
return `import ${name} from ${JSON.stringify(source[0])};
|
|
970
|
-
const Props_${index++} = ${JSON.stringify(source[1])};
|
|
971
|
-
`;
|
|
972
|
-
}
|
|
973
|
-
index++;
|
|
974
|
-
return `import ${name} from ${JSON.stringify(source)};
|
|
975
|
-
`;
|
|
976
|
-
}).concat(
|
|
977
|
-
`export default [${Array.from({ length: index }, (_, i) => {
|
|
978
|
-
if (Array.isArray(globalComponents[i])) {
|
|
979
|
-
return `[${`Comp_${i}`}, Props_${i}]`;
|
|
980
|
-
}
|
|
981
|
-
return `Comp_${i}`;
|
|
982
|
-
}).join(", ")}];`
|
|
983
|
-
).join("");
|
|
984
|
-
return {
|
|
985
|
-
["virtual-global-components" /* GlobalComponents */]: moduleContent
|
|
986
|
-
};
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
// src/node/runtimeModule/globalStyles.ts
|
|
990
|
-
import fs5 from "@rspress/shared/fs-extra";
|
|
991
|
-
async function globalStylesVMPlugin(context) {
|
|
992
|
-
const { config, pluginDriver } = context;
|
|
993
|
-
const globalStylesByPlugins = pluginDriver.globalStyles();
|
|
994
|
-
if (config.globalStyles) {
|
|
995
|
-
const source = config.globalStyles;
|
|
996
|
-
const styleContent = await fs5.readFile(source, "utf-8");
|
|
997
|
-
const patchedStyleContent = styleContent.replace(/--modern-/g, "--rp-").replace(/\.modern-doc/g, ".rspress").replace(/\.modern-/g, ".rspress-");
|
|
998
|
-
await fs5.writeFile(source, patchedStyleContent, "utf-8");
|
|
999
|
-
}
|
|
1000
|
-
const moduleContent = (await Promise.all(
|
|
1001
|
-
[config?.globalStyles || "", ...globalStylesByPlugins].filter((source) => source.length > 0).map(async (source) => {
|
|
1002
|
-
return `import ${JSON.stringify(source)};`;
|
|
1003
|
-
})
|
|
1004
|
-
)).join("");
|
|
1005
|
-
return {
|
|
1006
|
-
["virtual-global-styles" /* GlobalStyles */]: moduleContent
|
|
1007
|
-
};
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
// src/node/runtimeModule/searchHooks.ts
|
|
1011
|
-
import path7 from "path";
|
|
1012
|
-
async function searchHookVMPlugin(context) {
|
|
1013
|
-
const { config } = context;
|
|
1014
|
-
let content = "export const onSearch = () => {};";
|
|
1015
|
-
let extname = "";
|
|
1016
|
-
if (typeof config.search === "object" && typeof config.search.searchHooks === "string") {
|
|
1017
|
-
content = `export * from '${config.search.searchHooks}'`;
|
|
1018
|
-
extname = path7.extname(config.search.searchHooks);
|
|
1019
|
-
}
|
|
1020
|
-
return {
|
|
1021
|
-
[`${"virtual-search-hooks" /* SearchHooks */}${extname}`]: content
|
|
1022
|
-
};
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
1460
|
// src/node/runtimeModule/index.ts
|
|
1026
1461
|
var runtimeModuleFactory = [
|
|
1027
1462
|
/**
|
|
@@ -1080,326 +1515,84 @@ function rsbuildPluginDocVM(factoryContext) {
|
|
|
1080
1515
|
new RspackVirtualModulePlugin(
|
|
1081
1516
|
runtimeModule,
|
|
1082
1517
|
factoryContext.runtimeTempDir
|
|
1083
|
-
)
|
|
1084
|
-
);
|
|
1085
|
-
});
|
|
1086
|
-
}
|
|
1087
|
-
};
|
|
1088
|
-
}
|
|
1089
|
-
|
|
1090
|
-
// src/node/searchIndex.ts
|
|
1091
|
-
import path8, { join as join2 } from "path";
|
|
1092
|
-
import fs6 from "@rspress/shared/fs-extra";
|
|
1093
|
-
import chalk2 from "@rspress/shared/chalk";
|
|
1094
|
-
import { isSCM, SEARCH_INDEX_NAME as SEARCH_INDEX_NAME2 } from "@rspress/shared";
|
|
1095
|
-
import { logger as logger2 } from "@rspress/shared/logger";
|
|
1096
|
-
async function writeSearchIndex(config) {
|
|
1097
|
-
if (config?.search === false) {
|
|
1098
|
-
return;
|
|
1099
|
-
}
|
|
1100
|
-
const cwd = process.cwd();
|
|
1101
|
-
const searchIndexFiles = await fs6.readdir(TEMP_DIR);
|
|
1102
|
-
const outDir = config?.outDir ?? join2(cwd, OUTPUT_DIR);
|
|
1103
|
-
let searchIndexData = "[]";
|
|
1104
|
-
let scaning = false;
|
|
1105
|
-
for (const searchIndexFile of searchIndexFiles) {
|
|
1106
|
-
if (!searchIndexFile.includes(SEARCH_INDEX_NAME2) || !searchIndexFile.endsWith(".json")) {
|
|
1107
|
-
continue;
|
|
1108
|
-
}
|
|
1109
|
-
const source = join2(TEMP_DIR, searchIndexFile);
|
|
1110
|
-
const target = join2(outDir, "static", searchIndexFile);
|
|
1111
|
-
const searchIndex = await fs6.readFile(
|
|
1112
|
-
join2(TEMP_DIR, searchIndexFile),
|
|
1113
|
-
"utf-8"
|
|
1114
|
-
);
|
|
1115
|
-
searchIndexData = `${searchIndexData.slice(0, -1)}${scaning ? "," : ""}${searchIndex.slice(1)}`;
|
|
1116
|
-
await fs6.move(source, target, { overwrite: true });
|
|
1117
|
-
scaning = true;
|
|
1118
|
-
}
|
|
1119
|
-
if (isProduction() && isSCM() && config?.search?.mode === "remote") {
|
|
1120
|
-
const { apiUrl, indexName } = config.search;
|
|
1121
|
-
try {
|
|
1122
|
-
await fetch(`${apiUrl}?index=${indexName}`, {
|
|
1123
|
-
method: "PUT",
|
|
1124
|
-
body: searchIndexData,
|
|
1125
|
-
headers: { "Content-Type": "application/json" }
|
|
1518
|
+
)
|
|
1519
|
+
);
|
|
1126
1520
|
});
|
|
1127
|
-
logger2.info(
|
|
1128
|
-
chalk2.green(
|
|
1129
|
-
`[doc-tools] Search index uploaded to ${apiUrl}, indexName: ${indexName}`
|
|
1130
|
-
)
|
|
1131
|
-
);
|
|
1132
|
-
} catch (e) {
|
|
1133
|
-
logger2.info(
|
|
1134
|
-
chalk2.red(
|
|
1135
|
-
`[doc-tools] Upload search index \`${indexName}\` failed:
|
|
1136
|
-
${e}`
|
|
1137
|
-
)
|
|
1138
|
-
);
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
function serveSearchIndexMiddleware(config) {
|
|
1143
|
-
return (req, res, next) => {
|
|
1144
|
-
const searchIndexRequestMatch = `/${SEARCH_INDEX_NAME2}.`;
|
|
1145
|
-
if (req.url?.includes(searchIndexRequestMatch)) {
|
|
1146
|
-
res.setHeader("Content-Type", "application/json");
|
|
1147
|
-
const searchIndexFile = req.url?.split("/").pop();
|
|
1148
|
-
fs6.createReadStream(
|
|
1149
|
-
path8.join(
|
|
1150
|
-
process.cwd(),
|
|
1151
|
-
config?.outDir || OUTPUT_DIR,
|
|
1152
|
-
"static",
|
|
1153
|
-
searchIndexFile
|
|
1154
|
-
),
|
|
1155
|
-
"utf-8"
|
|
1156
|
-
).pipe(res, { end: true });
|
|
1157
|
-
} else {
|
|
1158
|
-
next?.();
|
|
1159
1521
|
}
|
|
1160
1522
|
};
|
|
1161
1523
|
}
|
|
1162
1524
|
|
|
1163
|
-
// src/node/
|
|
1164
|
-
import path9 from "path";
|
|
1525
|
+
// src/node/searchIndex.ts
|
|
1526
|
+
import path9, { join as join2 } from "path";
|
|
1527
|
+
import { SEARCH_INDEX_NAME as SEARCH_INDEX_NAME2, isSCM } from "@rspress/shared";
|
|
1528
|
+
import chalk2 from "@rspress/shared/chalk";
|
|
1165
1529
|
import fs7 from "@rspress/shared/fs-extra";
|
|
1166
|
-
import {
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
} from "@rspress/shared";
|
|
1171
|
-
var DEFAULT_PAGE_EXTENSIONS = ["js", "jsx", "ts", "tsx", "md", "mdx"];
|
|
1172
|
-
var normalizeRoutePath = (routePath, base, lang, version, langs, versions, extensions = DEFAULT_PAGE_EXTENSIONS) => {
|
|
1173
|
-
const hasTrailSlash = routePath.endsWith("/");
|
|
1174
|
-
let versionPart = "";
|
|
1175
|
-
let langPart = "";
|
|
1176
|
-
let purePathPart = "";
|
|
1177
|
-
const parts = routePath.split("/").filter(Boolean);
|
|
1178
|
-
if (version) {
|
|
1179
|
-
const versionToMatch = parts[0];
|
|
1180
|
-
if (versions.includes(versionToMatch)) {
|
|
1181
|
-
if (versionToMatch !== version) {
|
|
1182
|
-
versionPart = versionToMatch;
|
|
1183
|
-
}
|
|
1184
|
-
parts.shift();
|
|
1185
|
-
}
|
|
1186
|
-
}
|
|
1187
|
-
if (lang) {
|
|
1188
|
-
const langToMatch = parts[0];
|
|
1189
|
-
if (langs.includes(langToMatch)) {
|
|
1190
|
-
if (langToMatch !== lang) {
|
|
1191
|
-
langPart = langToMatch;
|
|
1192
|
-
}
|
|
1193
|
-
parts.shift();
|
|
1194
|
-
}
|
|
1195
|
-
}
|
|
1196
|
-
purePathPart = parts.join("/");
|
|
1197
|
-
let normalizedRoutePath = addLeadingSlash2(
|
|
1198
|
-
[versionPart, langPart, purePathPart].filter(Boolean).join("/")
|
|
1199
|
-
).replace(new RegExp(`\\.(${extensions.join("|")})$`), "").replace(/\.html$/, "").replace(/\/index$/, "/");
|
|
1200
|
-
if (hasTrailSlash) {
|
|
1201
|
-
normalizedRoutePath = addTrailingSlash(normalizedRoutePath);
|
|
1202
|
-
}
|
|
1203
|
-
return {
|
|
1204
|
-
routePath: withBase2(normalizedRoutePath, base),
|
|
1205
|
-
lang: langPart || lang,
|
|
1206
|
-
version: versionPart || version
|
|
1207
|
-
};
|
|
1208
|
-
};
|
|
1209
|
-
var _scanDir, _defaultLang, _defaultVersion, _extensions, _langs, _versions, _include, _exclude, _base, _tempDir, _pluginDriver, _writeTempFile, writeTempFile_fn, _generateRouteInfo, generateRouteInfo_fn;
|
|
1210
|
-
var RouteService = class {
|
|
1211
|
-
constructor(scanDir, userConfig, tempDir, pluginDriver) {
|
|
1212
|
-
__privateAdd(this, _writeTempFile);
|
|
1213
|
-
__privateAdd(this, _generateRouteInfo);
|
|
1214
|
-
__publicField(this, "routeData", /* @__PURE__ */ new Map());
|
|
1215
|
-
__privateAdd(this, _scanDir, void 0);
|
|
1216
|
-
__privateAdd(this, _defaultLang, void 0);
|
|
1217
|
-
__privateAdd(this, _defaultVersion, "");
|
|
1218
|
-
__privateAdd(this, _extensions, []);
|
|
1219
|
-
__privateAdd(this, _langs, []);
|
|
1220
|
-
__privateAdd(this, _versions, []);
|
|
1221
|
-
__privateAdd(this, _include, []);
|
|
1222
|
-
__privateAdd(this, _exclude, []);
|
|
1223
|
-
__privateAdd(this, _base, "");
|
|
1224
|
-
__privateAdd(this, _tempDir, "");
|
|
1225
|
-
__privateAdd(this, _pluginDriver, void 0);
|
|
1226
|
-
const routeOptions = userConfig?.route || {};
|
|
1227
|
-
__privateSet(this, _scanDir, scanDir);
|
|
1228
|
-
__privateSet(this, _extensions, routeOptions.extensions || DEFAULT_PAGE_EXTENSIONS);
|
|
1229
|
-
__privateSet(this, _include, routeOptions.include || []);
|
|
1230
|
-
__privateSet(this, _exclude, routeOptions.exclude || []);
|
|
1231
|
-
__privateSet(this, _defaultLang, userConfig?.lang || "");
|
|
1232
|
-
__privateSet(this, _langs, (userConfig?.locales ?? userConfig?.themeConfig?.locales ?? []).map((item) => item.lang));
|
|
1233
|
-
__privateSet(this, _base, userConfig?.base || "");
|
|
1234
|
-
__privateSet(this, _tempDir, tempDir);
|
|
1235
|
-
__privateSet(this, _pluginDriver, pluginDriver);
|
|
1236
|
-
if (userConfig.multiVersion) {
|
|
1237
|
-
__privateSet(this, _defaultVersion, userConfig.multiVersion.default || "");
|
|
1238
|
-
__privateSet(this, _versions, userConfig.multiVersion.versions || []);
|
|
1239
|
-
}
|
|
1240
|
-
}
|
|
1241
|
-
async init() {
|
|
1242
|
-
const globby = (await import(
|
|
1243
|
-
// @ts-expect-error
|
|
1244
|
-
// eslint-disable-next-line node/file-extension-in-import
|
|
1245
|
-
"../compiled/globby/index.js"
|
|
1246
|
-
)).default;
|
|
1247
|
-
const files = (await globby([`**/*.{${__privateGet(this, _extensions).join(",")}}`, ...__privateGet(this, _include)], {
|
|
1248
|
-
cwd: __privateGet(this, _scanDir),
|
|
1249
|
-
absolute: true,
|
|
1250
|
-
ignore: [
|
|
1251
|
-
...__privateGet(this, _exclude),
|
|
1252
|
-
"**/node_modules/**",
|
|
1253
|
-
"**/.eslintrc.js",
|
|
1254
|
-
"**/.nx-cache/**",
|
|
1255
|
-
`./${PUBLIC_DIR}/**`
|
|
1256
|
-
]
|
|
1257
|
-
})).sort();
|
|
1258
|
-
files.forEach((filePath) => {
|
|
1259
|
-
const fileRelativePath = normalizePath(
|
|
1260
|
-
path9.relative(__privateGet(this, _scanDir), filePath)
|
|
1261
|
-
);
|
|
1262
|
-
const { routePath, lang, version } = this.normalizeRoutePath(fileRelativePath);
|
|
1263
|
-
const absolutePath = path9.join(__privateGet(this, _scanDir), fileRelativePath);
|
|
1264
|
-
const routeInfo = {
|
|
1265
|
-
routePath,
|
|
1266
|
-
absolutePath: normalizePath(absolutePath),
|
|
1267
|
-
relativePath: fileRelativePath,
|
|
1268
|
-
pageName: getPageKey(fileRelativePath),
|
|
1269
|
-
lang,
|
|
1270
|
-
version
|
|
1271
|
-
};
|
|
1272
|
-
this.addRoute(routeInfo);
|
|
1273
|
-
});
|
|
1274
|
-
const externalPages = await __privateGet(this, _pluginDriver).addPages();
|
|
1275
|
-
await Promise.all(
|
|
1276
|
-
externalPages.map(async (route, index) => {
|
|
1277
|
-
const { routePath, content, filepath } = route;
|
|
1278
|
-
if (filepath) {
|
|
1279
|
-
const routeInfo = __privateMethod(this, _generateRouteInfo, generateRouteInfo_fn).call(this, routePath, filepath);
|
|
1280
|
-
this.addRoute(routeInfo);
|
|
1281
|
-
return;
|
|
1282
|
-
}
|
|
1283
|
-
if (content) {
|
|
1284
|
-
const filepath2 = await __privateMethod(this, _writeTempFile, writeTempFile_fn).call(this, index, content);
|
|
1285
|
-
const routeInfo = __privateMethod(this, _generateRouteInfo, generateRouteInfo_fn).call(this, routePath, filepath2);
|
|
1286
|
-
this.addRoute(routeInfo);
|
|
1287
|
-
}
|
|
1288
|
-
})
|
|
1289
|
-
);
|
|
1290
|
-
await __privateGet(this, _pluginDriver).routeGenerated(this.getRoutes());
|
|
1530
|
+
import { logger as logger2 } from "@rspress/shared/logger";
|
|
1531
|
+
async function writeSearchIndex(config) {
|
|
1532
|
+
if (config?.search === false) {
|
|
1533
|
+
return;
|
|
1291
1534
|
}
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1535
|
+
const cwd = process.cwd();
|
|
1536
|
+
const searchIndexFiles = await fs7.readdir(TEMP_DIR);
|
|
1537
|
+
const outDir = config?.outDir ?? join2(cwd, OUTPUT_DIR);
|
|
1538
|
+
let searchIndexData = "[]";
|
|
1539
|
+
let scanning = false;
|
|
1540
|
+
for (const searchIndexFile of searchIndexFiles) {
|
|
1541
|
+
if (!searchIndexFile.includes(SEARCH_INDEX_NAME2) || !searchIndexFile.endsWith(".json")) {
|
|
1542
|
+
continue;
|
|
1543
|
+
}
|
|
1544
|
+
const source = join2(TEMP_DIR, searchIndexFile);
|
|
1545
|
+
const target = join2(outDir, "static", searchIndexFile);
|
|
1546
|
+
const searchIndex = await fs7.readFile(
|
|
1547
|
+
join2(TEMP_DIR, searchIndexFile),
|
|
1548
|
+
"utf-8"
|
|
1549
|
+
);
|
|
1550
|
+
searchIndexData = `${searchIndexData.slice(0, -1)}${scanning ? "," : ""}${searchIndex.slice(1)}`;
|
|
1551
|
+
await fs7.move(source, target, { overwrite: true });
|
|
1552
|
+
scanning = true;
|
|
1553
|
+
}
|
|
1554
|
+
if (isProduction() && isSCM() && config?.search?.mode === "remote") {
|
|
1555
|
+
const { apiUrl, indexName } = config.search;
|
|
1556
|
+
try {
|
|
1557
|
+
await fetch(`${apiUrl}?index=${indexName}`, {
|
|
1558
|
+
method: "PUT",
|
|
1559
|
+
body: searchIndexData,
|
|
1560
|
+
headers: { "Content-Type": "application/json" }
|
|
1561
|
+
});
|
|
1562
|
+
logger2.info(
|
|
1563
|
+
chalk2.green(
|
|
1564
|
+
`[doc-tools] Search index uploaded to ${apiUrl}, indexName: ${indexName}`
|
|
1565
|
+
)
|
|
1566
|
+
);
|
|
1567
|
+
} catch (e) {
|
|
1568
|
+
logger2.info(
|
|
1569
|
+
chalk2.red(
|
|
1570
|
+
`[doc-tools] Upload search index \`${indexName}\` failed:
|
|
1571
|
+
${e}`
|
|
1572
|
+
)
|
|
1573
|
+
);
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
function serveSearchIndexMiddleware(config) {
|
|
1578
|
+
return (req, res, next) => {
|
|
1579
|
+
const searchIndexRequestMatch = `/${SEARCH_INDEX_NAME2}.`;
|
|
1580
|
+
if (req.url?.includes(searchIndexRequestMatch)) {
|
|
1581
|
+
res.setHeader("Content-Type", "application/json");
|
|
1582
|
+
const searchIndexFile = req.url?.split("/").pop();
|
|
1583
|
+
fs7.createReadStream(
|
|
1584
|
+
path9.join(
|
|
1585
|
+
process.cwd(),
|
|
1586
|
+
config?.outDir || OUTPUT_DIR,
|
|
1587
|
+
"static",
|
|
1588
|
+
searchIndexFile
|
|
1589
|
+
),
|
|
1590
|
+
"utf-8"
|
|
1591
|
+
).pipe(res, { end: true });
|
|
1592
|
+
} else {
|
|
1593
|
+
next?.();
|
|
1306
1594
|
}
|
|
1307
|
-
}
|
|
1308
|
-
removeRoute(filePath) {
|
|
1309
|
-
const fileRelativePath = path9.relative(__privateGet(this, _scanDir), filePath);
|
|
1310
|
-
const { routePath } = this.normalizeRoutePath(fileRelativePath);
|
|
1311
|
-
this.routeData.delete(routePath);
|
|
1312
|
-
}
|
|
1313
|
-
getRoutes() {
|
|
1314
|
-
return Array.from(this.routeData.values());
|
|
1315
|
-
}
|
|
1316
|
-
isExistRoute(routePath) {
|
|
1317
|
-
const { routePath: normalizedRoute } = this.normalizeRoutePath(routePath);
|
|
1318
|
-
return this.routeData.get(normalizedRoute);
|
|
1319
|
-
}
|
|
1320
|
-
isEmpty() {
|
|
1321
|
-
return this.routeData.size === 0;
|
|
1322
|
-
}
|
|
1323
|
-
generateRoutesCode(isStaticImport = false) {
|
|
1324
|
-
return this.generateRoutesCodeByRouteMeta(this.getRoutes(), isStaticImport);
|
|
1325
|
-
}
|
|
1326
|
-
generateRoutesCodeByRouteMeta(routeMeta, isStaticImport) {
|
|
1327
|
-
return `
|
|
1328
|
-
import React from 'react';
|
|
1329
|
-
import { lazyWithPreload } from "react-lazy-with-preload";
|
|
1330
|
-
${routeMeta.map((route, index) => {
|
|
1331
|
-
return isStaticImport ? `import * as Route${index} from '${route.absolutePath}';` : `const Route${index} = lazyWithPreload(() => import('${route.absolutePath}'))`;
|
|
1332
|
-
}).join("\n")}
|
|
1333
|
-
export const routes = [
|
|
1334
|
-
${routeMeta.map((route, index) => {
|
|
1335
|
-
const preload = isStaticImport ? `() => Route${index}` : `async () => {
|
|
1336
|
-
await Route${index}.preload();
|
|
1337
|
-
return import("${route.absolutePath}");
|
|
1338
|
-
}`;
|
|
1339
|
-
const component = isStaticImport ? `Route${index}.default` : `Route${index}`;
|
|
1340
|
-
return `{ path: '${route.routePath}', element: React.createElement(${component}), filePath: '${route.relativePath}', preload: ${preload}, lang: '${route.lang}', version: '${route.version}' }`;
|
|
1341
|
-
}).join(",\n")}
|
|
1342
|
-
];
|
|
1343
|
-
`;
|
|
1344
|
-
}
|
|
1345
|
-
normalizeRoutePath(routePath) {
|
|
1346
|
-
return normalizeRoutePath(
|
|
1347
|
-
routePath,
|
|
1348
|
-
__privateGet(this, _base),
|
|
1349
|
-
__privateGet(this, _defaultLang),
|
|
1350
|
-
__privateGet(this, _defaultVersion),
|
|
1351
|
-
__privateGet(this, _langs),
|
|
1352
|
-
__privateGet(this, _versions),
|
|
1353
|
-
__privateGet(this, _extensions)
|
|
1354
|
-
);
|
|
1355
|
-
}
|
|
1356
|
-
};
|
|
1357
|
-
_scanDir = new WeakMap();
|
|
1358
|
-
_defaultLang = new WeakMap();
|
|
1359
|
-
_defaultVersion = new WeakMap();
|
|
1360
|
-
_extensions = new WeakMap();
|
|
1361
|
-
_langs = new WeakMap();
|
|
1362
|
-
_versions = new WeakMap();
|
|
1363
|
-
_include = new WeakMap();
|
|
1364
|
-
_exclude = new WeakMap();
|
|
1365
|
-
_base = new WeakMap();
|
|
1366
|
-
_tempDir = new WeakMap();
|
|
1367
|
-
_pluginDriver = new WeakMap();
|
|
1368
|
-
_writeTempFile = new WeakSet();
|
|
1369
|
-
writeTempFile_fn = async function(index, content) {
|
|
1370
|
-
const tempFilePath = path9.join(__privateGet(this, _tempDir), `temp-${index}.mdx`);
|
|
1371
|
-
await fs7.writeFile(tempFilePath, content);
|
|
1372
|
-
return tempFilePath;
|
|
1373
|
-
};
|
|
1374
|
-
_generateRouteInfo = new WeakSet();
|
|
1375
|
-
generateRouteInfo_fn = function(routePath, filepath) {
|
|
1376
|
-
const {
|
|
1377
|
-
routePath: normalizedPath,
|
|
1378
|
-
lang,
|
|
1379
|
-
version
|
|
1380
|
-
} = this.normalizeRoutePath(routePath);
|
|
1381
|
-
return {
|
|
1382
|
-
routePath: normalizedPath,
|
|
1383
|
-
absolutePath: normalizePath(filepath),
|
|
1384
|
-
relativePath: normalizePath(path9.relative(__privateGet(this, _scanDir), filepath)),
|
|
1385
|
-
pageName: getPageKey(routePath),
|
|
1386
|
-
lang,
|
|
1387
|
-
version
|
|
1388
1595
|
};
|
|
1389
|
-
};
|
|
1390
|
-
|
|
1391
|
-
// src/node/route/init.ts
|
|
1392
|
-
var routeService = null;
|
|
1393
|
-
async function initRouteService(options) {
|
|
1394
|
-
const { scanDir, config, runtimeTempDir, pluginDriver } = options;
|
|
1395
|
-
routeService = new RouteService(
|
|
1396
|
-
scanDir,
|
|
1397
|
-
config,
|
|
1398
|
-
runtimeTempDir,
|
|
1399
|
-
pluginDriver
|
|
1400
|
-
);
|
|
1401
|
-
await routeService.init();
|
|
1402
|
-
return routeService;
|
|
1403
1596
|
}
|
|
1404
1597
|
|
|
1405
1598
|
// src/node/utils/detectCustomIcon.ts
|
|
@@ -1427,9 +1620,6 @@ var detectCustomIcon = async (customThemeDir) => {
|
|
|
1427
1620
|
};
|
|
1428
1621
|
|
|
1429
1622
|
// src/node/initRsbuild.ts
|
|
1430
|
-
import { PLUGIN_REACT_NAME, pluginReact } from "@rsbuild/plugin-react";
|
|
1431
|
-
import { PLUGIN_SASS_NAME, pluginSass } from "@rsbuild/plugin-sass";
|
|
1432
|
-
import { PLUGIN_LESS_NAME, pluginLess } from "@rsbuild/plugin-less";
|
|
1433
1623
|
function isPluginIncluded(config, pluginName) {
|
|
1434
1624
|
return config.builderPlugins?.some((plugin) => plugin.name === pluginName) || config.builderConfig?.plugins?.some(
|
|
1435
1625
|
(plugin) => plugin && plugin.name === pluginName
|
|
@@ -1668,194 +1858,106 @@ async function initRsbuild(rootDir, config, pluginDriver, enableSSG, extraRsbuil
|
|
|
1668
1858
|
return rsbuild;
|
|
1669
1859
|
}
|
|
1670
1860
|
|
|
1671
|
-
// src/node/
|
|
1672
|
-
import
|
|
1673
|
-
import
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
const themeConfig = config?.themeConfig || {};
|
|
1689
|
-
const enableLastUpdated = themeConfig?.lastUpdated || themeConfig?.locales?.some((locale) => locale.lastUpdated);
|
|
1690
|
-
const mediumZoomConfig = config?.mediumZoom ?? true;
|
|
1691
|
-
const haveNavSidebarConfig = themeConfig.nav || themeConfig.sidebar || themeConfig.locales?.[0]?.nav || themeConfig.locales?.[0]?.sidebar;
|
|
1692
|
-
if (enableLastUpdated) {
|
|
1693
|
-
const { pluginLastUpdated } = await import("@rspress/plugin-last-updated");
|
|
1694
|
-
this.addPlugin(pluginLastUpdated());
|
|
1695
|
-
}
|
|
1696
|
-
if (mediumZoomConfig) {
|
|
1697
|
-
const { pluginMediumZoom } = await import("@rspress/plugin-medium-zoom");
|
|
1698
|
-
this.addPlugin(
|
|
1699
|
-
pluginMediumZoom(
|
|
1700
|
-
typeof mediumZoomConfig === "object" ? mediumZoomConfig : void 0
|
|
1701
|
-
)
|
|
1702
|
-
);
|
|
1703
|
-
}
|
|
1704
|
-
this.addPlugin(pluginContainerSyntax());
|
|
1705
|
-
if (isDevDebugMode()) {
|
|
1706
|
-
const SourceBuildPlugin = await import(
|
|
1707
|
-
// @ts-expect-error need moduleResolution: Node16, NodeNext or Bundler to get type declerations work
|
|
1708
|
-
"@rspress/theme-default/node/source-build-plugin.js"
|
|
1709
|
-
).then(
|
|
1710
|
-
(r) => r.SourceBuildPlugin,
|
|
1711
|
-
() => null
|
|
1712
|
-
);
|
|
1713
|
-
if (SourceBuildPlugin) {
|
|
1714
|
-
this.addPlugin(SourceBuildPlugin());
|
|
1715
|
-
}
|
|
1716
|
-
}
|
|
1717
|
-
(config.plugins || []).forEach((plugin) => {
|
|
1718
|
-
this.addPlugin(plugin);
|
|
1719
|
-
});
|
|
1720
|
-
if (!haveNavSidebarConfig) {
|
|
1721
|
-
const { pluginAutoNavSidebar } = await import("@rspress/plugin-auto-nav-sidebar");
|
|
1722
|
-
this.addPlugin(pluginAutoNavSidebar());
|
|
1861
|
+
// src/node/utils/checkLanguageParity.ts
|
|
1862
|
+
import fs9 from "fs/promises";
|
|
1863
|
+
import path12 from "path";
|
|
1864
|
+
import { logger as logger3 } from "@rspress/shared/logger";
|
|
1865
|
+
function normalizePath2(filePath) {
|
|
1866
|
+
return filePath.split(path12.sep).join("/");
|
|
1867
|
+
}
|
|
1868
|
+
async function getAllMarkdownFilesFrom(dirPath) {
|
|
1869
|
+
const files = await fs9.readdir(dirPath, { withFileTypes: true });
|
|
1870
|
+
const allFiles = [];
|
|
1871
|
+
for (const file of files) {
|
|
1872
|
+
const fullPath = path12.join(dirPath, file.name);
|
|
1873
|
+
if (file.isDirectory()) {
|
|
1874
|
+
const nestedFiles = await getAllMarkdownFilesFrom(fullPath);
|
|
1875
|
+
allFiles.push(...nestedFiles);
|
|
1876
|
+
} else if ([".md", ".mdx"].includes(path12.extname(file.name))) {
|
|
1877
|
+
allFiles.push(fullPath);
|
|
1723
1878
|
}
|
|
1724
1879
|
}
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1880
|
+
return allFiles;
|
|
1881
|
+
}
|
|
1882
|
+
async function collectModuleFiles(contentPath, lang, includedDir, excludedDirs, fileLangMap) {
|
|
1883
|
+
const langDirPath = path12.join(contentPath, lang);
|
|
1884
|
+
const langModuleDir = path12.join(langDirPath, includedDir);
|
|
1885
|
+
try {
|
|
1886
|
+
const moduleDirStats = await fs9.stat(langModuleDir);
|
|
1887
|
+
if (!moduleDirStats.isDirectory())
|
|
1888
|
+
return fileLangMap;
|
|
1889
|
+
const files = await getAllMarkdownFilesFrom(langModuleDir);
|
|
1890
|
+
for (const file of files) {
|
|
1891
|
+
const relativePath = normalizePath2(path12.relative(langDirPath, file));
|
|
1892
|
+
if (excludedDirs.some((excludedDir) => {
|
|
1893
|
+
const isFilePath = /\.(md|mdx)$/.test(excludedDir);
|
|
1894
|
+
return relativePath.includes(excludedDir + (isFilePath ? "" : "/"));
|
|
1895
|
+
}))
|
|
1896
|
+
continue;
|
|
1897
|
+
const baseName = relativePath;
|
|
1898
|
+
if (!fileLangMap[baseName])
|
|
1899
|
+
fileLangMap[baseName] = /* @__PURE__ */ new Set();
|
|
1900
|
+
fileLangMap[baseName].add(lang);
|
|
1731
1901
|
}
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1902
|
+
} catch {
|
|
1903
|
+
throw new Error(
|
|
1904
|
+
`Failed to access directory: ${normalizePath2(langModuleDir)}`
|
|
1905
|
+
);
|
|
1736
1906
|
}
|
|
1737
|
-
|
|
1738
|
-
|
|
1907
|
+
return fileLangMap;
|
|
1908
|
+
}
|
|
1909
|
+
async function checkLanguageParity(config) {
|
|
1910
|
+
if (!config?.languageParity || config.languageParity.enabled === false) {
|
|
1911
|
+
return;
|
|
1739
1912
|
}
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1913
|
+
logger3.info("Checking language parity...");
|
|
1914
|
+
const contentPath = path12.resolve(config.root);
|
|
1915
|
+
const includedDirs = config.languageParity.include?.length ? config.languageParity.include : [""];
|
|
1916
|
+
const excludedDirs = config.languageParity.exclude ?? [];
|
|
1917
|
+
const supportedLanguages2 = (config.locales ?? config.themeConfig?.locales)?.map(
|
|
1918
|
+
(locale) => locale.lang
|
|
1919
|
+
) || [];
|
|
1920
|
+
if (!supportedLanguages2.length) {
|
|
1921
|
+
logger3.error("No supported languages found in the configuration.");
|
|
1922
|
+
return;
|
|
1745
1923
|
}
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
for (
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
this.removePlugin(pluginName);
|
|
1760
|
-
if (index <= i && index > 0) {
|
|
1761
|
-
i--;
|
|
1762
|
-
}
|
|
1763
|
-
}
|
|
1764
|
-
},
|
|
1765
|
-
__privateGet(this, _isProd)
|
|
1924
|
+
const missingLanguagesFile = [];
|
|
1925
|
+
try {
|
|
1926
|
+
for (const includedDir of includedDirs) {
|
|
1927
|
+
if (excludedDirs.some((excludedDir) => excludedDir === includedDir))
|
|
1928
|
+
continue;
|
|
1929
|
+
const curFileLangMap = {};
|
|
1930
|
+
for (const lang of supportedLanguages2) {
|
|
1931
|
+
await collectModuleFiles(
|
|
1932
|
+
contentPath,
|
|
1933
|
+
lang,
|
|
1934
|
+
includedDir,
|
|
1935
|
+
excludedDirs,
|
|
1936
|
+
curFileLangMap
|
|
1766
1937
|
);
|
|
1767
1938
|
}
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
__privateGet(this, _isProd)
|
|
1777
|
-
);
|
|
1778
|
-
}
|
|
1779
|
-
async afterBuild() {
|
|
1780
|
-
return this._runParallelAsyncHook(
|
|
1781
|
-
"afterBuild",
|
|
1782
|
-
__privateGet(this, _config) || {},
|
|
1783
|
-
__privateGet(this, _isProd)
|
|
1784
|
-
);
|
|
1785
|
-
}
|
|
1786
|
-
async modifySearchIndexData(pages) {
|
|
1787
|
-
return this._runParallelAsyncHook(
|
|
1788
|
-
"modifySearchIndexData",
|
|
1789
|
-
pages,
|
|
1790
|
-
__privateGet(this, _isProd)
|
|
1791
|
-
);
|
|
1792
|
-
}
|
|
1793
|
-
async extendPageData(pageData) {
|
|
1794
|
-
return this._runParallelAsyncHook("extendPageData", pageData, __privateGet(this, _isProd));
|
|
1795
|
-
}
|
|
1796
|
-
async addPages() {
|
|
1797
|
-
const result = await this._runParallelAsyncHook(
|
|
1798
|
-
"addPages",
|
|
1799
|
-
__privateGet(this, _config) || {},
|
|
1800
|
-
__privateGet(this, _isProd)
|
|
1801
|
-
);
|
|
1802
|
-
return result.flat();
|
|
1803
|
-
}
|
|
1804
|
-
async routeGenerated(routes) {
|
|
1805
|
-
return this._runParallelAsyncHook("routeGenerated", routes);
|
|
1806
|
-
}
|
|
1807
|
-
async addRuntimeModules() {
|
|
1808
|
-
const result = await this._runParallelAsyncHook(
|
|
1809
|
-
"addRuntimeModules",
|
|
1810
|
-
__privateGet(this, _config) || {},
|
|
1811
|
-
__privateGet(this, _isProd)
|
|
1812
|
-
);
|
|
1813
|
-
return result.reduce((prev, current) => {
|
|
1814
|
-
return {
|
|
1815
|
-
...prev,
|
|
1816
|
-
...current
|
|
1817
|
-
};
|
|
1818
|
-
}, {});
|
|
1819
|
-
}
|
|
1820
|
-
async addSSGRoutes() {
|
|
1821
|
-
const result = await this._runParallelAsyncHook(
|
|
1822
|
-
"addSSGRoutes",
|
|
1823
|
-
__privateGet(this, _config) || {},
|
|
1824
|
-
__privateGet(this, _isProd)
|
|
1825
|
-
);
|
|
1826
|
-
return result.flat();
|
|
1827
|
-
}
|
|
1828
|
-
globalUIComponents() {
|
|
1829
|
-
const result = __privateGet(this, _plugins).map((plugin) => {
|
|
1830
|
-
return plugin.globalUIComponents || [];
|
|
1831
|
-
});
|
|
1832
|
-
return result.flat();
|
|
1833
|
-
}
|
|
1834
|
-
globalStyles() {
|
|
1835
|
-
return __privateGet(this, _plugins).filter((plugin) => typeof plugin.globalStyles === "string").map((plugin) => {
|
|
1836
|
-
return plugin.globalStyles;
|
|
1837
|
-
});
|
|
1838
|
-
}
|
|
1839
|
-
_runParallelAsyncHook(hookName, ...args) {
|
|
1840
|
-
return Promise.all(
|
|
1841
|
-
__privateGet(this, _plugins).filter((plugin) => typeof plugin[hookName] === "function").map((plugin) => {
|
|
1842
|
-
return plugin[hookName](...args);
|
|
1843
|
-
})
|
|
1844
|
-
);
|
|
1845
|
-
}
|
|
1846
|
-
_runSerialAysncHook(hookName, ...args) {
|
|
1847
|
-
return __privateGet(this, _plugins).reduce(async (prev, plugin) => {
|
|
1848
|
-
if (typeof plugin[hookName] === "function") {
|
|
1849
|
-
await prev;
|
|
1850
|
-
return plugin[hookName](...args);
|
|
1939
|
+
for (const [baseName, langs] of Object.entries(curFileLangMap)) {
|
|
1940
|
+
const missingLangs = supportedLanguages2.filter(
|
|
1941
|
+
(lang) => !langs.has(lang)
|
|
1942
|
+
);
|
|
1943
|
+
missingLangs.forEach((lang) => {
|
|
1944
|
+
const missingPath = path12.join(lang, baseName);
|
|
1945
|
+
missingLanguagesFile.push(missingPath);
|
|
1946
|
+
});
|
|
1851
1947
|
}
|
|
1852
|
-
|
|
1853
|
-
|
|
1948
|
+
}
|
|
1949
|
+
if (missingLanguagesFile.length > 0) {
|
|
1950
|
+
throw new Error(
|
|
1951
|
+
`Check language parity failed! Missing content:
|
|
1952
|
+
${missingLanguagesFile.map((file) => ` - ${normalizePath2(file)}`).join("\n")}`
|
|
1953
|
+
);
|
|
1954
|
+
}
|
|
1955
|
+
logger3.success("Language parity checked successfully.");
|
|
1956
|
+
} catch (err) {
|
|
1957
|
+
logger3.error(`Error during language parity check: ${err.message}`);
|
|
1958
|
+
throw err;
|
|
1854
1959
|
}
|
|
1855
|
-
}
|
|
1856
|
-
_config = new WeakMap();
|
|
1857
|
-
_plugins = new WeakMap();
|
|
1858
|
-
_isProd = new WeakMap();
|
|
1960
|
+
}
|
|
1859
1961
|
|
|
1860
1962
|
// src/node/dev.ts
|
|
1861
1963
|
async function dev(options) {
|
|
@@ -1883,20 +1985,21 @@ async function dev(options) {
|
|
|
1883
1985
|
return server;
|
|
1884
1986
|
} finally {
|
|
1885
1987
|
await writeSearchIndex(modifiedConfig);
|
|
1988
|
+
await checkLanguageParity(modifiedConfig);
|
|
1886
1989
|
}
|
|
1887
1990
|
}
|
|
1888
1991
|
|
|
1889
1992
|
// src/node/build.ts
|
|
1890
1993
|
import { dirname as dirname2, join as join3 } from "path";
|
|
1891
1994
|
import { pathToFileURL } from "url";
|
|
1892
|
-
import chalk3 from "@rspress/shared/chalk";
|
|
1893
|
-
import fs9 from "@rspress/shared/fs-extra";
|
|
1894
1995
|
import {
|
|
1996
|
+
isDebugMode as isDebugMode2,
|
|
1895
1997
|
normalizeSlash,
|
|
1896
|
-
withBase as withBase3
|
|
1897
|
-
isDebugMode as isDebugMode2
|
|
1998
|
+
withBase as withBase3
|
|
1898
1999
|
} from "@rspress/shared";
|
|
1899
|
-
import
|
|
2000
|
+
import chalk3 from "@rspress/shared/chalk";
|
|
2001
|
+
import fs10 from "@rspress/shared/fs-extra";
|
|
2002
|
+
import { logger as logger4 } from "@rspress/shared/logger";
|
|
1900
2003
|
|
|
1901
2004
|
// src/node/utils/renderHead.ts
|
|
1902
2005
|
import fsExtra from "@rspress/shared/fs-extra";
|
|
@@ -1960,15 +2063,16 @@ async function bundle(docDirectory, config, pluginDriver, enableSSG) {
|
|
|
1960
2063
|
await rsbuild.build();
|
|
1961
2064
|
} finally {
|
|
1962
2065
|
await writeSearchIndex(config);
|
|
2066
|
+
await checkLanguageParity(config);
|
|
1963
2067
|
}
|
|
1964
2068
|
}
|
|
1965
2069
|
async function renderPages(appDirectory, config, pluginDriver, ssgConfig) {
|
|
1966
|
-
|
|
2070
|
+
logger4.info("Rendering pages...");
|
|
1967
2071
|
const startTime = Date.now();
|
|
1968
2072
|
const outputPath = config?.outDir ?? join3(appDirectory, OUTPUT_DIR);
|
|
1969
2073
|
const ssrBundlePath = join3(outputPath, "ssr", "main.cjs");
|
|
1970
2074
|
try {
|
|
1971
|
-
const { default:
|
|
2075
|
+
const { default: fs11 } = await import("@rspress/shared/fs-extra");
|
|
1972
2076
|
const { version } = await Promise.resolve().then(() => __toESM(require_package()));
|
|
1973
2077
|
let render = null;
|
|
1974
2078
|
if (ssgConfig) {
|
|
@@ -1977,13 +2081,13 @@ async function renderPages(appDirectory, config, pluginDriver, ssgConfig) {
|
|
|
1977
2081
|
({ render } = ssrExports);
|
|
1978
2082
|
} catch (e) {
|
|
1979
2083
|
if (typeof ssgConfig === "object" && ssgConfig.strict) {
|
|
1980
|
-
|
|
2084
|
+
logger4.error(
|
|
1981
2085
|
`Failed to load SSG bundle: ${chalk3.yellow(ssrBundlePath)}.`
|
|
1982
2086
|
);
|
|
1983
2087
|
throw e;
|
|
1984
2088
|
}
|
|
1985
|
-
|
|
1986
|
-
|
|
2089
|
+
logger4.error(e);
|
|
2090
|
+
logger4.warn(
|
|
1987
2091
|
`Failed to load SSG bundle: ${chalk3.yellow(ssrBundlePath)}, fallback to CSR.`
|
|
1988
2092
|
);
|
|
1989
2093
|
}
|
|
@@ -1991,7 +2095,7 @@ async function renderPages(appDirectory, config, pluginDriver, ssgConfig) {
|
|
|
1991
2095
|
const routes = routeService.getRoutes();
|
|
1992
2096
|
const base = config?.base ?? "";
|
|
1993
2097
|
const htmlTemplatePath = join3(outputPath, "index.html");
|
|
1994
|
-
const htmlTemplate = await
|
|
2098
|
+
const htmlTemplate = await fs11.readFile(htmlTemplatePath, "utf-8");
|
|
1995
2099
|
const additionalRoutes = (await pluginDriver.addSSGRoutes()).map((route) => ({
|
|
1996
2100
|
routePath: withBase3(route.path, base)
|
|
1997
2101
|
}));
|
|
@@ -2018,12 +2122,12 @@ async function renderPages(appDirectory, config, pluginDriver, ssgConfig) {
|
|
|
2018
2122
|
({ appHtml } = await render(routePath, helmetContext.context));
|
|
2019
2123
|
} catch (e) {
|
|
2020
2124
|
if (typeof ssgConfig === "object" && ssgConfig.strict) {
|
|
2021
|
-
|
|
2125
|
+
logger4.error(
|
|
2022
2126
|
`Page "${chalk3.yellow(routePath)}" SSG rendering failed.`
|
|
2023
2127
|
);
|
|
2024
2128
|
throw e;
|
|
2025
2129
|
}
|
|
2026
|
-
|
|
2130
|
+
logger4.warn(
|
|
2027
2131
|
`Page "${chalk3.yellow(routePath)}" SSG rendering error: ${e.message}, fallback to CSR.`
|
|
2028
2132
|
);
|
|
2029
2133
|
}
|
|
@@ -2056,26 +2160,26 @@ async function renderPages(appDirectory, config, pluginDriver, ssgConfig) {
|
|
|
2056
2160
|
`${BODY_START_TAG} ${helmet?.bodyAttributes?.toString()}`
|
|
2057
2161
|
);
|
|
2058
2162
|
}
|
|
2059
|
-
const normalizeHtmlFilePath = (
|
|
2163
|
+
const normalizeHtmlFilePath = (path13) => {
|
|
2060
2164
|
const normalizedBase = normalizeSlash(config?.base || "/");
|
|
2061
|
-
if (
|
|
2062
|
-
return `${
|
|
2165
|
+
if (path13.endsWith("/")) {
|
|
2166
|
+
return `${path13}index.html`.replace(normalizedBase, "");
|
|
2063
2167
|
}
|
|
2064
|
-
return `${
|
|
2168
|
+
return `${path13}.html`.replace(normalizedBase, "");
|
|
2065
2169
|
};
|
|
2066
2170
|
const fileName = normalizeHtmlFilePath(routePath);
|
|
2067
|
-
await
|
|
2068
|
-
await
|
|
2171
|
+
await fs11.ensureDir(join3(outputPath, dirname2(fileName)));
|
|
2172
|
+
await fs11.writeFile(join3(outputPath, fileName), html);
|
|
2069
2173
|
})
|
|
2070
2174
|
);
|
|
2071
2175
|
if (!isDebugMode2()) {
|
|
2072
|
-
await
|
|
2176
|
+
await fs11.remove(join3(outputPath, "ssr"));
|
|
2073
2177
|
}
|
|
2074
|
-
await
|
|
2178
|
+
await fs11.remove(join3(outputPath, "html"));
|
|
2075
2179
|
const totalTime = Date.now() - startTime;
|
|
2076
|
-
|
|
2180
|
+
logger4.success(`Pages rendered in ${chalk3.yellow(totalTime)} ms.`);
|
|
2077
2181
|
} catch (e) {
|
|
2078
|
-
|
|
2182
|
+
logger4.error(`Pages render error: ${e.stack}`);
|
|
2079
2183
|
throw e;
|
|
2080
2184
|
}
|
|
2081
2185
|
}
|
|
@@ -2086,7 +2190,7 @@ async function build(options) {
|
|
|
2086
2190
|
const modifiedConfig = await pluginDriver.modifyConfig();
|
|
2087
2191
|
await pluginDriver.beforeBuild();
|
|
2088
2192
|
const ssgConfig = modifiedConfig.ssg ?? true;
|
|
2089
|
-
await
|
|
2193
|
+
await fs10.emptyDir(TEMP_DIR);
|
|
2090
2194
|
await bundle(docDirectory, modifiedConfig, pluginDriver, Boolean(ssgConfig));
|
|
2091
2195
|
await renderPages(appDirectory, modifiedConfig, pluginDriver, ssgConfig);
|
|
2092
2196
|
await pluginDriver.afterBuild();
|