@rspress/plugin-client-redirects 1.42.1-canary-20240226 → 1.42.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +5 -4
- package/static/Redirect.tsx +43 -0
package/dist/index.js
CHANGED
@@ -7,7 +7,7 @@ function pluginClientRedirects(options = {}) {
|
|
7
7
|
name: '@rspress/plugin-client-redirects',
|
8
8
|
globalUIComponents: [
|
9
9
|
[
|
10
|
-
__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(src_dirname, '../
|
10
|
+
__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(src_dirname, '../static/Redirect.tsx'),
|
11
11
|
options
|
12
12
|
]
|
13
13
|
]
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@rspress/plugin-client-redirects",
|
3
|
-
"version": "1.42.1
|
3
|
+
"version": "1.42.1",
|
4
4
|
"description": "A plugin for rspress to client redirect in docs.",
|
5
5
|
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
6
6
|
"repository": {
|
@@ -20,10 +20,11 @@
|
|
20
20
|
"module": "./dist/node/index.js",
|
21
21
|
"types": "./dist/index.d.ts",
|
22
22
|
"files": [
|
23
|
-
"dist"
|
23
|
+
"dist",
|
24
|
+
"static"
|
24
25
|
],
|
25
26
|
"dependencies": {
|
26
|
-
"@rspress/shared": "1.42.1
|
27
|
+
"@rspress/shared": "1.42.1"
|
27
28
|
},
|
28
29
|
"devDependencies": {
|
29
30
|
"@microsoft/api-extractor": "^7.49.2",
|
@@ -36,7 +37,7 @@
|
|
36
37
|
"@rspress/config": "1.0.0"
|
37
38
|
},
|
38
39
|
"peerDependencies": {
|
39
|
-
"@rspress/runtime": "^1.42.
|
40
|
+
"@rspress/runtime": "^1.42.1"
|
40
41
|
},
|
41
42
|
"engines": {
|
42
43
|
"node": ">=14.17.6"
|
@@ -0,0 +1,43 @@
|
|
1
|
+
import { useLocation } from '@rspress/runtime';
|
2
|
+
import { isExternalUrl } from '@rspress/shared';
|
3
|
+
import { useEffect } from 'react';
|
4
|
+
|
5
|
+
// these are types copied from src/types.ts
|
6
|
+
type RedirectRule = {
|
7
|
+
to: string;
|
8
|
+
from: string | string[];
|
9
|
+
};
|
10
|
+
|
11
|
+
type RedirectsOptions = {
|
12
|
+
redirects?: RedirectRule[];
|
13
|
+
};
|
14
|
+
|
15
|
+
export default function Redirect(props: RedirectsOptions = {}) {
|
16
|
+
const { pathname } = useLocation();
|
17
|
+
const { redirects } = props;
|
18
|
+
|
19
|
+
useEffect(() => {
|
20
|
+
if (redirects) {
|
21
|
+
for (const redirect of redirects) {
|
22
|
+
const { from, to } = redirect;
|
23
|
+
const fromPaths = Array.isArray(from) ? from : [from];
|
24
|
+
|
25
|
+
fromPaths.forEach(item => {
|
26
|
+
const regex = new RegExp(item);
|
27
|
+
|
28
|
+
if (regex.test(pathname) && typeof window !== 'undefined') {
|
29
|
+
if (isExternalUrl(to)) {
|
30
|
+
window.location.replace(to);
|
31
|
+
} else {
|
32
|
+
window.location.replace(
|
33
|
+
pathname.replace(regex, to) + location.hash,
|
34
|
+
);
|
35
|
+
}
|
36
|
+
}
|
37
|
+
});
|
38
|
+
}
|
39
|
+
}
|
40
|
+
}, [pathname]);
|
41
|
+
|
42
|
+
return null;
|
43
|
+
}
|