@docusaurus/plugin-client-redirects 2.0.0-beta.2 → 2.0.0-beta.22
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 +44 -60
- package/lib/createRedirectPageContent.d.ts +2 -4
- package/lib/createRedirectPageContent.js +2 -4
- package/lib/extensionRedirects.d.ts +11 -3
- package/lib/extensionRedirects.js +18 -13
- package/lib/index.d.ts +5 -3
- package/lib/index.js +10 -16
- package/lib/options.d.ts +35 -0
- package/lib/options.js +36 -0
- package/lib/redirectValidation.d.ts +2 -2
- package/lib/types.d.ts +14 -16
- package/lib/writeRedirectFiles.d.ts +5 -5
- package/lib/writeRedirectFiles.js +40 -14
- package/package.json +16 -14
- package/src/collectRedirects.ts +75 -101
- package/src/createRedirectPageContent.ts +8 -10
- package/src/deps.d.ts +16 -0
- package/src/extensionRedirects.ts +28 -19
- package/src/index.ts +15 -27
- package/src/options.ts +75 -0
- package/src/redirectValidation.ts +3 -3
- package/src/types.ts +16 -30
- package/src/writeRedirectFiles.ts +51 -22
- 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 -140
- 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
|
@@ -1,203 +0,0 @@
|
|
|
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 fs from 'fs-extra';
|
|
8
|
-
import path from 'path';
|
|
9
|
-
|
|
10
|
-
import writeRedirectFiles, {
|
|
11
|
-
toRedirectFilesMetadata,
|
|
12
|
-
createToUrl,
|
|
13
|
-
} from '../writeRedirectFiles';
|
|
14
|
-
|
|
15
|
-
// Test to fix toUrl bugs we had:
|
|
16
|
-
// - https://github.com/facebook/docusaurus/issues/3886
|
|
17
|
-
// - https://github.com/facebook/docusaurus/issues/3925
|
|
18
|
-
describe('createToUrl', () => {
|
|
19
|
-
test('should create appropriate redirect urls', async () => {
|
|
20
|
-
expect(createToUrl('/', '/docs/something/else')).toEqual(
|
|
21
|
-
'/docs/something/else',
|
|
22
|
-
);
|
|
23
|
-
expect(createToUrl('/', '/docs/something/else/')).toEqual(
|
|
24
|
-
'/docs/something/else/',
|
|
25
|
-
);
|
|
26
|
-
expect(createToUrl('/', 'docs/something/else')).toEqual(
|
|
27
|
-
'/docs/something/else',
|
|
28
|
-
);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('should create appropriate redirect urls with baseUrl', async () => {
|
|
32
|
-
expect(createToUrl('/baseUrl/', '/docs/something/else')).toEqual(
|
|
33
|
-
'/baseUrl/docs/something/else',
|
|
34
|
-
);
|
|
35
|
-
expect(createToUrl('/baseUrl/', '/docs/something/else/')).toEqual(
|
|
36
|
-
'/baseUrl/docs/something/else/',
|
|
37
|
-
);
|
|
38
|
-
expect(createToUrl('/baseUrl/', 'docs/something/else')).toEqual(
|
|
39
|
-
'/baseUrl/docs/something/else',
|
|
40
|
-
);
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
describe('toRedirectFilesMetadata', () => {
|
|
45
|
-
test('should create appropriate metadatas trailingSlash=undefined', async () => {
|
|
46
|
-
const pluginContext = {
|
|
47
|
-
outDir: '/tmp/someFixedOutDir',
|
|
48
|
-
baseUrl: 'https://docusaurus.io',
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const redirectFiles = toRedirectFilesMetadata(
|
|
52
|
-
[
|
|
53
|
-
{from: '/abc.html', to: '/abc'},
|
|
54
|
-
{from: '/def', to: '/def.html'},
|
|
55
|
-
{from: '/xyz', to: '/'},
|
|
56
|
-
],
|
|
57
|
-
pluginContext,
|
|
58
|
-
undefined,
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
|
|
62
|
-
path.join(pluginContext.outDir, '/abc.html'),
|
|
63
|
-
path.join(pluginContext.outDir, '/def/index.html'),
|
|
64
|
-
path.join(pluginContext.outDir, '/xyz/index.html'),
|
|
65
|
-
]);
|
|
66
|
-
|
|
67
|
-
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
|
|
68
|
-
'fileContent',
|
|
69
|
-
);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test('should create appropriate metadatas trailingSlash=true', async () => {
|
|
73
|
-
const pluginContext = {
|
|
74
|
-
outDir: '/tmp/someFixedOutDir',
|
|
75
|
-
baseUrl: 'https://docusaurus.io',
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const redirectFiles = toRedirectFilesMetadata(
|
|
79
|
-
[
|
|
80
|
-
{from: '/abc.html', to: '/abc'},
|
|
81
|
-
{from: '/def', to: '/def.html'},
|
|
82
|
-
{from: '/xyz', to: '/'},
|
|
83
|
-
],
|
|
84
|
-
pluginContext,
|
|
85
|
-
true,
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
|
|
89
|
-
path.join(pluginContext.outDir, '/abc.html'),
|
|
90
|
-
path.join(pluginContext.outDir, '/def/index.html'),
|
|
91
|
-
path.join(pluginContext.outDir, '/xyz/index.html'),
|
|
92
|
-
]);
|
|
93
|
-
|
|
94
|
-
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
|
|
95
|
-
'fileContent',
|
|
96
|
-
);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test('should create appropriate metadatas trailingSlash=false', async () => {
|
|
100
|
-
const pluginContext = {
|
|
101
|
-
outDir: '/tmp/someFixedOutDir',
|
|
102
|
-
baseUrl: 'https://docusaurus.io',
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const redirectFiles = toRedirectFilesMetadata(
|
|
106
|
-
[
|
|
107
|
-
{from: '/abc.html', to: '/abc'},
|
|
108
|
-
{from: '/def', to: '/def.html'},
|
|
109
|
-
{from: '/xyz', to: '/'},
|
|
110
|
-
],
|
|
111
|
-
pluginContext,
|
|
112
|
-
false,
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
|
|
116
|
-
path.join(pluginContext.outDir, '/abc.html'),
|
|
117
|
-
path.join(pluginContext.outDir, '/def.html'),
|
|
118
|
-
path.join(pluginContext.outDir, '/xyz.html'),
|
|
119
|
-
]);
|
|
120
|
-
|
|
121
|
-
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
|
|
122
|
-
'fileContent',
|
|
123
|
-
);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('should create appropriate metadatas for root baseUrl', async () => {
|
|
127
|
-
const pluginContext = {
|
|
128
|
-
outDir: '/tmp/someFixedOutDir',
|
|
129
|
-
baseUrl: '/',
|
|
130
|
-
};
|
|
131
|
-
const redirectFiles = toRedirectFilesMetadata(
|
|
132
|
-
[{from: '/abc.html', to: '/abc'}],
|
|
133
|
-
pluginContext,
|
|
134
|
-
undefined,
|
|
135
|
-
);
|
|
136
|
-
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
|
|
137
|
-
'fileContent baseUrl=/',
|
|
138
|
-
);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
test('should create appropriate metadatas for empty baseUrl', async () => {
|
|
142
|
-
const pluginContext = {
|
|
143
|
-
outDir: '/tmp/someFixedOutDir',
|
|
144
|
-
baseUrl: '',
|
|
145
|
-
};
|
|
146
|
-
const redirectFiles = toRedirectFilesMetadata(
|
|
147
|
-
[{from: '/abc.html', to: '/abc'}],
|
|
148
|
-
pluginContext,
|
|
149
|
-
undefined,
|
|
150
|
-
);
|
|
151
|
-
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
|
|
152
|
-
'fileContent baseUrl=empty',
|
|
153
|
-
);
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
describe('writeRedirectFiles', () => {
|
|
158
|
-
test('write the files', async () => {
|
|
159
|
-
const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
|
|
160
|
-
|
|
161
|
-
const filesMetadata = [
|
|
162
|
-
{
|
|
163
|
-
fileAbsolutePath: path.join(outDir, 'someFileName'),
|
|
164
|
-
fileContent: 'content 1',
|
|
165
|
-
},
|
|
166
|
-
{
|
|
167
|
-
fileAbsolutePath: path.join(outDir, '/some/nested/filename'),
|
|
168
|
-
fileContent: 'content 2',
|
|
169
|
-
},
|
|
170
|
-
];
|
|
171
|
-
|
|
172
|
-
await writeRedirectFiles(filesMetadata);
|
|
173
|
-
|
|
174
|
-
await expect(
|
|
175
|
-
fs.readFile(filesMetadata[0].fileAbsolutePath, 'utf8'),
|
|
176
|
-
).resolves.toEqual('content 1');
|
|
177
|
-
|
|
178
|
-
await expect(
|
|
179
|
-
fs.readFile(filesMetadata[1].fileAbsolutePath, 'utf8'),
|
|
180
|
-
).resolves.toEqual('content 2');
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
test('avoid overwriting existing files', async () => {
|
|
184
|
-
const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
|
|
185
|
-
|
|
186
|
-
const filesMetadata = [
|
|
187
|
-
{
|
|
188
|
-
fileAbsolutePath: path.join(outDir, 'someFileName/index.html'),
|
|
189
|
-
fileContent: 'content 1',
|
|
190
|
-
},
|
|
191
|
-
];
|
|
192
|
-
|
|
193
|
-
await fs.ensureDir(path.dirname(filesMetadata[0].fileAbsolutePath));
|
|
194
|
-
await fs.writeFile(
|
|
195
|
-
filesMetadata[0].fileAbsolutePath,
|
|
196
|
-
'file already exists!',
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
await expect(writeRedirectFiles(filesMetadata)).rejects.toThrowError(
|
|
200
|
-
`Redirect file creation error for "${filesMetadata[0].fileAbsolutePath}" path: Error: The redirect plugin is not supposed to override existing files.`,
|
|
201
|
-
);
|
|
202
|
-
});
|
|
203
|
-
});
|
|
@@ -1,55 +0,0 @@
|
|
|
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
|
-
|
|
8
|
-
import {PluginOptions, RedirectOption, UserPluginOptions} from './types';
|
|
9
|
-
import {Joi, PathnameSchema} from '@docusaurus/utils-validation';
|
|
10
|
-
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
|
|
11
|
-
|
|
12
|
-
export const DefaultPluginOptions: PluginOptions = {
|
|
13
|
-
id: DEFAULT_PLUGIN_ID, // TODO temporary
|
|
14
|
-
fromExtensions: [],
|
|
15
|
-
toExtensions: [],
|
|
16
|
-
redirects: [],
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const RedirectPluginOptionValidation = Joi.object<RedirectOption>({
|
|
20
|
-
to: PathnameSchema.required(),
|
|
21
|
-
from: Joi.alternatives().try(
|
|
22
|
-
PathnameSchema.required(),
|
|
23
|
-
Joi.array().items(PathnameSchema.required()),
|
|
24
|
-
),
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
const isString = Joi.string().required().not(null);
|
|
28
|
-
|
|
29
|
-
const UserOptionsSchema = Joi.object<UserPluginOptions>({
|
|
30
|
-
id: Joi.string().optional(), // TODO remove once validation migrated to new system
|
|
31
|
-
fromExtensions: Joi.array().items(isString),
|
|
32
|
-
toExtensions: Joi.array().items(isString),
|
|
33
|
-
redirects: Joi.array().items(RedirectPluginOptionValidation),
|
|
34
|
-
createRedirects: Joi.function().arity(1),
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
function validateUserOptions(userOptions: UserPluginOptions) {
|
|
38
|
-
const {error} = UserOptionsSchema.validate(userOptions, {
|
|
39
|
-
abortEarly: true,
|
|
40
|
-
allowUnknown: false,
|
|
41
|
-
});
|
|
42
|
-
if (error) {
|
|
43
|
-
throw new Error(
|
|
44
|
-
`Invalid @docusaurus/plugin-client-redirects options: ${error.message}
|
|
45
|
-
${JSON.stringify(userOptions, null, 2)}`,
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export default function normalizePluginOptions(
|
|
51
|
-
userPluginOptions: UserPluginOptions = {},
|
|
52
|
-
): PluginOptions {
|
|
53
|
-
validateUserOptions(userPluginOptions);
|
|
54
|
-
return {...DefaultPluginOptions, ...userPluginOptions};
|
|
55
|
-
}
|