@rspress/plugin-client-redirects 0.0.0-nightly-20240614160114

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present Bytedance, Inc. and its affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @rspress/plugin-client-redirects
2
+
3
+ > Client redirects plugin for rspress
4
+
5
+ [Documentation](https://rspress.dev/plugin/official-plugins/client-redirects)
@@ -0,0 +1,16 @@
1
+ import { RspressPlugin } from '@rspress/shared';
2
+
3
+ type RedirectRule = {
4
+ to: string;
5
+ from: string | string[];
6
+ };
7
+ type RedirectsOptions = {
8
+ redirects?: RedirectRule[];
9
+ };
10
+
11
+ /**
12
+ * The plugin is used to add client redirect feature to the doc site.
13
+ */
14
+ declare function pluginClientRedirects(options?: RedirectsOptions): RspressPlugin;
15
+
16
+ export { pluginClientRedirects };
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ // src/index.ts
2
+ import path from "path";
3
+ function pluginClientRedirects(options = {}) {
4
+ return {
5
+ name: "@rspress/plugin-client-redirects",
6
+ globalUIComponents: [
7
+ [path.join(__dirname, "../src/components/Redirect.tsx"), options]
8
+ ]
9
+ };
10
+ }
11
+ export {
12
+ pluginClientRedirects
13
+ };
14
+
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"mappings":";AAAA,OAAO,UAAU;AAOV,SAAS,sBACd,UAA4B,CAAC,GACd;AACf,SAAO;AAAA,IACL,MAAM;AAAA,IACN,oBAAoB;AAAA,MAClB,CAAC,KAAK,KAAK,WAAW,gCAAgC,GAAG,OAAO;AAAA,IAClE;AAAA,EACF;AACF","names":[],"ignoreList":[],"sources":["../src/index.ts"],"sourcesContent":["import path from 'node:path';\nimport type { RspressPlugin } from '@rspress/shared';\nimport type { RedirectsOptions } from './types';\n\n/**\n * The plugin is used to add client redirect feature to the doc site.\n */\nexport function pluginClientRedirects(\n options: RedirectsOptions = {},\n): RspressPlugin {\n return {\n name: '@rspress/plugin-client-redirects',\n globalUIComponents: [\n [path.join(__dirname, '../src/components/Redirect.tsx'), options],\n ],\n };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@rspress/plugin-client-redirects",
3
+ "version": "0.0.0-nightly-20240614160114",
4
+ "description": "A plugin for rspress to client redirect in docs.",
5
+ "bugs": "https://github.com/web-infra-dev/rspress/issues",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/web-infra-dev/rspress",
9
+ "directory": "packages/plugin-client-redirects"
10
+ },
11
+ "license": "MIT",
12
+ "type": "module",
13
+ "jsnext:source": "./src/index.ts",
14
+ "types": "./dist/index.d.ts",
15
+ "main": "./dist/index.js",
16
+ "module": "./dist/node/index.js",
17
+ "engines": {
18
+ "node": ">=14.17.6"
19
+ },
20
+ "devDependencies": {
21
+ "@modern-js/tsconfig": "2.52.0",
22
+ "@types/node": "^18.11.17",
23
+ "@types/react": "^18",
24
+ "@types/react-dom": "^18",
25
+ "react": "^18.2.0",
26
+ "typescript": "^5",
27
+ "vitest": "0.34.1",
28
+ "@rspress/shared": "0.0.0-nightly-20240614160114"
29
+ },
30
+ "peerDependencies": {
31
+ "@rspress/runtime": "0.0.0-nightly-20240614160114"
32
+ },
33
+ "sideEffects": [
34
+ "*.css",
35
+ "*.less",
36
+ "*.sass",
37
+ "*.scss"
38
+ ],
39
+ "files": [
40
+ "dist",
41
+ "src"
42
+ ],
43
+ "publishConfig": {
44
+ "access": "public",
45
+ "provenance": true,
46
+ "registry": "https://registry.npmjs.org/"
47
+ },
48
+ "scripts": {
49
+ "dev": "modern build -w",
50
+ "build": "modern build",
51
+ "reset": "rimraf ./**/node_modules",
52
+ "test": "vitest run --passWithNoTests"
53
+ }
54
+ }
@@ -0,0 +1,34 @@
1
+ import { useEffect } from 'react';
2
+ import type { RedirectsOptions } from '../types';
3
+ import { useLocation } from '@rspress/runtime';
4
+ import { isExternalUrl } from '@rspress/shared';
5
+
6
+ export default function Redirect(props: RedirectsOptions = {}) {
7
+ const { pathname } = useLocation();
8
+ const { redirects } = props;
9
+
10
+ useEffect(() => {
11
+ if (redirects) {
12
+ for (const redirect of redirects) {
13
+ const { from, to } = redirect;
14
+ const fromPaths = Array.isArray(from) ? from : [from];
15
+
16
+ fromPaths.forEach(item => {
17
+ const regex = new RegExp(item);
18
+
19
+ if (regex.test(pathname) && typeof window !== 'undefined') {
20
+ if (isExternalUrl(to)) {
21
+ window.location.replace(to);
22
+ } else {
23
+ window.location.replace(
24
+ pathname.replace(regex, to) + location.hash,
25
+ );
26
+ }
27
+ }
28
+ });
29
+ }
30
+ }
31
+ }, [pathname]);
32
+
33
+ return null;
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,17 @@
1
+ import path from 'node:path';
2
+ import type { RspressPlugin } from '@rspress/shared';
3
+ import type { RedirectsOptions } from './types';
4
+
5
+ /**
6
+ * The plugin is used to add client redirect feature to the doc site.
7
+ */
8
+ export function pluginClientRedirects(
9
+ options: RedirectsOptions = {},
10
+ ): RspressPlugin {
11
+ return {
12
+ name: '@rspress/plugin-client-redirects',
13
+ globalUIComponents: [
14
+ [path.join(__dirname, '../src/components/Redirect.tsx'), options],
15
+ ],
16
+ };
17
+ }
@@ -0,0 +1 @@
1
+ /// <reference types='@modern-js/module-tools/types' />
package/src/types.ts ADDED
@@ -0,0 +1,8 @@
1
+ export type RedirectRule = {
2
+ to: string;
3
+ from: string | string[];
4
+ };
5
+
6
+ export type RedirectsOptions = {
7
+ redirects?: RedirectRule[];
8
+ };