@docusaurus/plugin-client-redirects 0.0.0-4726 → 0.0.0-4730

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.
@@ -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
- declare type CreateRedirectPageOptions = {
7
+ export default function createRedirectPageContent({ toUrl, }: {
8
8
  toUrl: string;
9
- };
10
- export default function createRedirectPageContent({ toUrl, }: CreateRedirectPageOptions): string;
11
- export {};
9
+ }): string;
package/lib/index.d.ts CHANGED
@@ -6,4 +6,5 @@
6
6
  */
7
7
  import type { LoadContext, Plugin } from '@docusaurus/types';
8
8
  import type { PluginOptions } from '@docusaurus/plugin-client-redirects';
9
- export default function pluginClientRedirectsPages(context: LoadContext, opts: PluginOptions): Plugin<unknown>;
9
+ export default function pluginClientRedirectsPages(context: LoadContext, options: PluginOptions): Plugin<unknown>;
10
+ export { validateOptions } from './options';
package/lib/index.js CHANGED
@@ -6,14 +6,13 @@
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(context, opts) {
14
+ function pluginClientRedirectsPages(context, options) {
15
15
  const { trailingSlash } = context.siteConfig;
16
- const options = (0, normalizePluginOptions_1.default)(opts);
17
16
  return {
18
17
  name: 'docusaurus-plugin-client-redirects',
19
18
  async postBuild(props) {
@@ -31,3 +30,5 @@ function pluginClientRedirectsPages(context, opts) {
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; } });
@@ -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 } from '@docusaurus/plugin-client-redirects';
8
+ import type { OptionValidationContext, ValidationResult } from '@docusaurus/types';
9
+ export declare const DEFAULT_OPTIONS: Partial<PluginOptions>;
10
+ export declare function validateOptions({ validate, options: userOptions, }: OptionValidationContext<PluginOptions>): ValidationResult<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().arity(1),
32
+ }).default(exports.DEFAULT_OPTIONS);
33
+ function validateOptions({ validate, options: userOptions, }) {
34
+ return validate(UserOptionsSchema, userOptions);
35
+ }
36
+ exports.validateOptions = validateOptions;
package/lib/types.d.ts CHANGED
@@ -6,11 +6,21 @@
6
6
  */
7
7
  import type { Props } from '@docusaurus/types';
8
8
  import type { PluginOptions } from '@docusaurus/plugin-client-redirects';
9
+ /**
10
+ * The minimal infos the plugin needs to work
11
+ */
9
12
  export declare type PluginContext = Pick<Props, 'outDir' | 'baseUrl'> & {
10
13
  options: PluginOptions;
11
14
  relativeRoutesPaths: string[];
12
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
+ */
13
21
  export declare type RedirectMetadata = {
22
+ /** Pathname of the new page we should create */
14
23
  from: string;
24
+ /** Pathname of an existing Docusaurus page */
15
25
  to: string;
16
26
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-client-redirects",
3
- "version": "0.0.0-4726",
3
+ "version": "0.0.0-4730",
4
4
  "description": "Client redirects plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/plugin-client-redirects.d.ts",
@@ -18,18 +18,18 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@docusaurus/core": "0.0.0-4726",
22
- "@docusaurus/logger": "0.0.0-4726",
23
- "@docusaurus/utils": "0.0.0-4726",
24
- "@docusaurus/utils-common": "0.0.0-4726",
25
- "@docusaurus/utils-validation": "0.0.0-4726",
21
+ "@docusaurus/core": "0.0.0-4730",
22
+ "@docusaurus/logger": "0.0.0-4730",
23
+ "@docusaurus/utils": "0.0.0-4730",
24
+ "@docusaurus/utils-common": "0.0.0-4730",
25
+ "@docusaurus/utils-validation": "0.0.0-4730",
26
26
  "eta": "^1.12.3",
27
27
  "fs-extra": "^10.0.1",
28
28
  "lodash": "^4.17.21",
29
29
  "tslib": "^2.3.1"
30
30
  },
31
31
  "devDependencies": {
32
- "@docusaurus/types": "0.0.0-4726"
32
+ "@docusaurus/types": "0.0.0-4730"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "react": "^16.8.4 || ^17.0.0",
@@ -38,5 +38,5 @@
38
38
  "engines": {
39
39
  "node": ">=14"
40
40
  },
41
- "gitHead": "f0fd448c4509295ecdfe665dcc017980c4b5153b"
41
+ "gitHead": "65b87b9d9f21c7904877ed1fade98e0b9fb61b56"
42
42
  }
@@ -9,10 +9,6 @@ import * as eta from 'eta';
9
9
  import redirectPageTemplate from './templates/redirectPage.template.html';
10
10
  import _ from 'lodash';
11
11
 
12
- type CreateRedirectPageOptions = {
13
- toUrl: string;
14
- };
15
-
16
12
  const getCompiledRedirectPageTemplate = _.memoize(() =>
17
13
  eta.compile(redirectPageTemplate.trim()),
18
14
  );
@@ -24,7 +20,9 @@ function renderRedirectPageTemplate(data: Record<string, unknown>) {
24
20
 
25
21
  export default function createRedirectPageContent({
26
22
  toUrl,
27
- }: CreateRedirectPageOptions): string {
23
+ }: {
24
+ toUrl: string;
25
+ }): string {
28
26
  return renderRedirectPageTemplate({
29
27
  toUrl: encodeURI(toUrl),
30
28
  });
package/src/index.ts CHANGED
@@ -9,7 +9,6 @@ import type {LoadContext, Plugin, Props} from '@docusaurus/types';
9
9
  import type {PluginContext, RedirectMetadata} from './types';
10
10
  import type {PluginOptions} from '@docusaurus/plugin-client-redirects';
11
11
 
12
- import normalizePluginOptions from './normalizePluginOptions';
13
12
  import collectRedirects from './collectRedirects';
14
13
  import writeRedirectFiles, {
15
14
  toRedirectFilesMetadata,
@@ -19,12 +18,10 @@ import {removePrefix, addLeadingSlash} from '@docusaurus/utils';
19
18
 
20
19
  export default function pluginClientRedirectsPages(
21
20
  context: LoadContext,
22
- opts: PluginOptions,
21
+ options: PluginOptions,
23
22
  ): Plugin<unknown> {
24
23
  const {trailingSlash} = context.siteConfig;
25
24
 
26
- const options = normalizePluginOptions(opts);
27
-
28
25
  return {
29
26
  name: 'docusaurus-plugin-client-redirects',
30
27
  async postBuild(props: Props) {
@@ -53,3 +50,5 @@ export default function pluginClientRedirectsPages(
53
50
  },
54
51
  };
55
52
  }
53
+
54
+ export {validateOptions} from './options';
package/src/options.ts ADDED
@@ -0,0 +1,52 @@
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 type {
9
+ PluginOptions,
10
+ RedirectOption,
11
+ } from '@docusaurus/plugin-client-redirects';
12
+ import type {
13
+ OptionValidationContext,
14
+ ValidationResult,
15
+ } from '@docusaurus/types';
16
+ import {Joi, PathnameSchema} from '@docusaurus/utils-validation';
17
+
18
+ export const DEFAULT_OPTIONS: Partial<PluginOptions> = {
19
+ fromExtensions: [],
20
+ toExtensions: [],
21
+ redirects: [],
22
+ };
23
+
24
+ const RedirectPluginOptionValidation = Joi.object<RedirectOption>({
25
+ to: PathnameSchema.required(),
26
+ from: Joi.alternatives().try(
27
+ PathnameSchema.required(),
28
+ Joi.array().items(PathnameSchema.required()),
29
+ ),
30
+ });
31
+
32
+ const isString = Joi.string().required().not(null);
33
+
34
+ const UserOptionsSchema = Joi.object<PluginOptions>({
35
+ fromExtensions: Joi.array()
36
+ .items(isString)
37
+ .default(DEFAULT_OPTIONS.fromExtensions),
38
+ toExtensions: Joi.array()
39
+ .items(isString)
40
+ .default(DEFAULT_OPTIONS.toExtensions),
41
+ redirects: Joi.array()
42
+ .items(RedirectPluginOptionValidation)
43
+ .default(DEFAULT_OPTIONS.redirects),
44
+ createRedirects: Joi.function().arity(1),
45
+ }).default(DEFAULT_OPTIONS);
46
+
47
+ export function validateOptions({
48
+ validate,
49
+ options: userOptions,
50
+ }: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions> {
51
+ return validate(UserOptionsSchema, userOptions);
52
+ }
@@ -10,18 +10,23 @@ export type RedirectOption = {
10
10
  from: string | string[];
11
11
  };
12
12
 
13
- // For a given existing route path,
14
- // return all the paths from which we should redirect from
15
- export type CreateRedirectsFnOption = (
16
- path: string,
17
- ) => string[] | string | null | undefined;
18
-
19
13
  export type PluginOptions = {
14
+ /** Plugin ID. */
20
15
  id: string;
16
+ /** The extensions to be removed from the route after redirecting. */
21
17
  fromExtensions: string[];
18
+ /** The extensions to be appended to the route after redirecting. */
22
19
  toExtensions: string[];
20
+ /** The list of redirect rules, each one with multiple `from`s → one `to`. */
23
21
  redirects: RedirectOption[];
24
- createRedirects?: CreateRedirectsFnOption;
22
+ /**
23
+ * A callback to create a redirect rule.
24
+ * @returns All the paths from which we should redirect to `path`
25
+ */
26
+ createRedirects?: (
27
+ /** An existing Docusaurus route path */
28
+ path: string,
29
+ ) => string[] | string | null | undefined;
25
30
  };
26
31
 
27
32
  export type Options = Partial<PluginOptions>;
package/src/types.ts CHANGED
@@ -8,16 +8,22 @@
8
8
  import type {Props} from '@docusaurus/types';
9
9
  import type {PluginOptions} from '@docusaurus/plugin-client-redirects';
10
10
 
11
- // The minimal infos the plugin needs to work
11
+ /**
12
+ * The minimal infos the plugin needs to work
13
+ */
12
14
  export type PluginContext = Pick<Props, 'outDir' | 'baseUrl'> & {
13
15
  options: PluginOptions;
14
16
  relativeRoutesPaths: string[];
15
17
  };
16
18
 
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
19
+ /**
20
+ * In-memory representation of redirects we want: easier to test
21
+ * /!\ easy to be confused: "from" is the new page we should create,
22
+ * that redirects to "to": the existing Docusaurus page
23
+ */
20
24
  export type RedirectMetadata = {
21
- from: string; // pathname
22
- to: string; // pathname
25
+ /** Pathname of the new page we should create */
26
+ from: string;
27
+ /** Pathname of an existing Docusaurus page */
28
+ to: string;
23
29
  };
@@ -1,9 +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 type { PluginOptions, Options as UserPluginOptions } from '@docusaurus/plugin-client-redirects';
8
- export declare const DefaultPluginOptions: PluginOptions;
9
- export default function normalizePluginOptions(userPluginOptions?: UserPluginOptions): PluginOptions;
@@ -1,44 +0,0 @@
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.DefaultPluginOptions = void 0;
10
- const utils_validation_1 = require("@docusaurus/utils-validation");
11
- const utils_1 = require("@docusaurus/utils");
12
- exports.DefaultPluginOptions = {
13
- id: utils_1.DEFAULT_PLUGIN_ID,
14
- fromExtensions: [],
15
- toExtensions: [],
16
- redirects: [],
17
- };
18
- const RedirectPluginOptionValidation = utils_validation_1.Joi.object({
19
- to: utils_validation_1.PathnameSchema.required(),
20
- from: utils_validation_1.Joi.alternatives().try(utils_validation_1.PathnameSchema.required(), utils_validation_1.Joi.array().items(utils_validation_1.PathnameSchema.required())),
21
- });
22
- const isString = utils_validation_1.Joi.string().required().not(null);
23
- const UserOptionsSchema = utils_validation_1.Joi.object({
24
- id: utils_validation_1.Joi.string().optional(),
25
- fromExtensions: utils_validation_1.Joi.array().items(isString),
26
- toExtensions: utils_validation_1.Joi.array().items(isString),
27
- redirects: utils_validation_1.Joi.array().items(RedirectPluginOptionValidation),
28
- createRedirects: utils_validation_1.Joi.function().arity(1),
29
- });
30
- function validateUserOptions(userOptions) {
31
- const { error } = UserOptionsSchema.validate(userOptions, {
32
- abortEarly: true,
33
- allowUnknown: false,
34
- });
35
- if (error) {
36
- throw new Error(`Invalid @docusaurus/plugin-client-redirects options: ${error.message}
37
- ${JSON.stringify(userOptions, null, 2)}`);
38
- }
39
- }
40
- function normalizePluginOptions(userPluginOptions = {}) {
41
- validateUserOptions(userPluginOptions);
42
- return { ...exports.DefaultPluginOptions, ...userPluginOptions };
43
- }
44
- exports.default = normalizePluginOptions;
@@ -1,59 +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 type {
9
- PluginOptions,
10
- Options as UserPluginOptions,
11
- RedirectOption,
12
- } from '@docusaurus/plugin-client-redirects';
13
- import {Joi, PathnameSchema} from '@docusaurus/utils-validation';
14
- import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
15
-
16
- export const DefaultPluginOptions: PluginOptions = {
17
- id: DEFAULT_PLUGIN_ID, // TODO temporary
18
- fromExtensions: [],
19
- toExtensions: [],
20
- redirects: [],
21
- };
22
-
23
- const RedirectPluginOptionValidation = Joi.object<RedirectOption>({
24
- to: PathnameSchema.required(),
25
- from: Joi.alternatives().try(
26
- PathnameSchema.required(),
27
- Joi.array().items(PathnameSchema.required()),
28
- ),
29
- });
30
-
31
- const isString = Joi.string().required().not(null);
32
-
33
- const UserOptionsSchema = Joi.object<UserPluginOptions>({
34
- id: Joi.string().optional(), // TODO remove once validation migrated to new system
35
- fromExtensions: Joi.array().items(isString),
36
- toExtensions: Joi.array().items(isString),
37
- redirects: Joi.array().items(RedirectPluginOptionValidation),
38
- createRedirects: Joi.function().arity(1),
39
- });
40
-
41
- function validateUserOptions(userOptions: UserPluginOptions) {
42
- const {error} = UserOptionsSchema.validate(userOptions, {
43
- abortEarly: true,
44
- allowUnknown: false,
45
- });
46
- if (error) {
47
- throw new Error(
48
- `Invalid @docusaurus/plugin-client-redirects options: ${error.message}
49
- ${JSON.stringify(userOptions, null, 2)}`,
50
- );
51
- }
52
- }
53
-
54
- export default function normalizePluginOptions(
55
- userPluginOptions: UserPluginOptions = {},
56
- ): PluginOptions {
57
- validateUserOptions(userPluginOptions);
58
- return {...DefaultPluginOptions, ...userPluginOptions};
59
- }