@rspress/core 2.0.0-rc.4 → 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.
Files changed (120) hide show
  1. package/dist/{0~7675.723d12b2.js → 0~7675.4468968d.js} +3 -3
  2. package/dist/{9643.2dff683e.js → 1820.985588a0.js} +1 -0
  3. package/dist/{5485.9fc7f63d.js → 5485.b9ce9bdf.js} +1 -1
  4. package/dist/_private/react/render.js +1 -1
  5. package/dist/cli/config/loadConfigFile.js +1 -1
  6. package/dist/cli/index.js +1 -1
  7. package/dist/eject-theme/components/CodeBlock/index.tsx +1 -1
  8. package/dist/eject-theme/components/FallbackHeading/index.tsx +4 -0
  9. package/dist/eject-theme/components/HomeBackground/useNavTransparent.tsx +2 -1
  10. package/dist/eject-theme/components/HomeHero/index.scss +8 -0
  11. package/dist/eject-theme/components/HomeHero/index.tsx +11 -2
  12. package/dist/eject-theme/components/Overview/index.tsx +8 -62
  13. package/dist/eject-theme/components/OverviewGroup/index.tsx +46 -0
  14. package/dist/eject-theme/components/Steps/index.scss +1 -0
  15. package/dist/index.js +2 -3
  16. package/dist/node/PluginDriver.js +1 -1
  17. package/dist/node/auto-nav-sidebar/index.js +1 -1
  18. package/dist/node/auto-nav-sidebar/normalize.js +1 -1
  19. package/dist/node/auto-nav-sidebar/utils.js +1 -1
  20. package/dist/node/auto-nav-sidebar/walk.js +1 -1
  21. package/dist/node/constants.js +1 -1
  22. package/dist/node/initRsbuild.js +4 -5
  23. package/dist/node/last-updated/index.js +1 -1
  24. package/dist/node/mdx/options.js +1 -1
  25. package/dist/node/mdx/processor.js +1 -1
  26. package/dist/node/mdx/rehypePlugins/headerAnchor.js +1 -1
  27. package/dist/node/mdx/remarkPlugins/fileCodeBlock.js +1 -1
  28. package/dist/node/mdx/remarkPlugins/image.js +1 -2
  29. package/dist/node/mdx/remarkPlugins/link.js +1 -1
  30. package/dist/node/mdx/remarkPlugins/toc.js +1 -1
  31. package/dist/node/route/RouteService.js +2 -2
  32. package/dist/node/route/extractPageData.js +2 -3
  33. package/dist/node/route/normalizeRoutePath.js +2 -2
  34. package/dist/node/runtimeModule/i18n.js +1 -1
  35. package/dist/node/runtimeModule/pageData/createPageData.js +2 -2
  36. package/dist/node/runtimeModule/pageData/rsbuildPlugin.js +1 -1
  37. package/dist/node/runtimeModule/siteData/createSiteData.js +1 -1
  38. package/dist/node/runtimeModule/siteData/normalizeThemeConfig.js +1 -1
  39. package/dist/node/serve.js +1 -1
  40. package/dist/node/ssg/renderPage.js +1 -1
  41. package/dist/node/ssg/renderPageWorker.js +1 -1
  42. package/dist/node/ssg/renderPages.js +2 -2
  43. package/dist/node/ssg/rsbuildPluginSSG.js +1 -1
  44. package/dist/node/ssg/ssgEnv.js +1 -1
  45. package/dist/node/ssg-md/llms/emitLlmsTxt.js +2 -2
  46. package/dist/node/ssg-md/llms/llmsTxt.js +1 -1
  47. package/dist/node/ssg-md/renderPage.js +1 -1
  48. package/dist/node/ssg-md/renderPages.js +1 -1
  49. package/dist/node/ssg-md/rsbuildPluginSSGMD.js +1 -1
  50. package/dist/node/utils/createHash.js +1 -1
  51. package/dist/node/utils/flattenMdxContent.js +2 -3
  52. package/dist/node/utils/getPageKey.js +1 -1
  53. package/dist/node/utils/normalizePath.js +1 -1
  54. package/dist/node/utils/reactAlias.js +1 -1
  55. package/dist/runtime/Content.d.ts +4 -0
  56. package/dist/runtime/Content.js +18 -0
  57. package/dist/runtime/NoSSR.d.ts +3 -0
  58. package/dist/runtime/NoSSR.js +14 -0
  59. package/dist/runtime/getSidebarDataGroup.test.js +68 -0
  60. package/dist/runtime/hooks/useActiveMatcher.d.ts +1 -0
  61. package/dist/runtime/hooks/useActiveMatcher.js +14 -0
  62. package/dist/runtime/hooks/useDark.d.ts +7 -0
  63. package/dist/runtime/hooks/useDark.js +7 -0
  64. package/dist/runtime/hooks/useFrontmatter.d.ts +4 -0
  65. package/dist/runtime/hooks/useFrontmatter.js +9 -0
  66. package/dist/runtime/hooks/useI18n.d.ts +2 -0
  67. package/dist/runtime/hooks/useI18n.js +14 -0
  68. package/dist/runtime/hooks/useLang.d.ts +1 -0
  69. package/dist/runtime/hooks/useLang.js +6 -0
  70. package/dist/runtime/hooks/useLocaleSiteData.d.ts +18 -0
  71. package/dist/runtime/hooks/useLocaleSiteData.js +20 -0
  72. package/dist/runtime/hooks/useNav.d.ts +1 -0
  73. package/dist/runtime/hooks/useNav.js +12 -0
  74. package/dist/runtime/hooks/usePage.d.ts +10 -0
  75. package/dist/runtime/hooks/usePage.js +9 -0
  76. package/dist/runtime/hooks/usePageData.d.ts +5 -0
  77. package/dist/runtime/hooks/usePageData.js +16 -0
  78. package/dist/runtime/hooks/usePages.d.ts +4 -0
  79. package/dist/runtime/hooks/usePages.js +7 -0
  80. package/dist/runtime/hooks/useSidebar.d.ts +16 -0
  81. package/dist/runtime/hooks/useSidebar.js +67 -0
  82. package/dist/runtime/hooks/useSite.d.ts +4 -0
  83. package/dist/runtime/hooks/useSite.js +7 -0
  84. package/dist/runtime/hooks/useVersion.d.ts +1 -0
  85. package/dist/runtime/hooks/useVersion.js +6 -0
  86. package/dist/runtime/hooks/useWindowSize.d.ts +9 -0
  87. package/dist/runtime/hooks/useWindowSize.js +31 -0
  88. package/dist/runtime/hooks/useWindowSize.test.js +36 -0
  89. package/dist/runtime/index.d.ts +21 -0
  90. package/dist/runtime/index.js +22 -0
  91. package/dist/runtime/route.d.ts +36 -0
  92. package/dist/runtime/route.js +53 -0
  93. package/dist/runtime/route.test.js +154 -0
  94. package/dist/runtime/ssrMdServerEntry.js +1 -1
  95. package/dist/runtime/utils.d.ts +12 -0
  96. package/dist/runtime/utils.js +34 -0
  97. package/dist/runtime/utils.test.js +34 -0
  98. package/dist/theme/components/CodeBlock/index.js +1 -1
  99. package/dist/theme/components/FallbackHeading/index.js +4 -1
  100. package/dist/theme/components/HomeBackground/useNavTransparent.js +1 -1
  101. package/dist/theme/components/HomeHero/index.css +6 -0
  102. package/dist/theme/components/HomeHero/index.js +10 -3
  103. package/dist/theme/components/Overview/index.js +11 -43
  104. package/dist/theme/components/OverviewGroup/index.js +33 -0
  105. package/dist/theme/components/Steps/index.css +1 -0
  106. package/dist/theme/index.d.ts +1 -1
  107. package/dist/theme/index.js +1 -1
  108. package/dist/theme/logic/useScrollAfterNav.js +2 -2
  109. package/dist/theme/logic/useScrollReset.js +2 -2
  110. package/dist/theme/logic/{useAppearance.d.ts → useThemeState.d.ts} +4 -1
  111. package/dist/theme/logic/useThemeState.js +50 -0
  112. package/dist/theme/styles/scrollbar.css +14 -2
  113. package/package.json +9 -10
  114. package/static/MediumZoom.tsx +1 -1
  115. package/dist/4669.69cb6b18.js +0 -1
  116. package/dist/runtime.72d450ac.js +0 -1
  117. package/dist/runtime.d.ts +0 -2
  118. package/dist/runtime.js +0 -3
  119. package/dist/theme/logic/useAppearance.js +0 -64
  120. /package/dist/{3521.9eb6b114.js → 5341.9eb6b114.js} +0 -0
@@ -1,7 +1,6 @@
1
- import { node_fs, core_rspack } from "../../3521.9eb6b114.js";
1
+ import { node_fs, core_rspack } from "../../5341.9eb6b114.js";
2
2
  import { node_path, logger } from "../../6289.29817fb4.js";
3
- import { createProcessor } from "../../9643.2dff683e.js";
4
- import { MDX_OR_MD_REGEXP } from "../../4669.69cb6b18.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;
@@ -1,4 +1,4 @@
1
- import { removeLeadingSlash } from "../../4669.69cb6b18.js";
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,4 +1,4 @@
1
- import { node_os } from "../../9643.2dff683e.js";
1
+ import { node_os } from "../../1820.985588a0.js";
2
2
  import { node_path } from "../../6289.29817fb4.js";
3
3
  const isWindows = 'win32' === node_os.platform();
4
4
  function slash(p) {
@@ -1,5 +1,5 @@
1
1
  import { node_path, logger, picocolors } from "../../6289.29817fb4.js";
2
- import { core_rspack } from "../../3521.9eb6b114.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";
@@ -0,0 +1,4 @@
1
+ import { type ReactNode } from 'react';
2
+ export declare const Content: ({ fallback }: {
3
+ fallback?: ReactNode;
4
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -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,3 @@
1
+ export declare function NoSSR(props: {
2
+ children: React.ReactNode;
3
+ }): import("react/jsx-runtime").JSX.Element | null;
@@ -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,7 @@
1
+ interface IThemeContext {
2
+ theme: 'light' | 'dark';
3
+ setTheme?: (theme: 'light' | 'dark') => void;
4
+ }
5
+ export declare const ThemeContext: import("react").Context<IThemeContext>;
6
+ export declare function useDark(): boolean;
7
+ export {};
@@ -0,0 +1,7 @@
1
+ import { createContext, useContext } from "react";
2
+ const ThemeContext = createContext({});
3
+ function useDark() {
4
+ const ctx = useContext(ThemeContext);
5
+ return 'dark' === ctx.theme;
6
+ }
7
+ export { ThemeContext, useDark };
@@ -0,0 +1,4 @@
1
+ import type { FrontMatterMeta } from '@rspress/shared';
2
+ export declare const useFrontmatter: () => {
3
+ frontmatter: FrontMatterMeta;
4
+ };
@@ -0,0 +1,9 @@
1
+ import { usePage } from "./usePage.js";
2
+ const useFrontmatter = ()=>{
3
+ const { page } = usePage();
4
+ const { frontmatter = {} } = page;
5
+ return {
6
+ frontmatter
7
+ };
8
+ };
9
+ export { useFrontmatter };
@@ -0,0 +1,2 @@
1
+ import type { I18nText } from '@rspress/shared';
2
+ export declare function useI18n<T>(): (key: keyof (T & I18nText)) => string;
@@ -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,6 @@
1
+ import { usePage } from "./usePage.js";
2
+ function useLang() {
3
+ const { page } = usePage();
4
+ return page.lang || '';
5
+ }
6
+ export { useLang };
@@ -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,9 @@
1
+ import { createContext, useContext } from "react";
2
+ const PageContext = createContext({});
3
+ function usePage() {
4
+ const ctx = useContext(PageContext);
5
+ return {
6
+ page: ctx.data
7
+ };
8
+ }
9
+ export { PageContext, usePage };
@@ -0,0 +1,5 @@
1
+ import type { PageDataLegacy } from '@rspress/shared';
2
+ /**
3
+ * should use `usePage` and `useSite` instead
4
+ */
5
+ export declare function usePageData(): PageDataLegacy;
@@ -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,4 @@
1
+ import type { PageData } from '@rspress/shared';
2
+ export declare function usePages(): {
3
+ pages: PageData['pages'];
4
+ };
@@ -0,0 +1,7 @@
1
+ import { pageData } from "virtual-page-data";
2
+ function usePages() {
3
+ return {
4
+ pages: pageData.pages
5
+ };
6
+ }
7
+ export { usePages };
@@ -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,4 @@
1
+ import type { SiteData } from '@rspress/shared';
2
+ export declare function useSite(): {
3
+ site: SiteData;
4
+ };
@@ -0,0 +1,7 @@
1
+ import virtual_site_data from "virtual-site-data";
2
+ function useSite() {
3
+ return {
4
+ site: virtual_site_data
5
+ };
6
+ }
7
+ export { useSite };
@@ -0,0 +1 @@
1
+ export declare function useVersion(): string;
@@ -0,0 +1,6 @@
1
+ import { usePage } from "./usePage.js";
2
+ function useVersion() {
3
+ const { page } = usePage();
4
+ return page.version || '';
5
+ }
6
+ export { useVersion };
@@ -0,0 +1,9 @@
1
+ declare global {
2
+ interface Window {
3
+ __MODERN_PAGE_DATA__: any;
4
+ }
5
+ }
6
+ export declare function useWindowSize(initialWidth?: number, initialHeight?: number): {
7
+ width: number;
8
+ height: number;
9
+ };
@@ -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';
@@ -0,0 +1,22 @@
1
+ import { isDataUrl, isExternalUrl, matchNavbar, matchSidebar, normalizeHref } from "@rspress/shared";
2
+ import { Head } from "@unhead/react";
3
+ import { Content } from "./Content.js";
4
+ import { useActiveMatcher } from "./hooks/useActiveMatcher.js";
5
+ import { ThemeContext, useDark } from "./hooks/useDark.js";
6
+ import { useFrontmatter } from "./hooks/useFrontmatter.js";
7
+ import { useI18n } from "./hooks/useI18n.js";
8
+ import { useLang } from "./hooks/useLang.js";
9
+ import { useLocaleSiteData } from "./hooks/useLocaleSiteData.js";
10
+ import { useNav } from "./hooks/useNav.js";
11
+ import { PageContext, usePage } from "./hooks/usePage.js";
12
+ import { usePageData } from "./hooks/usePageData.js";
13
+ import { usePages } from "./hooks/usePages.js";
14
+ import { getSidebarDataGroup, useSidebar, useSidebarDynamic } from "./hooks/useSidebar.js";
15
+ import { useSite } from "./hooks/useSite.js";
16
+ import { useVersion } from "./hooks/useVersion.js";
17
+ import { useWindowSize } from "./hooks/useWindowSize.js";
18
+ import { NoSSR } from "./NoSSR.js";
19
+ import { isActive, pathnameToRouteService, preloadLink } from "./route.js";
20
+ import { addLeadingSlash, cleanUrlByConfig, isEqualPath, isProduction, normalizeHrefInRuntime, normalizeImagePath, removeBase, removeTrailingSlash, routePathToMdPath, withBase } from "./utils.js";
21
+ export * from "react-router-dom";
22
+ export { Content, Head, NoSSR, PageContext, ThemeContext, addLeadingSlash, cleanUrlByConfig, getSidebarDataGroup, isActive, isDataUrl, isEqualPath, isExternalUrl, isProduction, matchNavbar, matchSidebar, normalizeHref, normalizeHrefInRuntime, normalizeImagePath, pathnameToRouteService, preloadLink, removeBase, removeTrailingSlash, routePathToMdPath, useActiveMatcher, useDark, useFrontmatter, useI18n, useLang, useLocaleSiteData, useNav, usePage, usePageData, usePages, useSidebar, useSidebarDynamic, useSite, useVersion, useWindowSize, withBase };
@@ -0,0 +1,36 @@
1
+ import type { Route } from '@rspress/shared';
2
+ /**
3
+ * Simple implementation of matchPath to check if a pattern matches a pathname
4
+ * Better performance alternative of `import { matchPath } from 'react-router-dom'`
5
+ * @param pattern - The route pattern to match against
6
+ * @param pathname - The pathname to check
7
+ * @returns Match object if matched, null otherwise
8
+ *
9
+ * @example
10
+ * matchPath('/api/config', '/api/config') // { path: '/api/config' }
11
+ * matchPath('/api/config/', '/api/config') // { path: '/api/config/' }
12
+ * matchPath('/api/config', '/api/other') // null
13
+ */
14
+ export declare function matchPath(pattern: string, pathname: string): {
15
+ path: string;
16
+ } | null;
17
+ /**
18
+ * this is a bridge of two core features Sidebar and RouteService
19
+ * @param pathname useLocation().pathname
20
+ * @returns
21
+ */
22
+ export declare function pathnameToRouteService(pathname: string): Route | undefined;
23
+ /**
24
+ * link: /api/config
25
+ * currentPathname:
26
+ * 0. /api/config
27
+ * 1. /api/config.html
28
+ * 2. /api/config/
29
+ * 3. /api/config/index
30
+ * 4. /api/config/index.html
31
+ * @param itemLink
32
+ * @param currentPathname
33
+ * @returns
34
+ */
35
+ export declare function isActive(itemLink: string, currentPathname: string): boolean;
36
+ export declare const preloadLink: (link: string) => void;