@rspress/core 2.0.0-beta.13 → 2.0.0-beta.15
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 +18 -4
- package/dist/index.js +241 -237
- package/dist/loader.d.ts +18 -4
- package/dist/processor.d.ts +21 -7
- package/dist/processor.js +96 -27
- package/dist/runtime/App.js +5 -5
- package/dist/runtime/initPageData.js +12 -12
- package/dist/shiki-transformers.d.ts +38 -0
- package/dist/shiki-transformers.js +79 -0
- package/dist/shiki-transformers.js.LICENSE.txt +5 -0
- package/package.json +17 -12
package/dist/index.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import __rslib_shim_module__ from 'module';
|
|
2
2
|
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(import.meta.url);
|
|
3
3
|
import { pluginContainerSyntax } from "@rspress/plugin-container-syntax";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
4
|
+
import node_fs_promises, { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
5
|
+
import node_path, { extname as external_node_path_extname, join } from "node:path";
|
|
6
6
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
7
7
|
import { PLUGIN_REACT_NAME, pluginReact } from "@rsbuild/plugin-react";
|
|
8
8
|
import { APPEARANCE_KEY, MDX_OR_MD_REGEXP, RSPRESS_TEMP_DIR, SEARCH_INDEX_NAME, addLeadingSlash, addTrailingSlash, isDebugMode, isExternalUrl, isSCM, normalizeHref, normalizeSlash, parseUrl, removeLeadingSlash, removeTrailingSlash, slash, withBase, withoutBase } from "@rspress/shared";
|
|
9
9
|
import { logger } from "@rspress/shared/logger";
|
|
10
|
-
import
|
|
10
|
+
import picocolors from "picocolors";
|
|
11
11
|
import { DEFAULT_PAGE_EXTENSIONS } from "@rspress/shared/constants";
|
|
12
12
|
import { glob } from "tinyglobby";
|
|
13
|
-
import
|
|
13
|
+
import node_os from "node:os";
|
|
14
14
|
import { RspackVirtualModulePlugin } from "rspack-plugin-virtual-module";
|
|
15
15
|
import { getIconUrlPath, getNodeAttribute, loadFrontMatter, mergeDocConfig } from "@rspress/shared/node-utils";
|
|
16
16
|
import { groupBy } from "lodash-es";
|
|
17
|
-
import { createHash } from "node:crypto";
|
|
18
17
|
import { compile } from "@rspress/mdx-rs";
|
|
19
18
|
import { htmlToText } from "html-to-text";
|
|
20
|
-
import
|
|
19
|
+
import node_fs, { createReadStream, existsSync } from "node:fs";
|
|
21
20
|
import { createProcessor } from "@mdx-js/mdx";
|
|
22
|
-
import
|
|
21
|
+
import enhanced_resolve from "enhanced-resolve";
|
|
22
|
+
import { createHash } from "node:crypto";
|
|
23
23
|
import { createHead, transformHtmlTemplate } from "@unhead/react/server";
|
|
24
24
|
import { mergeRsbuildConfig as core_mergeRsbuildConfig } from "@rsbuild/core";
|
|
25
25
|
import { visit } from "unist-util-visit";
|
|
@@ -36,7 +36,6 @@ class PluginDriver {
|
|
|
36
36
|
async init() {
|
|
37
37
|
this.clearPlugins();
|
|
38
38
|
const config = this.#config;
|
|
39
|
-
const markdownConfig = config.markdown || {};
|
|
40
39
|
const themeConfig = config?.themeConfig || {};
|
|
41
40
|
const enableLastUpdated = themeConfig?.lastUpdated || themeConfig?.locales?.some((locale)=>locale.lastUpdated);
|
|
42
41
|
const mediumZoomConfig = config?.mediumZoom ?? true;
|
|
@@ -50,11 +49,9 @@ class PluginDriver {
|
|
|
50
49
|
this.addPlugin(pluginMediumZoom('object' == typeof mediumZoomConfig ? mediumZoomConfig : void 0));
|
|
51
50
|
}
|
|
52
51
|
this.addPlugin(pluginContainerSyntax());
|
|
53
|
-
const { pluginShiki } = await import("@rspress/plugin-shiki");
|
|
54
52
|
(config.plugins || []).forEach((plugin)=>{
|
|
55
53
|
this.addPlugin(plugin);
|
|
56
54
|
});
|
|
57
|
-
this.addPlugin(pluginShiki(markdownConfig.shiki));
|
|
58
55
|
if (!haveNavSidebarConfig) {
|
|
59
56
|
const { pluginAutoNavSidebar } = await import("@rspress/plugin-auto-nav-sidebar");
|
|
60
57
|
this.addPlugin(pluginAutoNavSidebar());
|
|
@@ -182,7 +179,7 @@ const pluginVirtualModule = (pluginOptions = {})=>({
|
|
|
182
179
|
}
|
|
183
180
|
}
|
|
184
181
|
});
|
|
185
|
-
var package_namespaceObject = JSON.parse('{"i8":"2.0.0-beta.
|
|
182
|
+
var package_namespaceObject = JSON.parse('{"i8":"2.0.0-beta.15"}');
|
|
186
183
|
const RSPRESS_VERSION = package_namespaceObject.i8;
|
|
187
184
|
const isProduction = ()=>'production' === process.env.NODE_ENV;
|
|
188
185
|
const importStatementRegex = /import\s+(.*?)\s+from\s+(['"])(.*?)(?:"|');?/gm;
|
|
@@ -194,29 +191,29 @@ const inlineThemeScript = `{
|
|
|
194
191
|
document.documentElement.classList.toggle('rp-dark', isDark)
|
|
195
192
|
document.documentElement.style.colorScheme = isDark ? 'dark' : 'light'
|
|
196
193
|
}`.replace(/\n/g, ';').replace(/\s{2,}/g, '');
|
|
197
|
-
const dirname =
|
|
198
|
-
const PACKAGE_ROOT =
|
|
199
|
-
const TEMPLATE_PATH =
|
|
200
|
-
const CSR_CLIENT_ENTRY =
|
|
201
|
-
const SSR_CLIENT_ENTRY =
|
|
202
|
-
const SSR_SERVER_ENTRY =
|
|
194
|
+
const dirname = node_path.dirname(fileURLToPath(new URL(import.meta.url)));
|
|
195
|
+
const PACKAGE_ROOT = node_path.join(dirname, '..');
|
|
196
|
+
const TEMPLATE_PATH = node_path.join(PACKAGE_ROOT, 'index.html');
|
|
197
|
+
const CSR_CLIENT_ENTRY = node_path.join(PACKAGE_ROOT, 'dist', 'runtime', 'csrClientEntry.js');
|
|
198
|
+
const SSR_CLIENT_ENTRY = node_path.join(PACKAGE_ROOT, 'dist', 'runtime', 'ssrClientEntry.js');
|
|
199
|
+
const SSR_SERVER_ENTRY = node_path.join(PACKAGE_ROOT, 'dist', 'runtime', 'ssrServerEntry.js');
|
|
203
200
|
const OUTPUT_DIR = 'doc_build';
|
|
204
201
|
const APP_HTML_MARKER = '<!--<?- DOC_CONTENT ?>-->';
|
|
205
202
|
const HEAD_MARKER = '<!--<?- HEAD ?>-->';
|
|
206
203
|
const META_GENERATOR = '<!--<?- GENERATOR ?>-->';
|
|
207
204
|
const DEFAULT_TITLE = 'Rspress';
|
|
208
205
|
const PUBLIC_DIR = 'public';
|
|
209
|
-
const constants_TEMP_DIR =
|
|
206
|
+
const constants_TEMP_DIR = node_path.join(process.cwd(), 'node_modules', RSPRESS_TEMP_DIR);
|
|
210
207
|
async function pathExists(path) {
|
|
211
208
|
try {
|
|
212
|
-
await
|
|
209
|
+
await node_fs_promises.access(path);
|
|
213
210
|
return true;
|
|
214
211
|
} catch {
|
|
215
212
|
return false;
|
|
216
213
|
}
|
|
217
214
|
}
|
|
218
215
|
async function readJson(path) {
|
|
219
|
-
const raw = await
|
|
216
|
+
const raw = await node_fs_promises.readFile(path, 'utf8');
|
|
220
217
|
return JSON.parse(raw);
|
|
221
218
|
}
|
|
222
219
|
const THEME_DEFAULT_EXPORT_PATTERN = /export\s+default\s+\{/;
|
|
@@ -238,13 +235,13 @@ async function hintThemeBreakingChange(customThemeDir) {
|
|
|
238
235
|
break;
|
|
239
236
|
}
|
|
240
237
|
}
|
|
241
|
-
if (useDefaultExportFilePath) logger.warn(`[Rspress v2] Breaking Change: The "theme/index.tsx" is now using named export instead of default export, please update ${
|
|
238
|
+
if (useDefaultExportFilePath) logger.warn(`[Rspress v2] Breaking Change: The "theme/index.tsx" is now using named export instead of default export, please update ${picocolors.greenBright(useDefaultExportFilePath)} (https://github.com/web-infra-dev/rspress/discussions/1891#discussioncomment-12422737).\n`, picocolors.redBright(`
|
|
242
239
|
- import Theme from '@rspress/theme-default';
|
|
243
240
|
- export default {
|
|
244
241
|
- ...Theme,
|
|
245
242
|
- Layout,
|
|
246
243
|
- };
|
|
247
|
-
- export * from 'rspress/theme';`) +
|
|
244
|
+
- export * from 'rspress/theme';`) + picocolors.greenBright(`
|
|
248
245
|
+ import { Layout } from '@rspress/theme-default';
|
|
249
246
|
|
|
250
247
|
+ export { Layout };
|
|
@@ -262,17 +259,26 @@ function hintSSGFalse() {
|
|
|
262
259
|
function hintReactVersion() {
|
|
263
260
|
logger.info('[Rspress v2] Rspress support React 18 and 19, please confirm that both react and react-dom are installed in package.json with the same version. ');
|
|
264
261
|
}
|
|
265
|
-
const isWindows = 'win32' ===
|
|
262
|
+
const isWindows = 'win32' === node_os.platform();
|
|
266
263
|
function normalizePath_slash(p) {
|
|
267
264
|
return p.replace(/\\/g, '/');
|
|
268
265
|
}
|
|
269
266
|
function normalizePath(id) {
|
|
270
|
-
return
|
|
267
|
+
return node_path.posix.normalize(isWindows ? normalizePath_slash(id) : id);
|
|
271
268
|
}
|
|
272
269
|
function getPageKey(route) {
|
|
273
270
|
const cleanRoute = removeLeadingSlash(route);
|
|
274
271
|
return cleanRoute.replace(/\//g, '_').replace(/\.[^.]+$/, '') || 'index';
|
|
275
272
|
}
|
|
273
|
+
class RoutePage {
|
|
274
|
+
routeMeta;
|
|
275
|
+
static create(routeMeta) {
|
|
276
|
+
return new RoutePage(routeMeta);
|
|
277
|
+
}
|
|
278
|
+
constructor(routeMeta){
|
|
279
|
+
this.routeMeta = routeMeta;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
276
282
|
const getRoutePathParts = (routePath, lang, version, langs, versions)=>{
|
|
277
283
|
const hasTrailSlash = routePath.endsWith('/');
|
|
278
284
|
let versionPart = '';
|
|
@@ -327,6 +333,13 @@ class RouteService {
|
|
|
327
333
|
#base = '';
|
|
328
334
|
#tempDir = '';
|
|
329
335
|
#pluginDriver;
|
|
336
|
+
static async create(options) {
|
|
337
|
+
const { scanDir, config, runtimeTempDir, pluginDriver } = options;
|
|
338
|
+
const routeService = new RouteService(scanDir, config, runtimeTempDir, pluginDriver);
|
|
339
|
+
await routeService.#init();
|
|
340
|
+
await pluginDriver.routeServiceGenerated(routeService);
|
|
341
|
+
return routeService;
|
|
342
|
+
}
|
|
330
343
|
constructor(scanDir, userConfig, tempDir, pluginDriver){
|
|
331
344
|
const routeOptions = userConfig?.route || {};
|
|
332
345
|
this.#scanDir = scanDir;
|
|
@@ -346,7 +359,7 @@ class RouteService {
|
|
|
346
359
|
get extensions() {
|
|
347
360
|
return this.#extensions;
|
|
348
361
|
}
|
|
349
|
-
async init() {
|
|
362
|
+
async #init() {
|
|
350
363
|
const extensions = this.#extensions.map((i)=>i.slice(1));
|
|
351
364
|
const files = (await glob([
|
|
352
365
|
`**/*.{${extensions.join(',')}}`,
|
|
@@ -364,10 +377,10 @@ class RouteService {
|
|
|
364
377
|
]
|
|
365
378
|
})).sort();
|
|
366
379
|
files.forEach((filePath)=>{
|
|
367
|
-
const fileRelativePath = normalizePath(
|
|
380
|
+
const fileRelativePath = normalizePath(node_path.relative(this.#scanDir, filePath));
|
|
368
381
|
const { routePath, lang, version } = this.normalizeRoutePath(fileRelativePath);
|
|
369
|
-
const absolutePath =
|
|
370
|
-
const
|
|
382
|
+
const absolutePath = node_path.join(this.#scanDir, fileRelativePath);
|
|
383
|
+
const routeMeta = {
|
|
371
384
|
routePath,
|
|
372
385
|
absolutePath: normalizePath(absolutePath),
|
|
373
386
|
relativePath: fileRelativePath,
|
|
@@ -375,47 +388,45 @@ class RouteService {
|
|
|
375
388
|
lang,
|
|
376
389
|
version
|
|
377
390
|
};
|
|
378
|
-
this.addRoute(
|
|
391
|
+
this.addRoute(routeMeta);
|
|
379
392
|
});
|
|
380
393
|
const externalPages = await this.#pluginDriver.addPages();
|
|
381
394
|
await Promise.all(externalPages.map(async (route, index)=>{
|
|
382
395
|
const { routePath, content, filepath } = route;
|
|
383
396
|
if (filepath) {
|
|
384
|
-
const
|
|
385
|
-
this.addRoute(
|
|
397
|
+
const routeMeta = this.#generateRouteMeta(routePath, filepath);
|
|
398
|
+
this.addRoute(routeMeta);
|
|
386
399
|
return;
|
|
387
400
|
}
|
|
388
401
|
if (content) {
|
|
389
402
|
const filepath = await this.#writeTempFile(index, content);
|
|
390
|
-
const
|
|
391
|
-
this.addRoute(
|
|
403
|
+
const routeMeta = this.#generateRouteMeta(routePath, filepath);
|
|
404
|
+
this.addRoute(routeMeta);
|
|
392
405
|
}
|
|
393
406
|
}));
|
|
394
407
|
await this.#pluginDriver.routeGenerated(this.getRoutes());
|
|
395
408
|
}
|
|
396
|
-
addRoute(
|
|
397
|
-
const { routePath
|
|
398
|
-
if (this.routeData.has(routePath)) {
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
if (-1 !== currRouteExtIndex && (currRouteExtIndex < preRouteExtIndex || -1 === preRouteExtIndex)) this.routeData.set(routePath, routeInfo);
|
|
402
|
-
} else this.routeData.set(routePath, routeInfo);
|
|
409
|
+
async addRoute(routeMeta) {
|
|
410
|
+
const { routePath } = routeMeta;
|
|
411
|
+
if (this.routeData.has(routePath)) throw new Error(`routePath ${routePath} has already been added`);
|
|
412
|
+
const routePage = RoutePage.create(routeMeta);
|
|
413
|
+
this.routeData.set(routePath, routePage);
|
|
403
414
|
}
|
|
404
415
|
removeRoute(filePath) {
|
|
405
|
-
const fileRelativePath =
|
|
416
|
+
const fileRelativePath = node_path.relative(this.#scanDir, filePath);
|
|
406
417
|
const { routePath } = this.normalizeRoutePath(fileRelativePath);
|
|
407
418
|
this.routeData.delete(routePath);
|
|
408
419
|
}
|
|
409
420
|
getRoutes() {
|
|
421
|
+
return Array.from(this.routeData.values()).map((i)=>i.routeMeta);
|
|
422
|
+
}
|
|
423
|
+
getRoutePages() {
|
|
410
424
|
return Array.from(this.routeData.values());
|
|
411
425
|
}
|
|
412
426
|
isExistRoute(routePath) {
|
|
413
427
|
const { routePath: normalizedRoute } = this.normalizeRoutePath(routePath);
|
|
414
428
|
return Boolean(this.routeData.get(normalizedRoute));
|
|
415
429
|
}
|
|
416
|
-
isEmpty() {
|
|
417
|
-
return 0 === this.routeData.size;
|
|
418
|
-
}
|
|
419
430
|
generateRoutesCode() {
|
|
420
431
|
return this.generateRoutesCodeByRouteMeta(this.getRoutes());
|
|
421
432
|
}
|
|
@@ -443,48 +454,30 @@ ${routeMeta.map((route, index)=>{
|
|
|
443
454
|
return normalizeRoutePath(routePath, this.#base, this.#defaultLang, this.#defaultVersion, this.#langs, this.#versions, this.#extensions);
|
|
444
455
|
}
|
|
445
456
|
async #writeTempFile(index, content) {
|
|
446
|
-
const tempFilePath =
|
|
447
|
-
await
|
|
457
|
+
const tempFilePath = node_path.join(this.#tempDir, `temp-${index}.mdx`);
|
|
458
|
+
await node_fs_promises.writeFile(tempFilePath, content);
|
|
448
459
|
return tempFilePath;
|
|
449
460
|
}
|
|
450
|
-
#
|
|
461
|
+
#generateRouteMeta(routePath, filepath) {
|
|
451
462
|
const { routePath: normalizedPath, lang, version } = this.normalizeRoutePath(routePath);
|
|
452
463
|
return {
|
|
453
464
|
routePath: normalizedPath,
|
|
454
465
|
absolutePath: normalizePath(filepath),
|
|
455
|
-
relativePath: normalizePath(
|
|
466
|
+
relativePath: normalizePath(node_path.relative(this.#scanDir, filepath)),
|
|
456
467
|
pageName: getPageKey(routePath),
|
|
457
468
|
lang,
|
|
458
469
|
version
|
|
459
470
|
};
|
|
460
471
|
}
|
|
472
|
+
getRoutePageByRoutePath(routePath) {
|
|
473
|
+
return this.routeData.get(routePath);
|
|
474
|
+
}
|
|
461
475
|
}
|
|
462
|
-
let init_routeService = null;
|
|
463
|
-
async function initRouteService(options) {
|
|
464
|
-
const { scanDir, config, runtimeTempDir, pluginDriver } = options;
|
|
465
|
-
init_routeService = new RouteService(scanDir, config, runtimeTempDir, pluginDriver);
|
|
466
|
-
await init_routeService.init();
|
|
467
|
-
await pluginDriver.routeServiceGenerated(init_routeService);
|
|
468
|
-
return init_routeService;
|
|
469
|
-
}
|
|
470
|
-
function createHash_createHash(str) {
|
|
471
|
-
return createHash('sha256').update(str).digest('hex').slice(0, 8);
|
|
472
|
-
}
|
|
473
|
-
var types_RuntimeModuleID = /*#__PURE__*/ function(RuntimeModuleID) {
|
|
474
|
-
RuntimeModuleID["GlobalStyles"] = "virtual-global-styles";
|
|
475
|
-
RuntimeModuleID["GlobalComponents"] = "virtual-global-components";
|
|
476
|
-
RuntimeModuleID["Routes"] = "virtual-routes";
|
|
477
|
-
RuntimeModuleID["SiteData"] = "virtual-site-data";
|
|
478
|
-
RuntimeModuleID["SearchIndexHash"] = "virtual-search-index-hash";
|
|
479
|
-
RuntimeModuleID["I18nText"] = "virtual-i18n-text";
|
|
480
|
-
RuntimeModuleID["SearchHooks"] = "virtual-search-hooks";
|
|
481
|
-
return RuntimeModuleID;
|
|
482
|
-
}({});
|
|
483
476
|
let flattenMdxContent_resolver;
|
|
484
477
|
let startFlatten = false;
|
|
485
478
|
const processor = createProcessor();
|
|
486
|
-
const { CachedInputFileSystem, ResolverFactory } =
|
|
487
|
-
const fileSystem =
|
|
479
|
+
const { CachedInputFileSystem, ResolverFactory } = enhanced_resolve;
|
|
480
|
+
const fileSystem = node_fs;
|
|
488
481
|
async function resolveDepPath(importPath, importer, alias) {
|
|
489
482
|
if (!flattenMdxContent_resolver) flattenMdxContent_resolver = ResolverFactory.createResolver({
|
|
490
483
|
fileSystem: new CachedInputFileSystem(fileSystem, 0),
|
|
@@ -541,12 +534,12 @@ async function flattenMdxContent(content, basePath, alias) {
|
|
|
541
534
|
const importPath = importNode.source.value;
|
|
542
535
|
let absoluteImportPath;
|
|
543
536
|
try {
|
|
544
|
-
absoluteImportPath = await resolveDepPath(importPath,
|
|
537
|
+
absoluteImportPath = await resolveDepPath(importPath, node_path.dirname(basePath), alias);
|
|
545
538
|
} catch (_e) {
|
|
546
539
|
continue;
|
|
547
540
|
}
|
|
548
541
|
if (MDX_OR_MD_REGEXP.test(absoluteImportPath)) {
|
|
549
|
-
const importedContent =
|
|
542
|
+
const importedContent = node_fs.readFileSync(absoluteImportPath, 'utf-8');
|
|
550
543
|
const { flattenContent: replacedValue, deps: subDeps } = await flattenMdxContent(importedContent, absoluteImportPath, alias);
|
|
551
544
|
result = result.replace(new RegExp(`import\\s+${id}\\s+from\\s+['"](${importPath})['"];?`), '').replace(new RegExp(`<${id}\\s*/>`, 'g'), ()=>replacedValue);
|
|
552
545
|
deps.push(...subDeps, absoluteImportPath);
|
|
@@ -567,106 +560,119 @@ function applyReplaceRulesToNestedObject(obj, replaceRules) {
|
|
|
567
560
|
else if ('object' == typeof obj[key] && null !== obj[key]) obj[key] = applyReplaceRulesToNestedObject(obj[key], replaceRules);
|
|
568
561
|
return obj;
|
|
569
562
|
}
|
|
570
|
-
async function
|
|
571
|
-
const
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
563
|
+
async function getPageIndexInfoByRoute(route, options) {
|
|
564
|
+
const { alias, domain, replaceRules, root, searchCodeBlocks } = options;
|
|
565
|
+
const defaultIndexInfo = {
|
|
566
|
+
title: '',
|
|
567
|
+
content: '',
|
|
568
|
+
_html: '',
|
|
569
|
+
_flattenContent: '',
|
|
570
|
+
routePath: route.routePath,
|
|
571
|
+
lang: route.lang,
|
|
572
|
+
toc: [],
|
|
573
|
+
domain,
|
|
574
|
+
frontmatter: {},
|
|
575
|
+
version: route.version,
|
|
576
|
+
_filepath: route.absolutePath,
|
|
577
|
+
_relativePath: node_path.relative(root, route.absolutePath).split(node_path.sep).join('/')
|
|
578
|
+
};
|
|
579
|
+
if (!MDX_OR_MD_REGEXP.test(route.absolutePath)) return defaultIndexInfo;
|
|
580
|
+
let content = await node_fs_promises.readFile(route.absolutePath, 'utf8');
|
|
581
|
+
const { frontmatter, content: contentWithoutFrontMatter } = loadFrontMatter(content, route.absolutePath, root);
|
|
582
|
+
applyReplaceRulesToNestedObject(frontmatter, replaceRules);
|
|
583
|
+
const { flattenContent } = await flattenMdxContent(applyReplaceRules(contentWithoutFrontMatter, replaceRules), route.absolutePath, alias);
|
|
584
|
+
content = flattenContent.replace(importStatementRegex, '');
|
|
585
|
+
const { html: rawHtml, title, toc: rawToc } = await compile({
|
|
586
|
+
value: content,
|
|
587
|
+
filepath: route.absolutePath,
|
|
588
|
+
development: 'production' !== process.env.NODE_ENV,
|
|
589
|
+
root
|
|
590
|
+
});
|
|
591
|
+
function encodeHtml(html) {
|
|
592
|
+
return html.replace(/<code>([\s\S]*?)<\/\s?code>/gm, function(_match, innerContent) {
|
|
593
|
+
return `<code>${innerContent.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>')}</code>`;
|
|
598
594
|
});
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
595
|
+
}
|
|
596
|
+
const html = encodeHtml(String(rawHtml));
|
|
597
|
+
content = htmlToText(html, {
|
|
598
|
+
wordwrap: 80,
|
|
599
|
+
selectors: [
|
|
600
|
+
{
|
|
601
|
+
selector: 'a',
|
|
602
|
+
options: {
|
|
603
|
+
ignoreHref: true
|
|
604
|
+
}
|
|
605
|
+
},
|
|
606
|
+
{
|
|
607
|
+
selector: 'img',
|
|
608
|
+
format: 'skip'
|
|
609
|
+
},
|
|
610
|
+
{
|
|
611
|
+
selector: 'pre > code',
|
|
612
|
+
format: searchCodeBlocks ? 'block' : 'skip'
|
|
613
|
+
},
|
|
614
|
+
...[
|
|
615
|
+
'h1',
|
|
616
|
+
'h2',
|
|
617
|
+
'h3',
|
|
618
|
+
'h4',
|
|
619
|
+
'h5',
|
|
620
|
+
'h6'
|
|
621
|
+
].map((tag)=>({
|
|
622
|
+
selector: tag,
|
|
610
623
|
options: {
|
|
611
|
-
|
|
624
|
+
uppercase: false
|
|
612
625
|
}
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
'h6'
|
|
629
|
-
].map((tag)=>({
|
|
630
|
-
selector: tag,
|
|
631
|
-
options: {
|
|
632
|
-
uppercase: false
|
|
633
|
-
}
|
|
634
|
-
}))
|
|
635
|
-
],
|
|
636
|
-
tables: true,
|
|
637
|
-
longWordSplit: {
|
|
638
|
-
forceWrapOnLimit: true
|
|
639
|
-
}
|
|
640
|
-
});
|
|
641
|
-
if (content.startsWith(title)) content = content.slice(title.length);
|
|
642
|
-
languages.forEach((lang)=>highlighterLangs.add(lang));
|
|
643
|
-
const toc = rawToc.map((item)=>{
|
|
644
|
-
const match = item.id.match(/-(\d+)$/);
|
|
645
|
-
let position = -1;
|
|
646
|
-
if (match) for(let i = 0; i < Number(match[1]); i++){
|
|
647
|
-
position = content.indexOf(`\n${item.text}#\n\n`, position + 1);
|
|
648
|
-
if (-1 === position) break;
|
|
649
|
-
}
|
|
650
|
-
return {
|
|
651
|
-
...item,
|
|
652
|
-
charIndex: content.indexOf(`\n${item.text}#\n\n`, position + 1)
|
|
653
|
-
};
|
|
654
|
-
});
|
|
626
|
+
}))
|
|
627
|
+
],
|
|
628
|
+
tables: true,
|
|
629
|
+
longWordSplit: {
|
|
630
|
+
forceWrapOnLimit: true
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
if (content.startsWith(title)) content = content.slice(title.length);
|
|
634
|
+
const toc = rawToc.map((item)=>{
|
|
635
|
+
const match = item.id.match(/-(\d+)$/);
|
|
636
|
+
let position = -1;
|
|
637
|
+
if (match) for(let i = 0; i < Number(match[1]); i++){
|
|
638
|
+
position = content.indexOf(`\n${item.text}#\n\n`, position + 1);
|
|
639
|
+
if (-1 === position) break;
|
|
640
|
+
}
|
|
655
641
|
return {
|
|
656
|
-
...
|
|
657
|
-
|
|
658
|
-
toc,
|
|
659
|
-
content,
|
|
660
|
-
_flattenContent: flattenContent,
|
|
661
|
-
_html: html,
|
|
662
|
-
frontmatter: {
|
|
663
|
-
...frontmatter,
|
|
664
|
-
__content: void 0
|
|
665
|
-
}
|
|
642
|
+
...item,
|
|
643
|
+
charIndex: content.indexOf(`\n${item.text}#\n\n`, position + 1)
|
|
666
644
|
};
|
|
667
|
-
})
|
|
668
|
-
return
|
|
645
|
+
});
|
|
646
|
+
return {
|
|
647
|
+
...defaultIndexInfo,
|
|
648
|
+
title: frontmatter.title || title,
|
|
649
|
+
toc,
|
|
650
|
+
content,
|
|
651
|
+
_html: html,
|
|
652
|
+
_flattenContent: flattenContent,
|
|
653
|
+
frontmatter: {
|
|
654
|
+
...frontmatter,
|
|
655
|
+
__content: void 0
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
async function extractPageData(routeService, options) {
|
|
660
|
+
const pageData = await Promise.all(routeService.getRoutes().map((routeMeta)=>getPageIndexInfoByRoute(routeMeta, options)));
|
|
661
|
+
return pageData;
|
|
669
662
|
}
|
|
663
|
+
function createHash_createHash(str) {
|
|
664
|
+
return createHash('sha256').update(str).digest('hex').slice(0, 8);
|
|
665
|
+
}
|
|
666
|
+
var types_RuntimeModuleID = /*#__PURE__*/ function(RuntimeModuleID) {
|
|
667
|
+
RuntimeModuleID["GlobalStyles"] = "virtual-global-styles";
|
|
668
|
+
RuntimeModuleID["GlobalComponents"] = "virtual-global-components";
|
|
669
|
+
RuntimeModuleID["Routes"] = "virtual-routes";
|
|
670
|
+
RuntimeModuleID["SiteData"] = "virtual-site-data";
|
|
671
|
+
RuntimeModuleID["SearchIndexHash"] = "virtual-search-index-hash";
|
|
672
|
+
RuntimeModuleID["I18nText"] = "virtual-i18n-text";
|
|
673
|
+
RuntimeModuleID["SearchHooks"] = "virtual-search-hooks";
|
|
674
|
+
return RuntimeModuleID;
|
|
675
|
+
}({});
|
|
670
676
|
const DEFAULT_I18N_SOURCE = join(process.cwd(), 'i18n.json');
|
|
671
677
|
function getI18nData(docConfig) {
|
|
672
678
|
const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig;
|
|
@@ -731,7 +737,7 @@ function normalizeThemeConfig(docConfig, pages = []) {
|
|
|
731
737
|
return {
|
|
732
738
|
text: applyReplaceRules(page?.title || '', replaceRules),
|
|
733
739
|
link: normalizedItem,
|
|
734
|
-
_fileKey: page?._relativePath.replace(
|
|
740
|
+
_fileKey: page?._relativePath.replace(node_path.extname(page._relativePath), '')
|
|
735
741
|
};
|
|
736
742
|
}
|
|
737
743
|
return {
|
|
@@ -805,12 +811,17 @@ async function siteDataVMPlugin(context) {
|
|
|
805
811
|
const userConfig = config;
|
|
806
812
|
const tempSearchObj = Object.assign({}, userConfig.search);
|
|
807
813
|
if (tempSearchObj) tempSearchObj.searchHooks = void 0;
|
|
808
|
-
const highlightLanguages = new Set();
|
|
809
814
|
const replaceRules = userConfig?.replaceRules || [];
|
|
810
815
|
const searchConfig = userConfig?.search || {};
|
|
811
816
|
const domain = searchConfig?.mode === 'remote' ? searchConfig.domain ?? '' : '';
|
|
812
817
|
const searchCodeBlocks = 'codeBlocks' in searchConfig ? Boolean(searchConfig.codeBlocks) : true;
|
|
813
|
-
const pages = await extractPageData(
|
|
818
|
+
const pages = await extractPageData(routeService, {
|
|
819
|
+
replaceRules,
|
|
820
|
+
alias,
|
|
821
|
+
domain,
|
|
822
|
+
root: userDocRoot,
|
|
823
|
+
searchCodeBlocks
|
|
824
|
+
});
|
|
814
825
|
await pluginDriver.modifySearchIndexData(pages);
|
|
815
826
|
const versioned = userConfig.search && 'remote' !== userConfig.search.mode && userConfig.search.versioned;
|
|
816
827
|
const groupedPages = groupBy(pages, (page)=>{
|
|
@@ -828,10 +839,10 @@ async function siteDataVMPlugin(context) {
|
|
|
828
839
|
const [version, lang] = group.split('###');
|
|
829
840
|
const indexVersion = version ? `.${version.replace('.', '_')}` : '';
|
|
830
841
|
const indexLang = lang ? `.${lang}` : '';
|
|
831
|
-
await
|
|
842
|
+
await node_fs_promises.mkdir(constants_TEMP_DIR, {
|
|
832
843
|
recursive: true
|
|
833
844
|
});
|
|
834
|
-
await
|
|
845
|
+
await node_fs_promises.writeFile(node_path.join(constants_TEMP_DIR, `${SEARCH_INDEX_NAME}${indexVersion}${indexLang}.${indexHash}.json`), stringifiedIndex);
|
|
835
846
|
}));
|
|
836
847
|
await Promise.all(pages.map(async (pageData)=>pluginDriver.extendPageData(pageData)));
|
|
837
848
|
const siteData = {
|
|
@@ -854,7 +865,7 @@ async function siteDataVMPlugin(context) {
|
|
|
854
865
|
mode: 'local'
|
|
855
866
|
},
|
|
856
867
|
pages: pages.map((page)=>{
|
|
857
|
-
const { content,
|
|
868
|
+
const { content, domain, _filepath, _html, _flattenContent, ...rest } = page;
|
|
858
869
|
return isProduction() ? rest : {
|
|
859
870
|
...rest,
|
|
860
871
|
_filepath
|
|
@@ -866,7 +877,6 @@ async function siteDataVMPlugin(context) {
|
|
|
866
877
|
shiki: {}
|
|
867
878
|
}
|
|
868
879
|
};
|
|
869
|
-
if (Array.isArray(siteData.pages[0]?.extraHighlightLanguages)) siteData.pages[0].extraHighlightLanguages.forEach((lang)=>highlightLanguages.add(lang));
|
|
870
880
|
return {
|
|
871
881
|
[`${types_RuntimeModuleID.SiteData}.mjs`]: `export default ${JSON.stringify(siteData, null, 2)}`,
|
|
872
882
|
[types_RuntimeModuleID.SearchIndexHash]: `export default ${JSON.stringify(indexHashByGroup, null, 2)}`
|
|
@@ -876,7 +886,6 @@ const runtimeModuleFactory = [
|
|
|
876
886
|
siteDataVMPlugin
|
|
877
887
|
];
|
|
878
888
|
function rsbuildPluginDocVM(factoryContext) {
|
|
879
|
-
const { pluginDriver } = factoryContext;
|
|
880
889
|
return {
|
|
881
890
|
name: 'rsbuild-plugin-doc-vm',
|
|
882
891
|
setup (api) {
|
|
@@ -890,16 +899,20 @@ function rsbuildPluginDocVM(factoryContext) {
|
|
|
890
899
|
});
|
|
891
900
|
Object.assign(runtimeModule, moduleResult);
|
|
892
901
|
}
|
|
893
|
-
const modulesByPlugin = await pluginDriver.addRuntimeModules();
|
|
894
|
-
Object.keys(modulesByPlugin).forEach((key)=>{
|
|
895
|
-
if (runtimeModule[key]) throw new Error(`The runtime module ${key} is duplicated, please check your plugin`);
|
|
896
|
-
runtimeModule[key] = modulesByPlugin[key];
|
|
897
|
-
});
|
|
898
902
|
bundlerChain.plugin('rspress-runtime-module').use(new RspackVirtualModulePlugin(runtimeModule));
|
|
899
903
|
});
|
|
900
904
|
}
|
|
901
905
|
};
|
|
902
906
|
}
|
|
907
|
+
async function getVirtualModulesFromPlugins(pluginDriver) {
|
|
908
|
+
const runtimeModule = {};
|
|
909
|
+
const modulesByPlugin = await pluginDriver.addRuntimeModules();
|
|
910
|
+
Object.keys(modulesByPlugin).forEach((key)=>{
|
|
911
|
+
if (runtimeModule[key]) throw new Error(`The runtime module ${key} is duplicated, please check your plugin`);
|
|
912
|
+
runtimeModule[key] = ()=>modulesByPlugin[key];
|
|
913
|
+
});
|
|
914
|
+
return runtimeModule;
|
|
915
|
+
}
|
|
903
916
|
const globalStylesVMPlugin = (context)=>({
|
|
904
917
|
[types_RuntimeModuleID.GlobalStyles]: ()=>{
|
|
905
918
|
const { config, pluginDriver } = context;
|
|
@@ -952,10 +965,10 @@ const searchHookVMPlugin = (context)=>({
|
|
|
952
965
|
async function writeSearchIndex(config) {
|
|
953
966
|
if (config?.search === false) return;
|
|
954
967
|
const cwd = process.cwd();
|
|
955
|
-
const searchIndexFiles = await
|
|
968
|
+
const searchIndexFiles = await node_fs_promises.readdir(constants_TEMP_DIR);
|
|
956
969
|
const outDir = config?.outDir ?? join(cwd, OUTPUT_DIR);
|
|
957
970
|
const targetDir = join(outDir, 'static');
|
|
958
|
-
await
|
|
971
|
+
await node_fs_promises.mkdir(targetDir, {
|
|
959
972
|
recursive: true
|
|
960
973
|
});
|
|
961
974
|
let searchIndexData = '[]';
|
|
@@ -964,9 +977,9 @@ async function writeSearchIndex(config) {
|
|
|
964
977
|
if (!searchIndexFile.includes(SEARCH_INDEX_NAME) || !searchIndexFile.endsWith('.json')) continue;
|
|
965
978
|
const source = join(constants_TEMP_DIR, searchIndexFile);
|
|
966
979
|
const target = join(targetDir, searchIndexFile);
|
|
967
|
-
const searchIndex = await
|
|
980
|
+
const searchIndex = await node_fs_promises.readFile(join(constants_TEMP_DIR, searchIndexFile), 'utf-8');
|
|
968
981
|
searchIndexData = `${searchIndexData.slice(0, -1)}${scanning ? ',' : ''}${searchIndex.slice(1)}`;
|
|
969
|
-
await
|
|
982
|
+
await node_fs_promises.rename(source, target);
|
|
970
983
|
scanning = true;
|
|
971
984
|
}
|
|
972
985
|
if (isProduction() && isSCM() && config?.search?.mode === 'remote') {
|
|
@@ -979,9 +992,9 @@ async function writeSearchIndex(config) {
|
|
|
979
992
|
'Content-Type': 'application/json'
|
|
980
993
|
}
|
|
981
994
|
});
|
|
982
|
-
logger.info(
|
|
995
|
+
logger.info(picocolors.green(`[doc-tools] Search index uploaded to ${apiUrl}, indexName: ${indexName}`));
|
|
983
996
|
} catch (e) {
|
|
984
|
-
logger.info(
|
|
997
|
+
logger.info(picocolors.red(`[doc-tools] Upload search index \`${indexName}\` failed:\n ${e}`));
|
|
985
998
|
}
|
|
986
999
|
}
|
|
987
1000
|
}
|
|
@@ -1089,15 +1102,6 @@ async function pMap(iterable, mapper, { concurrency = Number.POSITIVE_INFINITY,
|
|
|
1089
1102
|
});
|
|
1090
1103
|
}
|
|
1091
1104
|
const pMapSkip = Symbol('skip');
|
|
1092
|
-
async function renderFrontmatterHead(route) {
|
|
1093
|
-
if (!isRouteMeta(route)) return '';
|
|
1094
|
-
const content = await promises_default.readFile(route.absolutePath, {
|
|
1095
|
-
encoding: 'utf-8'
|
|
1096
|
-
});
|
|
1097
|
-
const { frontmatter: { head } } = loadFrontMatter(content, route.absolutePath, '', true);
|
|
1098
|
-
if (!head || 0 === head.length) return '';
|
|
1099
|
-
return head.map(([tag, attrs])=>`<${tag} ${renderAttrs(attrs)}>`).join('');
|
|
1100
|
-
}
|
|
1101
1105
|
async function renderConfigHead(config, route) {
|
|
1102
1106
|
if (!isRouteMeta(route)) return '';
|
|
1103
1107
|
if (!config.head || 0 === config.head.length) return '';
|
|
@@ -1128,10 +1132,10 @@ async function renderPages(routeService, config, pluginDriver, ssrBundlePath, ht
|
|
|
1128
1132
|
let render;
|
|
1129
1133
|
try {
|
|
1130
1134
|
const { default: ssrExports } = await import(pathToFileURL(ssrBundlePath).toString());
|
|
1131
|
-
|
|
1135
|
+
render = await ssrExports.render;
|
|
1132
1136
|
} catch (e) {
|
|
1133
1137
|
if (e instanceof Error) {
|
|
1134
|
-
logger.error(`Failed to load SSG bundle: ${
|
|
1138
|
+
logger.error(`Failed to load SSG bundle: ${picocolors.yellow(ssrBundlePath)}: ${e.message}`);
|
|
1135
1139
|
logger.debug(e);
|
|
1136
1140
|
hintSSGFailed();
|
|
1137
1141
|
}
|
|
@@ -1160,13 +1164,12 @@ async function renderPages(routeService, config, pluginDriver, ssrBundlePath, ht
|
|
|
1160
1164
|
({ appHtml } = await render(routePath, head));
|
|
1161
1165
|
} catch (e) {
|
|
1162
1166
|
if (e instanceof Error) {
|
|
1163
|
-
logger.error(`Page "${
|
|
1167
|
+
logger.error(`Page "${picocolors.yellow(routePath)}" SSG rendering failed.\n ${picocolors.gray(e.toString())}`);
|
|
1164
1168
|
throw e;
|
|
1165
1169
|
}
|
|
1166
1170
|
}
|
|
1167
1171
|
const replacedHtmlTemplate = htmlTemplate.replace(/<title>(.*?)<\/title>/gi, '').replace(APP_HTML_MARKER, ()=>appHtml).replace(META_GENERATOR, ()=>`<meta name="generator" content="Rspress v${RSPRESS_VERSION}">`).replace(HEAD_MARKER, [
|
|
1168
|
-
await renderConfigHead(config, route)
|
|
1169
|
-
await renderFrontmatterHead(route)
|
|
1172
|
+
await renderConfigHead(config, route)
|
|
1170
1173
|
].join(''));
|
|
1171
1174
|
const html = await transformHtmlTemplate(head, replacedHtmlTemplate);
|
|
1172
1175
|
const normalizeHtmlFilePath = (path)=>{
|
|
@@ -1180,7 +1183,7 @@ async function renderPages(routeService, config, pluginDriver, ssrBundlePath, ht
|
|
|
1180
1183
|
concurrency: process.env.RSPRESS_SSG_CONCURRENCY ? Number.parseInt(process.env.RSPRESS_SSG_CONCURRENCY, 10) : 32
|
|
1181
1184
|
});
|
|
1182
1185
|
const totalTime = Date.now() - startTime;
|
|
1183
|
-
logger.success(`Pages rendered in ${
|
|
1186
|
+
logger.success(`Pages rendered in ${picocolors.yellow(totalTime)} ms.`);
|
|
1184
1187
|
} catch (e) {
|
|
1185
1188
|
if (e instanceof Error) {
|
|
1186
1189
|
logger.error(`Pages render error: ${e.message}`);
|
|
@@ -1244,12 +1247,12 @@ const rsbuildPluginSSG = ({ routeService, config, pluginDriver })=>({
|
|
|
1244
1247
|
});
|
|
1245
1248
|
}
|
|
1246
1249
|
});
|
|
1247
|
-
const { CachedInputFileSystem: detectReactVersion_CachedInputFileSystem, ResolverFactory: detectReactVersion_ResolverFactory } =
|
|
1250
|
+
const { CachedInputFileSystem: detectReactVersion_CachedInputFileSystem, ResolverFactory: detectReactVersion_ResolverFactory } = enhanced_resolve;
|
|
1248
1251
|
async function detectPackageMajorVersion(name) {
|
|
1249
1252
|
const cwd = process.cwd();
|
|
1250
|
-
const pkgPath =
|
|
1253
|
+
const pkgPath = node_path.join(cwd, 'node_modules', name);
|
|
1251
1254
|
if (await pathExists(pkgPath)) {
|
|
1252
|
-
const pkgJson = await readJson(
|
|
1255
|
+
const pkgJson = await readJson(node_path.join(pkgPath, 'package.json'));
|
|
1253
1256
|
const version = Number(pkgJson.version?.split('.')[0]);
|
|
1254
1257
|
return version;
|
|
1255
1258
|
}
|
|
@@ -1261,7 +1264,7 @@ async function detectReactVersion() {
|
|
|
1261
1264
|
async function resolveReactRouterDomAlias() {
|
|
1262
1265
|
const alias = {};
|
|
1263
1266
|
const resolver = detectReactVersion_ResolverFactory.createResolver({
|
|
1264
|
-
fileSystem: new detectReactVersion_CachedInputFileSystem(
|
|
1267
|
+
fileSystem: new detectReactVersion_CachedInputFileSystem(node_fs, 0),
|
|
1265
1268
|
mainFields: [
|
|
1266
1269
|
'browser',
|
|
1267
1270
|
'module',
|
|
@@ -1301,7 +1304,7 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
1301
1304
|
];
|
|
1302
1305
|
const alias = {};
|
|
1303
1306
|
const resolver = detectReactVersion_ResolverFactory.createResolver({
|
|
1304
|
-
fileSystem: new detectReactVersion_CachedInputFileSystem(
|
|
1307
|
+
fileSystem: new detectReactVersion_CachedInputFileSystem(node_fs, 0),
|
|
1305
1308
|
extensions: [
|
|
1306
1309
|
'.js'
|
|
1307
1310
|
],
|
|
@@ -1325,7 +1328,7 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
1325
1328
|
});
|
|
1326
1329
|
} catch (e) {
|
|
1327
1330
|
if (e instanceof Error) {
|
|
1328
|
-
logger.warn(`${lib} not found: \n ${
|
|
1331
|
+
logger.warn(`${lib} not found: \n ${picocolors.gray(e.toString())}`);
|
|
1329
1332
|
hintReactVersion();
|
|
1330
1333
|
}
|
|
1331
1334
|
}
|
|
@@ -1333,15 +1336,15 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
1333
1336
|
return alias;
|
|
1334
1337
|
}
|
|
1335
1338
|
const detectCustomIcon = async (customThemeDir)=>{
|
|
1336
|
-
const assetsDir =
|
|
1339
|
+
const assetsDir = node_path.join(customThemeDir, 'assets');
|
|
1337
1340
|
const alias = {};
|
|
1338
1341
|
if (!existsSync(assetsDir)) return alias;
|
|
1339
1342
|
const files = await glob('*.svg', {
|
|
1340
1343
|
cwd: assetsDir
|
|
1341
1344
|
});
|
|
1342
1345
|
files.forEach((file)=>{
|
|
1343
|
-
const name =
|
|
1344
|
-
alias[`@theme-assets/${name}`] =
|
|
1346
|
+
const name = node_path.basename(file, '.svg');
|
|
1347
|
+
alias[`@theme-assets/${name}`] = node_path.join(assetsDir, file);
|
|
1345
1348
|
});
|
|
1346
1349
|
return alias;
|
|
1347
1350
|
};
|
|
@@ -1379,7 +1382,7 @@ function isPluginIncluded(config, pluginName) {
|
|
|
1379
1382
|
}
|
|
1380
1383
|
async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeService, pluginDriver) {
|
|
1381
1384
|
const cwd = process.cwd();
|
|
1382
|
-
const CUSTOM_THEME_DIR = config?.themeDir ??
|
|
1385
|
+
const CUSTOM_THEME_DIR = config?.themeDir ?? node_path.join(process.cwd(), 'theme');
|
|
1383
1386
|
const outDir = config?.outDir ?? OUTPUT_DIR;
|
|
1384
1387
|
const DEFAULT_THEME = require.resolve('@rspress/theme-default');
|
|
1385
1388
|
const base = config?.base ?? '';
|
|
@@ -1389,7 +1392,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1389
1392
|
if (!icon) return;
|
|
1390
1393
|
icon = icon.toString();
|
|
1391
1394
|
if (icon.startsWith('file://')) icon = fileURLToPath(icon);
|
|
1392
|
-
else if (
|
|
1395
|
+
else if (node_path.isAbsolute(icon)) return node_path.join(userDocRoot, PUBLIC_DIR, icon);
|
|
1393
1396
|
return icon;
|
|
1394
1397
|
};
|
|
1395
1398
|
await hintThemeBreakingChange(CUSTOM_THEME_DIR);
|
|
@@ -1418,7 +1421,8 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1418
1421
|
...globalUIComponentsVMPlugin(context),
|
|
1419
1422
|
...globalStylesVMPlugin(context),
|
|
1420
1423
|
...searchHookVMPlugin(context),
|
|
1421
|
-
...routeListVMPlugin(context)
|
|
1424
|
+
...routeListVMPlugin(context),
|
|
1425
|
+
...await getVirtualModulesFromPlugins(pluginDriver)
|
|
1422
1426
|
}
|
|
1423
1427
|
}),
|
|
1424
1428
|
...enableSSG ? [
|
|
@@ -1433,7 +1437,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1433
1437
|
port: !isProduction() && process.env.PORT ? Number(process.env.PORT) : void 0,
|
|
1434
1438
|
printUrls: ({ urls })=>urls.map((url)=>`${url}/${removeLeadingSlash(base)}`),
|
|
1435
1439
|
publicDir: {
|
|
1436
|
-
name:
|
|
1440
|
+
name: node_path.join(userDocRoot, PUBLIC_DIR)
|
|
1437
1441
|
}
|
|
1438
1442
|
},
|
|
1439
1443
|
dev: {
|
|
@@ -1474,7 +1478,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1474
1478
|
CUSTOM_THEME_DIR,
|
|
1475
1479
|
DEFAULT_THEME
|
|
1476
1480
|
],
|
|
1477
|
-
'@theme-assets':
|
|
1481
|
+
'@theme-assets': node_path.join(DEFAULT_THEME, '../assets'),
|
|
1478
1482
|
'@rspress/core': PACKAGE_ROOT,
|
|
1479
1483
|
'react-lazy-with-preload': require.resolve('react-lazy-with-preload')
|
|
1480
1484
|
}
|
|
@@ -1482,7 +1486,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1482
1486
|
source: {
|
|
1483
1487
|
include: [
|
|
1484
1488
|
PACKAGE_ROOT,
|
|
1485
|
-
|
|
1489
|
+
node_path.join(cwd, 'node_modules', RSPRESS_TEMP_DIR)
|
|
1486
1490
|
],
|
|
1487
1491
|
define: {
|
|
1488
1492
|
'process.env.TEST': JSON.stringify(process.env.TEST),
|
|
@@ -1548,7 +1552,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1548
1552
|
index: enableSSG && isProduction() ? SSR_CLIENT_ENTRY : CSR_CLIENT_ENTRY
|
|
1549
1553
|
},
|
|
1550
1554
|
preEntry: [
|
|
1551
|
-
|
|
1555
|
+
node_path.join(DEFAULT_THEME, '../styles/index.js'),
|
|
1552
1556
|
'virtual-global-styles'
|
|
1553
1557
|
],
|
|
1554
1558
|
define: {
|
|
@@ -1602,14 +1606,14 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1602
1606
|
}
|
|
1603
1607
|
async function initRsbuild(rootDir, config, pluginDriver, enableSSG, extraRsbuildConfig) {
|
|
1604
1608
|
const cwd = process.cwd();
|
|
1605
|
-
const userDocRoot =
|
|
1609
|
+
const userDocRoot = node_path.resolve(rootDir || config?.root || cwd);
|
|
1606
1610
|
const builderPlugins = config?.builderPlugins ?? [];
|
|
1607
|
-
const runtimeTempDir =
|
|
1608
|
-
const runtimeAbsTempDir =
|
|
1609
|
-
await
|
|
1611
|
+
const runtimeTempDir = node_path.join(RSPRESS_TEMP_DIR, 'runtime');
|
|
1612
|
+
const runtimeAbsTempDir = node_path.join(cwd, 'node_modules', runtimeTempDir);
|
|
1613
|
+
await node_fs_promises.mkdir(runtimeAbsTempDir, {
|
|
1610
1614
|
recursive: true
|
|
1611
1615
|
});
|
|
1612
|
-
const routeService = await
|
|
1616
|
+
const routeService = await RouteService.create({
|
|
1613
1617
|
config,
|
|
1614
1618
|
runtimeTempDir: runtimeAbsTempDir,
|
|
1615
1619
|
scanDir: userDocRoot,
|
|
@@ -1625,34 +1629,34 @@ async function initRsbuild(rootDir, config, pluginDriver, enableSSG, extraRsbuil
|
|
|
1625
1629
|
return rsbuild;
|
|
1626
1630
|
}
|
|
1627
1631
|
function checkLanguageParity_normalizePath(filePath) {
|
|
1628
|
-
return filePath.split(
|
|
1632
|
+
return filePath.split(node_path.sep).join('/');
|
|
1629
1633
|
}
|
|
1630
1634
|
async function getAllMarkdownFilesFrom(dirPath) {
|
|
1631
|
-
const files = await
|
|
1635
|
+
const files = await node_fs_promises.readdir(dirPath, {
|
|
1632
1636
|
withFileTypes: true
|
|
1633
1637
|
});
|
|
1634
1638
|
const allFiles = [];
|
|
1635
1639
|
for (const file of files){
|
|
1636
|
-
const fullPath =
|
|
1640
|
+
const fullPath = node_path.join(dirPath, file.name);
|
|
1637
1641
|
if (file.isDirectory()) {
|
|
1638
1642
|
const nestedFiles = await getAllMarkdownFilesFrom(fullPath);
|
|
1639
1643
|
allFiles.push(...nestedFiles);
|
|
1640
1644
|
} else if ([
|
|
1641
1645
|
'.md',
|
|
1642
1646
|
'.mdx'
|
|
1643
|
-
].includes(
|
|
1647
|
+
].includes(node_path.extname(file.name))) allFiles.push(fullPath);
|
|
1644
1648
|
}
|
|
1645
1649
|
return allFiles;
|
|
1646
1650
|
}
|
|
1647
1651
|
async function collectModuleFiles(contentPath, lang, includedDir, excludedDirs, fileLangMap) {
|
|
1648
|
-
const langDirPath =
|
|
1649
|
-
const langModuleDir =
|
|
1652
|
+
const langDirPath = node_path.join(contentPath, lang);
|
|
1653
|
+
const langModuleDir = node_path.join(langDirPath, includedDir);
|
|
1650
1654
|
try {
|
|
1651
|
-
const moduleDirStats = await
|
|
1655
|
+
const moduleDirStats = await node_fs_promises.stat(langModuleDir);
|
|
1652
1656
|
if (!moduleDirStats.isDirectory()) return fileLangMap;
|
|
1653
1657
|
const files = await getAllMarkdownFilesFrom(langModuleDir);
|
|
1654
1658
|
for (const file of files){
|
|
1655
|
-
const relativePath = checkLanguageParity_normalizePath(
|
|
1659
|
+
const relativePath = checkLanguageParity_normalizePath(node_path.relative(langDirPath, file));
|
|
1656
1660
|
if (excludedDirs.some((excludedDir)=>{
|
|
1657
1661
|
const isFilePath = /\.(md|mdx)$/.test(excludedDir);
|
|
1658
1662
|
return relativePath.includes(excludedDir + (isFilePath ? '' : '/'));
|
|
@@ -1670,7 +1674,7 @@ async function collectModuleFiles(contentPath, lang, includedDir, excludedDirs,
|
|
|
1670
1674
|
async function checkLanguageParity(config) {
|
|
1671
1675
|
if (!config?.languageParity || false === config.languageParity.enabled) return;
|
|
1672
1676
|
logger.info('Checking language parity...');
|
|
1673
|
-
const contentPath =
|
|
1677
|
+
const contentPath = node_path.resolve(config.root);
|
|
1674
1678
|
const includedDirs = config.languageParity.include?.length ? config.languageParity.include : [
|
|
1675
1679
|
''
|
|
1676
1680
|
];
|
|
@@ -1686,7 +1690,7 @@ async function checkLanguageParity(config) {
|
|
|
1686
1690
|
for (const [baseName, langs] of Object.entries(curFileLangMap)){
|
|
1687
1691
|
const missingLangs = supportedLanguages.filter((lang)=>!langs.has(lang));
|
|
1688
1692
|
missingLangs.forEach((lang)=>{
|
|
1689
|
-
const missingPath =
|
|
1693
|
+
const missingPath = node_path.join(lang, baseName);
|
|
1690
1694
|
missingLanguagesFile.push(missingPath);
|
|
1691
1695
|
});
|
|
1692
1696
|
}
|
|
@@ -1729,7 +1733,7 @@ async function bundle(docDirectory, config, pluginDriver, enableSSG) {
|
|
|
1729
1733
|
}
|
|
1730
1734
|
}
|
|
1731
1735
|
function emptyDir(path) {
|
|
1732
|
-
return
|
|
1736
|
+
return node_fs_promises.rm(path, {
|
|
1733
1737
|
force: true,
|
|
1734
1738
|
recursive: true
|
|
1735
1739
|
});
|
|
@@ -1799,9 +1803,9 @@ function normalizeLink(nodeUrl, routeService, relativePath, cleanUrls) {
|
|
|
1799
1803
|
if (nodeUrl.startsWith('#')) return `#${nodeUrl.slice(1)}`;
|
|
1800
1804
|
let { url, hash } = parseUrl(nodeUrl);
|
|
1801
1805
|
if (isExternalUrl(url)) return url + (hash ? `#${hash}` : '');
|
|
1802
|
-
const extname =
|
|
1806
|
+
const extname = node_path.extname(url);
|
|
1803
1807
|
if ((routeService?.extensions ?? DEFAULT_PAGE_EXTENSIONS).includes(extname)) url = url.replace(new RegExp(`\\${extname}$`), '');
|
|
1804
|
-
if (url.startsWith('.')) url =
|
|
1808
|
+
if (url.startsWith('.')) url = node_path.posix.join(slash(node_path.dirname(relativePath)), url);
|
|
1805
1809
|
else if (routeService) {
|
|
1806
1810
|
const [pathVersion, pathLang] = routeService.getRoutePathParts(slash(relativePath));
|
|
1807
1811
|
const [urlVersion, urlLang, urlPath] = routeService.getRoutePathParts(url);
|
|
@@ -1825,12 +1829,12 @@ const remarkPluginNormalizeLink = ({ root, cleanUrls, routeService })=>(tree, fi
|
|
|
1825
1829
|
const images = [];
|
|
1826
1830
|
visit(tree, 'link', (node)=>{
|
|
1827
1831
|
const { url: nodeUrl } = node;
|
|
1828
|
-
const relativePath =
|
|
1832
|
+
const relativePath = node_path.relative(root, file.path);
|
|
1829
1833
|
node.url = normalizeLink(nodeUrl, routeService, relativePath, cleanUrls);
|
|
1830
1834
|
});
|
|
1831
1835
|
visit(tree, 'definition', (node)=>{
|
|
1832
1836
|
const { url: nodeUrl } = node;
|
|
1833
|
-
const relativePath =
|
|
1837
|
+
const relativePath = node_path.relative(root, file.path);
|
|
1834
1838
|
node.url = normalizeLink(nodeUrl, routeService, relativePath, cleanUrls);
|
|
1835
1839
|
});
|
|
1836
1840
|
const getMdxSrcAttribute = (tempVar)=>({
|