@docusaurus/plugin-client-redirects 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17
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/lib/collectRedirects.d.ts +1 -1
- package/lib/collectRedirects.js +30 -35
- package/lib/createRedirectPageContent.js +2 -4
- package/lib/extensionRedirects.d.ts +1 -1
- package/lib/extensionRedirects.js +9 -16
- package/lib/index.d.ts +3 -3
- package/lib/index.js +5 -5
- package/lib/normalizePluginOptions.d.ts +1 -1
- package/lib/normalizePluginOptions.js +2 -2
- package/lib/redirectValidation.d.ts +1 -1
- package/lib/types.d.ts +2 -14
- package/lib/writeRedirectFiles.d.ts +1 -1
- package/lib/writeRedirectFiles.js +15 -14
- package/package.json +16 -14
- package/src/collectRedirects.ts +36 -46
- package/src/createRedirectPageContent.ts +4 -4
- package/src/extensionRedirects.ts +9 -17
- package/src/index.ts +5 -4
- package/src/normalizePluginOptions.ts +6 -2
- package/src/plugin-client-redirects.d.ts +27 -0
- package/src/redirectValidation.ts +1 -1
- package/src/types.ts +2 -22
- package/src/writeRedirectFiles.ts +17 -14
- package/lib/.tsbuildinfo +0 -1
- package/src/__tests__/__snapshots__/collectRedirects.test.ts.snap +0 -27
- package/src/__tests__/__snapshots__/createRedirectPageContent.test.ts.snap +0 -29
- package/src/__tests__/__snapshots__/normalizePluginOptions.test.ts.snap +0 -35
- package/src/__tests__/__snapshots__/redirectValidation.test.ts.snap +0 -11
- package/src/__tests__/__snapshots__/writeRedirectFiles.test.ts.snap +0 -147
- package/src/__tests__/collectRedirects.test.ts +0 -340
- package/src/__tests__/createRedirectPageContent.test.ts +0 -26
- package/src/__tests__/extensionRedirects.test.ts +0 -143
- package/src/__tests__/normalizePluginOptions.test.ts +0 -76
- package/src/__tests__/redirectValidation.test.ts +0 -66
- package/src/__tests__/writeRedirectFiles.test.ts +0 -204
- package/tsconfig.json +0 -10
|
@@ -4,5 +4,5 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { PluginContext, RedirectMetadata } from './types';
|
|
7
|
+
import type { PluginContext, RedirectMetadata } from './types';
|
|
8
8
|
export default function collectRedirects(pluginContext: PluginContext, trailingSlash: boolean | undefined): RedirectMetadata[];
|
package/lib/collectRedirects.js
CHANGED
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
|
-
const lodash_1 = require("lodash");
|
|
10
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
11
11
|
const extensionRedirects_1 = require("./extensionRedirects");
|
|
12
12
|
const redirectValidation_1 = require("./redirectValidation");
|
|
13
13
|
const utils_common_1 = require("@docusaurus/utils-common");
|
|
14
|
-
const
|
|
14
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
15
15
|
function collectRedirects(pluginContext, trailingSlash) {
|
|
16
16
|
let redirects = doCollectRedirects(pluginContext);
|
|
17
17
|
redirects = applyRedirectsTrailingSlash(redirects, {
|
|
@@ -23,26 +23,27 @@ function collectRedirects(pluginContext, trailingSlash) {
|
|
|
23
23
|
}
|
|
24
24
|
exports.default = collectRedirects;
|
|
25
25
|
// If users wants to redirect to=/abc and they enable trailingSlash=true then
|
|
26
|
-
// => we don't want to reject the to=/abc (as only /abc/ is an existing/valid
|
|
27
|
-
//
|
|
28
|
-
//
|
|
26
|
+
// => we don't want to reject the to=/abc (as only /abc/ is an existing/valid
|
|
27
|
+
// path now)
|
|
28
|
+
// => we want to redirect to=/abc/ without the user having to change all its
|
|
29
|
+
// redirect plugin options
|
|
30
|
+
// It should be easy to toggle siteConfig.trailingSlash option without having to
|
|
31
|
+
// change other configs
|
|
29
32
|
function applyRedirectsTrailingSlash(redirects, params) {
|
|
30
|
-
return redirects.map((redirect) => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
};
|
|
35
|
-
});
|
|
33
|
+
return redirects.map((redirect) => ({
|
|
34
|
+
...redirect,
|
|
35
|
+
to: (0, utils_common_1.applyTrailingSlash)(redirect.to, params),
|
|
36
|
+
}));
|
|
36
37
|
}
|
|
37
38
|
function validateCollectedRedirects(redirects, pluginContext) {
|
|
38
39
|
const redirectValidationErrors = redirects
|
|
39
40
|
.map((redirect) => {
|
|
40
41
|
try {
|
|
41
|
-
redirectValidation_1.validateRedirect(redirect);
|
|
42
|
+
(0, redirectValidation_1.validateRedirect)(redirect);
|
|
42
43
|
return undefined;
|
|
43
44
|
}
|
|
44
|
-
catch (
|
|
45
|
-
return
|
|
45
|
+
catch (err) {
|
|
46
|
+
return err.message;
|
|
46
47
|
}
|
|
47
48
|
})
|
|
48
49
|
.filter(Boolean);
|
|
@@ -53,7 +54,7 @@ function validateCollectedRedirects(redirects, pluginContext) {
|
|
|
53
54
|
}
|
|
54
55
|
const allowedToPaths = pluginContext.relativeRoutesPaths;
|
|
55
56
|
const toPaths = redirects.map((redirect) => redirect.to);
|
|
56
|
-
const illegalToPaths = lodash_1.difference(toPaths, allowedToPaths);
|
|
57
|
+
const illegalToPaths = lodash_1.default.difference(toPaths, allowedToPaths);
|
|
57
58
|
if (illegalToPaths.length > 0) {
|
|
58
59
|
throw new Error(`You are trying to create client-side redirections to paths that do not exist:
|
|
59
60
|
- ${illegalToPaths.join('\n- ')}
|
|
@@ -66,35 +67,31 @@ Valid paths you can redirect to:
|
|
|
66
67
|
function filterUnwantedRedirects(redirects, pluginContext) {
|
|
67
68
|
// we don't want to create twice the same redirect
|
|
68
69
|
// that would lead to writing twice the same html redirection file
|
|
69
|
-
Object.entries(lodash_1.groupBy(redirects, (redirect) => redirect.from)).forEach(([from, groupedFromRedirects]) => {
|
|
70
|
+
Object.entries(lodash_1.default.groupBy(redirects, (redirect) => redirect.from)).forEach(([from, groupedFromRedirects]) => {
|
|
70
71
|
if (groupedFromRedirects.length > 1) {
|
|
71
|
-
|
|
72
|
-
It is not possible to redirect the same pathname to multiple destinations:
|
|
73
|
-
- ${groupedFromRedirects.map((r) => JSON.stringify(r)).join('\n- ')}
|
|
74
|
-
`));
|
|
72
|
+
logger_1.default.error `name=${'@docusaurus/plugin-client-redirects'}: multiple redirects are created with the same "from" pathname: path=${from}
|
|
73
|
+
It is not possible to redirect the same pathname to multiple destinations: ${groupedFromRedirects.map((r) => JSON.stringify(r))}`;
|
|
75
74
|
}
|
|
76
75
|
});
|
|
77
|
-
const collectedRedirects = lodash_1.uniqBy(redirects, (redirect) => redirect.from);
|
|
76
|
+
const collectedRedirects = lodash_1.default.uniqBy(redirects, (redirect) => redirect.from);
|
|
78
77
|
// We don't want to override an already existing route with a redirect file!
|
|
79
78
|
const redirectsOverridingExistingPath = collectedRedirects.filter((redirect) => pluginContext.relativeRoutesPaths.includes(redirect.from));
|
|
80
79
|
if (redirectsOverridingExistingPath.length > 0) {
|
|
81
|
-
|
|
82
|
-
- ${redirectsOverridingExistingPath.map((r) => JSON.stringify(r)).join('\n- ')}
|
|
83
|
-
`));
|
|
80
|
+
logger_1.default.error `name=${'@docusaurus/plugin-client-redirects'}: some redirects would override existing paths, and will be ignored: ${redirectsOverridingExistingPath.map((r) => JSON.stringify(r))}`;
|
|
84
81
|
}
|
|
85
82
|
return collectedRedirects.filter((redirect) => !pluginContext.relativeRoutesPaths.includes(redirect.from));
|
|
86
83
|
}
|
|
87
84
|
// For each plugin config option, create the appropriate redirects
|
|
88
85
|
function doCollectRedirects(pluginContext) {
|
|
89
86
|
return [
|
|
90
|
-
...extensionRedirects_1.createFromExtensionsRedirects(pluginContext.relativeRoutesPaths, pluginContext.options.fromExtensions),
|
|
91
|
-
...extensionRedirects_1.createToExtensionsRedirects(pluginContext.relativeRoutesPaths, pluginContext.options.toExtensions),
|
|
87
|
+
...(0, extensionRedirects_1.createFromExtensionsRedirects)(pluginContext.relativeRoutesPaths, pluginContext.options.fromExtensions),
|
|
88
|
+
...(0, extensionRedirects_1.createToExtensionsRedirects)(pluginContext.relativeRoutesPaths, pluginContext.options.toExtensions),
|
|
92
89
|
...createRedirectsOptionRedirects(pluginContext.options.redirects),
|
|
93
90
|
...createCreateRedirectsOptionRedirects(pluginContext.relativeRoutesPaths, pluginContext.options.createRedirects),
|
|
94
91
|
];
|
|
95
92
|
}
|
|
96
93
|
function createRedirectsOptionRedirects(redirectsOption) {
|
|
97
|
-
// For
|
|
94
|
+
// For convenience, user can use a string or a string[]
|
|
98
95
|
function optionToRedirects(option) {
|
|
99
96
|
if (typeof option.from === 'string') {
|
|
100
97
|
return [{ from: option.from, to: option.to }];
|
|
@@ -104,7 +101,7 @@ function createRedirectsOptionRedirects(redirectsOption) {
|
|
|
104
101
|
to: option.to,
|
|
105
102
|
}));
|
|
106
103
|
}
|
|
107
|
-
return
|
|
104
|
+
return redirectsOption.flatMap(optionToRedirects);
|
|
108
105
|
}
|
|
109
106
|
// Create redirects from the "createRedirects" fn provided by the user
|
|
110
107
|
function createCreateRedirectsOptionRedirects(paths, createRedirects) {
|
|
@@ -113,12 +110,10 @@ function createCreateRedirectsOptionRedirects(paths, createRedirects) {
|
|
|
113
110
|
? createRedirects(path) || []
|
|
114
111
|
: [];
|
|
115
112
|
const froms = typeof fromsMixed === 'string' ? [fromsMixed] : fromsMixed;
|
|
116
|
-
return froms.map((from) => {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
};
|
|
121
|
-
});
|
|
113
|
+
return froms.map((from) => ({
|
|
114
|
+
from,
|
|
115
|
+
to: path,
|
|
116
|
+
}));
|
|
122
117
|
}
|
|
123
|
-
return
|
|
118
|
+
return paths.flatMap(createPathRedirects);
|
|
124
119
|
}
|
|
@@ -9,10 +9,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
10
|
const eta = tslib_1.__importStar(require("eta"));
|
|
11
11
|
const redirectPage_template_html_1 = tslib_1.__importDefault(require("./templates/redirectPage.template.html"));
|
|
12
|
-
const lodash_1 = require("lodash");
|
|
13
|
-
const getCompiledRedirectPageTemplate = lodash_1.memoize(() =>
|
|
14
|
-
return eta.compile(redirectPage_template_html_1.default.trim());
|
|
15
|
-
});
|
|
12
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
13
|
+
const getCompiledRedirectPageTemplate = lodash_1.default.memoize(() => eta.compile(redirectPage_template_html_1.default.trim()));
|
|
16
14
|
function renderRedirectPageTemplate(data) {
|
|
17
15
|
const compiled = getCompiledRedirectPageTemplate();
|
|
18
16
|
return compiled(data, eta.defaultConfig);
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { RedirectMetadata } from './types';
|
|
7
|
+
import type { RedirectMetadata } from './types';
|
|
8
8
|
export declare function createToExtensionsRedirects(paths: string[], extensions: string[]): RedirectMetadata[];
|
|
9
9
|
export declare function createFromExtensionsRedirects(paths: string[], extensions: string[]): RedirectMetadata[];
|
|
@@ -7,9 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.createFromExtensionsRedirects = exports.createToExtensionsRedirects = void 0;
|
|
10
|
-
const lodash_1 = require("lodash");
|
|
11
10
|
const utils_1 = require("@docusaurus/utils");
|
|
12
|
-
const ExtensionAdditionalMessage = 'If the redirect extension system is not good enough for your
|
|
11
|
+
const ExtensionAdditionalMessage = 'If the redirect extension system is not good enough for your use case, you can create redirects yourself with the "createRedirects" plugin option.';
|
|
13
12
|
const validateExtension = (ext) => {
|
|
14
13
|
if (!ext) {
|
|
15
14
|
throw new Error(`Extension "${ext}" is not allowed.\n${ExtensionAdditionalMessage}`);
|
|
@@ -32,7 +31,7 @@ function createToExtensionsRedirects(paths, extensions) {
|
|
|
32
31
|
const createPathRedirects = (path) => {
|
|
33
32
|
const extensionFound = dottedExtensions.find((ext) => path.endsWith(ext));
|
|
34
33
|
if (extensionFound) {
|
|
35
|
-
const routePathWithoutExtension = utils_1.removeSuffix(path, extensionFound);
|
|
34
|
+
const routePathWithoutExtension = (0, utils_1.removeSuffix)(path, extensionFound);
|
|
36
35
|
return [routePathWithoutExtension].map((from) => ({
|
|
37
36
|
from,
|
|
38
37
|
to: path,
|
|
@@ -40,7 +39,7 @@ function createToExtensionsRedirects(paths, extensions) {
|
|
|
40
39
|
}
|
|
41
40
|
return [];
|
|
42
41
|
};
|
|
43
|
-
return
|
|
42
|
+
return paths.flatMap(createPathRedirects);
|
|
44
43
|
}
|
|
45
44
|
exports.createToExtensionsRedirects = createToExtensionsRedirects;
|
|
46
45
|
// Create new /path.html/index.html that redirects to existing an /path
|
|
@@ -53,21 +52,15 @@ function createFromExtensionsRedirects(paths, extensions) {
|
|
|
53
52
|
if (path === '' || path === '/' || alreadyEndsWithAnExtension(path)) {
|
|
54
53
|
return [];
|
|
55
54
|
}
|
|
56
|
-
// /path => /path.html
|
|
57
|
-
// /path/ => /path.html/
|
|
58
|
-
function getFrom(ext) {
|
|
59
|
-
if (path.endsWith('/')) {
|
|
60
|
-
return utils_1.addTrailingSlash(`${utils_1.removeTrailingSlash(path)}.${ext}`);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
return `${path}.${ext}`;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
55
|
return extensions.map((ext) => ({
|
|
67
|
-
|
|
56
|
+
// /path => /path.html
|
|
57
|
+
// /path/ => /path.html/
|
|
58
|
+
from: path.endsWith('/')
|
|
59
|
+
? (0, utils_1.addTrailingSlash)(`${(0, utils_1.removeTrailingSlash)(path)}.${ext}`)
|
|
60
|
+
: `${path}.${ext}`,
|
|
68
61
|
to: path,
|
|
69
62
|
}));
|
|
70
63
|
};
|
|
71
|
-
return
|
|
64
|
+
return paths.flatMap(createPathRedirects);
|
|
72
65
|
}
|
|
73
66
|
exports.createFromExtensionsRedirects = createFromExtensionsRedirects;
|
package/lib/index.d.ts
CHANGED
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { LoadContext, Plugin } from '@docusaurus/types';
|
|
8
|
-
import {
|
|
9
|
-
export default function pluginClientRedirectsPages(context: LoadContext, opts:
|
|
7
|
+
import type { LoadContext, Plugin } from '@docusaurus/types';
|
|
8
|
+
import type { PluginOptions } from '@docusaurus/plugin-client-redirects';
|
|
9
|
+
export default function pluginClientRedirectsPages(context: LoadContext, opts: PluginOptions): Plugin<unknown>;
|
package/lib/index.js
CHANGED
|
@@ -13,20 +13,20 @@ const writeRedirectFiles_1 = tslib_1.__importStar(require("./writeRedirectFiles"
|
|
|
13
13
|
const utils_1 = require("@docusaurus/utils");
|
|
14
14
|
function pluginClientRedirectsPages(context, opts) {
|
|
15
15
|
const { trailingSlash } = context.siteConfig;
|
|
16
|
-
const options = normalizePluginOptions_1.default(opts);
|
|
16
|
+
const options = (0, normalizePluginOptions_1.default)(opts);
|
|
17
17
|
return {
|
|
18
18
|
name: 'docusaurus-plugin-client-redirects',
|
|
19
19
|
async postBuild(props) {
|
|
20
20
|
const pluginContext = {
|
|
21
|
-
relativeRoutesPaths: props.routesPaths.map((path) => `${utils_1.addLeadingSlash(utils_1.removePrefix(path, props.baseUrl))}`),
|
|
21
|
+
relativeRoutesPaths: props.routesPaths.map((path) => `${(0, utils_1.addLeadingSlash)((0, utils_1.removePrefix)(path, props.baseUrl))}`),
|
|
22
22
|
baseUrl: props.baseUrl,
|
|
23
23
|
outDir: props.outDir,
|
|
24
24
|
options,
|
|
25
25
|
};
|
|
26
|
-
const redirects = collectRedirects_1.default(pluginContext, trailingSlash);
|
|
27
|
-
const redirectFiles = writeRedirectFiles_1.toRedirectFilesMetadata(redirects, pluginContext, trailingSlash);
|
|
26
|
+
const redirects = (0, collectRedirects_1.default)(pluginContext, trailingSlash);
|
|
27
|
+
const redirectFiles = (0, writeRedirectFiles_1.toRedirectFilesMetadata)(redirects, pluginContext, trailingSlash);
|
|
28
28
|
// Write files only at the end: make code more easy to test without IO
|
|
29
|
-
await writeRedirectFiles_1.default(redirectFiles);
|
|
29
|
+
await (0, writeRedirectFiles_1.default)(redirectFiles);
|
|
30
30
|
},
|
|
31
31
|
};
|
|
32
32
|
}
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { PluginOptions, UserPluginOptions } from '
|
|
7
|
+
import type { PluginOptions, Options as UserPluginOptions } from '@docusaurus/plugin-client-redirects';
|
|
8
8
|
export declare const DefaultPluginOptions: PluginOptions;
|
|
9
9
|
export default function normalizePluginOptions(userPluginOptions?: UserPluginOptions): PluginOptions;
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.DefaultPluginOptions = void 0;
|
|
10
10
|
const utils_validation_1 = require("@docusaurus/utils-validation");
|
|
11
|
-
const
|
|
11
|
+
const utils_1 = require("@docusaurus/utils");
|
|
12
12
|
exports.DefaultPluginOptions = {
|
|
13
|
-
id:
|
|
13
|
+
id: utils_1.DEFAULT_PLUGIN_ID,
|
|
14
14
|
fromExtensions: [],
|
|
15
15
|
toExtensions: [],
|
|
16
16
|
redirects: [],
|
|
@@ -4,5 +4,5 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { RedirectMetadata } from './types';
|
|
7
|
+
import type { RedirectMetadata } from './types';
|
|
8
8
|
export declare function validateRedirect(redirect: RedirectMetadata): void;
|
package/lib/types.d.ts
CHANGED
|
@@ -4,20 +4,8 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { Props } from '@docusaurus/types';
|
|
8
|
-
|
|
9
|
-
id: string;
|
|
10
|
-
fromExtensions: string[];
|
|
11
|
-
toExtensions: string[];
|
|
12
|
-
redirects: RedirectOption[];
|
|
13
|
-
createRedirects?: CreateRedirectsFnOption;
|
|
14
|
-
};
|
|
15
|
-
export declare type CreateRedirectsFnOption = (path: string) => string[] | string | null | undefined;
|
|
16
|
-
export declare type RedirectOption = {
|
|
17
|
-
to: string;
|
|
18
|
-
from: string | string[];
|
|
19
|
-
};
|
|
20
|
-
export declare type UserPluginOptions = Partial<PluginOptions>;
|
|
7
|
+
import type { Props } from '@docusaurus/types';
|
|
8
|
+
import type { PluginOptions } from '@docusaurus/plugin-client-redirects';
|
|
21
9
|
export declare type PluginContext = Pick<Props, 'outDir' | 'baseUrl'> & {
|
|
22
10
|
options: PluginOptions;
|
|
23
11
|
relativeRoutesPaths: string[];
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { PluginContext, RedirectMetadata } from './types';
|
|
7
|
+
import type { PluginContext, RedirectMetadata } from './types';
|
|
8
8
|
export declare type WriteFilesPluginContext = Pick<PluginContext, 'baseUrl' | 'outDir'>;
|
|
9
9
|
export declare type RedirectFileMetadata = {
|
|
10
10
|
fileAbsolutePath: string;
|
|
@@ -10,15 +10,16 @@ exports.writeRedirectFile = exports.toRedirectFilesMetadata = exports.createToUr
|
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
11
|
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
12
12
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
13
|
-
const lodash_1 = require("lodash");
|
|
13
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
14
14
|
const createRedirectPageContent_1 = tslib_1.__importDefault(require("./createRedirectPageContent"));
|
|
15
15
|
const utils_1 = require("@docusaurus/utils");
|
|
16
16
|
function createToUrl(baseUrl, to) {
|
|
17
|
-
return utils_1.normalizeUrl([baseUrl, to]);
|
|
17
|
+
return (0, utils_1.normalizeUrl)([baseUrl, to]);
|
|
18
18
|
}
|
|
19
19
|
exports.createToUrl = createToUrl;
|
|
20
20
|
// Create redirect file path
|
|
21
|
-
// Make sure this path has lower precedence over the original file path when
|
|
21
|
+
// Make sure this path has lower precedence over the original file path when
|
|
22
|
+
// served by host providers!
|
|
22
23
|
// Otherwise it can produce infinite redirect loops!
|
|
23
24
|
//
|
|
24
25
|
// See https://github.com/facebook/docusaurus/issues/5055
|
|
@@ -29,25 +30,25 @@ function getRedirectFilePath(fromPath, trailingSlash) {
|
|
|
29
30
|
const filePath = path_1.default.dirname(fromPath);
|
|
30
31
|
// Edge case for https://github.com/facebook/docusaurus/pull/5102
|
|
31
32
|
// If the redirect source path is /xyz, with file /xyz.html
|
|
32
|
-
// We can't write the redirect file at /xyz.html/index.html because for Unix
|
|
33
|
-
//
|
|
33
|
+
// We can't write the redirect file at /xyz.html/index.html because for Unix
|
|
34
|
+
// FS, a file/folder can't have the same name "xyz.html"
|
|
35
|
+
// The only possible solution for a redirect file is thus /xyz.html.html (I
|
|
36
|
+
// know, looks suspicious)
|
|
34
37
|
if (trailingSlash === false && fileName.endsWith('.html')) {
|
|
35
38
|
return path_1.default.join(filePath, `${fileName}.html`);
|
|
36
39
|
}
|
|
37
|
-
// If the target path is /xyz, with file /xyz/index.html, we don't want the
|
|
38
|
-
// otherwise it would be picked in priority and
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
40
|
+
// If the target path is /xyz, with file /xyz/index.html, we don't want the
|
|
41
|
+
// redirect file to be /xyz.html, otherwise it would be picked in priority and
|
|
42
|
+
// the redirect file would redirect to itself. We prefer the redirect file to
|
|
43
|
+
// be /xyz.html/index.html, served with lower priority for most static hosting
|
|
44
|
+
// tools
|
|
45
|
+
return path_1.default.join(filePath, `${fileName}/index.html`);
|
|
43
46
|
}
|
|
44
47
|
function toRedirectFilesMetadata(redirects, pluginContext, trailingSlash) {
|
|
45
48
|
// Perf: avoid rendering the template twice with the exact same "props"
|
|
46
49
|
// We might create multiple redirect pages for the same destination url
|
|
47
50
|
// note: the first fn arg is the cache key!
|
|
48
|
-
const createPageContentMemoized = lodash_1.memoize((toUrl) => {
|
|
49
|
-
return createRedirectPageContent_1.default({ toUrl });
|
|
50
|
-
});
|
|
51
|
+
const createPageContentMemoized = lodash_1.default.memoize((toUrl) => (0, createRedirectPageContent_1.default)({ toUrl }));
|
|
51
52
|
const createFileMetadata = (redirect) => {
|
|
52
53
|
const fileRelativePath = getRedirectFilePath(redirect.from, trailingSlash);
|
|
53
54
|
const fileAbsolutePath = path_1.default.join(pluginContext.outDir, fileRelativePath);
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/plugin-client-redirects",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.17",
|
|
4
4
|
"description": "Client redirects plugin for Docusaurus.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"types": "src/plugin-client-redirects.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"build": "tsc",
|
|
8
9
|
"watch": "tsc --watch"
|
|
@@ -17,24 +18,25 @@
|
|
|
17
18
|
},
|
|
18
19
|
"license": "MIT",
|
|
19
20
|
"dependencies": {
|
|
20
|
-
"@docusaurus/core": "2.0.0-beta.
|
|
21
|
-
"@docusaurus/
|
|
22
|
-
"@docusaurus/utils": "2.0.0-beta.
|
|
23
|
-
"@docusaurus/utils-common": "2.0.0-beta.
|
|
24
|
-
"@docusaurus/utils-validation": "2.0.0-beta.
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
21
|
+
"@docusaurus/core": "2.0.0-beta.17",
|
|
22
|
+
"@docusaurus/logger": "2.0.0-beta.17",
|
|
23
|
+
"@docusaurus/utils": "2.0.0-beta.17",
|
|
24
|
+
"@docusaurus/utils-common": "2.0.0-beta.17",
|
|
25
|
+
"@docusaurus/utils-validation": "2.0.0-beta.17",
|
|
26
|
+
"eta": "^1.12.3",
|
|
27
|
+
"fs-extra": "^10.0.1",
|
|
28
|
+
"lodash": "^4.17.21",
|
|
29
|
+
"tslib": "^2.3.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@docusaurus/types": "2.0.0-beta.17"
|
|
31
33
|
},
|
|
32
34
|
"peerDependencies": {
|
|
33
35
|
"react": "^16.8.4 || ^17.0.0",
|
|
34
36
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
35
37
|
},
|
|
36
38
|
"engines": {
|
|
37
|
-
"node": ">=
|
|
39
|
+
"node": ">=14"
|
|
38
40
|
},
|
|
39
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "0032c0b0480083227af2e1b4da2d3ee6ce992403"
|
|
40
42
|
}
|
package/src/collectRedirects.ts
CHANGED
|
@@ -5,13 +5,12 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
PluginContext,
|
|
11
|
-
RedirectMetadata,
|
|
8
|
+
import _ from 'lodash';
|
|
9
|
+
import type {
|
|
12
10
|
PluginOptions,
|
|
13
11
|
RedirectOption,
|
|
14
|
-
} from '
|
|
12
|
+
} from '@docusaurus/plugin-client-redirects';
|
|
13
|
+
import type {PluginContext, RedirectMetadata} from './types';
|
|
15
14
|
import {
|
|
16
15
|
createFromExtensionsRedirects,
|
|
17
16
|
createToExtensionsRedirects,
|
|
@@ -19,10 +18,10 @@ import {
|
|
|
19
18
|
import {validateRedirect} from './redirectValidation';
|
|
20
19
|
import {
|
|
21
20
|
applyTrailingSlash,
|
|
22
|
-
ApplyTrailingSlashParams,
|
|
21
|
+
type ApplyTrailingSlashParams,
|
|
23
22
|
} from '@docusaurus/utils-common';
|
|
24
23
|
|
|
25
|
-
import
|
|
24
|
+
import logger from '@docusaurus/logger';
|
|
26
25
|
|
|
27
26
|
export default function collectRedirects(
|
|
28
27
|
pluginContext: PluginContext,
|
|
@@ -40,32 +39,33 @@ export default function collectRedirects(
|
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
// If users wants to redirect to=/abc and they enable trailingSlash=true then
|
|
43
|
-
// => we don't want to reject the to=/abc (as only /abc/ is an existing/valid
|
|
44
|
-
//
|
|
45
|
-
//
|
|
42
|
+
// => we don't want to reject the to=/abc (as only /abc/ is an existing/valid
|
|
43
|
+
// path now)
|
|
44
|
+
// => we want to redirect to=/abc/ without the user having to change all its
|
|
45
|
+
// redirect plugin options
|
|
46
|
+
// It should be easy to toggle siteConfig.trailingSlash option without having to
|
|
47
|
+
// change other configs
|
|
46
48
|
function applyRedirectsTrailingSlash(
|
|
47
49
|
redirects: RedirectMetadata[],
|
|
48
50
|
params: ApplyTrailingSlashParams,
|
|
49
51
|
) {
|
|
50
|
-
return redirects.map((redirect) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
};
|
|
55
|
-
});
|
|
52
|
+
return redirects.map((redirect) => ({
|
|
53
|
+
...redirect,
|
|
54
|
+
to: applyTrailingSlash(redirect.to, params),
|
|
55
|
+
}));
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
function validateCollectedRedirects(
|
|
59
59
|
redirects: RedirectMetadata[],
|
|
60
60
|
pluginContext: PluginContext,
|
|
61
61
|
) {
|
|
62
|
-
const redirectValidationErrors
|
|
62
|
+
const redirectValidationErrors = redirects
|
|
63
63
|
.map((redirect) => {
|
|
64
64
|
try {
|
|
65
65
|
validateRedirect(redirect);
|
|
66
66
|
return undefined;
|
|
67
|
-
} catch (
|
|
68
|
-
return
|
|
67
|
+
} catch (err) {
|
|
68
|
+
return (err as Error).message;
|
|
69
69
|
}
|
|
70
70
|
})
|
|
71
71
|
.filter(Boolean);
|
|
@@ -79,7 +79,7 @@ function validateCollectedRedirects(
|
|
|
79
79
|
|
|
80
80
|
const allowedToPaths = pluginContext.relativeRoutesPaths;
|
|
81
81
|
const toPaths = redirects.map((redirect) => redirect.to);
|
|
82
|
-
const illegalToPaths = difference(toPaths, allowedToPaths);
|
|
82
|
+
const illegalToPaths = _.difference(toPaths, allowedToPaths);
|
|
83
83
|
if (illegalToPaths.length > 0) {
|
|
84
84
|
throw new Error(
|
|
85
85
|
`You are trying to create client-side redirections to paths that do not exist:
|
|
@@ -98,34 +98,26 @@ function filterUnwantedRedirects(
|
|
|
98
98
|
): RedirectMetadata[] {
|
|
99
99
|
// we don't want to create twice the same redirect
|
|
100
100
|
// that would lead to writing twice the same html redirection file
|
|
101
|
-
Object.entries(groupBy(redirects, (redirect) => redirect.from)).forEach(
|
|
101
|
+
Object.entries(_.groupBy(redirects, (redirect) => redirect.from)).forEach(
|
|
102
102
|
([from, groupedFromRedirects]) => {
|
|
103
103
|
if (groupedFromRedirects.length > 1) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
- ${groupedFromRedirects.map((r) => JSON.stringify(r)).join('\n- ')}
|
|
109
|
-
`,
|
|
110
|
-
),
|
|
111
|
-
);
|
|
104
|
+
logger.error`name=${'@docusaurus/plugin-client-redirects'}: multiple redirects are created with the same "from" pathname: path=${from}
|
|
105
|
+
It is not possible to redirect the same pathname to multiple destinations: ${groupedFromRedirects.map(
|
|
106
|
+
(r) => JSON.stringify(r),
|
|
107
|
+
)}`;
|
|
112
108
|
}
|
|
113
109
|
},
|
|
114
110
|
);
|
|
115
|
-
const collectedRedirects = uniqBy(redirects, (redirect) => redirect.from);
|
|
111
|
+
const collectedRedirects = _.uniqBy(redirects, (redirect) => redirect.from);
|
|
116
112
|
|
|
117
113
|
// We don't want to override an already existing route with a redirect file!
|
|
118
114
|
const redirectsOverridingExistingPath = collectedRedirects.filter(
|
|
119
115
|
(redirect) => pluginContext.relativeRoutesPaths.includes(redirect.from),
|
|
120
116
|
);
|
|
121
117
|
if (redirectsOverridingExistingPath.length > 0) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
- ${redirectsOverridingExistingPath.map((r) => JSON.stringify(r)).join('\n- ')}
|
|
126
|
-
`,
|
|
127
|
-
),
|
|
128
|
-
);
|
|
118
|
+
logger.error`name=${'@docusaurus/plugin-client-redirects'}: some redirects would override existing paths, and will be ignored: ${redirectsOverridingExistingPath.map(
|
|
119
|
+
(r) => JSON.stringify(r),
|
|
120
|
+
)}`;
|
|
129
121
|
}
|
|
130
122
|
return collectedRedirects.filter(
|
|
131
123
|
(redirect) => !pluginContext.relativeRoutesPaths.includes(redirect.from),
|
|
@@ -154,7 +146,7 @@ function doCollectRedirects(pluginContext: PluginContext): RedirectMetadata[] {
|
|
|
154
146
|
function createRedirectsOptionRedirects(
|
|
155
147
|
redirectsOption: PluginOptions['redirects'],
|
|
156
148
|
): RedirectMetadata[] {
|
|
157
|
-
// For
|
|
149
|
+
// For convenience, user can use a string or a string[]
|
|
158
150
|
function optionToRedirects(option: RedirectOption): RedirectMetadata[] {
|
|
159
151
|
if (typeof option.from === 'string') {
|
|
160
152
|
return [{from: option.from, to: option.to}];
|
|
@@ -165,7 +157,7 @@ function createRedirectsOptionRedirects(
|
|
|
165
157
|
}));
|
|
166
158
|
}
|
|
167
159
|
|
|
168
|
-
return
|
|
160
|
+
return redirectsOption.flatMap(optionToRedirects);
|
|
169
161
|
}
|
|
170
162
|
|
|
171
163
|
// Create redirects from the "createRedirects" fn provided by the user
|
|
@@ -181,13 +173,11 @@ function createCreateRedirectsOptionRedirects(
|
|
|
181
173
|
const froms: string[] =
|
|
182
174
|
typeof fromsMixed === 'string' ? [fromsMixed] : fromsMixed;
|
|
183
175
|
|
|
184
|
-
return froms.map((from) => {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
};
|
|
189
|
-
});
|
|
176
|
+
return froms.map((from) => ({
|
|
177
|
+
from,
|
|
178
|
+
to: path,
|
|
179
|
+
}));
|
|
190
180
|
}
|
|
191
181
|
|
|
192
|
-
return
|
|
182
|
+
return paths.flatMap(createPathRedirects);
|
|
193
183
|
}
|
|
@@ -7,15 +7,15 @@
|
|
|
7
7
|
|
|
8
8
|
import * as eta from 'eta';
|
|
9
9
|
import redirectPageTemplate from './templates/redirectPage.template.html';
|
|
10
|
-
import
|
|
10
|
+
import _ from 'lodash';
|
|
11
11
|
|
|
12
12
|
type CreateRedirectPageOptions = {
|
|
13
13
|
toUrl: string;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
const getCompiledRedirectPageTemplate = memoize(() =>
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
const getCompiledRedirectPageTemplate = _.memoize(() =>
|
|
17
|
+
eta.compile(redirectPageTemplate.trim()),
|
|
18
|
+
);
|
|
19
19
|
|
|
20
20
|
function renderRedirectPageTemplate(data: Record<string, unknown>) {
|
|
21
21
|
const compiled = getCompiledRedirectPageTemplate();
|