@rspress/core 2.0.0-rc.3 → 2.0.0-rc.5
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/{0~7675.723d12b2.js → 0~7675.4468968d.js} +4 -4
- package/dist/1820.985588a0.js +5 -0
- package/dist/{3521.56e42d70.js → 5341.9eb6b114.js} +12 -12
- package/dist/{5485.077f14ff.js → 5485.b9ce9bdf.js} +5 -5
- package/dist/6289.29817fb4.js +4 -0
- package/dist/8813.4b28f4bb.js +1 -0
- package/dist/_private/react/render.js +1 -1
- package/dist/cli/config/loadConfigFile.js +2 -2
- package/dist/cli/index.44f3184c.js +2 -0
- package/dist/cli/index.js +3 -3
- package/dist/eject-theme/components/CodeBlock/index.tsx +1 -1
- package/dist/eject-theme/components/FallbackHeading/index.tsx +4 -0
- package/dist/eject-theme/components/HomeBackground/useNavTransparent.tsx +2 -1
- package/dist/eject-theme/components/HomeHero/index.scss +8 -0
- package/dist/eject-theme/components/HomeHero/index.tsx +11 -2
- package/dist/eject-theme/components/HoverGroup/index.scss +3 -2
- package/dist/eject-theme/components/HoverGroup/index.tsx +104 -22
- package/dist/eject-theme/components/Nav/NavMenu.scss +1 -1
- package/dist/eject-theme/components/Nav/NavMenu.tsx +50 -28
- package/dist/eject-theme/components/NavScreen/NavScreenMenuItem.tsx +26 -11
- package/dist/eject-theme/components/Overview/index.tsx +11 -0
- package/dist/eject-theme/components/OverviewGroup/index.tsx +46 -0
- package/dist/eject-theme/components/Steps/index.scss +1 -0
- package/dist/index.js +6 -11
- package/dist/node/PluginDriver.js +3 -3
- package/dist/node/auto-nav-sidebar/index.js +3 -3
- package/dist/node/auto-nav-sidebar/locales.js +1 -1
- package/dist/node/auto-nav-sidebar/normalize.js +2 -2
- package/dist/node/auto-nav-sidebar/utils.js +2 -2
- package/dist/node/auto-nav-sidebar/walk.js +13 -9
- package/dist/node/constants.js +2 -2
- package/dist/node/eject.js +2 -5
- package/dist/node/initRsbuild.js +14 -9
- package/dist/node/last-updated/index.js +1 -1
- package/dist/node/logger/hint.js +1 -1
- package/dist/node/mdx/loader.7c249a26.js +12 -0
- package/dist/node/mdx/options.js +3 -3
- package/dist/node/mdx/processor.js +2 -2
- package/dist/node/mdx/rehypePlugins/codeMeta.js +1 -1
- package/dist/node/mdx/rehypePlugins/headerAnchor.js +4 -4
- package/dist/node/mdx/rehypePlugins/shiki.js +1 -1
- package/dist/node/mdx/rehypePlugins/transformers/index.js +5 -5
- package/dist/node/mdx/remarkPlugins/builtin.js +1 -1
- package/dist/node/mdx/remarkPlugins/containerSyntax.js +1 -1
- package/dist/node/mdx/remarkPlugins/fileCodeBlock.js +3 -3
- package/dist/node/mdx/remarkPlugins/image.js +2 -3
- package/dist/node/mdx/remarkPlugins/link.js +3 -3
- package/dist/node/mdx/remarkPlugins/toc.js +3 -3
- package/dist/node/medium-zoom/index.js +1 -1
- package/dist/node/route/RoutePage.js +1 -1
- package/dist/node/route/RouteService.js +3 -3
- package/dist/node/route/extractPageData.js +4 -5
- package/dist/node/route/normalizeRoutePath.js +2 -2
- package/dist/node/runtimeModule/i18n.js +2 -2
- package/dist/node/runtimeModule/pageData/createPageData.js +2 -2
- package/dist/node/runtimeModule/pageData/rsbuildPlugin.js +1 -1
- package/dist/node/runtimeModule/searchHooks.js +1 -1
- package/dist/node/runtimeModule/siteData/createSiteData.js +1 -1
- package/dist/node/runtimeModule/siteData/normalizeThemeConfig.js +1 -1
- package/dist/node/runtimeModule/siteData/rsbuildPlugin.js +1 -1
- package/dist/node/serve.js +1 -1
- package/dist/node/ssg/renderPage.js +2 -2
- package/dist/node/ssg/renderPageWorker.165b5d46.js +1 -0
- package/dist/node/ssg/renderPageWorker.js +2 -2
- package/dist/node/ssg/renderPages.js +3 -3
- package/dist/node/ssg/rsbuildPluginSSG.js +2 -2
- package/dist/node/ssg/ssgEnv.js +1 -1
- package/dist/node/ssg-md/llms/emitLlmsTxt.js +9 -7
- package/dist/node/ssg-md/llms/llmsTxt.js +2 -2
- package/dist/node/ssg-md/remarkSplitMdx.js +2 -2
- package/dist/node/ssg-md/renderPage.js +2 -2
- package/dist/node/ssg-md/renderPages.js +2 -2
- package/dist/node/ssg-md/rsbuildPluginSSGMD.js +2 -2
- package/dist/node/utils/checkLanguageParity.js +1 -1
- package/dist/node/utils/createHash.js +1 -1
- package/dist/node/utils/error.js +1 -1
- package/dist/node/utils/flattenMdxContent.js +4 -5
- package/dist/node/utils/fs.js +1 -1
- package/dist/node/utils/getPageKey.js +1 -1
- package/dist/node/utils/normalizePath.js +3 -3
- package/dist/node/utils/reactAlias.js +2 -2
- package/dist/runtime/App.js +4 -2
- package/dist/runtime/Content.d.ts +4 -0
- package/dist/runtime/Content.js +18 -0
- package/dist/runtime/NoSSR.d.ts +3 -0
- package/dist/runtime/NoSSR.js +14 -0
- package/dist/runtime/getSidebarDataGroup.test.js +68 -0
- package/dist/runtime/hooks/useActiveMatcher.d.ts +1 -0
- package/dist/runtime/hooks/useActiveMatcher.js +14 -0
- package/dist/runtime/hooks/useDark.d.ts +7 -0
- package/dist/runtime/hooks/useDark.js +7 -0
- package/dist/runtime/hooks/useFrontmatter.d.ts +4 -0
- package/dist/runtime/hooks/useFrontmatter.js +9 -0
- package/dist/runtime/hooks/useI18n.d.ts +2 -0
- package/dist/runtime/hooks/useI18n.js +14 -0
- package/dist/runtime/hooks/useLang.d.ts +1 -0
- package/dist/runtime/hooks/useLang.js +6 -0
- package/dist/runtime/hooks/useLocaleSiteData.d.ts +18 -0
- package/dist/runtime/hooks/useLocaleSiteData.js +20 -0
- package/dist/runtime/hooks/useNav.d.ts +1 -0
- package/dist/runtime/hooks/useNav.js +12 -0
- package/dist/runtime/hooks/usePage.d.ts +10 -0
- package/dist/runtime/hooks/usePage.js +9 -0
- package/dist/runtime/hooks/usePageData.d.ts +5 -0
- package/dist/runtime/hooks/usePageData.js +16 -0
- package/dist/runtime/hooks/usePages.d.ts +4 -0
- package/dist/runtime/hooks/usePages.js +7 -0
- package/dist/runtime/hooks/useSidebar.d.ts +16 -0
- package/dist/runtime/hooks/useSidebar.js +67 -0
- package/dist/runtime/hooks/useSite.d.ts +4 -0
- package/dist/runtime/hooks/useSite.js +7 -0
- package/dist/runtime/hooks/useVersion.d.ts +1 -0
- package/dist/runtime/hooks/useVersion.js +6 -0
- package/dist/runtime/hooks/useWindowSize.d.ts +9 -0
- package/dist/runtime/hooks/useWindowSize.js +31 -0
- package/dist/runtime/hooks/useWindowSize.test.js +36 -0
- package/dist/runtime/index.d.ts +21 -0
- package/dist/runtime/index.js +22 -0
- package/dist/runtime/route.d.ts +36 -0
- package/dist/runtime/route.js +53 -0
- package/dist/runtime/route.test.js +154 -0
- package/dist/runtime/ssrMdServerEntry.js +1 -1
- package/dist/runtime/utils.d.ts +12 -0
- package/dist/runtime/utils.js +34 -0
- package/dist/runtime/utils.test.js +34 -0
- package/dist/shiki-transformers.js +2 -2
- package/dist/theme/components/CodeBlock/index.js +1 -1
- package/dist/theme/components/FallbackHeading/index.js +4 -1
- package/dist/theme/components/HomeBackground/useNavTransparent.js +1 -1
- package/dist/theme/components/HomeHero/index.css +6 -0
- package/dist/theme/components/HomeHero/index.js +10 -3
- package/dist/theme/components/HoverGroup/index.css +3 -2
- package/dist/theme/components/HoverGroup/index.js +88 -17
- package/dist/theme/components/Nav/NavMenu.css +1 -1
- package/dist/theme/components/Nav/NavMenu.d.ts +6 -1
- package/dist/theme/components/Nav/NavMenu.js +47 -45
- package/dist/theme/components/NavScreen/NavScreenMenuItem.d.ts +1 -1
- package/dist/theme/components/NavScreen/NavScreenMenuItem.js +21 -11
- package/dist/theme/components/Overview/index.js +12 -1
- package/dist/theme/components/OverviewGroup/index.js +33 -0
- package/dist/theme/components/Steps/index.css +1 -0
- package/dist/theme/index.d.ts +1 -1
- package/dist/theme/index.js +1 -1
- package/dist/theme/layout/DocLayout/index.js +10 -0
- package/dist/theme/layout/HomeLayout/index.js +40 -0
- package/dist/theme/layout/Layout/index.js +10 -6
- package/dist/theme/logic/useScrollAfterNav.js +2 -2
- package/dist/theme/logic/useScrollReset.js +2 -2
- package/dist/theme/logic/useStorageValue.js +6 -3
- package/dist/theme/logic/{useAppearance.d.ts → useThemeState.d.ts} +4 -1
- package/dist/theme/logic/useThemeState.js +50 -0
- package/dist/theme/styles/scrollbar.css +14 -2
- package/dist/theme/styles/vars/base-vars.css +2 -2
- package/dist/theme/styles/vars/code-vars.css +2 -2
- package/nav-json-schema.json +2 -2
- package/package.json +10 -11
- package/static/MediumZoom.tsx +1 -1
- package/dist/4669.12208c6a.js +0 -2
- package/dist/6289.c67fe0d5.js +0 -5
- package/dist/8813.9da1f210.js +0 -2
- package/dist/9643.f109822d.js +0 -5
- package/dist/cli/index.cb89dbd1.js +0 -3
- package/dist/node/mdx/loader.e1578c62.js +0 -13
- package/dist/node/ssg/renderPageWorker.474f9f73.js +0 -2
- package/dist/runtime.72d450ac.js +0 -1
- package/dist/runtime.d.ts +0 -2
- package/dist/runtime.js +0 -4
- package/dist/theme/logic/useAppearance.js +0 -64
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { node_path, logger, promises } from "../../6289.
|
|
1
|
+
import { node_path, logger, promises } from "../../6289.29817fb4.js";
|
|
2
2
|
import { createError } from "./error.js";
|
|
3
3
|
function normalizePath(filePath) {
|
|
4
4
|
return filePath.split(node_path.sep).join('/');
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createHash as external_node_crypto_createHash } from "../../
|
|
1
|
+
import { createHash as external_node_crypto_createHash } from "../../5341.9eb6b114.js";
|
|
2
2
|
function createHash(str) {
|
|
3
3
|
return external_node_crypto_createHash('sha256').update(str).digest('hex').slice(0, 8);
|
|
4
4
|
}
|
package/dist/node/utils/error.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { node_fs, core_rspack } from "../../
|
|
2
|
-
import { node_path, logger } from "../../6289.
|
|
3
|
-
import { createProcessor } from "../../
|
|
4
|
-
import { MDX_OR_MD_REGEXP } from "../../4669.12208c6a.js";
|
|
1
|
+
import { node_fs, core_rspack } from "../../5341.9eb6b114.js";
|
|
2
|
+
import { node_path, logger } from "../../6289.29817fb4.js";
|
|
3
|
+
import { createProcessor, MDX_OR_MD_REGEXP } from "../../1820.985588a0.js";
|
|
5
4
|
import { createError } from "./error.js";
|
|
6
5
|
import { importStatementRegex } from "../constants.js";
|
|
7
6
|
const RspackResolveFactory = core_rspack.experiments.resolver.ResolverFactory;
|
|
@@ -71,4 +70,4 @@ async function flattenMdxContent(content, basePath, alias) {
|
|
|
71
70
|
deps
|
|
72
71
|
};
|
|
73
72
|
}
|
|
74
|
-
export { flattenMdxContent
|
|
73
|
+
export { flattenMdxContent };
|
package/dist/node/utils/fs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { removeLeadingSlash } from "../../
|
|
1
|
+
import { removeLeadingSlash } from "../../1820.985588a0.js";
|
|
2
2
|
function getPageKey(route) {
|
|
3
3
|
const cleanRoute = removeLeadingSlash(route);
|
|
4
4
|
return cleanRoute.replace(/\//g, '_').replace(/\.[^.]+$/, '') || 'index';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { node_os } from "../../
|
|
2
|
-
import { node_path } from "../../6289.
|
|
1
|
+
import { node_os } from "../../1820.985588a0.js";
|
|
2
|
+
import { node_path } from "../../6289.29817fb4.js";
|
|
3
3
|
const isWindows = 'win32' === node_os.platform();
|
|
4
4
|
function slash(p) {
|
|
5
5
|
return p.replace(/\\/g, '/');
|
|
@@ -7,4 +7,4 @@ function slash(p) {
|
|
|
7
7
|
function normalizePath(id) {
|
|
8
8
|
return node_path.posix.normalize(isWindows ? slash(id) : id);
|
|
9
9
|
}
|
|
10
|
-
export {
|
|
10
|
+
export { normalizePath, slash };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { node_path, logger, picocolors } from "../../6289.
|
|
2
|
-
import { core_rspack } from "../../
|
|
1
|
+
import { node_path, logger, picocolors } from "../../6289.29817fb4.js";
|
|
2
|
+
import { core_rspack } from "../../5341.9eb6b114.js";
|
|
3
3
|
import { pathExists, readJson } from "./fs.js";
|
|
4
4
|
import { PACKAGE_ROOT } from "../constants.js";
|
|
5
5
|
import { hintReactVersion } from "../logger/hint.js";
|
package/dist/runtime/App.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
2
|
+
import { PageContext, useLocation } from "@rspress/core/runtime";
|
|
3
3
|
import { Layout, Root } from "@theme";
|
|
4
4
|
import react, { useContext, useLayoutEffect } from "react";
|
|
5
5
|
import virtual_global_components from "virtual-global-components";
|
|
@@ -26,7 +26,9 @@ function App() {
|
|
|
26
26
|
const GLOBAL_COMPONENTS_KEY = 'globalUIComponents';
|
|
27
27
|
const query = new URLSearchParams(search);
|
|
28
28
|
const hideGlobalUIComponents = false === frontmatter[GLOBAL_COMPONENTS_KEY] || "0" === query.get(GLOBAL_COMPONENTS_KEY);
|
|
29
|
-
if (process.env.__SSR_MD__) return /*#__PURE__*/ jsx(
|
|
29
|
+
if (process.env.__SSR_MD__) return /*#__PURE__*/ jsx(Root, {
|
|
30
|
+
children: /*#__PURE__*/ jsx(Layout, {})
|
|
31
|
+
});
|
|
30
32
|
return /*#__PURE__*/ jsxs(Root, {
|
|
31
33
|
children: [
|
|
32
34
|
/*#__PURE__*/ jsx(Layout, {}),
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Suspense, useMemo } from "react";
|
|
3
|
+
import { useLocation } from "react-router-dom";
|
|
4
|
+
import { pathnameToRouteService } from "./route.js";
|
|
5
|
+
const Content = ({ fallback = /*#__PURE__*/ jsx(Fragment, {}) })=>{
|
|
6
|
+
const { pathname } = useLocation();
|
|
7
|
+
const matchedElement = useMemo(()=>{
|
|
8
|
+
const route = pathnameToRouteService(pathname);
|
|
9
|
+
return route?.element;
|
|
10
|
+
}, [
|
|
11
|
+
pathname
|
|
12
|
+
]);
|
|
13
|
+
return /*#__PURE__*/ jsx(Suspense, {
|
|
14
|
+
fallback: fallback,
|
|
15
|
+
children: matchedElement
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
export { Content };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
function NoSSR(props) {
|
|
4
|
+
const { children } = props;
|
|
5
|
+
const [isMounted, setIsMounted] = useState(false);
|
|
6
|
+
useEffect(()=>{
|
|
7
|
+
setIsMounted(true);
|
|
8
|
+
}, []);
|
|
9
|
+
if (!isMounted) return null;
|
|
10
|
+
return /*#__PURE__*/ jsx(Fragment, {
|
|
11
|
+
children: children
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export { NoSSR };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, expect, it, rs } from "@rstest/core";
|
|
2
|
+
import { getSidebarDataGroup } from "./hooks/useSidebar.js";
|
|
3
|
+
import { isActive } from "./route.js";
|
|
4
|
+
rs.mock('virtual-i18n-text', ()=>({
|
|
5
|
+
default: {}
|
|
6
|
+
}));
|
|
7
|
+
rs.mock('virtual-site-data', ()=>({
|
|
8
|
+
default: {
|
|
9
|
+
base: '/'
|
|
10
|
+
}
|
|
11
|
+
}));
|
|
12
|
+
rs.mock('virtual-routes', ()=>({
|
|
13
|
+
routes: []
|
|
14
|
+
}));
|
|
15
|
+
describe('getSidebarDataGroup', ()=>{
|
|
16
|
+
it('when using "/"', async ()=>{
|
|
17
|
+
expect(getSidebarDataGroup({
|
|
18
|
+
'/': [],
|
|
19
|
+
'/guide': [
|
|
20
|
+
{
|
|
21
|
+
text: 'Getting Started',
|
|
22
|
+
link: '/guide/getting-started'
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}, '/guide/getting-started')).toMatchInlineSnapshot(`
|
|
26
|
+
[
|
|
27
|
+
{
|
|
28
|
+
"link": "/guide/getting-started",
|
|
29
|
+
"text": "Getting Started",
|
|
30
|
+
},
|
|
31
|
+
]
|
|
32
|
+
`);
|
|
33
|
+
});
|
|
34
|
+
it('when using api-extractor', async ()=>{
|
|
35
|
+
expect(getSidebarDataGroup({
|
|
36
|
+
'/api/react': [
|
|
37
|
+
{
|
|
38
|
+
link: '/api/react.use',
|
|
39
|
+
text: 'react.use'
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
}, '/api/react.use.html')).toMatchInlineSnapshot(`
|
|
43
|
+
[
|
|
44
|
+
{
|
|
45
|
+
"link": "/api/react.use",
|
|
46
|
+
"text": "react.use",
|
|
47
|
+
},
|
|
48
|
+
]
|
|
49
|
+
`);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
describe('isActive', ()=>{
|
|
53
|
+
it('pass cases', ()=>{
|
|
54
|
+
const routes = [
|
|
55
|
+
'/api/config',
|
|
56
|
+
'/api/config.html',
|
|
57
|
+
'/api/config/',
|
|
58
|
+
'/api/config/index',
|
|
59
|
+
'/api/config/index.html'
|
|
60
|
+
];
|
|
61
|
+
for (const route of routes)for (const route2 of routes)expect(isActive(route, route2)).toBeTruthy();
|
|
62
|
+
});
|
|
63
|
+
it('failed cases', ()=>{
|
|
64
|
+
expect(isActive('/api/config', '/api/config2')).toBeFalsy();
|
|
65
|
+
expect(isActive('/api/config', '/api/config/config2')).toBeFalsy();
|
|
66
|
+
expect(isActive('/api/config/index', '/api/config/config2')).toBeFalsy();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useActiveMatcher: () => (link: string) => boolean;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import { useLocation } from "react-router-dom";
|
|
3
|
+
import { isActive } from "../route.js";
|
|
4
|
+
const useActiveMatcher = ()=>{
|
|
5
|
+
const { pathname: rawPathname } = useLocation();
|
|
6
|
+
const activeMatcher = useCallback((link)=>{
|
|
7
|
+
const pathname = decodeURIComponent(rawPathname);
|
|
8
|
+
return isActive(link, pathname);
|
|
9
|
+
}, [
|
|
10
|
+
rawPathname
|
|
11
|
+
]);
|
|
12
|
+
return activeMatcher;
|
|
13
|
+
};
|
|
14
|
+
export { useActiveMatcher };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
import virtual_i18n_text from "virtual-i18n-text";
|
|
3
|
+
import { useLang } from "./useLang.js";
|
|
4
|
+
function useI18n() {
|
|
5
|
+
const lang = useLang();
|
|
6
|
+
return useCallback((key)=>{
|
|
7
|
+
const text = virtual_i18n_text[key]?.[lang];
|
|
8
|
+
if ('string' != typeof text) throw new Error(`i18n key "${key}" not found for language "${lang}"`);
|
|
9
|
+
return text;
|
|
10
|
+
}, [
|
|
11
|
+
lang
|
|
12
|
+
]);
|
|
13
|
+
}
|
|
14
|
+
export { useI18n };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useLang(): string;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type NormalizedLocales } from '@rspress/shared';
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated should use useSite and useLang instead
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* const site = useSite();
|
|
7
|
+
* const lang = useLang();
|
|
8
|
+
* const locales = site.themeConfig.locales;
|
|
9
|
+
* const localeInfo = locales?.find((locale) => locale.lang === lang);
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* For i18n text:
|
|
13
|
+
* ```ts
|
|
14
|
+
* const t = useI18n();
|
|
15
|
+
* <div>{t('outlineTitle')}</div>;
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function useLocaleSiteData(): NormalizedLocales;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { addTrailingSlash } from "@rspress/shared";
|
|
2
|
+
import { useLang } from "./useLang.js";
|
|
3
|
+
import { useSite } from "./useSite.js";
|
|
4
|
+
function useLocaleSiteData() {
|
|
5
|
+
const { site } = useSite();
|
|
6
|
+
const lang = useLang();
|
|
7
|
+
const themeConfig = site?.themeConfig ?? {};
|
|
8
|
+
const defaultLang = site.lang ?? '';
|
|
9
|
+
const locales = themeConfig?.locales;
|
|
10
|
+
if (!locales || 0 === locales.length) return {
|
|
11
|
+
nav: themeConfig.nav,
|
|
12
|
+
sidebar: themeConfig.sidebar
|
|
13
|
+
};
|
|
14
|
+
const localeInfo = locales.find((locale)=>locale.lang === lang);
|
|
15
|
+
return {
|
|
16
|
+
...localeInfo,
|
|
17
|
+
langRoutePrefix: lang === defaultLang ? '/' : addTrailingSlash(lang)
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export { useLocaleSiteData };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useNav(): import("@rspress/shared").NavItem[];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useLocaleSiteData } from "./useLocaleSiteData.js";
|
|
2
|
+
import { useVersion } from "./useVersion.js";
|
|
3
|
+
function useNav() {
|
|
4
|
+
const { nav } = useLocaleSiteData();
|
|
5
|
+
const version = useVersion();
|
|
6
|
+
if (Array.isArray(nav)) return nav;
|
|
7
|
+
const navKey = version.length > 0 ? version : 'default';
|
|
8
|
+
return [
|
|
9
|
+
...nav[navKey]
|
|
10
|
+
];
|
|
11
|
+
}
|
|
12
|
+
export { useNav };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { PageDataLegacy } from '@rspress/shared';
|
|
2
|
+
interface IPageContext {
|
|
3
|
+
data: PageDataLegacy['page'];
|
|
4
|
+
setData?: (data: PageDataLegacy['page']) => void;
|
|
5
|
+
}
|
|
6
|
+
export declare const PageContext: import("react").Context<IPageContext>;
|
|
7
|
+
export declare function usePage(): {
|
|
8
|
+
page: PageDataLegacy['page'];
|
|
9
|
+
};
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { usePage } from "./usePage.js";
|
|
2
|
+
import { usePages } from "./usePages.js";
|
|
3
|
+
import { useSite } from "./useSite.js";
|
|
4
|
+
function usePageData() {
|
|
5
|
+
const { page } = usePage();
|
|
6
|
+
const { pages } = usePages();
|
|
7
|
+
const { site } = useSite();
|
|
8
|
+
return {
|
|
9
|
+
page,
|
|
10
|
+
siteData: {
|
|
11
|
+
...site,
|
|
12
|
+
pages
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export { usePageData };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type NormalizedSidebar, type SidebarData } from '@rspress/shared';
|
|
2
|
+
/**
|
|
3
|
+
* get the sidebar group for the current page
|
|
4
|
+
* @param sidebar const { sidebar } = useLocaleSiteData();
|
|
5
|
+
* @param currentPathname
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export declare const getSidebarDataGroup: (sidebar: NormalizedSidebar, currentPathname: string) => SidebarData;
|
|
9
|
+
export declare function useSidebar(): SidebarData;
|
|
10
|
+
/**
|
|
11
|
+
* handle the collapsed state of the sidebar groups
|
|
12
|
+
*/
|
|
13
|
+
export declare function useSidebarDynamic(): [
|
|
14
|
+
SidebarData,
|
|
15
|
+
React.Dispatch<React.SetStateAction<SidebarData>>
|
|
16
|
+
];
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { matchSidebar } from "@rspress/shared";
|
|
2
|
+
import { useLayoutEffect, useMemo, useState } from "react";
|
|
3
|
+
import { useLocation } from "react-router-dom";
|
|
4
|
+
import { useActiveMatcher } from "./useActiveMatcher.js";
|
|
5
|
+
import { useLocaleSiteData } from "./useLocaleSiteData.js";
|
|
6
|
+
const getSidebarDataGroup = (sidebar, currentPathname)=>{
|
|
7
|
+
const navRoutes = Object.keys(sidebar).sort((a, b)=>b.length - a.length);
|
|
8
|
+
for (const name of navRoutes)if (matchSidebar(name, currentPathname)) {
|
|
9
|
+
const sidebarGroup = sidebar[name];
|
|
10
|
+
return sidebarGroup;
|
|
11
|
+
}
|
|
12
|
+
return [];
|
|
13
|
+
};
|
|
14
|
+
function useSidebar() {
|
|
15
|
+
const { sidebar } = useLocaleSiteData();
|
|
16
|
+
const { pathname: rawPathname } = useLocation();
|
|
17
|
+
const pathname = decodeURIComponent(rawPathname);
|
|
18
|
+
const sidebarData = useMemo(()=>getSidebarDataGroup(sidebar, pathname), [
|
|
19
|
+
sidebar,
|
|
20
|
+
pathname
|
|
21
|
+
]);
|
|
22
|
+
return sidebarData;
|
|
23
|
+
}
|
|
24
|
+
function createInitialSidebar(rawSidebarData, activeMatcher) {
|
|
25
|
+
const matchCache = new WeakMap();
|
|
26
|
+
const match = (item)=>{
|
|
27
|
+
if (matchCache.has(item)) return matchCache.get(item);
|
|
28
|
+
if ('link' in item && item.link && activeMatcher(item.link)) {
|
|
29
|
+
matchCache.set(item, true);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
if ('items' in item) {
|
|
33
|
+
const result = item.items.some((child)=>match(child));
|
|
34
|
+
if (result) {
|
|
35
|
+
matchCache.set(item, true);
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
matchCache.set(item, false);
|
|
40
|
+
return false;
|
|
41
|
+
};
|
|
42
|
+
const traverse = (item)=>{
|
|
43
|
+
if ('items' in item) {
|
|
44
|
+
item.items.forEach(traverse);
|
|
45
|
+
if (match(item)) item.collapsed = false;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const newSidebarData = rawSidebarData.filter(Boolean).flat();
|
|
49
|
+
newSidebarData.forEach(traverse);
|
|
50
|
+
return newSidebarData;
|
|
51
|
+
}
|
|
52
|
+
function useSidebarDynamic() {
|
|
53
|
+
const rawSidebarData = useSidebar();
|
|
54
|
+
const activeMatcher = useActiveMatcher();
|
|
55
|
+
const [sidebar, setSidebar] = useState(()=>createInitialSidebar(structuredClone(rawSidebarData), activeMatcher));
|
|
56
|
+
useLayoutEffect(()=>{
|
|
57
|
+
setSidebar(createInitialSidebar(rawSidebarData, activeMatcher));
|
|
58
|
+
}, [
|
|
59
|
+
activeMatcher,
|
|
60
|
+
rawSidebarData
|
|
61
|
+
]);
|
|
62
|
+
return [
|
|
63
|
+
sidebar,
|
|
64
|
+
setSidebar
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
export { getSidebarDataGroup, useSidebar, useSidebarDynamic };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useVersion(): string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useLayoutEffect, useState } from "react";
|
|
2
|
+
const RESIZE_DEBOUNCE_MS = 150;
|
|
3
|
+
function useWindowSize(initialWidth, initialHeight) {
|
|
4
|
+
const [size, setSize] = useState({
|
|
5
|
+
width: initialWidth ?? 1 / 0,
|
|
6
|
+
height: initialHeight ?? 1 / 0
|
|
7
|
+
});
|
|
8
|
+
useLayoutEffect(()=>{
|
|
9
|
+
let timeoutId = null;
|
|
10
|
+
const handleResize = ()=>{
|
|
11
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
12
|
+
timeoutId = setTimeout(()=>{
|
|
13
|
+
setSize({
|
|
14
|
+
width: window.innerWidth,
|
|
15
|
+
height: window.innerHeight
|
|
16
|
+
});
|
|
17
|
+
}, RESIZE_DEBOUNCE_MS);
|
|
18
|
+
};
|
|
19
|
+
setSize({
|
|
20
|
+
width: window.innerWidth,
|
|
21
|
+
height: window.innerHeight
|
|
22
|
+
});
|
|
23
|
+
window.addEventListener('resize', handleResize);
|
|
24
|
+
return ()=>{
|
|
25
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
26
|
+
window.removeEventListener('resize', handleResize);
|
|
27
|
+
};
|
|
28
|
+
}, []);
|
|
29
|
+
return size;
|
|
30
|
+
}
|
|
31
|
+
export { useWindowSize };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { describe, expect, it } from "@rstest/core";
|
|
4
|
+
describe('useWindowSize', ()=>{
|
|
5
|
+
it('should have debounce implementation with RESIZE_DEBOUNCE_MS constant', ()=>{
|
|
6
|
+
const filePath = join(__dirname, 'useWindowSize.ts');
|
|
7
|
+
const fileContent = readFileSync(filePath, 'utf-8');
|
|
8
|
+
expect(fileContent).toContain('const RESIZE_DEBOUNCE_MS = 150');
|
|
9
|
+
expect(fileContent).toContain('setTimeout');
|
|
10
|
+
expect(fileContent).toContain('RESIZE_DEBOUNCE_MS');
|
|
11
|
+
});
|
|
12
|
+
it('should clear timeout with conditional check', ()=>{
|
|
13
|
+
const filePath = join(__dirname, 'useWindowSize.ts');
|
|
14
|
+
const fileContent = readFileSync(filePath, 'utf-8');
|
|
15
|
+
expect(fileContent).toContain('if (timeoutId) clearTimeout(timeoutId)');
|
|
16
|
+
expect(fileContent).not.toContain('clearTimeout(timeoutId!)');
|
|
17
|
+
});
|
|
18
|
+
it('should register and cleanup resize event listener', ()=>{
|
|
19
|
+
const filePath = join(__dirname, 'useWindowSize.ts');
|
|
20
|
+
const fileContent = readFileSync(filePath, 'utf-8');
|
|
21
|
+
expect(fileContent).toContain("addEventListener('resize'");
|
|
22
|
+
expect(fileContent).toContain("removeEventListener('resize'");
|
|
23
|
+
});
|
|
24
|
+
it('should set initial size immediately', ()=>{
|
|
25
|
+
const filePath = join(__dirname, 'useWindowSize.ts');
|
|
26
|
+
const fileContent = readFileSync(filePath, 'utf-8');
|
|
27
|
+
const setStateMatch = fileContent.match(/setSize\(\{[^}]+\}\)/g);
|
|
28
|
+
expect(setStateMatch).toBeTruthy();
|
|
29
|
+
expect(setStateMatch?.length).toBeGreaterThanOrEqual(2);
|
|
30
|
+
});
|
|
31
|
+
it('should export useWindowSize function', async ()=>{
|
|
32
|
+
const module = await import("./useWindowSize.js");
|
|
33
|
+
expect(module.useWindowSize).toBeDefined();
|
|
34
|
+
expect(typeof module.useWindowSize).toBe('function');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export { isDataUrl, isExternalUrl, matchNavbar, matchSidebar, normalizeHref, } from '@rspress/shared';
|
|
2
|
+
export { Head } from '@unhead/react';
|
|
3
|
+
export * from 'react-router-dom';
|
|
4
|
+
export { Content } from './Content.js';
|
|
5
|
+
export { useActiveMatcher } from './hooks/useActiveMatcher.js';
|
|
6
|
+
export { ThemeContext, useDark } from './hooks/useDark.js';
|
|
7
|
+
export { useFrontmatter } from './hooks/useFrontmatter.js';
|
|
8
|
+
export { useI18n } from './hooks/useI18n.js';
|
|
9
|
+
export { useLang } from './hooks/useLang.js';
|
|
10
|
+
export { useLocaleSiteData } from './hooks/useLocaleSiteData.js';
|
|
11
|
+
export { useNav } from './hooks/useNav.js';
|
|
12
|
+
export { PageContext, usePage } from './hooks/usePage.js';
|
|
13
|
+
export { usePageData } from './hooks/usePageData.js';
|
|
14
|
+
export { usePages } from './hooks/usePages.js';
|
|
15
|
+
export { getSidebarDataGroup, useSidebar, useSidebarDynamic, } from './hooks/useSidebar.js';
|
|
16
|
+
export { useSite } from './hooks/useSite.js';
|
|
17
|
+
export { useVersion } from './hooks/useVersion.js';
|
|
18
|
+
export { useWindowSize } from './hooks/useWindowSize.js';
|
|
19
|
+
export { NoSSR } from './NoSSR.js';
|
|
20
|
+
export { isActive, pathnameToRouteService, preloadLink } from './route.js';
|
|
21
|
+
export { addLeadingSlash, cleanUrlByConfig, isEqualPath, isProduction, normalizeHrefInRuntime, normalizeImagePath, removeBase, removeTrailingSlash, routePathToMdPath, withBase, } from './utils.js';
|