@docusaurus/plugin-client-redirects 2.0.0-beta.15d451942 → 2.0.0-beta.18
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 +2 -2
- package/lib/collectRedirects.js +41 -31
- package/lib/createRedirectPageContent.d.ts +2 -4
- package/lib/createRedirectPageContent.js +2 -4
- package/lib/extensionRedirects.d.ts +1 -1
- package/lib/extensionRedirects.js +16 -12
- package/lib/index.d.ts +4 -3
- package/lib/index.js +9 -7
- package/lib/options.d.ts +10 -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 +2 -2
- package/lib/writeRedirectFiles.js +36 -11
- package/package.json +16 -13
- package/src/collectRedirects.ts +55 -40
- package/src/createRedirectPageContent.ts +8 -10
- package/src/extensionRedirects.ts +21 -15
- package/src/index.ts +15 -9
- package/src/options.ts +50 -0
- package/src/plugin-client-redirects.d.ts +32 -0
- package/src/redirectValidation.ts +1 -1
- package/src/types.ts +14 -28
- package/src/writeRedirectFiles.ts +43 -14
- package/lib/.tsbuildinfo +0 -1970
- 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 -71
- package/src/__tests__/collectRedirects.test.ts +0 -255
- package/src/__tests__/createRedirectPageContent.test.ts +0 -26
- package/src/__tests__/extensionRedirects.test.ts +0 -109
- package/src/__tests__/normalizePluginOptions.test.ts +0 -76
- package/src/__tests__/redirectValidation.test.ts +0 -66
- package/src/__tests__/writeRedirectFiles.test.ts +0 -146
- 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';
|
|
8
|
-
export default function collectRedirects(pluginContext: PluginContext): RedirectMetadata[];
|
|
7
|
+
import type { PluginContext, RedirectMetadata } from './types';
|
|
8
|
+
export default function collectRedirects(pluginContext: PluginContext, trailingSlash: boolean | undefined): RedirectMetadata[];
|
package/lib/collectRedirects.js
CHANGED
|
@@ -7,25 +7,43 @@
|
|
|
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
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
const utils_common_1 = require("@docusaurus/utils-common");
|
|
14
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
15
|
+
function collectRedirects(pluginContext, trailingSlash) {
|
|
16
|
+
let redirects = doCollectRedirects(pluginContext);
|
|
17
|
+
redirects = applyRedirectsTrailingSlash(redirects, {
|
|
18
|
+
trailingSlash,
|
|
19
|
+
baseUrl: pluginContext.baseUrl,
|
|
20
|
+
});
|
|
16
21
|
validateCollectedRedirects(redirects, pluginContext);
|
|
17
22
|
return filterUnwantedRedirects(redirects, pluginContext);
|
|
18
23
|
}
|
|
19
24
|
exports.default = collectRedirects;
|
|
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
|
+
// 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
|
|
32
|
+
function applyRedirectsTrailingSlash(redirects, params) {
|
|
33
|
+
return redirects.map((redirect) => ({
|
|
34
|
+
...redirect,
|
|
35
|
+
to: (0, utils_common_1.applyTrailingSlash)(redirect.to, params),
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
20
38
|
function validateCollectedRedirects(redirects, pluginContext) {
|
|
21
39
|
const redirectValidationErrors = redirects
|
|
22
40
|
.map((redirect) => {
|
|
23
41
|
try {
|
|
24
|
-
redirectValidation_1.validateRedirect(redirect);
|
|
42
|
+
(0, redirectValidation_1.validateRedirect)(redirect);
|
|
25
43
|
return undefined;
|
|
26
44
|
}
|
|
27
|
-
catch (
|
|
28
|
-
return
|
|
45
|
+
catch (err) {
|
|
46
|
+
return err.message;
|
|
29
47
|
}
|
|
30
48
|
})
|
|
31
49
|
.filter(Boolean);
|
|
@@ -36,7 +54,7 @@ function validateCollectedRedirects(redirects, pluginContext) {
|
|
|
36
54
|
}
|
|
37
55
|
const allowedToPaths = pluginContext.relativeRoutesPaths;
|
|
38
56
|
const toPaths = redirects.map((redirect) => redirect.to);
|
|
39
|
-
const illegalToPaths = lodash_1.difference(toPaths, allowedToPaths);
|
|
57
|
+
const illegalToPaths = lodash_1.default.difference(toPaths, allowedToPaths);
|
|
40
58
|
if (illegalToPaths.length > 0) {
|
|
41
59
|
throw new Error(`You are trying to create client-side redirections to paths that do not exist:
|
|
42
60
|
- ${illegalToPaths.join('\n- ')}
|
|
@@ -49,35 +67,31 @@ Valid paths you can redirect to:
|
|
|
49
67
|
function filterUnwantedRedirects(redirects, pluginContext) {
|
|
50
68
|
// we don't want to create twice the same redirect
|
|
51
69
|
// that would lead to writing twice the same html redirection file
|
|
52
|
-
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]) => {
|
|
53
71
|
if (groupedFromRedirects.length > 1) {
|
|
54
|
-
|
|
55
|
-
It is not possible to redirect the same pathname to multiple destinations:
|
|
56
|
-
- ${groupedFromRedirects.map((r) => JSON.stringify(r)).join('\n- ')}
|
|
57
|
-
`));
|
|
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))}`;
|
|
58
74
|
}
|
|
59
75
|
});
|
|
60
|
-
const collectedRedirects = lodash_1.uniqBy(redirects, (redirect) => redirect.from);
|
|
76
|
+
const collectedRedirects = lodash_1.default.uniqBy(redirects, (redirect) => redirect.from);
|
|
61
77
|
// We don't want to override an already existing route with a redirect file!
|
|
62
78
|
const redirectsOverridingExistingPath = collectedRedirects.filter((redirect) => pluginContext.relativeRoutesPaths.includes(redirect.from));
|
|
63
79
|
if (redirectsOverridingExistingPath.length > 0) {
|
|
64
|
-
|
|
65
|
-
- ${redirectsOverridingExistingPath.map((r) => JSON.stringify(r)).join('\n- ')}
|
|
66
|
-
`));
|
|
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))}`;
|
|
67
81
|
}
|
|
68
82
|
return collectedRedirects.filter((redirect) => !pluginContext.relativeRoutesPaths.includes(redirect.from));
|
|
69
83
|
}
|
|
70
84
|
// For each plugin config option, create the appropriate redirects
|
|
71
85
|
function doCollectRedirects(pluginContext) {
|
|
72
86
|
return [
|
|
73
|
-
...extensionRedirects_1.createFromExtensionsRedirects(pluginContext.relativeRoutesPaths, pluginContext.options.fromExtensions),
|
|
74
|
-
...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),
|
|
75
89
|
...createRedirectsOptionRedirects(pluginContext.options.redirects),
|
|
76
90
|
...createCreateRedirectsOptionRedirects(pluginContext.relativeRoutesPaths, pluginContext.options.createRedirects),
|
|
77
91
|
];
|
|
78
92
|
}
|
|
79
93
|
function createRedirectsOptionRedirects(redirectsOption) {
|
|
80
|
-
// For
|
|
94
|
+
// For convenience, user can use a string or a string[]
|
|
81
95
|
function optionToRedirects(option) {
|
|
82
96
|
if (typeof option.from === 'string') {
|
|
83
97
|
return [{ from: option.from, to: option.to }];
|
|
@@ -87,21 +101,17 @@ function createRedirectsOptionRedirects(redirectsOption) {
|
|
|
87
101
|
to: option.to,
|
|
88
102
|
}));
|
|
89
103
|
}
|
|
90
|
-
return
|
|
104
|
+
return redirectsOption.flatMap(optionToRedirects);
|
|
91
105
|
}
|
|
92
106
|
// Create redirects from the "createRedirects" fn provided by the user
|
|
93
107
|
function createCreateRedirectsOptionRedirects(paths, createRedirects) {
|
|
94
108
|
function createPathRedirects(path) {
|
|
95
|
-
const fromsMixed = createRedirects
|
|
96
|
-
? createRedirects(path) || []
|
|
97
|
-
: [];
|
|
109
|
+
const fromsMixed = createRedirects?.(path) ?? [];
|
|
98
110
|
const froms = typeof fromsMixed === 'string' ? [fromsMixed] : fromsMixed;
|
|
99
|
-
return froms.map((from) => {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
};
|
|
104
|
-
});
|
|
111
|
+
return froms.map((from) => ({
|
|
112
|
+
from,
|
|
113
|
+
to: path,
|
|
114
|
+
}));
|
|
105
115
|
}
|
|
106
|
-
return
|
|
116
|
+
return paths.flatMap(createPathRedirects);
|
|
107
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;
|
|
@@ -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,21 +7,20 @@
|
|
|
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 =
|
|
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
|
-
throw new Error(`Extension
|
|
14
|
+
throw new Error(`Extension "${ext}" is not allowed.\n${ExtensionAdditionalMessage}`);
|
|
16
15
|
}
|
|
17
16
|
if (ext.includes('.')) {
|
|
18
|
-
throw new Error(`Extension
|
|
17
|
+
throw new Error(`Extension "${ext}" contains a "." (dot) which is not allowed.\n${ExtensionAdditionalMessage}`);
|
|
19
18
|
}
|
|
20
19
|
if (ext.includes('/')) {
|
|
21
|
-
throw new Error(`Extension
|
|
20
|
+
throw new Error(`Extension "${ext}" contains a "/" (slash) which is not allowed.\n${ExtensionAdditionalMessage}`);
|
|
22
21
|
}
|
|
23
22
|
if (encodeURIComponent(ext) !== ext) {
|
|
24
|
-
throw new Error(`Extension
|
|
23
|
+
throw new Error(`Extension "${ext}" contains invalid URI characters.\n${ExtensionAdditionalMessage}`);
|
|
25
24
|
}
|
|
26
25
|
};
|
|
27
26
|
const addLeadingDot = (extension) => `.${extension}`;
|
|
@@ -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,23 +39,28 @@ 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
|
-
// Create new /path.html that redirects to existing an /path
|
|
45
|
+
// Create new /path.html/index.html that redirects to existing an /path
|
|
46
|
+
// The filename pattern might look weird but it's on purpose (see https://github.com/facebook/docusaurus/issues/5055)
|
|
47
47
|
function createFromExtensionsRedirects(paths, extensions) {
|
|
48
48
|
extensions.forEach(validateExtension);
|
|
49
49
|
const dottedExtensions = extensions.map(addLeadingDot);
|
|
50
50
|
const alreadyEndsWithAnExtension = (str) => dottedExtensions.some((ext) => str.endsWith(ext));
|
|
51
51
|
const createPathRedirects = (path) => {
|
|
52
|
-
if (path === '' || path
|
|
52
|
+
if (path === '' || path === '/' || alreadyEndsWithAnExtension(path)) {
|
|
53
53
|
return [];
|
|
54
54
|
}
|
|
55
55
|
return extensions.map((ext) => ({
|
|
56
|
-
|
|
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}`,
|
|
57
61
|
to: path,
|
|
58
62
|
}));
|
|
59
63
|
};
|
|
60
|
-
return
|
|
64
|
+
return paths.flatMap(createPathRedirects);
|
|
61
65
|
}
|
|
62
66
|
exports.createFromExtensionsRedirects = createFromExtensionsRedirects;
|
package/lib/index.d.ts
CHANGED
|
@@ -4,6 +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 { LoadContext, Plugin } from '@docusaurus/types';
|
|
8
|
-
import {
|
|
9
|
-
export default function pluginClientRedirectsPages(
|
|
7
|
+
import type { LoadContext, Plugin } from '@docusaurus/types';
|
|
8
|
+
import type { PluginOptions } from '@docusaurus/plugin-client-redirects';
|
|
9
|
+
export default function pluginClientRedirectsPages(context: LoadContext, options: PluginOptions): Plugin<unknown>;
|
|
10
|
+
export { validateOptions } from './options';
|
package/lib/index.js
CHANGED
|
@@ -6,27 +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 normalizePluginOptions_1 = tslib_1.__importDefault(require("./normalizePluginOptions"));
|
|
11
11
|
const collectRedirects_1 = tslib_1.__importDefault(require("./collectRedirects"));
|
|
12
12
|
const writeRedirectFiles_1 = tslib_1.__importStar(require("./writeRedirectFiles"));
|
|
13
13
|
const utils_1 = require("@docusaurus/utils");
|
|
14
|
-
function pluginClientRedirectsPages(
|
|
15
|
-
const
|
|
14
|
+
function pluginClientRedirectsPages(context, options) {
|
|
15
|
+
const { trailingSlash } = context.siteConfig;
|
|
16
16
|
return {
|
|
17
17
|
name: 'docusaurus-plugin-client-redirects',
|
|
18
18
|
async postBuild(props) {
|
|
19
19
|
const pluginContext = {
|
|
20
|
-
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))}`),
|
|
21
21
|
baseUrl: props.baseUrl,
|
|
22
22
|
outDir: props.outDir,
|
|
23
23
|
options,
|
|
24
24
|
};
|
|
25
|
-
const redirects = collectRedirects_1.default(pluginContext);
|
|
26
|
-
const redirectFiles = writeRedirectFiles_1.toRedirectFilesMetadata(redirects, pluginContext);
|
|
25
|
+
const redirects = (0, collectRedirects_1.default)(pluginContext, trailingSlash);
|
|
26
|
+
const redirectFiles = (0, writeRedirectFiles_1.toRedirectFilesMetadata)(redirects, pluginContext, trailingSlash);
|
|
27
27
|
// Write files only at the end: make code more easy to test without IO
|
|
28
|
-
await writeRedirectFiles_1.default(redirectFiles);
|
|
28
|
+
await (0, writeRedirectFiles_1.default)(redirectFiles);
|
|
29
29
|
},
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
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,10 @@
|
|
|
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 { PluginOptions, Options } from '@docusaurus/plugin-client-redirects';
|
|
8
|
+
import type { OptionValidationContext } from '@docusaurus/types';
|
|
9
|
+
export declare const DEFAULT_OPTIONS: Partial<PluginOptions>;
|
|
10
|
+
export declare function validateOptions({ validate, options: userOptions, }: OptionValidationContext<Options, 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 '@docusaurus/plugin-client-redirects';
|
|
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,13 +4,13 @@
|
|
|
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;
|
|
11
11
|
fileContent: string;
|
|
12
12
|
};
|
|
13
13
|
export declare function createToUrl(baseUrl: string, to: string): string;
|
|
14
|
-
export declare function toRedirectFilesMetadata(redirects: RedirectMetadata[], pluginContext: WriteFilesPluginContext): RedirectFileMetadata[];
|
|
14
|
+
export declare function toRedirectFilesMetadata(redirects: RedirectMetadata[], pluginContext: WriteFilesPluginContext, trailingSlash: boolean | undefined): RedirectFileMetadata[];
|
|
15
15
|
export declare function writeRedirectFile(file: RedirectFileMetadata): Promise<void>;
|
|
16
16
|
export default function writeRedirectFiles(redirectFiles: RedirectFileMetadata[]): Promise<void>;
|
|
@@ -10,22 +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");
|
|
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
|
|
22
|
+
// served by host providers!
|
|
23
|
+
// Otherwise it can produce infinite redirect loops!
|
|
24
|
+
//
|
|
25
|
+
// See https://github.com/facebook/docusaurus/issues/5055
|
|
26
|
+
// See https://github.com/facebook/docusaurus/pull/5085
|
|
27
|
+
// See https://github.com/facebook/docusaurus/pull/5102
|
|
28
|
+
function getRedirectFilePath(fromPath, trailingSlash) {
|
|
29
|
+
const fileName = path_1.default.basename(fromPath);
|
|
30
|
+
const filePath = path_1.default.dirname(fromPath);
|
|
31
|
+
// Edge case for https://github.com/facebook/docusaurus/pull/5102
|
|
32
|
+
// If the redirect source path is /xyz, with file /xyz.html
|
|
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)
|
|
37
|
+
if (trailingSlash === false && fileName.endsWith('.html')) {
|
|
38
|
+
return path_1.default.join(filePath, `${fileName}.html`);
|
|
39
|
+
}
|
|
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`);
|
|
46
|
+
}
|
|
47
|
+
function toRedirectFilesMetadata(redirects, pluginContext, trailingSlash) {
|
|
21
48
|
// Perf: avoid rendering the template twice with the exact same "props"
|
|
22
49
|
// We might create multiple redirect pages for the same destination url
|
|
23
50
|
// note: the first fn arg is the cache key!
|
|
24
|
-
const createPageContentMemoized = lodash_1.memoize((toUrl) => {
|
|
25
|
-
return createRedirectPageContent_1.default({ toUrl });
|
|
26
|
-
});
|
|
51
|
+
const createPageContentMemoized = lodash_1.default.memoize((toUrl) => (0, createRedirectPageContent_1.default)({ toUrl }));
|
|
27
52
|
const createFileMetadata = (redirect) => {
|
|
28
|
-
const
|
|
53
|
+
const fileRelativePath = getRedirectFilePath(redirect.from, trailingSlash);
|
|
54
|
+
const fileAbsolutePath = path_1.default.join(pluginContext.outDir, fileRelativePath);
|
|
29
55
|
const toUrl = createToUrl(pluginContext.baseUrl, redirect.to);
|
|
30
56
|
const fileContent = createPageContentMemoized(toUrl);
|
|
31
57
|
return {
|
|
@@ -41,16 +67,15 @@ async function writeRedirectFile(file) {
|
|
|
41
67
|
try {
|
|
42
68
|
// User-friendly security to prevent file overrides
|
|
43
69
|
if (await fs_extra_1.default.pathExists(file.fileAbsolutePath)) {
|
|
44
|
-
throw new Error('The redirect plugin is not supposed to override existing files');
|
|
70
|
+
throw new Error('The redirect plugin is not supposed to override existing files.');
|
|
45
71
|
}
|
|
46
|
-
await fs_extra_1.default.
|
|
47
|
-
await fs_extra_1.default.writeFile(file.fileAbsolutePath, file.fileContent,
|
|
72
|
+
await fs_extra_1.default.outputFile(file.fileAbsolutePath, file.fileContent,
|
|
48
73
|
// Hard security to prevent file overrides
|
|
49
74
|
// See https://stackoverflow.com/a/34187712/82609
|
|
50
75
|
{ flag: 'wx' });
|
|
51
76
|
}
|
|
52
77
|
catch (err) {
|
|
53
|
-
throw new Error(`Redirect file creation error for
|
|
78
|
+
throw new Error(`Redirect file creation error for "${file.fileAbsolutePath}" path: ${err}.`);
|
|
54
79
|
}
|
|
55
80
|
}
|
|
56
81
|
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.18",
|
|
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,23 +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-
|
|
24
|
-
"
|
|
25
|
-
"eta": "^1.
|
|
26
|
-
"fs-extra": "^10.0.
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
21
|
+
"@docusaurus/core": "2.0.0-beta.18",
|
|
22
|
+
"@docusaurus/logger": "2.0.0-beta.18",
|
|
23
|
+
"@docusaurus/utils": "2.0.0-beta.18",
|
|
24
|
+
"@docusaurus/utils-common": "2.0.0-beta.18",
|
|
25
|
+
"@docusaurus/utils-validation": "2.0.0-beta.18",
|
|
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.18"
|
|
30
33
|
},
|
|
31
34
|
"peerDependencies": {
|
|
32
35
|
"react": "^16.8.4 || ^17.0.0",
|
|
33
36
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
34
37
|
},
|
|
35
38
|
"engines": {
|
|
36
|
-
"node": ">=
|
|
39
|
+
"node": ">=14"
|
|
37
40
|
},
|
|
38
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "1a945d06993d53376e61bed2c942799fe07dc336"
|
|
39
42
|
}
|