@rspress/core 1.20.0 → 1.21.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.js +307 -288
- 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.0",
|
|
70
70
|
description: "The Rspress Documentation Framework",
|
|
71
71
|
bugs: "https://github.com/web-infra-dev/rspress/issues",
|
|
72
72
|
repository: {
|
|
@@ -109,15 +109,15 @@ 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.
|
|
113
|
-
"@rsbuild/core": "0.6.
|
|
114
|
-
"@rsbuild/plugin-react": "0.6.
|
|
112
|
+
"@modern-js/utils": "2.49.3",
|
|
113
|
+
"@rsbuild/core": "0.6.13",
|
|
114
|
+
"@rsbuild/plugin-react": "0.6.13",
|
|
115
115
|
"@rspress/theme-default": "workspace:*",
|
|
116
116
|
"@rspress/runtime": "workspace:*",
|
|
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.5",
|
|
121
121
|
"@rspress/plugin-container-syntax": "workspace:*",
|
|
122
122
|
"@rspress/shared": "workspace:*",
|
|
123
123
|
"body-scroll-lock": "4.0.0-beta.0",
|
|
@@ -156,8 +156,8 @@ 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.3",
|
|
160
|
+
"@modern-js/tsconfig": "2.49.3",
|
|
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",
|
|
@@ -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(),
|
|
@@ -268,224 +269,19 @@ async function routeVMPlugin(context) {
|
|
|
268
269
|
|
|
269
270
|
// src/node/runtimeModule/siteData/index.ts
|
|
270
271
|
import path6 from "path";
|
|
272
|
+
import fs4 from "@rspress/shared/fs-extra";
|
|
271
273
|
import { groupBy } from "lodash-es";
|
|
272
274
|
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
275
|
|
|
480
276
|
// src/node/utils/normalizePath.ts
|
|
481
277
|
import os from "os";
|
|
482
278
|
import path2 from "path";
|
|
483
279
|
var isWindows = os.platform() === "win32";
|
|
484
|
-
function
|
|
280
|
+
function slash(p) {
|
|
485
281
|
return p.replace(/\\/g, "/");
|
|
486
282
|
}
|
|
487
283
|
function normalizePath(id) {
|
|
488
|
-
return path2.posix.normalize(isWindows ?
|
|
284
|
+
return path2.posix.normalize(isWindows ? slash(id) : id);
|
|
489
285
|
}
|
|
490
286
|
|
|
491
287
|
// src/node/utils/getPageKey.ts
|
|
@@ -504,15 +300,17 @@ function createHash(str) {
|
|
|
504
300
|
// src/node/utils/detectReactVersion.ts
|
|
505
301
|
import path3 from "path";
|
|
506
302
|
import fs from "@rspress/shared/fs-extra";
|
|
507
|
-
import { logger } from "@rspress/shared/logger";
|
|
508
303
|
import enhancedResolve from "enhanced-resolve";
|
|
304
|
+
import { logger } from "@rspress/shared/logger";
|
|
509
305
|
var { CachedInputFileSystem, ResolverFactory } = enhancedResolve;
|
|
510
306
|
var DEFAULT_REACT_VERSION = 18;
|
|
511
307
|
async function detectReactVersion() {
|
|
512
308
|
const cwd = process.cwd();
|
|
513
309
|
const reactPath = path3.join(cwd, "node_modules", "react");
|
|
514
310
|
if (await fs.pathExists(reactPath)) {
|
|
515
|
-
const reactPkg = await fs.readJson(
|
|
311
|
+
const reactPkg = await fs.readJson(
|
|
312
|
+
path3.join(reactPath, "package.json")
|
|
313
|
+
);
|
|
516
314
|
const version = Number(reactPkg.version.split(".")[0]);
|
|
517
315
|
return version;
|
|
518
316
|
}
|
|
@@ -532,7 +330,7 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
532
330
|
}
|
|
533
331
|
const alias = {};
|
|
534
332
|
const resolver2 = ResolverFactory.createResolver({
|
|
535
|
-
fileSystem: new CachedInputFileSystem(fs),
|
|
333
|
+
fileSystem: new CachedInputFileSystem(fs, 0),
|
|
536
334
|
extensions: [".js"],
|
|
537
335
|
alias,
|
|
538
336
|
conditionNames: isSSR ? ["..."] : ["browser", "..."]
|
|
@@ -565,18 +363,18 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
565
363
|
|
|
566
364
|
// src/node/utils/flattenMdxContent.ts
|
|
567
365
|
import path4 from "path";
|
|
568
|
-
import enhancedResolve2 from "enhanced-resolve";
|
|
569
366
|
import fs2 from "@rspress/shared/fs-extra";
|
|
570
|
-
import
|
|
367
|
+
import enhancedResolve2 from "enhanced-resolve";
|
|
571
368
|
import { MDX_REGEXP } from "@rspress/shared";
|
|
572
|
-
|
|
573
|
-
var processor = createProcessor();
|
|
369
|
+
import { createProcessor } from "@mdx-js/mdx";
|
|
574
370
|
var resolver;
|
|
575
371
|
var startFlatten = false;
|
|
372
|
+
var processor = createProcessor();
|
|
373
|
+
var { CachedInputFileSystem: CachedInputFileSystem2, ResolverFactory: ResolverFactory2 } = enhancedResolve2;
|
|
576
374
|
async function resolveDepPath(importPath, importer, alias) {
|
|
577
375
|
if (!resolver) {
|
|
578
376
|
resolver = ResolverFactory2.createResolver({
|
|
579
|
-
fileSystem: new CachedInputFileSystem2(fs2),
|
|
377
|
+
fileSystem: new CachedInputFileSystem2(fs2, 0),
|
|
580
378
|
extensions: [".mdx", ".md"],
|
|
581
379
|
alias
|
|
582
380
|
});
|
|
@@ -612,24 +410,22 @@ async function flattenMdxContent(content, basePath, alias) {
|
|
|
612
410
|
if (!regex.test(content)) {
|
|
613
411
|
return { flattenContent: content, deps };
|
|
614
412
|
}
|
|
615
|
-
let result = content;
|
|
616
413
|
if (!startFlatten) {
|
|
617
414
|
resolver = ResolverFactory2.createResolver({
|
|
618
|
-
fileSystem: new CachedInputFileSystem2(fs2),
|
|
415
|
+
fileSystem: new CachedInputFileSystem2(fs2, 0),
|
|
619
416
|
extensions: [".mdx", ".md", ".js"],
|
|
620
417
|
alias
|
|
621
418
|
});
|
|
622
419
|
startFlatten = true;
|
|
623
420
|
}
|
|
624
421
|
let ast;
|
|
422
|
+
let result = content;
|
|
625
423
|
try {
|
|
626
424
|
ast = processor.parse(content);
|
|
627
425
|
} catch (e) {
|
|
628
426
|
return { flattenContent: content, deps };
|
|
629
427
|
}
|
|
630
|
-
const importNodes = ast.children.filter((node) => node.type === "mdxjsEsm").
|
|
631
|
-
return node.data?.estree?.body || [];
|
|
632
|
-
}).flat().filter((node) => node.type === "ImportDeclaration");
|
|
428
|
+
const importNodes = ast.children.filter((node) => node.type === "mdxjsEsm").flatMap((node) => node.data?.estree?.body || []).filter((node) => node.type === "ImportDeclaration");
|
|
633
429
|
for (const importNode of importNodes) {
|
|
634
430
|
const id = importNode.specifiers[0].local.name;
|
|
635
431
|
const importPath = importNode.source.value;
|
|
@@ -656,8 +452,25 @@ async function flattenMdxContent(content, basePath, alias) {
|
|
|
656
452
|
return { flattenContent: result, deps };
|
|
657
453
|
}
|
|
658
454
|
|
|
455
|
+
// src/node/utils/applyReplaceRules.ts
|
|
456
|
+
function applyReplaceRules(code = "", replaceRules = []) {
|
|
457
|
+
let result = code;
|
|
458
|
+
for (const rule of replaceRules) {
|
|
459
|
+
result = result.replace(rule.search, rule.replace);
|
|
460
|
+
}
|
|
461
|
+
return result;
|
|
462
|
+
}
|
|
463
|
+
|
|
659
464
|
// src/node/runtimeModule/siteData/extractPageData.ts
|
|
660
|
-
|
|
465
|
+
import path5 from "path";
|
|
466
|
+
import fs3 from "@rspress/shared/fs-extra";
|
|
467
|
+
import { htmlToText } from "html-to-text";
|
|
468
|
+
import { compile } from "@rspress/mdx-rs";
|
|
469
|
+
import { loadFrontMatter } from "@rspress/shared/node-utils";
|
|
470
|
+
import {
|
|
471
|
+
MDX_REGEXP as MDX_REGEXP2
|
|
472
|
+
} from "@rspress/shared";
|
|
473
|
+
async function extractPageData(replaceRules, alias, domain, root, routeService2, highlighterLangs) {
|
|
661
474
|
return Promise.all(
|
|
662
475
|
routeService2.getRoutes().map(async (route, index) => {
|
|
663
476
|
const defaultIndexInfo = {
|
|
@@ -696,7 +509,8 @@ async function extractPageData(replaceRules, alias, domain, root, routeService2)
|
|
|
696
509
|
const {
|
|
697
510
|
html,
|
|
698
511
|
title,
|
|
699
|
-
toc: rawToc
|
|
512
|
+
toc: rawToc,
|
|
513
|
+
languages
|
|
700
514
|
} = await compile({
|
|
701
515
|
value: content,
|
|
702
516
|
filepath: route.absolutePath,
|
|
@@ -739,6 +553,7 @@ async function extractPageData(replaceRules, alias, domain, root, routeService2)
|
|
|
739
553
|
if (content.startsWith(title)) {
|
|
740
554
|
content = content.slice(title.length);
|
|
741
555
|
}
|
|
556
|
+
languages.forEach((lang) => highlighterLangs.add(lang));
|
|
742
557
|
const toc = rawToc.map((item) => {
|
|
743
558
|
const match = item.id.match(/-(\d+)$/);
|
|
744
559
|
let position = -1;
|
|
@@ -773,6 +588,238 @@ ${item.text}#
|
|
|
773
588
|
);
|
|
774
589
|
}
|
|
775
590
|
|
|
591
|
+
// src/node/runtimeModule/siteData/normalizeThemeConfig.ts
|
|
592
|
+
import {
|
|
593
|
+
withBase,
|
|
594
|
+
addLeadingSlash,
|
|
595
|
+
isExternalUrl,
|
|
596
|
+
withoutBase,
|
|
597
|
+
slash as slash2
|
|
598
|
+
} from "@rspress/shared";
|
|
599
|
+
|
|
600
|
+
// src/node/runtimeModule/i18n.ts
|
|
601
|
+
import { join } from "path";
|
|
602
|
+
var DEFAULT_I18N_SOURCE = join(process.cwd(), "i18n.json");
|
|
603
|
+
function getI18nData(docConfig) {
|
|
604
|
+
const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig;
|
|
605
|
+
try {
|
|
606
|
+
const i18nSource = __require(i18nSourcePath);
|
|
607
|
+
return i18nSource;
|
|
608
|
+
} catch (e) {
|
|
609
|
+
return {};
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function i18nVMPlugin(context) {
|
|
613
|
+
const { config } = context;
|
|
614
|
+
const i18nData = getI18nData(config);
|
|
615
|
+
return {
|
|
616
|
+
["virtual-i18n-text" /* I18nText */]: `export default ${JSON.stringify(i18nData)}`
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// src/node/runtimeModule/siteData/normalizeThemeConfig.ts
|
|
621
|
+
function normalizeThemeConfig(docConfig, pages = []) {
|
|
622
|
+
const {
|
|
623
|
+
locales: siteLocales,
|
|
624
|
+
base = "",
|
|
625
|
+
lang,
|
|
626
|
+
replaceRules = [],
|
|
627
|
+
multiVersion
|
|
628
|
+
} = docConfig;
|
|
629
|
+
const { versions = [] } = multiVersion || {};
|
|
630
|
+
const hasMultiVersion = versions.length > 0;
|
|
631
|
+
docConfig.themeConfig = docConfig.themeConfig || {};
|
|
632
|
+
const { themeConfig } = docConfig;
|
|
633
|
+
const locales = siteLocales ?? (themeConfig?.locales || []);
|
|
634
|
+
const i18nTextData = getI18nData(docConfig);
|
|
635
|
+
const normalizeLinkPrefix = (link = "", currentLang = "") => {
|
|
636
|
+
const normalizedLink = slash2(link);
|
|
637
|
+
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
|
|
638
|
+
hasMultiVersion) {
|
|
639
|
+
return normalizedLink;
|
|
640
|
+
}
|
|
641
|
+
return lang === currentLang ? normalizedLink : `/${currentLang}${addLeadingSlash(normalizedLink)}`;
|
|
642
|
+
};
|
|
643
|
+
const getI18nText = (key = "", currentLang = "") => {
|
|
644
|
+
const text = i18nTextData[key]?.[currentLang];
|
|
645
|
+
return text || key;
|
|
646
|
+
};
|
|
647
|
+
const normalizeSidebar = (sidebar, currentLang = "") => {
|
|
648
|
+
const normalizedSidebar = {};
|
|
649
|
+
if (!sidebar) {
|
|
650
|
+
return {};
|
|
651
|
+
}
|
|
652
|
+
const normalizeSidebarItem = (item) => {
|
|
653
|
+
if (typeof item === "object" && "dividerType" in item) {
|
|
654
|
+
return item;
|
|
655
|
+
}
|
|
656
|
+
if (typeof item === "object" && "sectionHeaderText" in item) {
|
|
657
|
+
item.sectionHeaderText = applyReplaceRules(
|
|
658
|
+
getI18nText(item.sectionHeaderText, currentLang),
|
|
659
|
+
replaceRules
|
|
660
|
+
);
|
|
661
|
+
return item;
|
|
662
|
+
}
|
|
663
|
+
if (typeof item === "object" && "items" in item) {
|
|
664
|
+
return {
|
|
665
|
+
...item,
|
|
666
|
+
text: applyReplaceRules(
|
|
667
|
+
getI18nText(item.text, currentLang),
|
|
668
|
+
replaceRules
|
|
669
|
+
),
|
|
670
|
+
link: normalizeLinkPrefix(item.link),
|
|
671
|
+
collapsed: item.collapsed ?? false,
|
|
672
|
+
collapsible: item.collapsible ?? true,
|
|
673
|
+
tag: item.tag,
|
|
674
|
+
items: item.items.map((subItem) => {
|
|
675
|
+
return normalizeSidebarItem(subItem);
|
|
676
|
+
})
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
if (typeof item === "string") {
|
|
680
|
+
const normalizedItem = normalizeLinkPrefix(item, currentLang);
|
|
681
|
+
const page = pages.find(
|
|
682
|
+
(page2) => page2.routePath === withBase(normalizedItem, base)
|
|
683
|
+
);
|
|
684
|
+
return {
|
|
685
|
+
text: applyReplaceRules(page?.title || "", replaceRules),
|
|
686
|
+
link: normalizedItem,
|
|
687
|
+
_fileKey: page._relativePath.replace(/\.mdx?$/, "")
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
return {
|
|
691
|
+
...item,
|
|
692
|
+
text: applyReplaceRules(
|
|
693
|
+
getI18nText(item.text, currentLang),
|
|
694
|
+
replaceRules
|
|
695
|
+
),
|
|
696
|
+
link: normalizeLinkPrefix(item.link),
|
|
697
|
+
tag: item.tag
|
|
698
|
+
};
|
|
699
|
+
};
|
|
700
|
+
const normalizeSidebar2 = (sidebar2) => {
|
|
701
|
+
Object.keys(sidebar2).forEach((key) => {
|
|
702
|
+
const value = sidebar2[key];
|
|
703
|
+
normalizedSidebar[key] = value.map(normalizeSidebarItem);
|
|
704
|
+
});
|
|
705
|
+
};
|
|
706
|
+
normalizeSidebar2(sidebar);
|
|
707
|
+
return normalizedSidebar;
|
|
708
|
+
};
|
|
709
|
+
const normalizeNav = (nav, currentLang) => {
|
|
710
|
+
if (!nav) {
|
|
711
|
+
return [];
|
|
712
|
+
}
|
|
713
|
+
const transformNavItem = (navItem) => {
|
|
714
|
+
const text = applyReplaceRules(
|
|
715
|
+
getI18nText(navItem.text, currentLang),
|
|
716
|
+
replaceRules
|
|
717
|
+
);
|
|
718
|
+
if ("link" in navItem) {
|
|
719
|
+
return {
|
|
720
|
+
...navItem,
|
|
721
|
+
text,
|
|
722
|
+
link: normalizeLinkPrefix(navItem.link, currentLang)
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
if ("items" in navItem) {
|
|
726
|
+
return {
|
|
727
|
+
...navItem,
|
|
728
|
+
text,
|
|
729
|
+
items: navItem.items.map((item) => {
|
|
730
|
+
return {
|
|
731
|
+
...item,
|
|
732
|
+
text: applyReplaceRules(
|
|
733
|
+
getI18nText(item.text, currentLang),
|
|
734
|
+
replaceRules
|
|
735
|
+
),
|
|
736
|
+
link: normalizeLinkPrefix(item.link, currentLang)
|
|
737
|
+
};
|
|
738
|
+
})
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
return navItem;
|
|
742
|
+
};
|
|
743
|
+
if (Array.isArray(nav)) {
|
|
744
|
+
return nav.map(transformNavItem);
|
|
745
|
+
}
|
|
746
|
+
return Object.entries(nav).reduce((acc, [key, value]) => {
|
|
747
|
+
acc[key] = value.map(transformNavItem);
|
|
748
|
+
return acc;
|
|
749
|
+
}, {});
|
|
750
|
+
};
|
|
751
|
+
if (locales.length) {
|
|
752
|
+
themeConfig.locales = locales.map(({ lang: currentLang, label }) => {
|
|
753
|
+
const localeInThemeConfig = themeConfig.locales?.find(
|
|
754
|
+
(locale) => locale.lang === currentLang
|
|
755
|
+
);
|
|
756
|
+
return {
|
|
757
|
+
lang: currentLang,
|
|
758
|
+
label,
|
|
759
|
+
...localeInThemeConfig || {},
|
|
760
|
+
sidebar: normalizeSidebar(
|
|
761
|
+
localeInThemeConfig?.sidebar ?? themeConfig.sidebar,
|
|
762
|
+
currentLang
|
|
763
|
+
),
|
|
764
|
+
nav: normalizeNav(
|
|
765
|
+
localeInThemeConfig?.nav ?? themeConfig.nav,
|
|
766
|
+
currentLang
|
|
767
|
+
)
|
|
768
|
+
};
|
|
769
|
+
});
|
|
770
|
+
} else {
|
|
771
|
+
themeConfig.sidebar = normalizeSidebar(themeConfig?.sidebar);
|
|
772
|
+
themeConfig.nav = normalizeNav(themeConfig?.nav);
|
|
773
|
+
}
|
|
774
|
+
return themeConfig;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// src/node/runtimeModule/siteData/highlightLanguages.ts
|
|
778
|
+
import chalk from "@rspress/shared/chalk";
|
|
779
|
+
import { DEFAULT_HIGHLIGHT_LANGUAGES } from "@rspress/shared";
|
|
780
|
+
var supportedLanguages;
|
|
781
|
+
function handleHighlightLanguages(highlightLanguages, defaultLanguages) {
|
|
782
|
+
const aliases = {};
|
|
783
|
+
if (highlightLanguages.size) {
|
|
784
|
+
if (!supportedLanguages) {
|
|
785
|
+
const langs = __require("react-syntax-highlighter/dist/cjs/languages/prism/supported-languages").default;
|
|
786
|
+
supportedLanguages = new Set(langs);
|
|
787
|
+
}
|
|
788
|
+
let useDeprecatedTypeWarning = true;
|
|
789
|
+
const names = {};
|
|
790
|
+
[...DEFAULT_HIGHLIGHT_LANGUAGES, ...defaultLanguages].forEach((lang) => {
|
|
791
|
+
if (Array.isArray(lang)) {
|
|
792
|
+
const [alias, name] = lang;
|
|
793
|
+
names[alias] = name;
|
|
794
|
+
} else if (useDeprecatedTypeWarning) {
|
|
795
|
+
console.log(
|
|
796
|
+
`${chalk.yellowBright(
|
|
797
|
+
"warning"
|
|
798
|
+
)} Automatic import is supported. \`highlightLanguages\` is now used only as alias, and string types will be ignored.
|
|
799
|
+
`
|
|
800
|
+
);
|
|
801
|
+
useDeprecatedTypeWarning = false;
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
[...highlightLanguages.values()].forEach((lang) => {
|
|
805
|
+
const name = names[lang];
|
|
806
|
+
if (name && supportedLanguages.has(name)) {
|
|
807
|
+
const temp = aliases[name] || (aliases[name] = []);
|
|
808
|
+
if (!temp.includes(lang)) {
|
|
809
|
+
temp.push(lang);
|
|
810
|
+
}
|
|
811
|
+
highlightLanguages.add(name);
|
|
812
|
+
highlightLanguages.delete(lang);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
if (!supportedLanguages.has(lang)) {
|
|
816
|
+
highlightLanguages.delete(lang);
|
|
817
|
+
}
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
return aliases;
|
|
821
|
+
}
|
|
822
|
+
|
|
776
823
|
// src/node/runtimeModule/siteData/index.ts
|
|
777
824
|
function deletePriviteKey(obj) {
|
|
778
825
|
if (typeof obj !== "object" || obj === null) {
|
|
@@ -793,6 +840,7 @@ async function siteDataVMPlugin(context) {
|
|
|
793
840
|
if (tempSearchObj) {
|
|
794
841
|
tempSearchObj.searchHooks = void 0;
|
|
795
842
|
}
|
|
843
|
+
const highlightLanguages = /* @__PURE__ */ new Set();
|
|
796
844
|
const replaceRules = userConfig?.replaceRules || [];
|
|
797
845
|
const domain = userConfig?.search && userConfig?.search?.mode === "remote" ? userConfig?.search.domain ?? "" : "";
|
|
798
846
|
const pages = (await extractPageData(
|
|
@@ -800,7 +848,8 @@ async function siteDataVMPlugin(context) {
|
|
|
800
848
|
alias,
|
|
801
849
|
domain,
|
|
802
850
|
userDocRoot,
|
|
803
|
-
routeService2
|
|
851
|
+
routeService2,
|
|
852
|
+
highlightLanguages
|
|
804
853
|
)).filter(Boolean);
|
|
805
854
|
await pluginDriver.modifySearchIndexData(pages);
|
|
806
855
|
const versioned = userConfig.search && userConfig.search.mode !== "remote" && userConfig.search.versioned;
|
|
@@ -864,13 +913,30 @@ async function siteDataVMPlugin(context) {
|
|
|
864
913
|
codeHighlighter: userConfig?.markdown?.codeHighlighter || "prism"
|
|
865
914
|
}
|
|
866
915
|
};
|
|
916
|
+
const { highlightLanguages: defaultLanguages = [] } = config.markdown || {};
|
|
917
|
+
const aliases = handleHighlightLanguages(
|
|
918
|
+
highlightLanguages,
|
|
919
|
+
defaultLanguages
|
|
920
|
+
);
|
|
921
|
+
const sortedAliases = Object.fromEntries(Object.entries(aliases).sort());
|
|
922
|
+
const sortedHighlightLanguages = Array.from(highlightLanguages).sort();
|
|
867
923
|
return {
|
|
868
924
|
[`${"virtual-site-data" /* SiteData */}.mjs`]: `export default ${JSON.stringify(
|
|
869
925
|
siteData
|
|
870
926
|
)}`,
|
|
871
927
|
["virtual-search-index-hash" /* SearchIndexHash */]: `export default ${JSON.stringify(
|
|
872
928
|
indexHashByGroup
|
|
873
|
-
)}
|
|
929
|
+
)}`,
|
|
930
|
+
["virtual-prism-languages" /* PrismLanguages */]: `export const aliases = ${JSON.stringify(
|
|
931
|
+
sortedAliases
|
|
932
|
+
)};
|
|
933
|
+
export const languages = {
|
|
934
|
+
${sortedHighlightLanguages.map((lang) => {
|
|
935
|
+
return `"${lang}": require(
|
|
936
|
+
"react-syntax-highlighter/dist/cjs/languages/prism/${lang}"
|
|
937
|
+
).default`;
|
|
938
|
+
})}
|
|
939
|
+
}`
|
|
874
940
|
};
|
|
875
941
|
}
|
|
876
942
|
|
|
@@ -942,51 +1008,6 @@ async function searchHookVMPlugin(context) {
|
|
|
942
1008
|
};
|
|
943
1009
|
}
|
|
944
1010
|
|
|
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
1011
|
// src/node/runtimeModule/index.ts
|
|
991
1012
|
var runtimeModuleFactory = [
|
|
992
1013
|
/**
|
|
@@ -995,6 +1016,7 @@ var runtimeModuleFactory = [
|
|
|
995
1016
|
routeVMPlugin,
|
|
996
1017
|
/**
|
|
997
1018
|
* Generate search index and site data for client runtime
|
|
1019
|
+
* Also responsible for automatically importing prism languages
|
|
998
1020
|
*/
|
|
999
1021
|
siteDataVMPlugin,
|
|
1000
1022
|
/**
|
|
@@ -1012,11 +1034,7 @@ var runtimeModuleFactory = [
|
|
|
1012
1034
|
/**
|
|
1013
1035
|
* Generate search hook module
|
|
1014
1036
|
*/
|
|
1015
|
-
searchHookVMPlugin
|
|
1016
|
-
/**
|
|
1017
|
-
* Generate prism languages module
|
|
1018
|
-
*/
|
|
1019
|
-
prismLanguageVMPlugin
|
|
1037
|
+
searchHookVMPlugin
|
|
1020
1038
|
];
|
|
1021
1039
|
function rsbuildPluginDocVM(factoryContext) {
|
|
1022
1040
|
const { pluginDriver } = factoryContext;
|
|
@@ -1057,7 +1075,7 @@ function rsbuildPluginDocVM(factoryContext) {
|
|
|
1057
1075
|
// src/node/searchIndex.ts
|
|
1058
1076
|
import path8, { join as join2 } from "path";
|
|
1059
1077
|
import fs6 from "@rspress/shared/fs-extra";
|
|
1060
|
-
import
|
|
1078
|
+
import chalk2 from "@rspress/shared/chalk";
|
|
1061
1079
|
import fetch from "node-fetch";
|
|
1062
1080
|
import { isSCM, SEARCH_INDEX_NAME as SEARCH_INDEX_NAME2 } from "@rspress/shared";
|
|
1063
1081
|
import { logger as logger2 } from "@rspress/shared/logger";
|
|
@@ -1093,13 +1111,13 @@ async function writeSearchIndex(config) {
|
|
|
1093
1111
|
headers: { "Content-Type": "application/json" }
|
|
1094
1112
|
});
|
|
1095
1113
|
logger2.info(
|
|
1096
|
-
|
|
1114
|
+
chalk2.green(
|
|
1097
1115
|
`[doc-tools] Search index uploaded to ${apiUrl}, indexName: ${indexName}`
|
|
1098
1116
|
)
|
|
1099
1117
|
);
|
|
1100
1118
|
} catch (e) {
|
|
1101
1119
|
logger2.info(
|
|
1102
|
-
|
|
1120
|
+
chalk2.red(
|
|
1103
1121
|
`[doc-tools] Upload search index \`${indexName}\` failed:
|
|
1104
1122
|
${e}`
|
|
1105
1123
|
)
|
|
@@ -1452,7 +1470,7 @@ async function createInternalBuildConfig(userDocRoot, config, isSSR, routeServic
|
|
|
1452
1470
|
]
|
|
1453
1471
|
},
|
|
1454
1472
|
html: {
|
|
1455
|
-
title: config?.title,
|
|
1473
|
+
title: config?.title ?? DEFAULT_TITLE,
|
|
1456
1474
|
favicon: normalizeIcon(config?.icon),
|
|
1457
1475
|
template: path11.join(PACKAGE_ROOT, "index.html"),
|
|
1458
1476
|
tags: [
|
|
@@ -1538,6 +1556,7 @@ async function createInternalBuildConfig(userDocRoot, config, isSSR, routeServic
|
|
|
1538
1556
|
});
|
|
1539
1557
|
}
|
|
1540
1558
|
chain.resolve.extensions.prepend(".md").prepend(".mdx").prepend(".mjs");
|
|
1559
|
+
chain.module.rule("css-virtual-module").test(/\.rspress[\\/]runtime[\\/]virtual-global-styles/).merge({ sideEffects: true });
|
|
1541
1560
|
}
|
|
1542
1561
|
}
|
|
1543
1562
|
};
|
|
@@ -1790,7 +1809,7 @@ async function dev(options) {
|
|
|
1790
1809
|
// src/node/build.ts
|
|
1791
1810
|
import { dirname as dirname2, join as join3 } from "path";
|
|
1792
1811
|
import { pathToFileURL } from "url";
|
|
1793
|
-
import
|
|
1812
|
+
import chalk3 from "@rspress/shared/chalk";
|
|
1794
1813
|
import fs9 from "@rspress/shared/fs-extra";
|
|
1795
1814
|
import {
|
|
1796
1815
|
normalizeSlash,
|
|
@@ -1919,7 +1938,7 @@ async function renderPages(appDirectory, config, pluginDriver, enableSSG) {
|
|
|
1919
1938
|
}
|
|
1920
1939
|
await fs10.remove(join3(outputPath, "html"));
|
|
1921
1940
|
const totalTime = Date.now() - startTime;
|
|
1922
|
-
logger3.success(`Pages rendered in ${
|
|
1941
|
+
logger3.success(`Pages rendered in ${chalk3.yellow(totalTime)} ms.`);
|
|
1923
1942
|
} catch (e) {
|
|
1924
1943
|
logger3.error(`Pages render error: ${e.stack}`);
|
|
1925
1944
|
throw e;
|