@docusaurus/plugin-sitemap 2.0.0-beta.1decd6f80 → 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.
@@ -4,6 +4,9 @@
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 { PluginOptions } from './types';
8
- import { DocusaurusConfig } from '@docusaurus/types';
9
- export default function createSitemap(siteConfig: DocusaurusConfig, routesPaths: string[], options: PluginOptions): Promise<string>;
7
+ import type { DocusaurusConfig } from '@docusaurus/types';
8
+ import type { HelmetServerState } from 'react-helmet-async';
9
+ import type { PluginOptions } from './options';
10
+ export default function createSitemap(siteConfig: DocusaurusConfig, routesPaths: string[], head: {
11
+ [location: string]: HelmetServerState;
12
+ }, options: PluginOptions): Promise<string>;
@@ -7,25 +7,34 @@
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  const sitemap_1 = require("sitemap");
10
+ const utils_common_1 = require("@docusaurus/utils-common");
10
11
  const utils_1 = require("@docusaurus/utils");
11
- async function createSitemap(siteConfig, routesPaths, options) {
12
+ async function createSitemap(siteConfig, routesPaths, head, options) {
12
13
  const { url: hostname } = siteConfig;
13
14
  if (!hostname) {
14
- throw new Error('url in docusaurus.config.js cannot be empty/undefined');
15
+ throw new Error('URL in docusaurus.config.js cannot be empty/undefined.');
15
16
  }
16
- const { changefreq, priority, trailingSlash } = options;
17
- const sitemapStream = new sitemap_1.SitemapStream({
18
- hostname,
19
- });
20
- routesPaths
21
- .filter((route) => !route.endsWith('404.html'))
22
- .map((routePath) => sitemapStream.write({
23
- url: trailingSlash ? utils_1.addTrailingSlash(routePath) : routePath,
17
+ const { changefreq, priority, ignorePatterns } = options;
18
+ const ignoreMatcher = (0, utils_1.createMatcher)(ignorePatterns);
19
+ const sitemapStream = new sitemap_1.SitemapStream({ hostname });
20
+ function routeShouldBeIncluded(route) {
21
+ if (route.endsWith('404.html') || ignoreMatcher(route)) {
22
+ return false;
23
+ }
24
+ // https://github.com/staylor/react-helmet-async/pull/167
25
+ const meta = head[route]?.meta.toComponent();
26
+ return !meta?.some((tag) => tag.props.name === 'robots' && tag.props.content === 'noindex');
27
+ }
28
+ routesPaths.filter(routeShouldBeIncluded).forEach((routePath) => sitemapStream.write({
29
+ url: (0, utils_common_1.applyTrailingSlash)(routePath, {
30
+ trailingSlash: siteConfig.trailingSlash,
31
+ baseUrl: siteConfig.baseUrl,
32
+ }),
24
33
  changefreq,
25
34
  priority,
26
35
  }));
27
36
  sitemapStream.end();
28
- const generatedSitemap = await sitemap_1.streamToPromise(sitemapStream).then((sm) => sm.toString());
37
+ const generatedSitemap = (await (0, sitemap_1.streamToPromise)(sitemapStream)).toString();
29
38
  return generatedSitemap;
30
39
  }
31
40
  exports.default = createSitemap;
package/lib/index.d.ts CHANGED
@@ -4,7 +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 { PluginOptions } from './types';
8
- import { LoadContext, OptionValidationContext, ValidationResult, Plugin } from '@docusaurus/types';
9
- export default function pluginSitemap(_context: LoadContext, options: PluginOptions): Plugin<void>;
10
- export declare function validateOptions({ validate, options, }: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions>;
7
+ import type { PluginOptions, Options } from './options';
8
+ import type { LoadContext, Plugin } from '@docusaurus/types';
9
+ export default function pluginSitemap(context: LoadContext, options: PluginOptions): Plugin<void>;
10
+ export { validateOptions } from './options';
11
+ export type { PluginOptions, Options };
package/lib/index.js CHANGED
@@ -11,27 +11,26 @@ 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
13
  const createSitemap_1 = tslib_1.__importDefault(require("./createSitemap"));
14
- const pluginOptionSchema_1 = require("./pluginOptionSchema");
15
- function pluginSitemap(_context, options) {
14
+ function pluginSitemap(context, options) {
16
15
  return {
17
16
  name: 'docusaurus-plugin-sitemap',
18
- async postBuild({ siteConfig, routesPaths, outDir }) {
17
+ async postBuild({ siteConfig, routesPaths, outDir, head }) {
18
+ if (siteConfig.noIndex) {
19
+ return;
20
+ }
19
21
  // Generate sitemap.
20
- const generatedSitemap = await createSitemap_1.default(siteConfig, routesPaths, options);
22
+ const generatedSitemap = await (0, createSitemap_1.default)(siteConfig, routesPaths, head, options);
21
23
  // Write sitemap file.
22
24
  const sitemapPath = path_1.default.join(outDir, 'sitemap.xml');
23
25
  try {
24
26
  await fs_extra_1.default.outputFile(sitemapPath, generatedSitemap);
25
27
  }
26
28
  catch (err) {
27
- throw new Error(`Sitemap error: ${err}`);
29
+ throw new Error(`Writing sitemap failed: ${err}`);
28
30
  }
29
31
  },
30
32
  };
31
33
  }
32
34
  exports.default = pluginSitemap;
33
- function validateOptions({ validate, options, }) {
34
- const validatedOptions = validate(pluginOptionSchema_1.PluginOptionSchema, options);
35
- return validatedOptions;
36
- }
37
- exports.validateOptions = validateOptions;
35
+ var options_1 = require("./options");
36
+ Object.defineProperty(exports, "validateOptions", { enumerable: true, get: function () { return options_1.validateOptions; } });
@@ -0,0 +1,22 @@
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 { EnumChangefreq } from 'sitemap';
8
+ import type { OptionValidationContext } from '@docusaurus/types';
9
+ export declare type PluginOptions = {
10
+ /** @see https://www.sitemaps.org/protocol.html#xmlTagDefinitions */
11
+ changefreq: EnumChangefreq;
12
+ /** @see https://www.sitemaps.org/protocol.html#xmlTagDefinitions */
13
+ priority: number;
14
+ /**
15
+ * A list of glob patterns; matching route paths will be filtered from the
16
+ * sitemap. Note that you may need to include the base URL in here.
17
+ */
18
+ ignorePatterns: string[];
19
+ };
20
+ export declare type Options = Partial<PluginOptions>;
21
+ export declare const DEFAULT_OPTIONS: PluginOptions;
22
+ export declare function validateOptions({ validate, options, }: OptionValidationContext<Options, PluginOptions>): PluginOptions;
@@ -6,16 +6,15 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.PluginOptionSchema = exports.DEFAULT_OPTIONS = void 0;
9
+ exports.validateOptions = exports.DEFAULT_OPTIONS = void 0;
10
10
  const utils_validation_1 = require("@docusaurus/utils-validation");
11
11
  const sitemap_1 = require("sitemap");
12
12
  exports.DEFAULT_OPTIONS = {
13
13
  changefreq: sitemap_1.EnumChangefreq.WEEKLY,
14
14
  priority: 0.5,
15
- trailingSlash: false,
15
+ ignorePatterns: [],
16
16
  };
17
- exports.PluginOptionSchema = utils_validation_1.Joi.object({
18
- // TODO temporary (@alpha-71)
17
+ const PluginOptionSchema = utils_validation_1.Joi.object({
19
18
  cacheTime: utils_validation_1.Joi.forbidden().messages({
20
19
  'any.unknown': 'Option `cacheTime` in sitemap config is deprecated. Please remove it.',
21
20
  }),
@@ -23,5 +22,15 @@ exports.PluginOptionSchema = utils_validation_1.Joi.object({
23
22
  .valid(...Object.values(sitemap_1.EnumChangefreq))
24
23
  .default(exports.DEFAULT_OPTIONS.changefreq),
25
24
  priority: utils_validation_1.Joi.number().min(0).max(1).default(exports.DEFAULT_OPTIONS.priority),
26
- trailingSlash: utils_validation_1.Joi.bool().default(false),
25
+ ignorePatterns: utils_validation_1.Joi.array()
26
+ .items(utils_validation_1.Joi.string())
27
+ .default(exports.DEFAULT_OPTIONS.ignorePatterns),
28
+ trailingSlash: utils_validation_1.Joi.forbidden().messages({
29
+ 'any.unknown': 'Please use the new Docusaurus global trailingSlash config instead, and the sitemaps plugin will use it.',
30
+ }),
27
31
  });
32
+ function validateOptions({ validate, options, }) {
33
+ const validatedOptions = validate(PluginOptionSchema, options);
34
+ return validatedOptions;
35
+ }
36
+ exports.validateOptions = validateOptions;
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-sitemap",
3
- "version": "2.0.0-beta.1decd6f80",
3
+ "version": "2.0.0-beta.20",
4
4
  "description": "Simple sitemap generation 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,20 +18,23 @@
17
18
  },
18
19
  "license": "MIT",
19
20
  "dependencies": {
20
- "@docusaurus/core": "2.0.0-beta.1decd6f80",
21
- "@docusaurus/types": "2.0.0-beta.1decd6f80",
22
- "@docusaurus/utils": "2.0.0-beta.1decd6f80",
23
- "@docusaurus/utils-validation": "2.0.0-beta.1decd6f80",
24
- "fs-extra": "^10.0.0",
25
- "sitemap": "^7.0.0",
26
- "tslib": "^2.2.0"
21
+ "@docusaurus/core": "2.0.0-beta.20",
22
+ "@docusaurus/utils": "2.0.0-beta.20",
23
+ "@docusaurus/utils-common": "2.0.0-beta.20",
24
+ "@docusaurus/utils-validation": "2.0.0-beta.20",
25
+ "fs-extra": "^10.1.0",
26
+ "sitemap": "^7.1.1",
27
+ "tslib": "^2.4.0"
28
+ },
29
+ "devDependencies": {
30
+ "@docusaurus/types": "2.0.0-beta.20"
27
31
  },
28
32
  "peerDependencies": {
29
33
  "react": "^16.8.4 || ^17.0.0",
30
34
  "react-dom": "^16.8.4 || ^17.0.0"
31
35
  },
32
36
  "engines": {
33
- "node": ">=12.13.0"
37
+ "node": ">=14"
34
38
  },
35
- "gitHead": "0f82d5997e05666fa8c0e8852e551c1a8ffe3876"
39
+ "gitHead": "ed5cdba401a5948e187e927039b142a0decc702d"
36
40
  }
@@ -6,40 +6,56 @@
6
6
  */
7
7
 
8
8
  import {SitemapStream, streamToPromise} from 'sitemap';
9
- import {PluginOptions} from './types';
10
- import {DocusaurusConfig} from '@docusaurus/types';
11
- import {addTrailingSlash} from '@docusaurus/utils';
9
+ import {applyTrailingSlash} from '@docusaurus/utils-common';
10
+ import {createMatcher} from '@docusaurus/utils';
11
+ import type {DocusaurusConfig} from '@docusaurus/types';
12
+ import type {HelmetServerState} from 'react-helmet-async';
13
+ import type {PluginOptions} from './options';
14
+ import type {ReactElement} from 'react';
12
15
 
13
16
  export default async function createSitemap(
14
17
  siteConfig: DocusaurusConfig,
15
18
  routesPaths: string[],
19
+ head: {[location: string]: HelmetServerState},
16
20
  options: PluginOptions,
17
21
  ): Promise<string> {
18
22
  const {url: hostname} = siteConfig;
19
23
  if (!hostname) {
20
- throw new Error('url in docusaurus.config.js cannot be empty/undefined');
24
+ throw new Error('URL in docusaurus.config.js cannot be empty/undefined.');
21
25
  }
22
- const {changefreq, priority, trailingSlash} = options;
23
-
24
- const sitemapStream = new SitemapStream({
25
- hostname,
26
- });
27
-
28
- routesPaths
29
- .filter((route) => !route.endsWith('404.html'))
30
- .map((routePath) =>
31
- sitemapStream.write({
32
- url: trailingSlash ? addTrailingSlash(routePath) : routePath,
33
- changefreq,
34
- priority,
35
- }),
26
+ const {changefreq, priority, ignorePatterns} = options;
27
+
28
+ const ignoreMatcher = createMatcher(ignorePatterns);
29
+
30
+ const sitemapStream = new SitemapStream({hostname});
31
+
32
+ function routeShouldBeIncluded(route: string) {
33
+ if (route.endsWith('404.html') || ignoreMatcher(route)) {
34
+ return false;
35
+ }
36
+ // https://github.com/staylor/react-helmet-async/pull/167
37
+ const meta = head[route]?.meta.toComponent() as unknown as
38
+ | ReactElement[]
39
+ | undefined;
40
+ return !meta?.some(
41
+ (tag) => tag.props.name === 'robots' && tag.props.content === 'noindex',
36
42
  );
43
+ }
44
+
45
+ routesPaths.filter(routeShouldBeIncluded).forEach((routePath) =>
46
+ sitemapStream.write({
47
+ url: applyTrailingSlash(routePath, {
48
+ trailingSlash: siteConfig.trailingSlash,
49
+ baseUrl: siteConfig.baseUrl,
50
+ }),
51
+ changefreq,
52
+ priority,
53
+ }),
54
+ );
37
55
 
38
56
  sitemapStream.end();
39
57
 
40
- const generatedSitemap = await streamToPromise(sitemapStream).then((sm) =>
41
- sm.toString(),
42
- );
58
+ const generatedSitemap = (await streamToPromise(sitemapStream)).toString();
43
59
 
44
60
  return generatedSitemap;
45
61
  }
package/src/index.ts CHANGED
@@ -7,29 +7,26 @@
7
7
 
8
8
  import fs from 'fs-extra';
9
9
  import path from 'path';
10
- import {PluginOptions} from './types';
11
10
  import createSitemap from './createSitemap';
12
- import {
13
- LoadContext,
14
- Props,
15
- OptionValidationContext,
16
- ValidationResult,
17
- Plugin,
18
- } from '@docusaurus/types';
19
- import {PluginOptionSchema} from './pluginOptionSchema';
11
+ import type {PluginOptions, Options} from './options';
12
+ import type {LoadContext, Plugin} from '@docusaurus/types';
20
13
 
21
14
  export default function pluginSitemap(
22
- _context: LoadContext,
15
+ context: LoadContext,
23
16
  options: PluginOptions,
24
17
  ): Plugin<void> {
25
18
  return {
26
19
  name: 'docusaurus-plugin-sitemap',
27
20
 
28
- async postBuild({siteConfig, routesPaths, outDir}: Props) {
21
+ async postBuild({siteConfig, routesPaths, outDir, head}) {
22
+ if (siteConfig.noIndex) {
23
+ return;
24
+ }
29
25
  // Generate sitemap.
30
26
  const generatedSitemap = await createSitemap(
31
27
  siteConfig,
32
28
  routesPaths,
29
+ head,
33
30
  options,
34
31
  );
35
32
 
@@ -38,16 +35,11 @@ export default function pluginSitemap(
38
35
  try {
39
36
  await fs.outputFile(sitemapPath, generatedSitemap);
40
37
  } catch (err) {
41
- throw new Error(`Sitemap error: ${err}`);
38
+ throw new Error(`Writing sitemap failed: ${err}`);
42
39
  }
43
40
  },
44
41
  };
45
42
  }
46
43
 
47
- export function validateOptions({
48
- validate,
49
- options,
50
- }: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions> {
51
- const validatedOptions = validate(PluginOptionSchema, options);
52
- return validatedOptions;
53
- }
44
+ export {validateOptions} from './options';
45
+ export type {PluginOptions, Options};
package/src/options.ts ADDED
@@ -0,0 +1,56 @@
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 {Joi} from '@docusaurus/utils-validation';
9
+ import {EnumChangefreq} from 'sitemap';
10
+ import type {OptionValidationContext} from '@docusaurus/types';
11
+
12
+ export type PluginOptions = {
13
+ /** @see https://www.sitemaps.org/protocol.html#xmlTagDefinitions */
14
+ changefreq: EnumChangefreq;
15
+ /** @see https://www.sitemaps.org/protocol.html#xmlTagDefinitions */
16
+ priority: number;
17
+ /**
18
+ * A list of glob patterns; matching route paths will be filtered from the
19
+ * sitemap. Note that you may need to include the base URL in here.
20
+ */
21
+ ignorePatterns: string[];
22
+ };
23
+
24
+ export type Options = Partial<PluginOptions>;
25
+
26
+ export const DEFAULT_OPTIONS: PluginOptions = {
27
+ changefreq: EnumChangefreq.WEEKLY,
28
+ priority: 0.5,
29
+ ignorePatterns: [],
30
+ };
31
+
32
+ const PluginOptionSchema = Joi.object({
33
+ cacheTime: Joi.forbidden().messages({
34
+ 'any.unknown':
35
+ 'Option `cacheTime` in sitemap config is deprecated. Please remove it.',
36
+ }),
37
+ changefreq: Joi.string()
38
+ .valid(...Object.values(EnumChangefreq))
39
+ .default(DEFAULT_OPTIONS.changefreq),
40
+ priority: Joi.number().min(0).max(1).default(DEFAULT_OPTIONS.priority),
41
+ ignorePatterns: Joi.array()
42
+ .items(Joi.string())
43
+ .default(DEFAULT_OPTIONS.ignorePatterns),
44
+ trailingSlash: Joi.forbidden().messages({
45
+ 'any.unknown':
46
+ 'Please use the new Docusaurus global trailingSlash config instead, and the sitemaps plugin will use it.',
47
+ }),
48
+ });
49
+
50
+ export function validateOptions({
51
+ validate,
52
+ options,
53
+ }: OptionValidationContext<Options, PluginOptions>): PluginOptions {
54
+ const validatedOptions = validate(PluginOptionSchema, options);
55
+ return validatedOptions;
56
+ }