@rspress/core 1.20.1 → 1.21.1
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.js +313 -289
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +7 -7
- package/dist/loader.js +95 -101
- package/dist/loader.js.map +1 -1
- package/package.json +14 -14
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.21.1",
|
|
70
70
|
description: "The Rspress Documentation Framework",
|
|
71
71
|
bugs: "https://github.com/web-infra-dev/rspress/issues",
|
|
72
72
|
repository: {
|
|
@@ -109,7 +109,7 @@ var require_package = __commonJS({
|
|
|
109
109
|
"@mdx-js/loader": "2.3.0",
|
|
110
110
|
"@mdx-js/mdx": "2.3.0",
|
|
111
111
|
"@mdx-js/react": "2.3.0",
|
|
112
|
-
"@modern-js/utils": "2.49.
|
|
112
|
+
"@modern-js/utils": "2.49.4",
|
|
113
113
|
"@rsbuild/core": "0.6.13",
|
|
114
114
|
"@rsbuild/plugin-react": "0.6.13",
|
|
115
115
|
"@rspress/theme-default": "workspace:*",
|
|
@@ -117,7 +117,7 @@ var require_package = __commonJS({
|
|
|
117
117
|
"@rspress/plugin-medium-zoom": "workspace:*",
|
|
118
118
|
"@rspress/plugin-last-updated": "workspace:*",
|
|
119
119
|
"@rspress/plugin-auto-nav-sidebar": "workspace:*",
|
|
120
|
-
"@rspress/mdx-rs": "0.5.
|
|
120
|
+
"@rspress/mdx-rs": "0.5.7",
|
|
121
121
|
"@rspress/plugin-container-syntax": "workspace:*",
|
|
122
122
|
"@rspress/shared": "workspace:*",
|
|
123
123
|
"body-scroll-lock": "4.0.0-beta.0",
|
|
@@ -156,12 +156,12 @@ var require_package = __commonJS({
|
|
|
156
156
|
},
|
|
157
157
|
devDependencies: {
|
|
158
158
|
tailwindcss: "^3.4.3",
|
|
159
|
-
"@modern-js/plugin-tailwindcss": "2.49.
|
|
160
|
-
"@modern-js/tsconfig": "2.49.
|
|
159
|
+
"@modern-js/plugin-tailwindcss": "2.49.4",
|
|
160
|
+
"@modern-js/tsconfig": "2.49.4",
|
|
161
161
|
"@types/body-scroll-lock": "^3.1.0",
|
|
162
162
|
"@types/hast": "3.0.3",
|
|
163
163
|
"@types/html-to-text": "^8.1.1",
|
|
164
|
-
"@types/jest": "~29.
|
|
164
|
+
"@types/jest": "~29.5.12",
|
|
165
165
|
"@types/lodash-es": "^4.17.8",
|
|
166
166
|
"@types/mdast": "^3.0.10",
|
|
167
167
|
"@types/node": "^18.11.17",
|
|
@@ -172,7 +172,7 @@ var require_package = __commonJS({
|
|
|
172
172
|
rimraf: "^3.0.2",
|
|
173
173
|
typescript: "^5",
|
|
174
174
|
vitest: "0.34.1",
|
|
175
|
-
webpack: "^5.
|
|
175
|
+
webpack: "^5.91.0"
|
|
176
176
|
},
|
|
177
177
|
sideEffects: [
|
|
178
178
|
"*.css",
|
|
@@ -247,6 +247,7 @@ var HEAD_MARKER = "<!--<?- HEAD ?>-->";
|
|
|
247
247
|
var META_GENERATOR = "<!--<?- GENERATOR ?>-->";
|
|
248
248
|
var HTML_START_TAG = "<html";
|
|
249
249
|
var BODY_START_TAG = "<body";
|
|
250
|
+
var DEFAULT_TITLE = "Rspress";
|
|
250
251
|
var PUBLIC_DIR = "public";
|
|
251
252
|
var TEMP_DIR = path.join(
|
|
252
253
|
process.cwd(),
|
|
@@ -259,7 +260,12 @@ import { RspackVirtualModulePlugin } from "rspack-plugin-virtual-module";
|
|
|
259
260
|
|
|
260
261
|
// src/node/runtimeModule/routeData.ts
|
|
261
262
|
async function routeVMPlugin(context) {
|
|
262
|
-
const { routeService: routeService2 } = context;
|
|
263
|
+
const { routeService: routeService2, isSSR } = context;
|
|
264
|
+
if (!isSSR) {
|
|
265
|
+
return {
|
|
266
|
+
["virtual-routes" /* RouteForClient */]: routeService2.generateRoutesCode(false)
|
|
267
|
+
};
|
|
268
|
+
}
|
|
263
269
|
return {
|
|
264
270
|
["virtual-routes" /* RouteForClient */]: routeService2.generateRoutesCode(false),
|
|
265
271
|
["virtual-routes-ssr" /* RouteForSSR */]: routeService2.generateRoutesCode(true)
|
|
@@ -268,224 +274,19 @@ async function routeVMPlugin(context) {
|
|
|
268
274
|
|
|
269
275
|
// src/node/runtimeModule/siteData/index.ts
|
|
270
276
|
import path6 from "path";
|
|
277
|
+
import fs4 from "@rspress/shared/fs-extra";
|
|
271
278
|
import { groupBy } from "lodash-es";
|
|
272
279
|
import { SEARCH_INDEX_NAME } from "@rspress/shared";
|
|
273
|
-
import fs4 from "@rspress/shared/fs-extra";
|
|
274
|
-
|
|
275
|
-
// src/node/runtimeModule/siteData/normalizeThemeConfig.ts
|
|
276
|
-
import {
|
|
277
|
-
withBase,
|
|
278
|
-
addLeadingSlash,
|
|
279
|
-
isExternalUrl,
|
|
280
|
-
withoutBase,
|
|
281
|
-
slash
|
|
282
|
-
} from "@rspress/shared";
|
|
283
|
-
|
|
284
|
-
// src/node/utils/applyReplaceRules.ts
|
|
285
|
-
function applyReplaceRules(code = "", replaceRules = []) {
|
|
286
|
-
let result = code;
|
|
287
|
-
for (const rule of replaceRules) {
|
|
288
|
-
result = result.replace(rule.search, rule.replace);
|
|
289
|
-
}
|
|
290
|
-
return result;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// src/node/runtimeModule/i18n.ts
|
|
294
|
-
import { join } from "path";
|
|
295
|
-
var DEFAULT_I18N_SOURCE = join(process.cwd(), "i18n.json");
|
|
296
|
-
function getI18nData(docConfig) {
|
|
297
|
-
const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig;
|
|
298
|
-
try {
|
|
299
|
-
const i18nSource = __require(i18nSourcePath);
|
|
300
|
-
return i18nSource;
|
|
301
|
-
} catch (e) {
|
|
302
|
-
return {};
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
function i18nVMPlugin(context) {
|
|
306
|
-
const { config } = context;
|
|
307
|
-
const i18nData = getI18nData(config);
|
|
308
|
-
return {
|
|
309
|
-
["virtual-i18n-text" /* I18nText */]: `export default ${JSON.stringify(i18nData)}`
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// src/node/runtimeModule/siteData/normalizeThemeConfig.ts
|
|
314
|
-
function normalizeThemeConfig(docConfig, pages = []) {
|
|
315
|
-
const {
|
|
316
|
-
locales: siteLocales,
|
|
317
|
-
base = "",
|
|
318
|
-
lang,
|
|
319
|
-
replaceRules = [],
|
|
320
|
-
multiVersion
|
|
321
|
-
} = docConfig;
|
|
322
|
-
const { versions = [] } = multiVersion || {};
|
|
323
|
-
const hasMultiVersion = versions.length > 0;
|
|
324
|
-
docConfig.themeConfig = docConfig.themeConfig || {};
|
|
325
|
-
const { themeConfig } = docConfig;
|
|
326
|
-
const locales = siteLocales ?? (themeConfig?.locales || []);
|
|
327
|
-
const i18nTextData = getI18nData(docConfig);
|
|
328
|
-
const normalizeLinkPrefix = (link = "", currentLang = "") => {
|
|
329
|
-
const normalizedLink = slash(link);
|
|
330
|
-
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
|
|
331
|
-
hasMultiVersion) {
|
|
332
|
-
return normalizedLink;
|
|
333
|
-
}
|
|
334
|
-
return lang === currentLang ? normalizedLink : `/${currentLang}${addLeadingSlash(normalizedLink)}`;
|
|
335
|
-
};
|
|
336
|
-
const getI18nText = (key = "", currentLang = "") => {
|
|
337
|
-
const text = i18nTextData[key]?.[currentLang];
|
|
338
|
-
return text || key;
|
|
339
|
-
};
|
|
340
|
-
const normalizeSidebar = (sidebar, currentLang = "") => {
|
|
341
|
-
const normalizedSidebar = {};
|
|
342
|
-
if (!sidebar) {
|
|
343
|
-
return {};
|
|
344
|
-
}
|
|
345
|
-
const normalizeSidebarItem = (item) => {
|
|
346
|
-
if (typeof item === "object" && "dividerType" in item) {
|
|
347
|
-
return item;
|
|
348
|
-
}
|
|
349
|
-
if (typeof item === "object" && "sectionHeaderText" in item) {
|
|
350
|
-
item.sectionHeaderText = applyReplaceRules(
|
|
351
|
-
getI18nText(item.sectionHeaderText, currentLang),
|
|
352
|
-
replaceRules
|
|
353
|
-
);
|
|
354
|
-
return item;
|
|
355
|
-
}
|
|
356
|
-
if (typeof item === "object" && "items" in item) {
|
|
357
|
-
return {
|
|
358
|
-
...item,
|
|
359
|
-
text: applyReplaceRules(
|
|
360
|
-
getI18nText(item.text, currentLang),
|
|
361
|
-
replaceRules
|
|
362
|
-
),
|
|
363
|
-
link: normalizeLinkPrefix(item.link),
|
|
364
|
-
collapsed: item.collapsed ?? false,
|
|
365
|
-
collapsible: item.collapsible ?? true,
|
|
366
|
-
tag: item.tag,
|
|
367
|
-
items: item.items.map((subItem) => {
|
|
368
|
-
return normalizeSidebarItem(subItem);
|
|
369
|
-
})
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
if (typeof item === "string") {
|
|
373
|
-
const normalizedItem = normalizeLinkPrefix(item, currentLang);
|
|
374
|
-
const page = pages.find(
|
|
375
|
-
(page2) => page2.routePath === withBase(normalizedItem, base)
|
|
376
|
-
);
|
|
377
|
-
return {
|
|
378
|
-
text: applyReplaceRules(page?.title || "", replaceRules),
|
|
379
|
-
link: normalizedItem,
|
|
380
|
-
_fileKey: page._relativePath.replace(/\.mdx?$/, "")
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
return {
|
|
384
|
-
...item,
|
|
385
|
-
text: applyReplaceRules(
|
|
386
|
-
getI18nText(item.text, currentLang),
|
|
387
|
-
replaceRules
|
|
388
|
-
),
|
|
389
|
-
link: normalizeLinkPrefix(item.link),
|
|
390
|
-
tag: item.tag
|
|
391
|
-
};
|
|
392
|
-
};
|
|
393
|
-
const normalizeSidebar2 = (sidebar2) => {
|
|
394
|
-
Object.keys(sidebar2).forEach((key) => {
|
|
395
|
-
const value = sidebar2[key];
|
|
396
|
-
normalizedSidebar[key] = value.map(normalizeSidebarItem);
|
|
397
|
-
});
|
|
398
|
-
};
|
|
399
|
-
normalizeSidebar2(sidebar);
|
|
400
|
-
return normalizedSidebar;
|
|
401
|
-
};
|
|
402
|
-
const normalizeNav = (nav, currentLang) => {
|
|
403
|
-
if (!nav) {
|
|
404
|
-
return [];
|
|
405
|
-
}
|
|
406
|
-
const transformNavItem = (navItem) => {
|
|
407
|
-
const text = applyReplaceRules(
|
|
408
|
-
getI18nText(navItem.text, currentLang),
|
|
409
|
-
replaceRules
|
|
410
|
-
);
|
|
411
|
-
if ("link" in navItem) {
|
|
412
|
-
return {
|
|
413
|
-
...navItem,
|
|
414
|
-
text,
|
|
415
|
-
link: normalizeLinkPrefix(navItem.link, currentLang)
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
if ("items" in navItem) {
|
|
419
|
-
return {
|
|
420
|
-
...navItem,
|
|
421
|
-
text,
|
|
422
|
-
items: navItem.items.map((item) => {
|
|
423
|
-
return {
|
|
424
|
-
...item,
|
|
425
|
-
text: applyReplaceRules(
|
|
426
|
-
getI18nText(item.text, currentLang),
|
|
427
|
-
replaceRules
|
|
428
|
-
),
|
|
429
|
-
link: normalizeLinkPrefix(item.link, currentLang)
|
|
430
|
-
};
|
|
431
|
-
})
|
|
432
|
-
};
|
|
433
|
-
}
|
|
434
|
-
return navItem;
|
|
435
|
-
};
|
|
436
|
-
if (Array.isArray(nav)) {
|
|
437
|
-
return nav.map(transformNavItem);
|
|
438
|
-
}
|
|
439
|
-
return Object.entries(nav).reduce((acc, [key, value]) => {
|
|
440
|
-
acc[key] = value.map(transformNavItem);
|
|
441
|
-
return acc;
|
|
442
|
-
}, {});
|
|
443
|
-
};
|
|
444
|
-
if (locales.length) {
|
|
445
|
-
themeConfig.locales = locales.map(({ lang: currentLang, label }) => {
|
|
446
|
-
const localeInThemeConfig = themeConfig.locales?.find(
|
|
447
|
-
(locale) => locale.lang === currentLang
|
|
448
|
-
);
|
|
449
|
-
return {
|
|
450
|
-
lang: currentLang,
|
|
451
|
-
label,
|
|
452
|
-
...localeInThemeConfig || {},
|
|
453
|
-
sidebar: normalizeSidebar(
|
|
454
|
-
localeInThemeConfig?.sidebar ?? themeConfig.sidebar,
|
|
455
|
-
currentLang
|
|
456
|
-
),
|
|
457
|
-
nav: normalizeNav(
|
|
458
|
-
localeInThemeConfig?.nav ?? themeConfig.nav,
|
|
459
|
-
currentLang
|
|
460
|
-
)
|
|
461
|
-
};
|
|
462
|
-
});
|
|
463
|
-
} else {
|
|
464
|
-
themeConfig.sidebar = normalizeSidebar(themeConfig?.sidebar);
|
|
465
|
-
themeConfig.nav = normalizeNav(themeConfig?.nav);
|
|
466
|
-
}
|
|
467
|
-
return themeConfig;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
// src/node/runtimeModule/siteData/extractPageData.ts
|
|
471
|
-
import path5 from "path";
|
|
472
|
-
import {
|
|
473
|
-
MDX_REGEXP as MDX_REGEXP2
|
|
474
|
-
} from "@rspress/shared";
|
|
475
|
-
import { htmlToText } from "html-to-text";
|
|
476
|
-
import fs3 from "@rspress/shared/fs-extra";
|
|
477
|
-
import { compile } from "@rspress/mdx-rs";
|
|
478
|
-
import { loadFrontMatter } from "@rspress/shared/node-utils";
|
|
479
280
|
|
|
480
281
|
// src/node/utils/normalizePath.ts
|
|
481
282
|
import os from "os";
|
|
482
283
|
import path2 from "path";
|
|
483
284
|
var isWindows = os.platform() === "win32";
|
|
484
|
-
function
|
|
285
|
+
function slash(p) {
|
|
485
286
|
return p.replace(/\\/g, "/");
|
|
486
287
|
}
|
|
487
288
|
function normalizePath(id) {
|
|
488
|
-
return path2.posix.normalize(isWindows ?
|
|
289
|
+
return path2.posix.normalize(isWindows ? slash(id) : id);
|
|
489
290
|
}
|
|
490
291
|
|
|
491
292
|
// src/node/utils/getPageKey.ts
|
|
@@ -504,15 +305,17 @@ function createHash(str) {
|
|
|
504
305
|
// src/node/utils/detectReactVersion.ts
|
|
505
306
|
import path3 from "path";
|
|
506
307
|
import fs from "@rspress/shared/fs-extra";
|
|
507
|
-
import { logger } from "@rspress/shared/logger";
|
|
508
308
|
import enhancedResolve from "enhanced-resolve";
|
|
309
|
+
import { logger } from "@rspress/shared/logger";
|
|
509
310
|
var { CachedInputFileSystem, ResolverFactory } = enhancedResolve;
|
|
510
311
|
var DEFAULT_REACT_VERSION = 18;
|
|
511
312
|
async function detectReactVersion() {
|
|
512
313
|
const cwd = process.cwd();
|
|
513
314
|
const reactPath = path3.join(cwd, "node_modules", "react");
|
|
514
315
|
if (await fs.pathExists(reactPath)) {
|
|
515
|
-
const reactPkg = await fs.readJson(
|
|
316
|
+
const reactPkg = await fs.readJson(
|
|
317
|
+
path3.join(reactPath, "package.json")
|
|
318
|
+
);
|
|
516
319
|
const version = Number(reactPkg.version.split(".")[0]);
|
|
517
320
|
return version;
|
|
518
321
|
}
|
|
@@ -532,7 +335,7 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
532
335
|
}
|
|
533
336
|
const alias = {};
|
|
534
337
|
const resolver2 = ResolverFactory.createResolver({
|
|
535
|
-
fileSystem: new CachedInputFileSystem(fs),
|
|
338
|
+
fileSystem: new CachedInputFileSystem(fs, 0),
|
|
536
339
|
extensions: [".js"],
|
|
537
340
|
alias,
|
|
538
341
|
conditionNames: isSSR ? ["..."] : ["browser", "..."]
|
|
@@ -565,18 +368,18 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
565
368
|
|
|
566
369
|
// src/node/utils/flattenMdxContent.ts
|
|
567
370
|
import path4 from "path";
|
|
568
|
-
import enhancedResolve2 from "enhanced-resolve";
|
|
569
371
|
import fs2 from "@rspress/shared/fs-extra";
|
|
570
|
-
import
|
|
372
|
+
import enhancedResolve2 from "enhanced-resolve";
|
|
571
373
|
import { MDX_REGEXP } from "@rspress/shared";
|
|
572
|
-
|
|
573
|
-
var processor = createProcessor();
|
|
374
|
+
import { createProcessor } from "@mdx-js/mdx";
|
|
574
375
|
var resolver;
|
|
575
376
|
var startFlatten = false;
|
|
377
|
+
var processor = createProcessor();
|
|
378
|
+
var { CachedInputFileSystem: CachedInputFileSystem2, ResolverFactory: ResolverFactory2 } = enhancedResolve2;
|
|
576
379
|
async function resolveDepPath(importPath, importer, alias) {
|
|
577
380
|
if (!resolver) {
|
|
578
381
|
resolver = ResolverFactory2.createResolver({
|
|
579
|
-
fileSystem: new CachedInputFileSystem2(fs2),
|
|
382
|
+
fileSystem: new CachedInputFileSystem2(fs2, 0),
|
|
580
383
|
extensions: [".mdx", ".md"],
|
|
581
384
|
alias
|
|
582
385
|
});
|
|
@@ -612,24 +415,22 @@ async function flattenMdxContent(content, basePath, alias) {
|
|
|
612
415
|
if (!regex.test(content)) {
|
|
613
416
|
return { flattenContent: content, deps };
|
|
614
417
|
}
|
|
615
|
-
let result = content;
|
|
616
418
|
if (!startFlatten) {
|
|
617
419
|
resolver = ResolverFactory2.createResolver({
|
|
618
|
-
fileSystem: new CachedInputFileSystem2(fs2),
|
|
420
|
+
fileSystem: new CachedInputFileSystem2(fs2, 0),
|
|
619
421
|
extensions: [".mdx", ".md", ".js"],
|
|
620
422
|
alias
|
|
621
423
|
});
|
|
622
424
|
startFlatten = true;
|
|
623
425
|
}
|
|
624
426
|
let ast;
|
|
427
|
+
let result = content;
|
|
625
428
|
try {
|
|
626
429
|
ast = processor.parse(content);
|
|
627
430
|
} catch (e) {
|
|
628
431
|
return { flattenContent: content, deps };
|
|
629
432
|
}
|
|
630
|
-
const importNodes = ast.children.filter((node) => node.type === "mdxjsEsm").
|
|
631
|
-
return node.data?.estree?.body || [];
|
|
632
|
-
}).flat().filter((node) => node.type === "ImportDeclaration");
|
|
433
|
+
const importNodes = ast.children.filter((node) => node.type === "mdxjsEsm").flatMap((node) => node.data?.estree?.body || []).filter((node) => node.type === "ImportDeclaration");
|
|
633
434
|
for (const importNode of importNodes) {
|
|
634
435
|
const id = importNode.specifiers[0].local.name;
|
|
635
436
|
const importPath = importNode.source.value;
|
|
@@ -656,8 +457,25 @@ async function flattenMdxContent(content, basePath, alias) {
|
|
|
656
457
|
return { flattenContent: result, deps };
|
|
657
458
|
}
|
|
658
459
|
|
|
460
|
+
// src/node/utils/applyReplaceRules.ts
|
|
461
|
+
function applyReplaceRules(code = "", replaceRules = []) {
|
|
462
|
+
let result = code;
|
|
463
|
+
for (const rule of replaceRules) {
|
|
464
|
+
result = result.replace(rule.search, rule.replace);
|
|
465
|
+
}
|
|
466
|
+
return result;
|
|
467
|
+
}
|
|
468
|
+
|
|
659
469
|
// src/node/runtimeModule/siteData/extractPageData.ts
|
|
660
|
-
|
|
470
|
+
import path5 from "path";
|
|
471
|
+
import fs3 from "@rspress/shared/fs-extra";
|
|
472
|
+
import { htmlToText } from "html-to-text";
|
|
473
|
+
import { compile } from "@rspress/mdx-rs";
|
|
474
|
+
import { loadFrontMatter } from "@rspress/shared/node-utils";
|
|
475
|
+
import {
|
|
476
|
+
MDX_REGEXP as MDX_REGEXP2
|
|
477
|
+
} from "@rspress/shared";
|
|
478
|
+
async function extractPageData(replaceRules, alias, domain, root, routeService2, highlighterLangs) {
|
|
661
479
|
return Promise.all(
|
|
662
480
|
routeService2.getRoutes().map(async (route, index) => {
|
|
663
481
|
const defaultIndexInfo = {
|
|
@@ -696,7 +514,8 @@ async function extractPageData(replaceRules, alias, domain, root, routeService2)
|
|
|
696
514
|
const {
|
|
697
515
|
html,
|
|
698
516
|
title,
|
|
699
|
-
toc: rawToc
|
|
517
|
+
toc: rawToc,
|
|
518
|
+
languages
|
|
700
519
|
} = await compile({
|
|
701
520
|
value: content,
|
|
702
521
|
filepath: route.absolutePath,
|
|
@@ -739,6 +558,7 @@ async function extractPageData(replaceRules, alias, domain, root, routeService2)
|
|
|
739
558
|
if (content.startsWith(title)) {
|
|
740
559
|
content = content.slice(title.length);
|
|
741
560
|
}
|
|
561
|
+
languages.forEach((lang) => highlighterLangs.add(lang));
|
|
742
562
|
const toc = rawToc.map((item) => {
|
|
743
563
|
const match = item.id.match(/-(\d+)$/);
|
|
744
564
|
let position = -1;
|
|
@@ -773,6 +593,238 @@ ${item.text}#
|
|
|
773
593
|
);
|
|
774
594
|
}
|
|
775
595
|
|
|
596
|
+
// src/node/runtimeModule/siteData/normalizeThemeConfig.ts
|
|
597
|
+
import {
|
|
598
|
+
withBase,
|
|
599
|
+
addLeadingSlash,
|
|
600
|
+
isExternalUrl,
|
|
601
|
+
withoutBase,
|
|
602
|
+
slash as slash2
|
|
603
|
+
} from "@rspress/shared";
|
|
604
|
+
|
|
605
|
+
// src/node/runtimeModule/i18n.ts
|
|
606
|
+
import { join } from "path";
|
|
607
|
+
var DEFAULT_I18N_SOURCE = join(process.cwd(), "i18n.json");
|
|
608
|
+
function getI18nData(docConfig) {
|
|
609
|
+
const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig;
|
|
610
|
+
try {
|
|
611
|
+
const i18nSource = __require(i18nSourcePath);
|
|
612
|
+
return i18nSource;
|
|
613
|
+
} catch (e) {
|
|
614
|
+
return {};
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
function i18nVMPlugin(context) {
|
|
618
|
+
const { config } = context;
|
|
619
|
+
const i18nData = getI18nData(config);
|
|
620
|
+
return {
|
|
621
|
+
["virtual-i18n-text" /* I18nText */]: `export default ${JSON.stringify(i18nData)}`
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// src/node/runtimeModule/siteData/normalizeThemeConfig.ts
|
|
626
|
+
function normalizeThemeConfig(docConfig, pages = []) {
|
|
627
|
+
const {
|
|
628
|
+
locales: siteLocales,
|
|
629
|
+
base = "",
|
|
630
|
+
lang,
|
|
631
|
+
replaceRules = [],
|
|
632
|
+
multiVersion
|
|
633
|
+
} = docConfig;
|
|
634
|
+
const { versions = [] } = multiVersion || {};
|
|
635
|
+
const hasMultiVersion = versions.length > 0;
|
|
636
|
+
docConfig.themeConfig = docConfig.themeConfig || {};
|
|
637
|
+
const { themeConfig } = docConfig;
|
|
638
|
+
const locales = siteLocales ?? (themeConfig?.locales || []);
|
|
639
|
+
const i18nTextData = getI18nData(docConfig);
|
|
640
|
+
const normalizeLinkPrefix = (link = "", currentLang = "") => {
|
|
641
|
+
const normalizedLink = slash2(link);
|
|
642
|
+
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
|
|
643
|
+
hasMultiVersion) {
|
|
644
|
+
return normalizedLink;
|
|
645
|
+
}
|
|
646
|
+
return lang === currentLang ? normalizedLink : `/${currentLang}${addLeadingSlash(normalizedLink)}`;
|
|
647
|
+
};
|
|
648
|
+
const getI18nText = (key = "", currentLang = "") => {
|
|
649
|
+
const text = i18nTextData[key]?.[currentLang];
|
|
650
|
+
return text || key;
|
|
651
|
+
};
|
|
652
|
+
const normalizeSidebar = (sidebar, currentLang = "") => {
|
|
653
|
+
const normalizedSidebar = {};
|
|
654
|
+
if (!sidebar) {
|
|
655
|
+
return {};
|
|
656
|
+
}
|
|
657
|
+
const normalizeSidebarItem = (item) => {
|
|
658
|
+
if (typeof item === "object" && "dividerType" in item) {
|
|
659
|
+
return item;
|
|
660
|
+
}
|
|
661
|
+
if (typeof item === "object" && "sectionHeaderText" in item) {
|
|
662
|
+
item.sectionHeaderText = applyReplaceRules(
|
|
663
|
+
getI18nText(item.sectionHeaderText, currentLang),
|
|
664
|
+
replaceRules
|
|
665
|
+
);
|
|
666
|
+
return item;
|
|
667
|
+
}
|
|
668
|
+
if (typeof item === "object" && "items" in item) {
|
|
669
|
+
return {
|
|
670
|
+
...item,
|
|
671
|
+
text: applyReplaceRules(
|
|
672
|
+
getI18nText(item.text, currentLang),
|
|
673
|
+
replaceRules
|
|
674
|
+
),
|
|
675
|
+
link: normalizeLinkPrefix(item.link),
|
|
676
|
+
collapsed: item.collapsed ?? false,
|
|
677
|
+
collapsible: item.collapsible ?? true,
|
|
678
|
+
tag: item.tag,
|
|
679
|
+
items: item.items.map((subItem) => {
|
|
680
|
+
return normalizeSidebarItem(subItem);
|
|
681
|
+
})
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
if (typeof item === "string") {
|
|
685
|
+
const normalizedItem = normalizeLinkPrefix(item, currentLang);
|
|
686
|
+
const page = pages.find(
|
|
687
|
+
(page2) => page2.routePath === withBase(normalizedItem, base)
|
|
688
|
+
);
|
|
689
|
+
return {
|
|
690
|
+
text: applyReplaceRules(page?.title || "", replaceRules),
|
|
691
|
+
link: normalizedItem,
|
|
692
|
+
_fileKey: page._relativePath.replace(/\.mdx?$/, "")
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
return {
|
|
696
|
+
...item,
|
|
697
|
+
text: applyReplaceRules(
|
|
698
|
+
getI18nText(item.text, currentLang),
|
|
699
|
+
replaceRules
|
|
700
|
+
),
|
|
701
|
+
link: normalizeLinkPrefix(item.link),
|
|
702
|
+
tag: item.tag
|
|
703
|
+
};
|
|
704
|
+
};
|
|
705
|
+
const normalizeSidebar2 = (sidebar2) => {
|
|
706
|
+
Object.keys(sidebar2).forEach((key) => {
|
|
707
|
+
const value = sidebar2[key];
|
|
708
|
+
normalizedSidebar[key] = value.map(normalizeSidebarItem);
|
|
709
|
+
});
|
|
710
|
+
};
|
|
711
|
+
normalizeSidebar2(sidebar);
|
|
712
|
+
return normalizedSidebar;
|
|
713
|
+
};
|
|
714
|
+
const normalizeNav = (nav, currentLang) => {
|
|
715
|
+
if (!nav) {
|
|
716
|
+
return [];
|
|
717
|
+
}
|
|
718
|
+
const transformNavItem = (navItem) => {
|
|
719
|
+
const text = applyReplaceRules(
|
|
720
|
+
getI18nText(navItem.text, currentLang),
|
|
721
|
+
replaceRules
|
|
722
|
+
);
|
|
723
|
+
if ("link" in navItem) {
|
|
724
|
+
return {
|
|
725
|
+
...navItem,
|
|
726
|
+
text,
|
|
727
|
+
link: normalizeLinkPrefix(navItem.link, currentLang)
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
if ("items" in navItem) {
|
|
731
|
+
return {
|
|
732
|
+
...navItem,
|
|
733
|
+
text,
|
|
734
|
+
items: navItem.items.map((item) => {
|
|
735
|
+
return {
|
|
736
|
+
...item,
|
|
737
|
+
text: applyReplaceRules(
|
|
738
|
+
getI18nText(item.text, currentLang),
|
|
739
|
+
replaceRules
|
|
740
|
+
),
|
|
741
|
+
link: normalizeLinkPrefix(item.link, currentLang)
|
|
742
|
+
};
|
|
743
|
+
})
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
return navItem;
|
|
747
|
+
};
|
|
748
|
+
if (Array.isArray(nav)) {
|
|
749
|
+
return nav.map(transformNavItem);
|
|
750
|
+
}
|
|
751
|
+
return Object.entries(nav).reduce((acc, [key, value]) => {
|
|
752
|
+
acc[key] = value.map(transformNavItem);
|
|
753
|
+
return acc;
|
|
754
|
+
}, {});
|
|
755
|
+
};
|
|
756
|
+
if (locales.length) {
|
|
757
|
+
themeConfig.locales = locales.map(({ lang: currentLang, label }) => {
|
|
758
|
+
const localeInThemeConfig = themeConfig.locales?.find(
|
|
759
|
+
(locale) => locale.lang === currentLang
|
|
760
|
+
);
|
|
761
|
+
return {
|
|
762
|
+
lang: currentLang,
|
|
763
|
+
label,
|
|
764
|
+
...localeInThemeConfig || {},
|
|
765
|
+
sidebar: normalizeSidebar(
|
|
766
|
+
localeInThemeConfig?.sidebar ?? themeConfig.sidebar,
|
|
767
|
+
currentLang
|
|
768
|
+
),
|
|
769
|
+
nav: normalizeNav(
|
|
770
|
+
localeInThemeConfig?.nav ?? themeConfig.nav,
|
|
771
|
+
currentLang
|
|
772
|
+
)
|
|
773
|
+
};
|
|
774
|
+
});
|
|
775
|
+
} else {
|
|
776
|
+
themeConfig.sidebar = normalizeSidebar(themeConfig?.sidebar);
|
|
777
|
+
themeConfig.nav = normalizeNav(themeConfig?.nav);
|
|
778
|
+
}
|
|
779
|
+
return themeConfig;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// src/node/runtimeModule/siteData/highlightLanguages.ts
|
|
783
|
+
import chalk from "@rspress/shared/chalk";
|
|
784
|
+
import { DEFAULT_HIGHLIGHT_LANGUAGES } from "@rspress/shared";
|
|
785
|
+
var supportedLanguages;
|
|
786
|
+
function handleHighlightLanguages(highlightLanguages, defaultLanguages) {
|
|
787
|
+
const aliases = {};
|
|
788
|
+
if (highlightLanguages.size) {
|
|
789
|
+
if (!supportedLanguages) {
|
|
790
|
+
const langs = __require("react-syntax-highlighter/dist/cjs/languages/prism/supported-languages").default;
|
|
791
|
+
supportedLanguages = new Set(langs);
|
|
792
|
+
}
|
|
793
|
+
let useDeprecatedTypeWarning = true;
|
|
794
|
+
const names = {};
|
|
795
|
+
[...DEFAULT_HIGHLIGHT_LANGUAGES, ...defaultLanguages].forEach((lang) => {
|
|
796
|
+
if (Array.isArray(lang)) {
|
|
797
|
+
const [alias, name] = lang;
|
|
798
|
+
names[alias] = name;
|
|
799
|
+
} else if (useDeprecatedTypeWarning) {
|
|
800
|
+
console.log(
|
|
801
|
+
`${chalk.yellowBright(
|
|
802
|
+
"warning"
|
|
803
|
+
)} Automatic import is supported. \`highlightLanguages\` is now used only as alias, and string types will be ignored.
|
|
804
|
+
`
|
|
805
|
+
);
|
|
806
|
+
useDeprecatedTypeWarning = false;
|
|
807
|
+
}
|
|
808
|
+
});
|
|
809
|
+
[...highlightLanguages.values()].forEach((lang) => {
|
|
810
|
+
const name = names[lang];
|
|
811
|
+
if (name && supportedLanguages.has(name)) {
|
|
812
|
+
const temp = aliases[name] || (aliases[name] = []);
|
|
813
|
+
if (!temp.includes(lang)) {
|
|
814
|
+
temp.push(lang);
|
|
815
|
+
}
|
|
816
|
+
highlightLanguages.add(name);
|
|
817
|
+
highlightLanguages.delete(lang);
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
if (!supportedLanguages.has(lang)) {
|
|
821
|
+
highlightLanguages.delete(lang);
|
|
822
|
+
}
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
return aliases;
|
|
826
|
+
}
|
|
827
|
+
|
|
776
828
|
// src/node/runtimeModule/siteData/index.ts
|
|
777
829
|
function deletePriviteKey(obj) {
|
|
778
830
|
if (typeof obj !== "object" || obj === null) {
|
|
@@ -793,6 +845,7 @@ async function siteDataVMPlugin(context) {
|
|
|
793
845
|
if (tempSearchObj) {
|
|
794
846
|
tempSearchObj.searchHooks = void 0;
|
|
795
847
|
}
|
|
848
|
+
const highlightLanguages = /* @__PURE__ */ new Set();
|
|
796
849
|
const replaceRules = userConfig?.replaceRules || [];
|
|
797
850
|
const domain = userConfig?.search && userConfig?.search?.mode === "remote" ? userConfig?.search.domain ?? "" : "";
|
|
798
851
|
const pages = (await extractPageData(
|
|
@@ -800,7 +853,8 @@ async function siteDataVMPlugin(context) {
|
|
|
800
853
|
alias,
|
|
801
854
|
domain,
|
|
802
855
|
userDocRoot,
|
|
803
|
-
routeService2
|
|
856
|
+
routeService2,
|
|
857
|
+
highlightLanguages
|
|
804
858
|
)).filter(Boolean);
|
|
805
859
|
await pluginDriver.modifySearchIndexData(pages);
|
|
806
860
|
const versioned = userConfig.search && userConfig.search.mode !== "remote" && userConfig.search.versioned;
|
|
@@ -864,13 +918,30 @@ async function siteDataVMPlugin(context) {
|
|
|
864
918
|
codeHighlighter: userConfig?.markdown?.codeHighlighter || "prism"
|
|
865
919
|
}
|
|
866
920
|
};
|
|
921
|
+
const { highlightLanguages: defaultLanguages = [] } = config.markdown || {};
|
|
922
|
+
const aliases = handleHighlightLanguages(
|
|
923
|
+
highlightLanguages,
|
|
924
|
+
defaultLanguages
|
|
925
|
+
);
|
|
926
|
+
const sortedAliases = Object.fromEntries(Object.entries(aliases).sort());
|
|
927
|
+
const sortedHighlightLanguages = Array.from(highlightLanguages).sort();
|
|
867
928
|
return {
|
|
868
929
|
[`${"virtual-site-data" /* SiteData */}.mjs`]: `export default ${JSON.stringify(
|
|
869
930
|
siteData
|
|
870
931
|
)}`,
|
|
871
932
|
["virtual-search-index-hash" /* SearchIndexHash */]: `export default ${JSON.stringify(
|
|
872
933
|
indexHashByGroup
|
|
873
|
-
)}
|
|
934
|
+
)}`,
|
|
935
|
+
["virtual-prism-languages" /* PrismLanguages */]: `export const aliases = ${JSON.stringify(
|
|
936
|
+
sortedAliases
|
|
937
|
+
)};
|
|
938
|
+
export const languages = {
|
|
939
|
+
${sortedHighlightLanguages.map((lang) => {
|
|
940
|
+
return `"${lang}": require(
|
|
941
|
+
"react-syntax-highlighter/dist/cjs/languages/prism/${lang}"
|
|
942
|
+
).default`;
|
|
943
|
+
})}
|
|
944
|
+
}`
|
|
874
945
|
};
|
|
875
946
|
}
|
|
876
947
|
|
|
@@ -942,51 +1013,6 @@ async function searchHookVMPlugin(context) {
|
|
|
942
1013
|
};
|
|
943
1014
|
}
|
|
944
1015
|
|
|
945
|
-
// src/node/runtimeModule/prismLanguages.ts
|
|
946
|
-
import { DEFAULT_HIGHLIGHT_LANGUAGES } from "@rspress/shared";
|
|
947
|
-
import { uniqBy } from "lodash-es";
|
|
948
|
-
function getLanguageMeta(highlightLanguages) {
|
|
949
|
-
const aliases = {};
|
|
950
|
-
const languageMeta = uniqBy(
|
|
951
|
-
[...DEFAULT_HIGHLIGHT_LANGUAGES, ...highlightLanguages].map((language) => {
|
|
952
|
-
const isArray = Array.isArray(language);
|
|
953
|
-
const [alias, name] = isArray ? language : [language, language];
|
|
954
|
-
if (isArray) {
|
|
955
|
-
const temp = aliases[name] || (aliases[name] = []);
|
|
956
|
-
if (!temp.includes(alias)) {
|
|
957
|
-
temp.push(alias);
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
return { alias, name };
|
|
961
|
-
}),
|
|
962
|
-
"name"
|
|
963
|
-
);
|
|
964
|
-
return { languageMeta, aliases };
|
|
965
|
-
}
|
|
966
|
-
async function prismLanguageVMPlugin(context) {
|
|
967
|
-
const { config } = context;
|
|
968
|
-
const { highlightLanguages = [] } = config.markdown || {};
|
|
969
|
-
const { languageMeta, aliases } = getLanguageMeta(highlightLanguages);
|
|
970
|
-
const importStatement = languageMeta.map((language) => {
|
|
971
|
-
const { alias, name } = language;
|
|
972
|
-
return `import ${alias} from 'react-syntax-highlighter/dist/esm/languages/prism/${name}'`;
|
|
973
|
-
});
|
|
974
|
-
const moduleContent = `
|
|
975
|
-
${importStatement.join("\n")}
|
|
976
|
-
|
|
977
|
-
export const aliases = ${JSON.stringify(aliases)};
|
|
978
|
-
|
|
979
|
-
export default {
|
|
980
|
-
${languageMeta.map(({ alias, name }) => {
|
|
981
|
-
return `"${name}": ${alias}`;
|
|
982
|
-
})}
|
|
983
|
-
};
|
|
984
|
-
`;
|
|
985
|
-
return {
|
|
986
|
-
["virtual-prism-languages" /* PrismLanguages */]: moduleContent
|
|
987
|
-
};
|
|
988
|
-
}
|
|
989
|
-
|
|
990
1016
|
// src/node/runtimeModule/index.ts
|
|
991
1017
|
var runtimeModuleFactory = [
|
|
992
1018
|
/**
|
|
@@ -995,6 +1021,7 @@ var runtimeModuleFactory = [
|
|
|
995
1021
|
routeVMPlugin,
|
|
996
1022
|
/**
|
|
997
1023
|
* Generate search index and site data for client runtime
|
|
1024
|
+
* Also responsible for automatically importing prism languages
|
|
998
1025
|
*/
|
|
999
1026
|
siteDataVMPlugin,
|
|
1000
1027
|
/**
|
|
@@ -1012,11 +1039,7 @@ var runtimeModuleFactory = [
|
|
|
1012
1039
|
/**
|
|
1013
1040
|
* Generate search hook module
|
|
1014
1041
|
*/
|
|
1015
|
-
searchHookVMPlugin
|
|
1016
|
-
/**
|
|
1017
|
-
* Generate prism languages module
|
|
1018
|
-
*/
|
|
1019
|
-
prismLanguageVMPlugin
|
|
1042
|
+
searchHookVMPlugin
|
|
1020
1043
|
];
|
|
1021
1044
|
function rsbuildPluginDocVM(factoryContext) {
|
|
1022
1045
|
const { pluginDriver } = factoryContext;
|
|
@@ -1057,7 +1080,7 @@ function rsbuildPluginDocVM(factoryContext) {
|
|
|
1057
1080
|
// src/node/searchIndex.ts
|
|
1058
1081
|
import path8, { join as join2 } from "path";
|
|
1059
1082
|
import fs6 from "@rspress/shared/fs-extra";
|
|
1060
|
-
import
|
|
1083
|
+
import chalk2 from "@rspress/shared/chalk";
|
|
1061
1084
|
import fetch from "node-fetch";
|
|
1062
1085
|
import { isSCM, SEARCH_INDEX_NAME as SEARCH_INDEX_NAME2 } from "@rspress/shared";
|
|
1063
1086
|
import { logger as logger2 } from "@rspress/shared/logger";
|
|
@@ -1093,13 +1116,13 @@ async function writeSearchIndex(config) {
|
|
|
1093
1116
|
headers: { "Content-Type": "application/json" }
|
|
1094
1117
|
});
|
|
1095
1118
|
logger2.info(
|
|
1096
|
-
|
|
1119
|
+
chalk2.green(
|
|
1097
1120
|
`[doc-tools] Search index uploaded to ${apiUrl}, indexName: ${indexName}`
|
|
1098
1121
|
)
|
|
1099
1122
|
);
|
|
1100
1123
|
} catch (e) {
|
|
1101
1124
|
logger2.info(
|
|
1102
|
-
|
|
1125
|
+
chalk2.red(
|
|
1103
1126
|
`[doc-tools] Upload search index \`${indexName}\` failed:
|
|
1104
1127
|
${e}`
|
|
1105
1128
|
)
|
|
@@ -1452,7 +1475,7 @@ async function createInternalBuildConfig(userDocRoot, config, isSSR, routeServic
|
|
|
1452
1475
|
]
|
|
1453
1476
|
},
|
|
1454
1477
|
html: {
|
|
1455
|
-
title: config?.title,
|
|
1478
|
+
title: config?.title ?? DEFAULT_TITLE,
|
|
1456
1479
|
favicon: normalizeIcon(config?.icon),
|
|
1457
1480
|
template: path11.join(PACKAGE_ROOT, "index.html"),
|
|
1458
1481
|
tags: [
|
|
@@ -1538,6 +1561,7 @@ async function createInternalBuildConfig(userDocRoot, config, isSSR, routeServic
|
|
|
1538
1561
|
});
|
|
1539
1562
|
}
|
|
1540
1563
|
chain.resolve.extensions.prepend(".md").prepend(".mdx").prepend(".mjs");
|
|
1564
|
+
chain.module.rule("css-virtual-module").test(/\.rspress[\\/]runtime[\\/]virtual-global-styles/).merge({ sideEffects: true });
|
|
1541
1565
|
}
|
|
1542
1566
|
}
|
|
1543
1567
|
};
|
|
@@ -1790,7 +1814,7 @@ async function dev(options) {
|
|
|
1790
1814
|
// src/node/build.ts
|
|
1791
1815
|
import { dirname as dirname2, join as join3 } from "path";
|
|
1792
1816
|
import { pathToFileURL } from "url";
|
|
1793
|
-
import
|
|
1817
|
+
import chalk3 from "@rspress/shared/chalk";
|
|
1794
1818
|
import fs9 from "@rspress/shared/fs-extra";
|
|
1795
1819
|
import {
|
|
1796
1820
|
normalizeSlash,
|
|
@@ -1919,7 +1943,7 @@ async function renderPages(appDirectory, config, pluginDriver, enableSSG) {
|
|
|
1919
1943
|
}
|
|
1920
1944
|
await fs10.remove(join3(outputPath, "html"));
|
|
1921
1945
|
const totalTime = Date.now() - startTime;
|
|
1922
|
-
logger3.success(`Pages rendered in ${
|
|
1946
|
+
logger3.success(`Pages rendered in ${chalk3.yellow(totalTime)} ms.`);
|
|
1923
1947
|
} catch (e) {
|
|
1924
1948
|
logger3.error(`Pages render error: ${e.stack}`);
|
|
1925
1949
|
throw e;
|