@docusaurus/plugin-sitemap 2.0.0-beta.ff31de0ff → 2.0.0-rc.1

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