@docusaurus/plugin-client-redirects 2.0.0-beta.2 → 2.0.0-beta.20

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.
Files changed (39) hide show
  1. package/lib/collectRedirects.d.ts +1 -1
  2. package/lib/collectRedirects.js +38 -42
  3. package/lib/createRedirectPageContent.d.ts +2 -4
  4. package/lib/createRedirectPageContent.js +2 -4
  5. package/lib/extensionRedirects.d.ts +1 -1
  6. package/lib/extensionRedirects.js +12 -8
  7. package/lib/index.d.ts +5 -3
  8. package/lib/index.js +8 -15
  9. package/lib/options.d.ts +31 -0
  10. package/lib/options.js +36 -0
  11. package/lib/redirectValidation.d.ts +1 -1
  12. package/lib/types.d.ts +12 -14
  13. package/lib/writeRedirectFiles.d.ts +1 -1
  14. package/lib/writeRedirectFiles.js +33 -9
  15. package/package.json +16 -14
  16. package/src/collectRedirects.ts +48 -55
  17. package/src/createRedirectPageContent.ts +8 -10
  18. package/src/extensionRedirects.ts +17 -9
  19. package/src/index.ts +9 -21
  20. package/src/options.ts +71 -0
  21. package/src/redirectValidation.ts +1 -1
  22. package/src/types.ts +14 -28
  23. package/src/writeRedirectFiles.ts +40 -10
  24. package/lib/.tsbuildinfo +0 -1
  25. package/lib/normalizePluginOptions.d.ts +0 -9
  26. package/lib/normalizePluginOptions.js +0 -44
  27. package/src/__tests__/__snapshots__/collectRedirects.test.ts.snap +0 -27
  28. package/src/__tests__/__snapshots__/createRedirectPageContent.test.ts.snap +0 -29
  29. package/src/__tests__/__snapshots__/normalizePluginOptions.test.ts.snap +0 -35
  30. package/src/__tests__/__snapshots__/redirectValidation.test.ts.snap +0 -11
  31. package/src/__tests__/__snapshots__/writeRedirectFiles.test.ts.snap +0 -147
  32. package/src/__tests__/collectRedirects.test.ts +0 -340
  33. package/src/__tests__/createRedirectPageContent.test.ts +0 -26
  34. package/src/__tests__/extensionRedirects.test.ts +0 -140
  35. package/src/__tests__/normalizePluginOptions.test.ts +0 -76
  36. package/src/__tests__/redirectValidation.test.ts +0 -66
  37. package/src/__tests__/writeRedirectFiles.test.ts +0 -203
  38. package/src/normalizePluginOptions.ts +0 -55
  39. package/tsconfig.json +0 -10
@@ -1,140 +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 {
9
- createFromExtensionsRedirects,
10
- createToExtensionsRedirects,
11
- } from '../extensionRedirects';
12
-
13
- describe('createToExtensionsRedirects', () => {
14
- test('should reject empty extensions', () => {
15
- expect(() => {
16
- createToExtensionsRedirects(['/'], ['.html']);
17
- }).toThrowErrorMatchingInlineSnapshot(`
18
- "Extension \\".html\\" contains a \\".\\" (dot) which is not allowed.
19
- If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
20
- `);
21
- });
22
-
23
- test('should reject extensions with .', () => {
24
- expect(() => {
25
- createToExtensionsRedirects(['/'], ['.html']);
26
- }).toThrowErrorMatchingInlineSnapshot(`
27
- "Extension \\".html\\" contains a \\".\\" (dot) which is not allowed.
28
- If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
29
- `);
30
- });
31
-
32
- test('should reject extensions with /', () => {
33
- expect(() => {
34
- createToExtensionsRedirects(['/'], ['ht/ml']);
35
- }).toThrowErrorMatchingInlineSnapshot(`
36
- "Extension \\"ht/ml\\" contains a \\"/\\" (slash) which is not allowed.
37
- If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
38
- `);
39
- });
40
-
41
- test('should reject extensions with illegal url char', () => {
42
- expect(() => {
43
- createToExtensionsRedirects(['/'], [',']);
44
- }).toThrowErrorMatchingInlineSnapshot(`
45
- "Extension \\",\\" contains invalid URI characters.
46
- If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
47
- `);
48
- });
49
-
50
- test('should create redirects from html/htm extensions', () => {
51
- const ext = ['html', 'htm'];
52
- expect(createToExtensionsRedirects([''], ext)).toEqual([]);
53
- expect(createToExtensionsRedirects(['/'], ext)).toEqual([]);
54
- expect(createToExtensionsRedirects(['/abc.html'], ext)).toEqual([
55
- {from: '/abc', to: '/abc.html'},
56
- ]);
57
- expect(createToExtensionsRedirects(['/abc.htm'], ext)).toEqual([
58
- {from: '/abc', to: '/abc.htm'},
59
- ]);
60
- expect(createToExtensionsRedirects(['/abc.xyz'], ext)).toEqual([]);
61
- });
62
-
63
- test('should create "to" redirects when relativeRoutesPath contains a prefix', () => {
64
- expect(
65
- createToExtensionsRedirects(['/prefix/file.html'], ['html']),
66
- ).toEqual([{from: '/prefix/file', to: '/prefix/file.html'}]);
67
- });
68
-
69
- test('should not create redirection for an empty extension array', () => {
70
- const ext: string[] = [];
71
- expect(createToExtensionsRedirects([''], ext)).toEqual([]);
72
- expect(createToExtensionsRedirects(['/'], ext)).toEqual([]);
73
- expect(createToExtensionsRedirects(['/abc.html'], ext)).toEqual([]);
74
- });
75
- });
76
-
77
- describe('createFromExtensionsRedirects', () => {
78
- test('should reject empty extensions', () => {
79
- expect(() => {
80
- createFromExtensionsRedirects(['/'], ['.html']);
81
- }).toThrowErrorMatchingInlineSnapshot(`
82
- "Extension \\".html\\" contains a \\".\\" (dot) which is not allowed.
83
- If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
84
- `);
85
- });
86
-
87
- test('should reject extensions with .', () => {
88
- expect(() => {
89
- createFromExtensionsRedirects(['/'], ['.html']);
90
- }).toThrowErrorMatchingInlineSnapshot(`
91
- "Extension \\".html\\" contains a \\".\\" (dot) which is not allowed.
92
- If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
93
- `);
94
- });
95
-
96
- test('should reject extensions with /', () => {
97
- expect(() => {
98
- createFromExtensionsRedirects(['/'], ['ht/ml']);
99
- }).toThrowErrorMatchingInlineSnapshot(`
100
- "Extension \\"ht/ml\\" contains a \\"/\\" (slash) which is not allowed.
101
- If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
102
- `);
103
- });
104
-
105
- test('should reject extensions with illegal url char', () => {
106
- expect(() => {
107
- createFromExtensionsRedirects(['/'], [',']);
108
- }).toThrowErrorMatchingInlineSnapshot(`
109
- "Extension \\",\\" contains invalid URI characters.
110
- If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option."
111
- `);
112
- });
113
-
114
- test('should create redirects to html/htm extensions', () => {
115
- const ext = ['html', 'htm'];
116
- expect(createFromExtensionsRedirects([''], ext)).toEqual([]);
117
- expect(createFromExtensionsRedirects(['/'], ext)).toEqual([]);
118
- expect(createFromExtensionsRedirects(['/abc'], ext)).toEqual([
119
- {from: '/abc.html', to: '/abc'},
120
- {from: '/abc.htm', to: '/abc'},
121
- ]);
122
- expect(createFromExtensionsRedirects(['/def.html'], ext)).toEqual([]);
123
- expect(createFromExtensionsRedirects(['/def/'], ext)).toEqual([]);
124
- });
125
-
126
- test('should create "from" redirects when relativeRoutesPath contains a prefix', () => {
127
- expect(createFromExtensionsRedirects(['/prefix/file'], ['html'])).toEqual([
128
- {from: '/prefix/file.html', to: '/prefix/file'},
129
- ]);
130
- });
131
-
132
- test('should not create redirection for an empty extension array', () => {
133
- const ext: string[] = [];
134
- expect(createFromExtensionsRedirects([''], ext)).toEqual([]);
135
- expect(createFromExtensionsRedirects(['/'], ext)).toEqual([]);
136
- expect(createFromExtensionsRedirects(['/abc'], ext)).toEqual([]);
137
- expect(createFromExtensionsRedirects(['/def.html'], ext)).toEqual([]);
138
- expect(createFromExtensionsRedirects(['/def/'], ext)).toEqual([]);
139
- });
140
- });
@@ -1,76 +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 normalizePluginOptions, {
9
- DefaultPluginOptions,
10
- } from '../normalizePluginOptions';
11
- import {CreateRedirectsFnOption} from '../types';
12
-
13
- describe('normalizePluginOptions', () => {
14
- test('should return default options for undefined user options', () => {
15
- expect(normalizePluginOptions()).toEqual(DefaultPluginOptions);
16
- });
17
-
18
- test('should return default options for empty user options', () => {
19
- expect(normalizePluginOptions()).toEqual(DefaultPluginOptions);
20
- });
21
-
22
- test('should override one default options with valid user options', () => {
23
- expect(
24
- normalizePluginOptions({
25
- toExtensions: ['html'],
26
- }),
27
- ).toEqual({...DefaultPluginOptions, toExtensions: ['html']});
28
- });
29
-
30
- test('should override all default options with valid user options', () => {
31
- const createRedirects: CreateRedirectsFnOption = (_routePath: string) => {
32
- return [];
33
- };
34
- expect(
35
- normalizePluginOptions({
36
- fromExtensions: ['exe', 'zip'],
37
- toExtensions: ['html'],
38
- createRedirects,
39
- redirects: [{from: '/x', to: '/y'}],
40
- }),
41
- ).toEqual({
42
- id: 'default',
43
- fromExtensions: ['exe', 'zip'],
44
- toExtensions: ['html'],
45
- createRedirects,
46
- redirects: [{from: '/x', to: '/y'}],
47
- });
48
- });
49
-
50
- test('should reject bad fromExtensions user inputs', () => {
51
- expect(() =>
52
- normalizePluginOptions({
53
- fromExtensions: ([null, undefined, 123, true] as unknown) as string[],
54
- }),
55
- ).toThrowErrorMatchingSnapshot();
56
- });
57
-
58
- test('should reject bad toExtensions user inputs', () => {
59
- expect(() =>
60
- normalizePluginOptions({
61
- toExtensions: ([null, undefined, 123, true] as unknown) as string[],
62
- }),
63
- ).toThrowErrorMatchingSnapshot();
64
- });
65
-
66
- test('should reject bad createRedirects user inputs', () => {
67
- expect(() =>
68
- normalizePluginOptions({
69
- createRedirects: ([
70
- 'bad',
71
- 'value',
72
- ] as unknown) as CreateRedirectsFnOption,
73
- }),
74
- ).toThrowErrorMatchingSnapshot();
75
- });
76
- });
@@ -1,66 +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 {validateRedirect} from '../redirectValidation';
9
-
10
- describe('validateRedirect', () => {
11
- test('validate good redirects without throwing', () => {
12
- validateRedirect({
13
- from: '/fromSomePath',
14
- to: '/toSomePath',
15
- });
16
- validateRedirect({
17
- from: '/from/Some/Path',
18
- to: '/toSomePath',
19
- });
20
- validateRedirect({
21
- from: '/fromSomePath',
22
- to: '/toSomePath',
23
- });
24
- validateRedirect({
25
- from: '/fromSomePath',
26
- to: '/to/Some/Path',
27
- });
28
- });
29
-
30
- test('throw for bad redirects', () => {
31
- expect(() =>
32
- validateRedirect({
33
- from: 'https://fb.com/fromSomePath',
34
- to: '/toSomePath',
35
- }),
36
- ).toThrowErrorMatchingSnapshot();
37
-
38
- expect(() =>
39
- validateRedirect({
40
- from: '/fromSomePath',
41
- to: 'https://fb.com/toSomePath',
42
- }),
43
- ).toThrowErrorMatchingSnapshot();
44
-
45
- expect(() =>
46
- validateRedirect({
47
- from: '/fromSomePath',
48
- to: '/toSomePath?queryString=xyz',
49
- }),
50
- ).toThrowErrorMatchingSnapshot();
51
-
52
- expect(() =>
53
- validateRedirect({
54
- from: (null as unknown) as string,
55
- to: '/toSomePath?queryString=xyz',
56
- }),
57
- ).toThrowErrorMatchingSnapshot();
58
-
59
- expect(() =>
60
- validateRedirect({
61
- from: (['heyho'] as unknown) as string,
62
- to: '/toSomePath?queryString=xyz',
63
- }),
64
- ).toThrowErrorMatchingSnapshot();
65
- });
66
- });
@@ -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
- }
package/tsconfig.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "incremental": true,
5
- "tsBuildInfoFile": "./lib/.tsbuildinfo",
6
- "rootDir": "src",
7
- "outDir": "lib",
8
- "typeRoots": ["./types", "./node_modules/@types"]
9
- }
10
- }