@rspress/runtime 1.41.3 → 1.42.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/Content.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { type ReactNode } from 'react';
2
2
  export declare const Content: ({ fallback }: {
3
3
  fallback?: ReactNode;
4
- }) => any;
4
+ }) => import("react/jsx-runtime").JSX.Element;
package/dist/Content.js CHANGED
@@ -1,10 +1,9 @@
1
1
  import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__ from "react/jsx-runtime";
2
- import * as __WEBPACK_EXTERNAL_MODULE__VIRTUAL_ROUTES__3c8bd3e0__ from "__VIRTUAL_ROUTES__";
3
2
  import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
4
3
  import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__ from "react-router-dom";
5
4
  import * as __WEBPACK_EXTERNAL_MODULE_virtual_site_data_fa42d4c0__ from "virtual-site-data";
6
5
  import * as __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__ from "./hooks.js";
7
- import * as __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__ from "./utils.js";
6
+ import * as __WEBPACK_EXTERNAL_MODULE__route_js_d0361f8a__ from "./route.js";
8
7
  function TransitionContentImpl(props) {
9
8
  var _siteData_themeConfig;
10
9
  let element = props.el;
@@ -14,9 +13,9 @@ function TransitionContentImpl(props) {
14
13
  const TransitionContent = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.memo)(TransitionContentImpl, (prevProps, nextProps)=>prevProps.el === nextProps.el);
15
14
  const Content = ({ fallback = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.Fragment, {}) })=>{
16
15
  const { pathname } = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__.useLocation)();
17
- const matched = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__.matchRoutes)(__WEBPACK_EXTERNAL_MODULE__VIRTUAL_ROUTES__3c8bd3e0__.routes, (0, __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.normalizeRoutePath)(pathname));
16
+ const matched = (0, __WEBPACK_EXTERNAL_MODULE__route_js_d0361f8a__.pathnameToRouteService)(pathname);
18
17
  if (!matched) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("div", {});
19
- const routesElement = matched[0].route.element;
18
+ const routesElement = matched.element;
20
19
  if (!process.env.__REACT_GTE_18__ && process.env.__SSR__) return routesElement;
21
20
  return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)(__WEBPACK_EXTERNAL_MODULE_react__.Suspense, {
22
21
  fallback: fallback,
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- export * from './hooks';
2
- export * from './Content';
3
- export { normalizeHrefInRuntime, normalizeImagePath, withBase, removeBase, addLeadingSlash, removeTrailingSlash, normalizeSlash, isProduction, normalizeRoutePath, isEqualPath, } from './utils';
1
+ export { DataContext, ThemeContext, useDark, useI18n, useLang, usePageData, useVersion, useViewTransition, } from './hooks';
2
+ export { Content } from './Content';
3
+ export { normalizeHrefInRuntime, normalizeImagePath, withBase, removeBase, addLeadingSlash, removeTrailingSlash, normalizeSlash, isProduction, isEqualPath, } from './utils';
4
4
  export { useLocation, useNavigate, matchRoutes, BrowserRouter, useSearchParams, } from 'react-router-dom';
5
+ export { pathnameToRouteService } from './route';
5
6
  export { Helmet } from 'react-helmet-async';
6
7
  export { NoSSR } from './NoSSR';
package/dist/index.js CHANGED
@@ -1,24 +1,34 @@
1
+ import * as __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__ from "./hooks.js";
2
+ import * as __WEBPACK_EXTERNAL_MODULE__Content_js_303edc82__ from "./Content.js";
1
3
  import * as __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__ from "./utils.js";
2
4
  import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__ from "react-router-dom";
5
+ import * as __WEBPACK_EXTERNAL_MODULE__route_js_d0361f8a__ from "./route.js";
3
6
  import * as __WEBPACK_EXTERNAL_MODULE_react_helmet_async_048d06c6__ from "react-helmet-async";
4
7
  import * as __WEBPACK_EXTERNAL_MODULE__NoSSR_js_7f7b6e71__ from "./NoSSR.js";
5
- export * from "./hooks.js";
6
- export * from "./Content.js";
7
8
  var __webpack_exports__BrowserRouter = __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__.BrowserRouter;
9
+ var __webpack_exports__Content = __WEBPACK_EXTERNAL_MODULE__Content_js_303edc82__.Content;
10
+ var __webpack_exports__DataContext = __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__.DataContext;
8
11
  var __webpack_exports__Helmet = __WEBPACK_EXTERNAL_MODULE_react_helmet_async_048d06c6__.Helmet;
9
12
  var __webpack_exports__NoSSR = __WEBPACK_EXTERNAL_MODULE__NoSSR_js_7f7b6e71__.NoSSR;
13
+ var __webpack_exports__ThemeContext = __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__.ThemeContext;
10
14
  var __webpack_exports__addLeadingSlash = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.addLeadingSlash;
11
15
  var __webpack_exports__isEqualPath = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.isEqualPath;
12
16
  var __webpack_exports__isProduction = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.isProduction;
13
17
  var __webpack_exports__matchRoutes = __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__.matchRoutes;
14
18
  var __webpack_exports__normalizeHrefInRuntime = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.normalizeHrefInRuntime;
15
19
  var __webpack_exports__normalizeImagePath = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.normalizeImagePath;
16
- var __webpack_exports__normalizeRoutePath = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.normalizeRoutePath;
17
20
  var __webpack_exports__normalizeSlash = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.normalizeSlash;
21
+ var __webpack_exports__pathnameToRouteService = __WEBPACK_EXTERNAL_MODULE__route_js_d0361f8a__.pathnameToRouteService;
18
22
  var __webpack_exports__removeBase = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.removeBase;
19
23
  var __webpack_exports__removeTrailingSlash = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.removeTrailingSlash;
24
+ var __webpack_exports__useDark = __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__.useDark;
25
+ var __webpack_exports__useI18n = __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__.useI18n;
26
+ var __webpack_exports__useLang = __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__.useLang;
20
27
  var __webpack_exports__useLocation = __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__.useLocation;
21
28
  var __webpack_exports__useNavigate = __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__.useNavigate;
29
+ var __webpack_exports__usePageData = __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__.usePageData;
22
30
  var __webpack_exports__useSearchParams = __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__.useSearchParams;
31
+ var __webpack_exports__useVersion = __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__.useVersion;
32
+ var __webpack_exports__useViewTransition = __WEBPACK_EXTERNAL_MODULE__hooks_js_5a9aefb7__.useViewTransition;
23
33
  var __webpack_exports__withBase = __WEBPACK_EXTERNAL_MODULE__utils_js_d88b7fe1__.withBase;
24
- export { __webpack_exports__BrowserRouter as BrowserRouter, __webpack_exports__Helmet as Helmet, __webpack_exports__NoSSR as NoSSR, __webpack_exports__addLeadingSlash as addLeadingSlash, __webpack_exports__isEqualPath as isEqualPath, __webpack_exports__isProduction as isProduction, __webpack_exports__matchRoutes as matchRoutes, __webpack_exports__normalizeHrefInRuntime as normalizeHrefInRuntime, __webpack_exports__normalizeImagePath as normalizeImagePath, __webpack_exports__normalizeRoutePath as normalizeRoutePath, __webpack_exports__normalizeSlash as normalizeSlash, __webpack_exports__removeBase as removeBase, __webpack_exports__removeTrailingSlash as removeTrailingSlash, __webpack_exports__useLocation as useLocation, __webpack_exports__useNavigate as useNavigate, __webpack_exports__useSearchParams as useSearchParams, __webpack_exports__withBase as withBase };
34
+ export { __webpack_exports__BrowserRouter as BrowserRouter, __webpack_exports__Content as Content, __webpack_exports__DataContext as DataContext, __webpack_exports__Helmet as Helmet, __webpack_exports__NoSSR as NoSSR, __webpack_exports__ThemeContext as ThemeContext, __webpack_exports__addLeadingSlash as addLeadingSlash, __webpack_exports__isEqualPath as isEqualPath, __webpack_exports__isProduction as isProduction, __webpack_exports__matchRoutes as matchRoutes, __webpack_exports__normalizeHrefInRuntime as normalizeHrefInRuntime, __webpack_exports__normalizeImagePath as normalizeImagePath, __webpack_exports__normalizeSlash as normalizeSlash, __webpack_exports__pathnameToRouteService as pathnameToRouteService, __webpack_exports__removeBase as removeBase, __webpack_exports__removeTrailingSlash as removeTrailingSlash, __webpack_exports__useDark as useDark, __webpack_exports__useI18n as useI18n, __webpack_exports__useLang as useLang, __webpack_exports__useLocation as useLocation, __webpack_exports__useNavigate as useNavigate, __webpack_exports__usePageData as usePageData, __webpack_exports__useSearchParams as useSearchParams, __webpack_exports__useVersion as useVersion, __webpack_exports__useViewTransition as useViewTransition, __webpack_exports__withBase as withBase };
@@ -0,0 +1,7 @@
1
+ import type { Route } from '@rspress/shared';
2
+ /**
3
+ * this is a bridge of two core features Sidebar and RouteService
4
+ * @param pathname useLocation().pathname
5
+ * @returns
6
+ */
7
+ export declare function pathnameToRouteService(pathname: string): Route | undefined;
package/dist/route.js ADDED
@@ -0,0 +1,16 @@
1
+ import * as __WEBPACK_EXTERNAL_MODULE__VIRTUAL_ROUTES__3c8bd3e0__ from "__VIRTUAL_ROUTES__";
2
+ import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__ from "react-router-dom";
3
+ function normalizeRoutePath(routePath) {
4
+ return decodeURIComponent(routePath).replace(/\.html$/, '').replace(/\/index$/, '/');
5
+ }
6
+ const cache = new Map();
7
+ function pathnameToRouteService(pathname) {
8
+ var _matched_;
9
+ const cacheItem = cache.get(pathname);
10
+ if (cacheItem) return cacheItem;
11
+ const matched = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__.matchRoutes)(__WEBPACK_EXTERNAL_MODULE__VIRTUAL_ROUTES__3c8bd3e0__.routes, normalizeRoutePath(pathname));
12
+ const route = null == matched ? void 0 : null === (_matched_ = matched[0]) || void 0 === _matched_ ? void 0 : _matched_.route;
13
+ if (route) cache.set(pathname, route);
14
+ return route;
15
+ }
16
+ export { pathnameToRouteService };
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { addLeadingSlash, isProduction, normalizeSlash, removeTrailingSlash } from '@rspress/shared';
2
- export declare function normalizeRoutePath(routePath: string): string;
3
2
  export declare function withBase(url?: string): string;
4
3
  export declare function removeBase(url: string): string;
5
4
  export declare function isEqualPath(a: string, b: string): boolean;
package/dist/utils.js CHANGED
@@ -1,8 +1,5 @@
1
1
  import * as __WEBPACK_EXTERNAL_MODULE__rspress_shared_baa012d0__ from "@rspress/shared";
2
2
  import * as __WEBPACK_EXTERNAL_MODULE_virtual_site_data_fa42d4c0__ from "virtual-site-data";
3
- function normalizeRoutePath(routePath) {
4
- return decodeURIComponent(routePath).replace(/\.html$/, '').replace(/\/index$/, '/');
5
- }
6
3
  function withBase(url = '/') {
7
4
  return (0, __WEBPACK_EXTERNAL_MODULE__rspress_shared_baa012d0__.withBase)(url, __WEBPACK_EXTERNAL_MODULE_virtual_site_data_fa42d4c0__["default"].base);
8
5
  }
@@ -31,4 +28,4 @@ var __webpack_exports__addLeadingSlash = __WEBPACK_EXTERNAL_MODULE__rspress_shar
31
28
  var __webpack_exports__isProduction = __WEBPACK_EXTERNAL_MODULE__rspress_shared_baa012d0__.isProduction;
32
29
  var __webpack_exports__normalizeSlash = __WEBPACK_EXTERNAL_MODULE__rspress_shared_baa012d0__.normalizeSlash;
33
30
  var __webpack_exports__removeTrailingSlash = __WEBPACK_EXTERNAL_MODULE__rspress_shared_baa012d0__.removeTrailingSlash;
34
- export { isAbsoluteUrl, isEqualPath, normalizeHrefInRuntime, normalizeImagePath, normalizeRoutePath, removeBase, withBase, __webpack_exports__addLeadingSlash as addLeadingSlash, __webpack_exports__isProduction as isProduction, __webpack_exports__normalizeSlash as normalizeSlash, __webpack_exports__removeTrailingSlash as removeTrailingSlash };
31
+ export { isAbsoluteUrl, isEqualPath, normalizeHrefInRuntime, normalizeImagePath, removeBase, withBase, __webpack_exports__addLeadingSlash as addLeadingSlash, __webpack_exports__isProduction as isProduction, __webpack_exports__normalizeSlash as normalizeSlash, __webpack_exports__removeTrailingSlash as removeTrailingSlash };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspress/runtime",
3
- "version": "1.41.3",
3
+ "version": "1.42.0",
4
4
  "description": "The Runtime of Rspress Documentation Framework",
5
5
  "bugs": "https://github.com/web-infra-dev/rspress/issues",
6
6
  "repository": {
@@ -32,7 +32,6 @@
32
32
  "files": [
33
33
  "dist",
34
34
  "src",
35
- "runtime.ts",
36
35
  "server.js"
37
36
  ],
38
37
  "dependencies": {
@@ -40,7 +39,7 @@
40
39
  "react-dom": "^18.3.1",
41
40
  "react-helmet-async": "^1.3.0",
42
41
  "react-router-dom": "^6.29.0",
43
- "@rspress/shared": "1.41.3"
42
+ "@rspress/shared": "1.42.0"
44
43
  },
45
44
  "devDependencies": {
46
45
  "@modern-js/tsconfig": "2.64.0",
package/src/Content.tsx CHANGED
@@ -1,17 +1,12 @@
1
- // @ts-expect-error __VIRTUAL_ROUTES__ will be determined at build time
2
- import { routes } from '__VIRTUAL_ROUTES__';
3
- /* eslint-disable @typescript-eslint/no-var-requires */
4
- /* eslint-disable @typescript-eslint/no-require-imports */
5
1
  import { type ReactElement, type ReactNode, Suspense, memo } from 'react';
6
- import { matchRoutes, useLocation } from 'react-router-dom';
2
+ import { useLocation } from 'react-router-dom';
7
3
  import siteData from 'virtual-site-data';
8
4
  import { useViewTransition } from './hooks';
9
- import { normalizeRoutePath } from './utils';
5
+ import { pathnameToRouteService } from './route';
10
6
 
11
7
  function TransitionContentImpl(props: { el: ReactElement }) {
12
8
  let element = props.el;
13
9
  if (siteData?.themeConfig?.enableContentAnimation) {
14
- // eslint-disable-next-line react-hooks/rules-of-hooks
15
10
  element = useViewTransition(props.el);
16
11
  }
17
12
  return element;
@@ -24,14 +19,11 @@ const TransitionContent = memo(
24
19
 
25
20
  export const Content = ({ fallback = <></> }: { fallback?: ReactNode }) => {
26
21
  const { pathname } = useLocation();
27
- const matched = matchRoutes(
28
- routes as typeof import('virtual-routes')['routes'],
29
- normalizeRoutePath(pathname),
30
- );
22
+ const matched = pathnameToRouteService(pathname);
31
23
  if (!matched) {
32
24
  return <div></div>;
33
25
  }
34
- const routesElement = matched[0].route.element;
26
+ const routesElement = matched.element;
35
27
 
36
28
  // React 17 Suspense SSR is not supported
37
29
  if (!process.env.__REACT_GTE_18__ && process.env.__SSR__) {
package/src/index.ts CHANGED
@@ -1,5 +1,14 @@
1
- export * from './hooks';
2
- export * from './Content';
1
+ export {
2
+ DataContext,
3
+ ThemeContext,
4
+ useDark,
5
+ useI18n,
6
+ useLang,
7
+ usePageData,
8
+ useVersion,
9
+ useViewTransition,
10
+ } from './hooks';
11
+ export { Content } from './Content';
3
12
  export {
4
13
  normalizeHrefInRuntime,
5
14
  normalizeImagePath,
@@ -9,7 +18,6 @@ export {
9
18
  removeTrailingSlash,
10
19
  normalizeSlash,
11
20
  isProduction,
12
- normalizeRoutePath,
13
21
  isEqualPath,
14
22
  } from './utils';
15
23
  export {
@@ -19,5 +27,6 @@ export {
19
27
  BrowserRouter,
20
28
  useSearchParams,
21
29
  } from 'react-router-dom';
30
+ export { pathnameToRouteService } from './route';
22
31
  export { Helmet } from 'react-helmet-async';
23
32
  export { NoSSR } from './NoSSR';
@@ -0,0 +1,70 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { pathnameToRouteService } from './route';
3
+
4
+ vi.mock('__VIRTUAL_ROUTES__', () => {
5
+ const element = vi.fn();
6
+ const routes = [
7
+ {
8
+ path: '/api/config',
9
+ element,
10
+ filePath: 'api/config.mdx',
11
+ preload: async () => {},
12
+ lang: '',
13
+ version: '',
14
+ },
15
+ {
16
+ path: '/api/config/',
17
+ element,
18
+ filePath: 'api/config/index.mdx',
19
+ preload: async () => {},
20
+ lang: '',
21
+ version: '',
22
+ },
23
+ {
24
+ path: '/',
25
+ element,
26
+ filePath: 'index.mdx',
27
+ preload: async () => {},
28
+ lang: '',
29
+ version: '',
30
+ },
31
+ ];
32
+ return { routes };
33
+ });
34
+
35
+ describe('pathnameToRouteService', () => {
36
+ it('0. /api/config', () => {
37
+ const pathname = '/api/config';
38
+ // currently we do not support both /api/config.mdx and /api/config/index.mdx
39
+ expect(pathnameToRouteService(pathname)?.path).toMatchInlineSnapshot(
40
+ `"/api/config/"`,
41
+ );
42
+ });
43
+
44
+ it('1. /api/config.html', () => {
45
+ const pathname = '/api/config.html';
46
+ expect(pathnameToRouteService(pathname)?.path).toMatchInlineSnapshot(
47
+ `"/api/config/"`,
48
+ );
49
+ });
50
+
51
+ it('2. /api/config/', () => {
52
+ const pathname = '/api/config/';
53
+ expect(pathnameToRouteService(pathname)?.path).toMatchInlineSnapshot(
54
+ `"/api/config/"`,
55
+ );
56
+ });
57
+
58
+ it('3. /api/config/index', () => {
59
+ const pathname = '/api/config/index';
60
+ expect(pathnameToRouteService(pathname)?.path).toMatchInlineSnapshot(
61
+ `"/api/config/"`,
62
+ );
63
+ });
64
+ it('4. /api/config/index.html', () => {
65
+ const pathname = '/api/config/index.html';
66
+ expect(pathnameToRouteService(pathname)?.path).toMatchInlineSnapshot(
67
+ `"/api/config/"`,
68
+ );
69
+ });
70
+ });
package/src/route.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { Route } from '@rspress/shared';
2
+ // @ts-expect-error __VIRTUAL_ROUTES__ will be determined at build time
3
+ import { routes } from '__VIRTUAL_ROUTES__';
4
+ import { matchRoutes } from 'react-router-dom';
5
+
6
+ function normalizeRoutePath(routePath: string) {
7
+ return decodeURIComponent(routePath)
8
+ .replace(/\.html$/, '')
9
+ .replace(/\/index$/, '/');
10
+ }
11
+
12
+ const cache = new Map<string, Route>();
13
+ /**
14
+ * this is a bridge of two core features Sidebar and RouteService
15
+ * @param pathname useLocation().pathname
16
+ * @returns
17
+ */
18
+ export function pathnameToRouteService(pathname: string): Route | undefined {
19
+ const cacheItem = cache.get(pathname);
20
+ if (cacheItem) {
21
+ return cacheItem;
22
+ }
23
+ const matched = matchRoutes(
24
+ routes as typeof import('virtual-routes')['routes'],
25
+ normalizeRoutePath(pathname),
26
+ );
27
+ const route: Route | undefined = matched?.[0]?.route;
28
+ if (route) {
29
+ cache.set(pathname, route);
30
+ }
31
+ return route;
32
+ }
package/src/utils.ts CHANGED
@@ -12,12 +12,6 @@ import {
12
12
  } from '@rspress/shared';
13
13
  import siteData from 'virtual-site-data';
14
14
 
15
- export function normalizeRoutePath(routePath: string) {
16
- return decodeURIComponent(routePath)
17
- .replace(/\.html$/, '')
18
- .replace(/\/index$/, '/');
19
- }
20
-
21
15
  export function withBase(url = '/'): string {
22
16
  return rawWithBase(url, siteData.base);
23
17
  }