@docusaurus/plugin-client-redirects 2.0.0-beta.1ec2c95e3 → 2.0.0-beta.21
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 +34 -41
- package/lib/createRedirectPageContent.d.ts +2 -4
- package/lib/createRedirectPageContent.js +2 -4
- package/lib/extensionRedirects.d.ts +1 -1
- package/lib/extensionRedirects.js +9 -16
- package/lib/index.d.ts +5 -3
- package/lib/index.js +9 -8
- package/lib/options.d.ts +31 -0
- package/lib/options.js +36 -0
- package/lib/redirectValidation.d.ts +1 -1
- package/lib/types.d.ts +12 -14
- package/lib/writeRedirectFiles.d.ts +1 -1
- package/lib/writeRedirectFiles.js +33 -21
- package/package.json +16 -14
- package/src/collectRedirects.ts +40 -56
- package/src/createRedirectPageContent.ts +8 -10
- package/src/extensionRedirects.ts +9 -17
- package/src/index.ts +11 -11
- package/src/options.ts +71 -0
- package/src/redirectValidation.ts +1 -1
- package/src/types.ts +14 -28
- package/src/writeRedirectFiles.ts +38 -28
- package/lib/.tsbuildinfo +0 -1
- package/lib/normalizePluginOptions.d.ts +0 -9
- package/lib/normalizePluginOptions.js +0 -44
- 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 -203
- package/src/normalizePluginOptions.ts +0 -55
- 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
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
12
|
+
const utils_common_1 = require("@docusaurus/utils-common");
|
|
11
13
|
const extensionRedirects_1 = require("./extensionRedirects");
|
|
12
14
|
const redirectValidation_1 = require("./redirectValidation");
|
|
13
|
-
const utils_common_1 = require("@docusaurus/utils-common");
|
|
14
|
-
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
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- ')}
|
|
@@ -64,37 +65,33 @@ Valid paths you can redirect to:
|
|
|
64
65
|
}
|
|
65
66
|
}
|
|
66
67
|
function filterUnwantedRedirects(redirects, pluginContext) {
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
Object.entries(lodash_1.groupBy(redirects, (redirect) => redirect.from)).forEach(([from, groupedFromRedirects]) => {
|
|
68
|
+
// We don't want to create the same redirect twice, since that would lead to
|
|
69
|
+
// writing the same html redirection file twice.
|
|
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,21 +101,17 @@ 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) {
|
|
111
108
|
function createPathRedirects(path) {
|
|
112
|
-
const fromsMixed = createRedirects
|
|
113
|
-
? createRedirects(path) || []
|
|
114
|
-
: [];
|
|
109
|
+
const fromsMixed = createRedirects?.(path) ?? [];
|
|
115
110
|
const froms = typeof fromsMixed === 'string' ? [fromsMixed] : fromsMixed;
|
|
116
|
-
return froms.map((from) => {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
};
|
|
121
|
-
});
|
|
111
|
+
return froms.map((from) => ({
|
|
112
|
+
from,
|
|
113
|
+
to: path,
|
|
114
|
+
}));
|
|
122
115
|
}
|
|
123
|
-
return
|
|
116
|
+
return paths.flatMap(createPathRedirects);
|
|
124
117
|
}
|
|
@@ -4,8 +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
|
-
|
|
7
|
+
export default function createRedirectPageContent({ toUrl, }: {
|
|
8
8
|
toUrl: string;
|
|
9
|
-
};
|
|
10
|
-
export default function createRedirectPageContent({ toUrl, }: CreateRedirectPageOptions): string;
|
|
11
|
-
export {};
|
|
9
|
+
}): string;
|
|
@@ -7,12 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
10
11
|
const eta = tslib_1.__importStar(require("eta"));
|
|
11
12
|
const redirectPage_template_html_1 = tslib_1.__importDefault(require("./templates/redirectPage.template.html"));
|
|
12
|
-
const
|
|
13
|
-
const getCompiledRedirectPageTemplate = lodash_1.memoize(() => {
|
|
14
|
-
return eta.compile(redirectPage_template_html_1.default.trim());
|
|
15
|
-
});
|
|
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,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 { LoadContext, Plugin } from '@docusaurus/types';
|
|
8
|
-
import {
|
|
9
|
-
export default function pluginClientRedirectsPages(context: LoadContext,
|
|
7
|
+
import type { LoadContext, Plugin } from '@docusaurus/types';
|
|
8
|
+
import type { PluginOptions, Options } from './options';
|
|
9
|
+
export default function pluginClientRedirectsPages(context: LoadContext, options: PluginOptions): Plugin<void>;
|
|
10
|
+
export { validateOptions } from './options';
|
|
11
|
+
export type { PluginOptions, Options };
|
package/lib/index.js
CHANGED
|
@@ -6,28 +6,29 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.validateOptions = void 0;
|
|
9
10
|
const tslib_1 = require("tslib");
|
|
10
|
-
const
|
|
11
|
+
const utils_1 = require("@docusaurus/utils");
|
|
11
12
|
const collectRedirects_1 = tslib_1.__importDefault(require("./collectRedirects"));
|
|
12
13
|
const writeRedirectFiles_1 = tslib_1.__importStar(require("./writeRedirectFiles"));
|
|
13
|
-
|
|
14
|
-
function pluginClientRedirectsPages(context, opts) {
|
|
14
|
+
function pluginClientRedirectsPages(context, options) {
|
|
15
15
|
const { trailingSlash } = context.siteConfig;
|
|
16
|
-
const options = normalizePluginOptions_1.default(opts);
|
|
17
16
|
return {
|
|
18
17
|
name: 'docusaurus-plugin-client-redirects',
|
|
19
18
|
async postBuild(props) {
|
|
20
19
|
const pluginContext = {
|
|
21
|
-
relativeRoutesPaths: props.routesPaths.map((path) => `${utils_1.addLeadingSlash(utils_1.removePrefix(path, props.baseUrl))}`),
|
|
20
|
+
relativeRoutesPaths: props.routesPaths.map((path) => `${(0, utils_1.addLeadingSlash)((0, utils_1.removePrefix)(path, props.baseUrl))}`),
|
|
22
21
|
baseUrl: props.baseUrl,
|
|
23
22
|
outDir: props.outDir,
|
|
24
23
|
options,
|
|
25
24
|
};
|
|
26
|
-
const redirects = collectRedirects_1.default(pluginContext, trailingSlash);
|
|
27
|
-
const redirectFiles = writeRedirectFiles_1.toRedirectFilesMetadata(redirects, pluginContext, trailingSlash);
|
|
25
|
+
const redirects = (0, collectRedirects_1.default)(pluginContext, trailingSlash);
|
|
26
|
+
const redirectFiles = (0, writeRedirectFiles_1.toRedirectFilesMetadata)(redirects, pluginContext, trailingSlash);
|
|
28
27
|
// Write files only at the end: make code more easy to test without IO
|
|
29
|
-
await writeRedirectFiles_1.default(redirectFiles);
|
|
28
|
+
await (0, writeRedirectFiles_1.default)(redirectFiles);
|
|
30
29
|
},
|
|
31
30
|
};
|
|
32
31
|
}
|
|
33
32
|
exports.default = pluginClientRedirectsPages;
|
|
33
|
+
var options_1 = require("./options");
|
|
34
|
+
Object.defineProperty(exports, "validateOptions", { enumerable: true, get: function () { return options_1.validateOptions; } });
|
package/lib/options.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { OptionValidationContext } from '@docusaurus/types';
|
|
8
|
+
export declare type RedirectOption = {
|
|
9
|
+
to: string;
|
|
10
|
+
from: string | string[];
|
|
11
|
+
};
|
|
12
|
+
export declare type PluginOptions = {
|
|
13
|
+
/** Plugin ID. */
|
|
14
|
+
id: string;
|
|
15
|
+
/** The extensions to be removed from the route after redirecting. */
|
|
16
|
+
fromExtensions: string[];
|
|
17
|
+
/** The extensions to be appended to the route after redirecting. */
|
|
18
|
+
toExtensions: string[];
|
|
19
|
+
/** The list of redirect rules, each one with multiple `from`s → one `to`. */
|
|
20
|
+
redirects: RedirectOption[];
|
|
21
|
+
/**
|
|
22
|
+
* A callback to create a redirect rule.
|
|
23
|
+
* @returns All the paths from which we should redirect to `path`
|
|
24
|
+
*/
|
|
25
|
+
createRedirects?: (
|
|
26
|
+
/** An existing Docusaurus route path */
|
|
27
|
+
path: string) => string[] | string | null | undefined;
|
|
28
|
+
};
|
|
29
|
+
export declare type Options = Partial<PluginOptions>;
|
|
30
|
+
export declare const DEFAULT_OPTIONS: Partial<PluginOptions>;
|
|
31
|
+
export declare function validateOptions({ validate, options: userOptions, }: OptionValidationContext<Options | undefined, PluginOptions>): PluginOptions;
|
package/lib/options.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.validateOptions = exports.DEFAULT_OPTIONS = void 0;
|
|
10
|
+
const utils_validation_1 = require("@docusaurus/utils-validation");
|
|
11
|
+
exports.DEFAULT_OPTIONS = {
|
|
12
|
+
fromExtensions: [],
|
|
13
|
+
toExtensions: [],
|
|
14
|
+
redirects: [],
|
|
15
|
+
};
|
|
16
|
+
const RedirectPluginOptionValidation = utils_validation_1.Joi.object({
|
|
17
|
+
to: utils_validation_1.PathnameSchema.required(),
|
|
18
|
+
from: utils_validation_1.Joi.alternatives().try(utils_validation_1.PathnameSchema.required(), utils_validation_1.Joi.array().items(utils_validation_1.PathnameSchema.required())),
|
|
19
|
+
});
|
|
20
|
+
const isString = utils_validation_1.Joi.string().required().not(null);
|
|
21
|
+
const UserOptionsSchema = utils_validation_1.Joi.object({
|
|
22
|
+
fromExtensions: utils_validation_1.Joi.array()
|
|
23
|
+
.items(isString)
|
|
24
|
+
.default(exports.DEFAULT_OPTIONS.fromExtensions),
|
|
25
|
+
toExtensions: utils_validation_1.Joi.array()
|
|
26
|
+
.items(isString)
|
|
27
|
+
.default(exports.DEFAULT_OPTIONS.toExtensions),
|
|
28
|
+
redirects: utils_validation_1.Joi.array()
|
|
29
|
+
.items(RedirectPluginOptionValidation)
|
|
30
|
+
.default(exports.DEFAULT_OPTIONS.redirects),
|
|
31
|
+
createRedirects: utils_validation_1.Joi.function().maxArity(1),
|
|
32
|
+
}).default(exports.DEFAULT_OPTIONS);
|
|
33
|
+
function validateOptions({ validate, options: userOptions, }) {
|
|
34
|
+
return validate(UserOptionsSchema, userOptions);
|
|
35
|
+
}
|
|
36
|
+
exports.validateOptions = validateOptions;
|
|
@@ -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,25 +4,23 @@
|
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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 './options';
|
|
9
|
+
/**
|
|
10
|
+
* The minimal infos the plugin needs to work
|
|
11
|
+
*/
|
|
21
12
|
export declare type PluginContext = Pick<Props, 'outDir' | 'baseUrl'> & {
|
|
22
13
|
options: PluginOptions;
|
|
23
14
|
relativeRoutesPaths: string[];
|
|
24
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* In-memory representation of redirects we want: easier to test
|
|
18
|
+
* /!\ easy to be confused: "from" is the new page we should create,
|
|
19
|
+
* that redirects to "to": the existing Docusaurus page
|
|
20
|
+
*/
|
|
25
21
|
export declare type RedirectMetadata = {
|
|
22
|
+
/** Pathname of the new page we should create */
|
|
26
23
|
from: string;
|
|
24
|
+
/** Pathname of an existing Docusaurus page */
|
|
27
25
|
to: string;
|
|
28
26
|
};
|
|
@@ -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,36 +10,48 @@ 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");
|
|
14
|
-
const
|
|
13
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
14
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
15
15
|
const utils_1 = require("@docusaurus/utils");
|
|
16
|
+
const createRedirectPageContent_1 = tslib_1.__importDefault(require("./createRedirectPageContent"));
|
|
16
17
|
function createToUrl(baseUrl, to) {
|
|
17
|
-
return utils_1.normalizeUrl([baseUrl, to]);
|
|
18
|
+
return (0, utils_1.normalizeUrl)([baseUrl, to]);
|
|
18
19
|
}
|
|
19
20
|
exports.createToUrl = createToUrl;
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
21
|
+
// Create redirect file path
|
|
22
|
+
// Make sure this path has lower precedence over the original file path when
|
|
23
|
+
// served by host providers!
|
|
24
|
+
// Otherwise it can produce infinite redirect loops!
|
|
24
25
|
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
// See
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
26
|
+
// See https://github.com/facebook/docusaurus/issues/5055
|
|
27
|
+
// See https://github.com/facebook/docusaurus/pull/5085
|
|
28
|
+
// See https://github.com/facebook/docusaurus/pull/5102
|
|
29
|
+
function getRedirectFilePath(fromPath, trailingSlash) {
|
|
30
|
+
const fileName = path_1.default.basename(fromPath);
|
|
31
|
+
const filePath = path_1.default.dirname(fromPath);
|
|
32
|
+
// Edge case for https://github.com/facebook/docusaurus/pull/5102
|
|
33
|
+
// If the redirect source path is /xyz, with file /xyz.html
|
|
34
|
+
// We can't write the redirect file at /xyz.html/index.html because for Unix
|
|
35
|
+
// FS, a file/folder can't have the same name "xyz.html"
|
|
36
|
+
// The only possible solution for a redirect file is thus /xyz.html.html (I
|
|
37
|
+
// know, looks suspicious)
|
|
38
|
+
if (trailingSlash === false && fileName.endsWith('.html')) {
|
|
39
|
+
return path_1.default.join(filePath, `${fileName}.html`);
|
|
40
|
+
}
|
|
41
|
+
// If the target path is /xyz, with file /xyz/index.html, we don't want the
|
|
42
|
+
// redirect file to be /xyz.html, otherwise it would be picked in priority and
|
|
43
|
+
// the redirect file would redirect to itself. We prefer the redirect file to
|
|
44
|
+
// be /xyz.html/index.html, served with lower priority for most static hosting
|
|
45
|
+
// tools
|
|
32
46
|
return path_1.default.join(filePath, `${fileName}/index.html`);
|
|
33
47
|
}
|
|
34
48
|
function toRedirectFilesMetadata(redirects, pluginContext, trailingSlash) {
|
|
35
49
|
// Perf: avoid rendering the template twice with the exact same "props"
|
|
36
50
|
// We might create multiple redirect pages for the same destination url
|
|
37
51
|
// note: the first fn arg is the cache key!
|
|
38
|
-
const createPageContentMemoized = lodash_1.memoize((toUrl) => {
|
|
39
|
-
return createRedirectPageContent_1.default({ toUrl });
|
|
40
|
-
});
|
|
52
|
+
const createPageContentMemoized = lodash_1.default.memoize((toUrl) => (0, createRedirectPageContent_1.default)({ toUrl }));
|
|
41
53
|
const createFileMetadata = (redirect) => {
|
|
42
|
-
const fileRelativePath =
|
|
54
|
+
const fileRelativePath = getRedirectFilePath(redirect.from, trailingSlash);
|
|
43
55
|
const fileAbsolutePath = path_1.default.join(pluginContext.outDir, fileRelativePath);
|
|
44
56
|
const toUrl = createToUrl(pluginContext.baseUrl, redirect.to);
|
|
45
57
|
const fileContent = createPageContentMemoized(toUrl);
|
|
@@ -58,14 +70,14 @@ async function writeRedirectFile(file) {
|
|
|
58
70
|
if (await fs_extra_1.default.pathExists(file.fileAbsolutePath)) {
|
|
59
71
|
throw new Error('The redirect plugin is not supposed to override existing files.');
|
|
60
72
|
}
|
|
61
|
-
await fs_extra_1.default.
|
|
62
|
-
await fs_extra_1.default.writeFile(file.fileAbsolutePath, file.fileContent,
|
|
73
|
+
await fs_extra_1.default.outputFile(file.fileAbsolutePath, file.fileContent,
|
|
63
74
|
// Hard security to prevent file overrides
|
|
64
75
|
// See https://stackoverflow.com/a/34187712/82609
|
|
65
76
|
{ flag: 'wx' });
|
|
66
77
|
}
|
|
67
78
|
catch (err) {
|
|
68
|
-
|
|
79
|
+
logger_1.default.error `Redirect file creation error for path=${file.fileAbsolutePath}.`;
|
|
80
|
+
throw err;
|
|
69
81
|
}
|
|
70
82
|
}
|
|
71
83
|
exports.writeRedirectFile = writeRedirectFile;
|
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.21",
|
|
4
4
|
"description": "Client redirects plugin for Docusaurus.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.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.21",
|
|
22
|
+
"@docusaurus/logger": "2.0.0-beta.21",
|
|
23
|
+
"@docusaurus/utils": "2.0.0-beta.21",
|
|
24
|
+
"@docusaurus/utils-common": "2.0.0-beta.21",
|
|
25
|
+
"@docusaurus/utils-validation": "2.0.0-beta.21",
|
|
26
|
+
"eta": "^1.12.3",
|
|
27
|
+
"fs-extra": "^10.1.0",
|
|
28
|
+
"lodash": "^4.17.21",
|
|
29
|
+
"tslib": "^2.4.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@docusaurus/types": "2.0.0-beta.21"
|
|
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": ">=16.14"
|
|
38
40
|
},
|
|
39
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "69ac49fc6909517f13615ee40290c4bd00c39df4"
|
|
40
42
|
}
|