@alauda/doom 2.11.1 → 2.11.3
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/lib/cli/translate-pipeline.d.ts +10 -0
- package/lib/cli/translate-pipeline.js +47 -4
- package/lib/cli/translate.js +1 -0
- package/lib/plugins/global/index.d.ts +2 -1
- package/lib/plugins/global/index.js +41 -0
- package/lib/plugins/global/sidebar.d.ts +16 -0
- package/lib/plugins/global/sidebar.js +11 -0
- package/lib/theme/DeferredSidebar.d.ts +1 -0
- package/lib/theme/DeferredSidebar.js +67 -0
- package/lib/theme/DocLayout.d.ts +17 -0
- package/lib/theme/DocLayout.js +27 -0
- package/lib/theme/Layout.js +97 -5
- package/lib/theme/sidebar.d.ts +2 -0
- package/lib/theme/sidebar.js +14 -0
- package/package.json +1 -1
|
@@ -138,6 +138,8 @@ export interface TranslateDocumentOptions {
|
|
|
138
138
|
maxAssemblyRounds?: number;
|
|
139
139
|
contextTail?: number;
|
|
140
140
|
repairer?: SegmentRepairer;
|
|
141
|
+
/** Maximum number of uncached segments worked on at once. */
|
|
142
|
+
segmentConcurrency?: number;
|
|
141
143
|
/**
|
|
142
144
|
* The translation already on disk, for reusing the segments that did not
|
|
143
145
|
* change. Absent on a first translation, and when `--force` is given.
|
|
@@ -159,6 +161,14 @@ export interface TranslateDocumentOptions {
|
|
|
159
161
|
*/
|
|
160
162
|
export declare const maskFindingToTranslationFinding: (finding: MaskIntegrityError["findings"][number]) => TranslationFinding;
|
|
161
163
|
export declare const translateDocument: (options: TranslateDocumentOptions) => Promise<TranslateDocumentResult>;
|
|
164
|
+
/**
|
|
165
|
+
* Run independent segment jobs with a small worker pool.
|
|
166
|
+
*
|
|
167
|
+
* A rejected job stops new work from being scheduled but lets already-started
|
|
168
|
+
* jobs settle. This preserves the document-level error boundary while avoiding
|
|
169
|
+
* dangling promises when one model call fails.
|
|
170
|
+
*/
|
|
171
|
+
export declare const runWithConcurrency: <T>(items: readonly T[], concurrency: number, worker: (item: T, index: number) => Promise<unknown>) => Promise<void>;
|
|
162
172
|
/**
|
|
163
173
|
* Whether a segment has any text a reviewer could judge.
|
|
164
174
|
*
|
|
@@ -55,7 +55,7 @@ export const maskFindingToTranslationFinding = (finding) => ({
|
|
|
55
55
|
placeholder: finding.placeholder,
|
|
56
56
|
});
|
|
57
57
|
export const translateDocument = async (options) => {
|
|
58
|
-
const { tree, maskedSource, maskEntries, processor, compose, targetPath, sourceLabel, sourceLanguage, targetLanguage, translator, checker, segmentJudge, documentJudge, terms, segmentCap, segmentHardCap, segmentFloor, maxSegmentAttempts = DEFAULT_MAX_SEGMENT_ATTEMPTS, maxAssemblyRounds = DEFAULT_MAX_ASSEMBLY_ROUNDS, contextTail, repairer, previous, onProgress, } = options;
|
|
58
|
+
const { tree, maskedSource, maskEntries, processor, compose, targetPath, sourceLabel, sourceLanguage, targetLanguage, translator, checker, segmentJudge, documentJudge, terms, segmentCap, segmentHardCap, segmentFloor, maxSegmentAttempts = DEFAULT_MAX_SEGMENT_ATTEMPTS, maxAssemblyRounds = DEFAULT_MAX_ASSEMBLY_ROUNDS, contextTail, repairer, segmentConcurrency = 1, previous, onProgress, } = options;
|
|
59
59
|
let plan;
|
|
60
60
|
try {
|
|
61
61
|
plan = planSegments({
|
|
@@ -262,7 +262,8 @@ export const translateDocument = async (options) => {
|
|
|
262
262
|
}
|
|
263
263
|
return true;
|
|
264
264
|
};
|
|
265
|
-
// ------------------------------------------------
|
|
265
|
+
// ------------------------------------------------ cached segments first
|
|
266
|
+
const pending = [];
|
|
266
267
|
for (const segment of plan.segments) {
|
|
267
268
|
const reused = reuse.get(segment.index);
|
|
268
269
|
if (reused != null) {
|
|
@@ -270,8 +271,10 @@ export const translateDocument = async (options) => {
|
|
|
270
271
|
outcomes[segment.index].status = 'cached';
|
|
271
272
|
continue;
|
|
272
273
|
}
|
|
273
|
-
|
|
274
|
+
pending.push(segment);
|
|
274
275
|
}
|
|
276
|
+
// ------------------------------------------------ uncached segments, bounded
|
|
277
|
+
await runWithConcurrency(pending, segmentConcurrency, (segment) => workOn(segment));
|
|
275
278
|
if (reuse.size > 0) {
|
|
276
279
|
onProgress?.(`${sourceLabel}: reused ${reuse.size} of ${plan.segments.length} segment(s) from the previous translation`);
|
|
277
280
|
}
|
|
@@ -369,11 +372,51 @@ export const translateDocument = async (options) => {
|
|
|
369
372
|
};
|
|
370
373
|
}
|
|
371
374
|
assemblyRounds++;
|
|
375
|
+
const rerouted = [];
|
|
372
376
|
for (const [index, sentBack] of routed) {
|
|
373
377
|
const segment = plan.segments[index];
|
|
374
378
|
onProgress?.(`${sourceLabel} [segment ${index + 1}/${plan.segments.length}${describe(segment)}] sent back by the assembled document: ${summarise(sentBack)}${reasons(sentBack)}`);
|
|
375
|
-
|
|
379
|
+
rerouted.push({ segment, findings: sentBack });
|
|
376
380
|
}
|
|
381
|
+
await runWithConcurrency(rerouted, segmentConcurrency, ({ segment, findings }) => workOn(segment, findings));
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
/**
|
|
385
|
+
* Run independent segment jobs with a small worker pool.
|
|
386
|
+
*
|
|
387
|
+
* A rejected job stops new work from being scheduled but lets already-started
|
|
388
|
+
* jobs settle. This preserves the document-level error boundary while avoiding
|
|
389
|
+
* dangling promises when one model call fails.
|
|
390
|
+
*/
|
|
391
|
+
export const runWithConcurrency = async (items, concurrency, worker) => {
|
|
392
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) {
|
|
393
|
+
throw new Error(`segment concurrency must be a positive integer, got ${concurrency}`);
|
|
394
|
+
}
|
|
395
|
+
let next = 0;
|
|
396
|
+
let stopped = false;
|
|
397
|
+
const errors = [];
|
|
398
|
+
const runWorker = async () => {
|
|
399
|
+
while (!stopped) {
|
|
400
|
+
const index = next++;
|
|
401
|
+
const item = items[index];
|
|
402
|
+
if (item === undefined) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
try {
|
|
406
|
+
await worker(item, index);
|
|
407
|
+
}
|
|
408
|
+
catch (error) {
|
|
409
|
+
if (errors.length === 0) {
|
|
410
|
+
errors.push(error);
|
|
411
|
+
stopped = true;
|
|
412
|
+
}
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, () => runWorker()));
|
|
418
|
+
if (errors.length > 0) {
|
|
419
|
+
throw errors[0];
|
|
377
420
|
}
|
|
378
421
|
};
|
|
379
422
|
const describe = (segment) => segment.label.heading ? ` ‹${segment.label.heading}›` : '';
|
package/lib/cli/translate.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type RspressPlugin } from '@rspress/core';
|
|
2
2
|
import { type DoomSite } from '../../shared/index.ts';
|
|
3
3
|
import type { ExportItem } from '../../types.ts';
|
|
4
4
|
export interface GlobalPluginOptions {
|
|
@@ -10,5 +10,6 @@ export interface GlobalVirtual extends GlobalPluginOptions {
|
|
|
10
10
|
prefix?: string;
|
|
11
11
|
sites?: DoomSite[];
|
|
12
12
|
export?: ExportItem[];
|
|
13
|
+
sidebarManifestUrl?: string;
|
|
13
14
|
}
|
|
14
15
|
export declare const globalPlugin: ({ version, download, }: GlobalPluginOptions) => RspressPlugin;
|
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import {} from '@rspress/core';
|
|
3
4
|
import { addTrailingSlash } from '@rspress/shared';
|
|
4
5
|
import { ACP_BASE } from "../../shared/index.js";
|
|
5
6
|
import { baseResolve, pkgResolve } from "../../utils/index.js";
|
|
6
7
|
import { getGlobalComponentFiles } from "./components.js";
|
|
8
|
+
import { createSidebarAsset } from "./sidebar.js";
|
|
7
9
|
const globalComponentsDir = baseResolve('global');
|
|
10
|
+
const stableEntryFilename = (pathData) => pathData.chunk?.name === 'index' ? '[name].js' : '[name].[contenthash:10].js';
|
|
8
11
|
export const globalPlugin = ({ version, download, }) => {
|
|
12
|
+
let sidebarAsset;
|
|
13
|
+
const updateSidebarAsset = (config) => {
|
|
14
|
+
const themeConfig = config.themeConfig;
|
|
15
|
+
const payload = {
|
|
16
|
+
sidebar: themeConfig?.sidebar,
|
|
17
|
+
locales: themeConfig?.locales?.map(({ lang, sidebar }) => ({
|
|
18
|
+
lang,
|
|
19
|
+
sidebar: sidebar,
|
|
20
|
+
})),
|
|
21
|
+
};
|
|
22
|
+
sidebarAsset = createSidebarAsset(payload);
|
|
23
|
+
};
|
|
9
24
|
return {
|
|
10
25
|
name: 'doom-global',
|
|
11
26
|
globalStyles: pkgResolve('styles/global.scss'),
|
|
@@ -15,6 +30,17 @@ export const globalPlugin = ({ version, download, }) => {
|
|
|
15
30
|
markdown: {
|
|
16
31
|
globalComponents: getGlobalComponentFiles(),
|
|
17
32
|
},
|
|
33
|
+
builderConfig: {
|
|
34
|
+
output: {
|
|
35
|
+
filename: {
|
|
36
|
+
js: stableEntryFilename,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
config(config) {
|
|
41
|
+
updateSidebarAsset(config);
|
|
42
|
+
return config;
|
|
43
|
+
},
|
|
18
44
|
addRuntimeModules(config, isProd) {
|
|
19
45
|
return {
|
|
20
46
|
'doom-@global-virtual': `export default ${JSON.stringify({
|
|
@@ -28,9 +54,24 @@ export const globalPlugin = ({ version, download, }) => {
|
|
|
28
54
|
version: site.version,
|
|
29
55
|
})),
|
|
30
56
|
export: config.export,
|
|
57
|
+
sidebarManifestUrl: sidebarAsset
|
|
58
|
+
? `${addTrailingSlash(config.base || '/')}static/${sidebarAsset.manifestFileName}`
|
|
59
|
+
: undefined,
|
|
31
60
|
}, null, isProd ? 0 : 2)}`,
|
|
32
61
|
};
|
|
33
62
|
},
|
|
63
|
+
afterBuild(config) {
|
|
64
|
+
if (!sidebarAsset) {
|
|
65
|
+
updateSidebarAsset(config);
|
|
66
|
+
}
|
|
67
|
+
if (!sidebarAsset) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const staticDir = path.resolve(config.outDir || 'dist', 'static');
|
|
71
|
+
fs.mkdirSync(staticDir, { recursive: true });
|
|
72
|
+
fs.writeFileSync(path.join(staticDir, sidebarAsset.fileName), sidebarAsset.content);
|
|
73
|
+
fs.writeFileSync(path.join(staticDir, sidebarAsset.manifestFileName), sidebarAsset.manifestContent);
|
|
74
|
+
},
|
|
34
75
|
addPages(config) {
|
|
35
76
|
let loginPath;
|
|
36
77
|
let productsPath;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { NormalizedSidebar } from '@rspress/shared';
|
|
2
|
+
export interface SharedSidebarLocale {
|
|
3
|
+
lang: string;
|
|
4
|
+
sidebar?: NormalizedSidebar;
|
|
5
|
+
}
|
|
6
|
+
export interface SharedSidebarPayload {
|
|
7
|
+
sidebar?: NormalizedSidebar;
|
|
8
|
+
locales?: SharedSidebarLocale[];
|
|
9
|
+
}
|
|
10
|
+
export interface SidebarAsset {
|
|
11
|
+
content: string;
|
|
12
|
+
fileName: string;
|
|
13
|
+
manifestContent: string;
|
|
14
|
+
manifestFileName: string;
|
|
15
|
+
}
|
|
16
|
+
export declare const createSidebarAsset: (payload: SharedSidebarPayload) => SidebarAsset;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
export const createSidebarAsset = (payload) => {
|
|
3
|
+
const content = JSON.stringify(payload);
|
|
4
|
+
const hash = createHash('sha256').update(content).digest('hex');
|
|
5
|
+
return {
|
|
6
|
+
content,
|
|
7
|
+
fileName: `sidebar.${hash}.json`,
|
|
8
|
+
manifestContent: JSON.stringify({ fileName: `sidebar.${hash}.json` }),
|
|
9
|
+
manifestFileName: 'sidebar.json',
|
|
10
|
+
};
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DeferredSidebar: () => import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { getSidebarDataGroup, useActiveMatcher, useLang, useLocation, useSidebar, } from '@rspress/core/runtime';
|
|
3
|
+
import { SidebarList } from '@rspress/core/theme';
|
|
4
|
+
import virtual from 'doom-@global-virtual';
|
|
5
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
6
|
+
import { expandActiveSidebar } from "./sidebar.js";
|
|
7
|
+
const isSidebarPayload = (value) => typeof value === 'object' && value !== null;
|
|
8
|
+
export const DeferredSidebar = () => {
|
|
9
|
+
const fallback = useSidebar();
|
|
10
|
+
const lang = useLang();
|
|
11
|
+
const { pathname } = useLocation();
|
|
12
|
+
const activeMatcher = useActiveMatcher();
|
|
13
|
+
const [payload, setPayload] = useState();
|
|
14
|
+
const [sidebarData, setSidebarData] = useState([]);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!virtual.sidebarManifestUrl) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
let active = true;
|
|
20
|
+
void fetch(virtual.sidebarManifestUrl, { cache: 'no-store' })
|
|
21
|
+
.then((response) => {
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
throw new Error(`sidebar request failed: ${response.status}`);
|
|
24
|
+
}
|
|
25
|
+
return response.json();
|
|
26
|
+
})
|
|
27
|
+
.then((value) => {
|
|
28
|
+
if (!isSidebarManifest(value) || !virtual.sidebarManifestUrl) {
|
|
29
|
+
throw new Error('invalid sidebar manifest');
|
|
30
|
+
}
|
|
31
|
+
return fetch(new URL(value.fileName, new URL(virtual.sidebarManifestUrl, window.location.href)), {
|
|
32
|
+
cache: 'force-cache',
|
|
33
|
+
});
|
|
34
|
+
})
|
|
35
|
+
.then((response) => {
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
throw new Error(`sidebar asset request failed: ${response.status}`);
|
|
38
|
+
}
|
|
39
|
+
return response.json();
|
|
40
|
+
})
|
|
41
|
+
.then((value) => {
|
|
42
|
+
if (active && isSidebarPayload(value)) {
|
|
43
|
+
setPayload(value);
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
.catch(() => {
|
|
47
|
+
// Keep the Rspress fallback when the shared asset is unavailable.
|
|
48
|
+
});
|
|
49
|
+
return () => {
|
|
50
|
+
active = false;
|
|
51
|
+
};
|
|
52
|
+
}, []);
|
|
53
|
+
const sharedSidebar = useMemo(() => {
|
|
54
|
+
const localeSidebar = payload?.locales?.find((item) => item.lang === lang)?.sidebar;
|
|
55
|
+
const sidebar = localeSidebar ?? payload?.sidebar;
|
|
56
|
+
return sidebar ? getSidebarDataGroup(sidebar, pathname) : fallback;
|
|
57
|
+
}, [fallback, lang, pathname, payload]);
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
setSidebarData(expandActiveSidebar(sharedSidebar, activeMatcher));
|
|
60
|
+
}, [activeMatcher, sharedSidebar]);
|
|
61
|
+
return (_jsx(SidebarList, { sidebarData: sidebarData, setSidebarData: setSidebarData }));
|
|
62
|
+
};
|
|
63
|
+
const isSidebarManifest = (value) => typeof value === 'object' &&
|
|
64
|
+
value !== null &&
|
|
65
|
+
'fileName' in value &&
|
|
66
|
+
typeof value.fileName === 'string' &&
|
|
67
|
+
/^sidebar\.[0-9a-f]{64}\.json$/.test(value.fileName);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import '@rspress/core/dist/theme/layout/DocLayout/index.css';
|
|
2
|
+
import { type FC, type ReactNode } from 'react';
|
|
3
|
+
export interface DocLayoutProps {
|
|
4
|
+
beforeSidebar?: ReactNode;
|
|
5
|
+
afterSidebar?: ReactNode;
|
|
6
|
+
beforeDocFooter?: ReactNode;
|
|
7
|
+
afterDocFooter?: ReactNode;
|
|
8
|
+
beforeDoc?: ReactNode;
|
|
9
|
+
afterDoc?: ReactNode;
|
|
10
|
+
beforeDocContent?: ReactNode;
|
|
11
|
+
afterDocContent?: ReactNode;
|
|
12
|
+
beforeOutline?: ReactNode;
|
|
13
|
+
afterOutline?: ReactNode;
|
|
14
|
+
navTitle?: ReactNode;
|
|
15
|
+
components?: Record<string, FC>;
|
|
16
|
+
}
|
|
17
|
+
export declare const DocLayout: (props: DocLayoutProps) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import '@rspress/core/dist/theme/layout/DocLayout/index.css';
|
|
3
|
+
import { useSidebarMenu } from '@rspress/core/dist/theme/components/SidebarMenu/useSidebarMenu.js';
|
|
4
|
+
import { useFrontmatter } from '@rspress/core/runtime';
|
|
5
|
+
import { DocContent, DocFooter, Outline, Overview, useWatchToc, } from '@rspress/core/theme';
|
|
6
|
+
import clsx from 'clsx';
|
|
7
|
+
import { Fragment } from 'react';
|
|
8
|
+
import { DeferredSidebar } from "./DeferredSidebar.js";
|
|
9
|
+
export const DocLayout = (props) => {
|
|
10
|
+
const { beforeDocFooter, afterDocFooter, beforeDoc, afterDoc, beforeDocContent, afterDocContent, beforeOutline, afterOutline, beforeSidebar, afterSidebar, components, } = props;
|
|
11
|
+
const { frontmatter } = useFrontmatter();
|
|
12
|
+
const isOverviewPage = frontmatter.overview ?? false;
|
|
13
|
+
const sidebar = frontmatter.sidebar ?? true;
|
|
14
|
+
const showSidebar = true === sidebar;
|
|
15
|
+
const showSidebarPlaceholder = false === sidebar || 'placeholder' === sidebar;
|
|
16
|
+
const isLegacyPlaceholder = 'placeholder' === sidebar;
|
|
17
|
+
const { outline: showOutline = true, footer: showDocFooter = true, pageType, } = frontmatter;
|
|
18
|
+
const showSidebarMenu = showSidebar || (!isOverviewPage && showOutline);
|
|
19
|
+
const { isOutlineOpen, isSidebarOpen, sidebarMenu, asideLayoutRef, sidebarLayoutRef, } = useSidebarMenu();
|
|
20
|
+
const { rspressDocRef } = useWatchToc();
|
|
21
|
+
if (process.env.__SSR_MD__) {
|
|
22
|
+
return (_jsx(Fragment, { children: isOverviewPage ? (_jsx(Overview, { content: _jsx(DocContent, { components: components, isOverviewPage: true }) })) : (_jsx(DocContent, { components: components })) }));
|
|
23
|
+
}
|
|
24
|
+
const isDocWide = 'doc-wide' === pageType;
|
|
25
|
+
return (_jsxs(Fragment, { children: [showSidebarMenu && (_jsx("div", { className: "rp-doc-layout__menu", children: sidebarMenu })), beforeDoc, _jsxs("div", { className: clsx('rp-doc-layout__container', !showSidebarMenu && 'rp-doc-layout__container--no-menu'), children: [showSidebar ? (_jsxs("aside", { className: clsx('rp-doc-layout__sidebar', isSidebarOpen && 'rp-doc-layout__sidebar--open', 'rp-scrollbar'), ref: sidebarLayoutRef, children: [beforeSidebar, _jsx(DeferredSidebar, {}), afterSidebar] })) : showSidebarPlaceholder ? (_jsx("aside", { className: clsx('rp-doc-layout__sidebar-placeholder', isLegacyPlaceholder &&
|
|
26
|
+
'rp-doc-layout__sidebar-placeholder--legacy'), style: isDocWide ? { width: '0' } : {} })) : null, isOverviewPage ? (_jsx(Fragment, { children: _jsxs("main", { className: "rp-doc-layout__overview", children: [beforeDocContent, _jsx(Overview, { content: _jsx(DocContent, { components: components, isOverviewPage: true }) }), afterDocContent] }) })) : (_jsx("div", { className: clsx('rp-doc-layout__doc', isDocWide && 'rp-doc-layout__doc--wide'), children: _jsxs("main", { className: "rp-doc-layout__doc-container", children: [beforeDocContent, _jsx("div", { className: "rp-doc rspress-doc", ref: rspressDocRef, children: _jsx(DocContent, { components: components }) }), afterDocContent, beforeDocFooter, showDocFooter && _jsx(DocFooter, {}), afterDocFooter] }) })), isOverviewPage ? null : showOutline ? (_jsxs("aside", { className: clsx('rp-doc-layout__outline', isOutlineOpen && 'rp-doc-layout__outline--open', 'rp-scrollbar'), ref: asideLayoutRef, children: [beforeOutline, _jsx(Outline, {}), afterOutline] })) : (_jsx("aside", { className: "rp-doc-layout__outline-placeholder", style: isDocWide ? { width: '0' } : {} }))] }), afterDoc] }));
|
|
27
|
+
};
|
package/lib/theme/Layout.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useLang, useSite } from '@rspress/core/runtime';
|
|
3
|
-
import {
|
|
2
|
+
import { Content, Head, useFrontmatter, useHead, useLang, usePage, useSite, } from '@rspress/core/runtime';
|
|
3
|
+
import { HomeLayout, Nav, NotFoundLayout, useRedirect4FirstVisit, useScrollReset, useSetup, } from '@rspress/core/theme';
|
|
4
4
|
import virtual from 'doom-@global-virtual';
|
|
5
|
-
import { useCallback, useMemo, useState } from 'react';
|
|
5
|
+
import { createElement, Fragment, memo, useCallback, useMemo, useState, } from 'react';
|
|
6
6
|
import { useLocation } from 'react-router';
|
|
7
7
|
import { useTranslation } from "../runtime/index.js";
|
|
8
8
|
import { BuildInfoContext } from "../shared/context.js";
|
|
9
9
|
import { BreadCrumb } from "./Breadcrumb/index.js";
|
|
10
|
+
import { DocLayout } from "./DocLayout.js";
|
|
10
11
|
import { ForceRenderContext } from "./VersionsNav/context.js";
|
|
11
12
|
import { VersionsNav } from "./VersionsNav/index.js";
|
|
12
13
|
import { X } from "./_X.js";
|
|
@@ -58,6 +59,97 @@ const getClosestSidebar = (sidebarItems, pathname) => {
|
|
|
58
59
|
}
|
|
59
60
|
return found;
|
|
60
61
|
};
|
|
62
|
+
const concatTitle = (title, suffix) => {
|
|
63
|
+
if (!suffix)
|
|
64
|
+
return title;
|
|
65
|
+
title = title.trim();
|
|
66
|
+
suffix = suffix.trim();
|
|
67
|
+
if (!suffix.startsWith('-') && !suffix.startsWith('|')) {
|
|
68
|
+
return `${title} - ${suffix}`;
|
|
69
|
+
}
|
|
70
|
+
return `${title} ${suffix}`;
|
|
71
|
+
};
|
|
72
|
+
const HeadTags = memo(({ lang, frontmatter, description, title }) => {
|
|
73
|
+
const head = frontmatter.head;
|
|
74
|
+
const frontmatterTags = useMemo(() => head?.map(([tagName, attrs]) => tagName ? createElement(tagName, attrs) : null), [head]);
|
|
75
|
+
const frontmatterMetaProperties = useMemo(() => {
|
|
76
|
+
const properties = new Set();
|
|
77
|
+
for (const [tagName, attrs] of head || []) {
|
|
78
|
+
if (tagName === 'meta' && attrs.property) {
|
|
79
|
+
properties.add(attrs.property);
|
|
80
|
+
}
|
|
81
|
+
if (tagName === 'meta' && attrs.name) {
|
|
82
|
+
properties.add(attrs.name);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return properties;
|
|
86
|
+
}, [head]);
|
|
87
|
+
useHead({
|
|
88
|
+
htmlAttrs: { lang: lang || 'en' },
|
|
89
|
+
title: title || undefined,
|
|
90
|
+
meta: [
|
|
91
|
+
frontmatterMetaProperties.has('og:type')
|
|
92
|
+
? undefined
|
|
93
|
+
: { property: 'og:type', content: 'website' },
|
|
94
|
+
title && !frontmatterMetaProperties.has('og:title')
|
|
95
|
+
? { property: 'og:title', content: title }
|
|
96
|
+
: undefined,
|
|
97
|
+
...(description
|
|
98
|
+
? [
|
|
99
|
+
frontmatterMetaProperties.has('description')
|
|
100
|
+
? undefined
|
|
101
|
+
: { name: 'description', content: description },
|
|
102
|
+
frontmatterMetaProperties.has('og:description')
|
|
103
|
+
? undefined
|
|
104
|
+
: { property: 'og:description', content: description },
|
|
105
|
+
]
|
|
106
|
+
: []),
|
|
107
|
+
],
|
|
108
|
+
});
|
|
109
|
+
return _jsx(Head, { children: frontmatterTags ?? null });
|
|
110
|
+
});
|
|
111
|
+
const CoreLayout = ({ afterNav, beforeOutline }) => {
|
|
112
|
+
const { page } = usePage();
|
|
113
|
+
const { site } = useSite();
|
|
114
|
+
const { frontmatter } = useFrontmatter();
|
|
115
|
+
const { pageType, lang: currentLang, title: articleTitle } = page;
|
|
116
|
+
const localeData = site.themeConfig.locales.find((locale) => locale.lang === currentLang);
|
|
117
|
+
let title = frontmatter.title ?? articleTitle;
|
|
118
|
+
const mainTitle = site.title || localeData?.title || '';
|
|
119
|
+
title =
|
|
120
|
+
pageType === 'doc' || pageType === 'doc-wide'
|
|
121
|
+
? concatTitle(title || '', frontmatter.titleSuffix || mainTitle)
|
|
122
|
+
: pageType === 'home'
|
|
123
|
+
? concatTitle(mainTitle, frontmatter.titleSuffix)
|
|
124
|
+
: pageType === '404'
|
|
125
|
+
? concatTitle('404', mainTitle)
|
|
126
|
+
: mainTitle;
|
|
127
|
+
const description = page.description || site.description || localeData?.description;
|
|
128
|
+
useSetup();
|
|
129
|
+
useScrollReset();
|
|
130
|
+
useRedirect4FirstVisit();
|
|
131
|
+
const { navbar: showNavbar = true } = frontmatter;
|
|
132
|
+
const getContentLayout = () => {
|
|
133
|
+
switch (pageType) {
|
|
134
|
+
case 'home':
|
|
135
|
+
return _jsx(HomeLayout, {});
|
|
136
|
+
case 'doc':
|
|
137
|
+
case 'doc-wide':
|
|
138
|
+
return _jsx(DocLayout, { beforeOutline: beforeOutline });
|
|
139
|
+
case '404':
|
|
140
|
+
return _jsx(NotFoundLayout, {});
|
|
141
|
+
case 'custom':
|
|
142
|
+
case 'blank':
|
|
143
|
+
return _jsx(Content, {});
|
|
144
|
+
default:
|
|
145
|
+
return _jsx(DocLayout, { beforeOutline: beforeOutline });
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
if (process.env.__SSR_MD__) {
|
|
149
|
+
return _jsx(Fragment, { children: getContentLayout() });
|
|
150
|
+
}
|
|
151
|
+
return (_jsxs(Fragment, { children: [_jsx(HeadTags, { lang: currentLang, title: title, description: description, frontmatter: frontmatter }), 'blank' !== pageType && showNavbar && (_jsxs(Fragment, { children: [_jsx(Nav, {}), afterNav] })), getContentLayout()] }));
|
|
152
|
+
};
|
|
61
153
|
export const Layout = () => {
|
|
62
154
|
const { site: { lang: siteLang, themeConfig }, } = useSite();
|
|
63
155
|
const lang = useLang();
|
|
@@ -85,8 +177,8 @@ export const Layout = () => {
|
|
|
85
177
|
const pdfLink = useMemo(() => found && `/${found.exportItem.name ?? found.sidebar.text}-${lang}.pdf`, [found, lang]);
|
|
86
178
|
const [render, setRender] = useState(false);
|
|
87
179
|
const forceRender = useCallback(() => {
|
|
88
|
-
setRender((
|
|
180
|
+
setRender((value) => !value);
|
|
89
181
|
}, []);
|
|
90
182
|
const [buildInfoGroups, setBuildInfoGroups] = useState([]);
|
|
91
|
-
return (_jsxs(ForceRenderContext, { value: useMemo(() => ({ value: render, setValue: forceRender }), [forceRender, render]), children: [_jsx(VersionsNav, {}), _jsx(BuildInfoContext, { value: useMemo(() => ({ groups: buildInfoGroups, setGroups: setBuildInfoGroups }), [buildInfoGroups, setBuildInfoGroups]), children: _jsx(
|
|
183
|
+
return (_jsxs(ForceRenderContext, { value: useMemo(() => ({ value: render, setValue: forceRender }), [forceRender, render]), children: [_jsx(VersionsNav, {}), _jsx(BuildInfoContext, { value: useMemo(() => ({ groups: buildInfoGroups, setGroups: setBuildInfoGroups }), [buildInfoGroups, setBuildInfoGroups]), children: _jsx(CoreLayout, { afterNav: _jsx(BreadCrumb, {}), beforeOutline: pdfLink && (_jsx(X.p, { className: "rp-doc", style: { marginBottom: 16 }, children: _jsx(X.a, { href: pdfLink, target: "_blank", rel: "noopener noreferrer", children: t('view_docs_as_pdf') }) })) }) })] }));
|
|
92
184
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Match Rspress's initial sidebar expansion without mutating the shared payload.
|
|
2
|
+
export const expandActiveSidebar = (sidebar, activeMatcher) => {
|
|
3
|
+
const result = structuredClone(sidebar);
|
|
4
|
+
const visit = (item) => {
|
|
5
|
+
const activeChild = 'items' in item && item.items.map(visit).some(Boolean);
|
|
6
|
+
const active = activeChild || ('link' in item && !!item.link && activeMatcher(item.link));
|
|
7
|
+
if ('items' in item && active) {
|
|
8
|
+
item.collapsed = false;
|
|
9
|
+
}
|
|
10
|
+
return active;
|
|
11
|
+
};
|
|
12
|
+
result.forEach(visit);
|
|
13
|
+
return result;
|
|
14
|
+
};
|