@docusaurus/plugin-content-docs 2.0.0-beta.16 → 2.0.0-beta.19

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 (93) hide show
  1. package/lib/categoryGeneratedIndex.d.ts +1 -1
  2. package/lib/categoryGeneratedIndex.js +5 -7
  3. package/lib/cli.d.ts +3 -2
  4. package/lib/cli.js +49 -41
  5. package/lib/client/docsClientUtils.d.ts +8 -5
  6. package/lib/client/docsClientUtils.js +13 -15
  7. package/lib/client/index.d.ts +12 -9
  8. package/lib/client/index.js +30 -33
  9. package/lib/constants.d.ts +4 -0
  10. package/lib/constants.js +4 -1
  11. package/lib/docs.d.ts +8 -15
  12. package/lib/docs.js +34 -39
  13. package/lib/{docFrontMatter.d.ts → frontMatter.d.ts} +4 -2
  14. package/lib/{docFrontMatter.js → frontMatter.js} +3 -0
  15. package/lib/globalData.d.ts +3 -7
  16. package/lib/globalData.js +9 -13
  17. package/lib/index.d.ts +1 -2
  18. package/lib/index.js +33 -26
  19. package/lib/lastUpdate.d.ts +4 -6
  20. package/lib/lastUpdate.js +14 -5
  21. package/lib/markdown/index.js +1 -1
  22. package/lib/markdown/linkify.js +5 -2
  23. package/lib/numberPrefix.js +16 -22
  24. package/lib/options.d.ts +3 -5
  25. package/lib/options.js +6 -5
  26. package/lib/props.d.ts +3 -3
  27. package/lib/props.js +10 -10
  28. package/lib/routes.d.ts +5 -4
  29. package/lib/routes.js +10 -24
  30. package/lib/server-export.d.ts +2 -1
  31. package/lib/server-export.js +3 -4
  32. package/lib/sidebars/generator.js +63 -44
  33. package/lib/sidebars/index.js +20 -16
  34. package/lib/sidebars/normalization.js +3 -3
  35. package/lib/sidebars/postProcessor.js +18 -25
  36. package/lib/sidebars/processor.d.ts +3 -1
  37. package/lib/sidebars/processor.js +17 -6
  38. package/lib/sidebars/types.d.ts +31 -19
  39. package/lib/sidebars/utils.d.ts +17 -6
  40. package/lib/sidebars/utils.js +27 -37
  41. package/lib/sidebars/validation.d.ts +3 -1
  42. package/lib/sidebars/validation.js +1 -0
  43. package/lib/slug.d.ts +1 -2
  44. package/lib/slug.js +4 -5
  45. package/lib/tags.d.ts +2 -1
  46. package/lib/tags.js +2 -2
  47. package/lib/translations.d.ts +3 -3
  48. package/lib/translations.js +28 -81
  49. package/lib/types.d.ts +10 -95
  50. package/lib/versions/files.d.ts +44 -0
  51. package/lib/versions/files.js +142 -0
  52. package/lib/versions/index.d.ts +36 -0
  53. package/lib/versions/index.js +155 -0
  54. package/lib/versions/validation.d.ts +17 -0
  55. package/lib/versions/validation.js +71 -0
  56. package/package.json +14 -12
  57. package/src/categoryGeneratedIndex.ts +10 -9
  58. package/src/cli.ts +64 -69
  59. package/src/client/docsClientUtils.ts +14 -16
  60. package/src/client/index.ts +42 -43
  61. package/src/constants.ts +4 -2
  62. package/src/deps.d.ts +1 -1
  63. package/src/docs.ts +48 -51
  64. package/src/{docFrontMatter.ts → frontMatter.ts} +9 -6
  65. package/src/globalData.ts +15 -16
  66. package/src/index.ts +45 -40
  67. package/src/lastUpdate.ts +20 -8
  68. package/src/markdown/index.ts +1 -3
  69. package/src/markdown/linkify.ts +6 -3
  70. package/src/numberPrefix.ts +18 -28
  71. package/src/options.ts +6 -8
  72. package/src/plugin-content-docs.d.ts +457 -116
  73. package/src/props.ts +12 -9
  74. package/src/routes.ts +13 -39
  75. package/src/server-export.ts +1 -3
  76. package/src/sidebars/generator.ts +88 -59
  77. package/src/sidebars/index.ts +20 -15
  78. package/src/sidebars/normalization.ts +1 -1
  79. package/src/sidebars/postProcessor.ts +6 -11
  80. package/src/sidebars/processor.ts +27 -14
  81. package/src/sidebars/types.ts +25 -23
  82. package/src/sidebars/utils.ts +45 -46
  83. package/src/sidebars/validation.ts +4 -3
  84. package/src/slug.ts +7 -6
  85. package/src/tags.ts +3 -2
  86. package/src/translations.ts +32 -84
  87. package/src/types.ts +15 -107
  88. package/src/versions/files.ts +220 -0
  89. package/src/versions/index.ts +247 -0
  90. package/src/versions/validation.ts +113 -0
  91. package/lib/versions.d.ts +0 -41
  92. package/lib/versions.js +0 -329
  93. package/src/versions.ts +0 -606
@@ -0,0 +1,113 @@
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 _ from 'lodash';
9
+ import type {VersionsOptions} from '@docusaurus/plugin-content-docs';
10
+
11
+ export function validateVersionName(name: unknown): asserts name is string {
12
+ if (typeof name !== 'string') {
13
+ throw new Error(
14
+ `Versions should be strings. Found type "${typeof name}" for version "${name}".`,
15
+ );
16
+ }
17
+ if (!name.trim()) {
18
+ throw new Error(
19
+ `Invalid version name "${name}": version name must contain at least one non-whitespace character.`,
20
+ );
21
+ }
22
+ const errors: [RegExp, string][] = [
23
+ [/[/\\]/, 'should not include slash (/) or backslash (\\)'],
24
+ [/.{33,}/, 'cannot be longer than 32 characters'],
25
+ // eslint-disable-next-line no-control-regex
26
+ [/[<>:"|?*\x00-\x1F]/, 'should be a valid file path'],
27
+ [/^\.\.?$/, 'should not be "." or ".."'],
28
+ ];
29
+
30
+ errors.forEach(([pattern, message]) => {
31
+ if (pattern.test(name)) {
32
+ throw new Error(
33
+ `Invalid version name "${name}": version name ${message}.`,
34
+ );
35
+ }
36
+ });
37
+ }
38
+
39
+ export function validateVersionNames(
40
+ names: unknown,
41
+ ): asserts names is string[] {
42
+ if (!Array.isArray(names)) {
43
+ throw new Error(
44
+ `The versions file should contain an array of version names! Found content: ${JSON.stringify(
45
+ names,
46
+ )}`,
47
+ );
48
+ }
49
+
50
+ names.forEach(validateVersionName);
51
+ }
52
+
53
+ /**
54
+ * @throws Throws for one of the following invalid options:
55
+ * - `lastVersion` is non-existent
56
+ * - `versions` includes unknown keys
57
+ * - `onlyIncludeVersions` is empty, contains unknown names, or doesn't include
58
+ * `latestVersion` (if provided)
59
+ */
60
+ export function validateVersionsOptions(
61
+ availableVersionNames: string[],
62
+ options: VersionsOptions,
63
+ ): void {
64
+ const availableVersionNamesMsg = `Available version names are: ${availableVersionNames.join(
65
+ ', ',
66
+ )}`;
67
+ if (
68
+ options.lastVersion &&
69
+ !availableVersionNames.includes(options.lastVersion)
70
+ ) {
71
+ throw new Error(
72
+ `Docs option lastVersion: ${options.lastVersion} is invalid. ${availableVersionNamesMsg}`,
73
+ );
74
+ }
75
+ const unknownVersionConfigNames = _.difference(
76
+ Object.keys(options.versions),
77
+ availableVersionNames,
78
+ );
79
+ if (unknownVersionConfigNames.length > 0) {
80
+ throw new Error(
81
+ `Invalid docs option "versions": unknown versions (${unknownVersionConfigNames.join(
82
+ ',',
83
+ )}) found. ${availableVersionNamesMsg}`,
84
+ );
85
+ }
86
+
87
+ if (options.onlyIncludeVersions) {
88
+ if (options.onlyIncludeVersions.length === 0) {
89
+ throw new Error(
90
+ `Invalid docs option "onlyIncludeVersions": an empty array is not allowed, at least one version is needed.`,
91
+ );
92
+ }
93
+ const unknownOnlyIncludeVersionNames = _.difference(
94
+ options.onlyIncludeVersions,
95
+ availableVersionNames,
96
+ );
97
+ if (unknownOnlyIncludeVersionNames.length > 0) {
98
+ throw new Error(
99
+ `Invalid docs option "onlyIncludeVersions": unknown versions (${unknownOnlyIncludeVersionNames.join(
100
+ ',',
101
+ )}) found. ${availableVersionNamesMsg}`,
102
+ );
103
+ }
104
+ if (
105
+ options.lastVersion &&
106
+ !options.onlyIncludeVersions.includes(options.lastVersion)
107
+ ) {
108
+ throw new Error(
109
+ `Invalid docs option "lastVersion": if you use both the "onlyIncludeVersions" and "lastVersion" options, then "lastVersion" must be present in the provided "onlyIncludeVersions" array.`,
110
+ );
111
+ }
112
+ }
113
+ }
package/lib/versions.d.ts DELETED
@@ -1,41 +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 { VersionMetadata } from './types';
8
- import type { PluginOptions, VersionBanner } from '@docusaurus/plugin-content-docs';
9
- import type { LoadContext } from '@docusaurus/types';
10
- export declare function getVersionedDocsDirPath(siteDir: string, pluginId: string): string;
11
- export declare function getVersionedSidebarsDirPath(siteDir: string, pluginId: string): string;
12
- export declare function getVersionsFilePath(siteDir: string, pluginId: string): string;
13
- export declare function readVersionsFile(siteDir: string, pluginId: string): Promise<string[] | null>;
14
- export declare function readVersionNames(siteDir: string, options: Pick<PluginOptions, 'id' | 'disableVersioning' | 'includeCurrentVersion'>): Promise<string[]>;
15
- export declare function getDefaultVersionBanner({ versionName, versionNames, lastVersionName, }: {
16
- versionName: string;
17
- versionNames: string[];
18
- lastVersionName: string;
19
- }): VersionBanner | null;
20
- export declare function getVersionBanner({ versionName, versionNames, lastVersionName, options, }: {
21
- versionName: string;
22
- versionNames: string[];
23
- lastVersionName: string;
24
- options: Pick<PluginOptions, 'versions'>;
25
- }): VersionBanner | null;
26
- export declare function getVersionBadge({ versionName, versionNames, options, }: {
27
- versionName: string;
28
- versionNames: string[];
29
- options: Pick<PluginOptions, 'versions'>;
30
- }): boolean;
31
- /**
32
- * Filter versions according to provided options.
33
- * Note: we preserve the order in which versions are provided;
34
- * the order of the onlyIncludeVersions array does not matter
35
- */
36
- export declare function filterVersions(versionNamesUnfiltered: string[], options: Pick<PluginOptions, 'onlyIncludeVersions'>): string[];
37
- export declare function readVersionsMetadata({ context, options, }: {
38
- context: Pick<LoadContext, 'siteDir' | 'baseUrl' | 'i18n'>;
39
- options: Pick<PluginOptions, 'id' | 'path' | 'sidebarPath' | 'routeBasePath' | 'tagsBasePath' | 'includeCurrentVersion' | 'disableVersioning' | 'lastVersion' | 'versions' | 'onlyIncludeVersions' | 'editUrl' | 'editCurrentVersion'>;
40
- }): Promise<VersionMetadata[]>;
41
- export declare function getDocsDirPaths(versionMetadata: Pick<VersionMetadata, 'contentPath' | 'contentPathLocalized'>): [string, string];
package/lib/versions.js DELETED
@@ -1,329 +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.getDocsDirPaths = exports.readVersionsMetadata = exports.filterVersions = exports.getVersionBadge = exports.getVersionBanner = exports.getDefaultVersionBanner = exports.readVersionNames = exports.readVersionsFile = exports.getVersionsFilePath = exports.getVersionedSidebarsDirPath = exports.getVersionedDocsDirPath = void 0;
10
- const tslib_1 = require("tslib");
11
- const path_1 = (0, tslib_1.__importDefault)(require("path"));
12
- const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
13
- const constants_1 = require("./constants");
14
- const utils_1 = require("@docusaurus/utils");
15
- const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
16
- const sidebars_1 = require("./sidebars");
17
- // retro-compatibility: no prefix for the default plugin id
18
- function addPluginIdPrefix(fileOrDir, pluginId) {
19
- return pluginId === utils_1.DEFAULT_PLUGIN_ID
20
- ? fileOrDir
21
- : `${pluginId}_${fileOrDir}`;
22
- }
23
- function getVersionedDocsDirPath(siteDir, pluginId) {
24
- return path_1.default.join(siteDir, addPluginIdPrefix(constants_1.VERSIONED_DOCS_DIR, pluginId));
25
- }
26
- exports.getVersionedDocsDirPath = getVersionedDocsDirPath;
27
- function getVersionedSidebarsDirPath(siteDir, pluginId) {
28
- return path_1.default.join(siteDir, addPluginIdPrefix(constants_1.VERSIONED_SIDEBARS_DIR, pluginId));
29
- }
30
- exports.getVersionedSidebarsDirPath = getVersionedSidebarsDirPath;
31
- function getVersionsFilePath(siteDir, pluginId) {
32
- return path_1.default.join(siteDir, addPluginIdPrefix(constants_1.VERSIONS_JSON_FILE, pluginId));
33
- }
34
- exports.getVersionsFilePath = getVersionsFilePath;
35
- function ensureValidVersionString(version) {
36
- if (typeof version !== 'string') {
37
- throw new Error(`Versions should be strings. Found type "${typeof version}" for version "${version}".`);
38
- }
39
- // Should we forbid versions with special chars like / ?
40
- if (version.trim().length === 0) {
41
- throw new Error(`Invalid version "${version}".`);
42
- }
43
- }
44
- function ensureValidVersionArray(versionArray) {
45
- if (!(versionArray instanceof Array)) {
46
- throw new Error(`The versions file should contain an array of versions! Found content: ${JSON.stringify(versionArray)}`);
47
- }
48
- versionArray.forEach(ensureValidVersionString);
49
- }
50
- async function readVersionsFile(siteDir, pluginId) {
51
- const versionsFilePath = getVersionsFilePath(siteDir, pluginId);
52
- if (await fs_extra_1.default.pathExists(versionsFilePath)) {
53
- const content = JSON.parse(await fs_extra_1.default.readFile(versionsFilePath, 'utf8'));
54
- ensureValidVersionArray(content);
55
- return content;
56
- }
57
- return null;
58
- }
59
- exports.readVersionsFile = readVersionsFile;
60
- async function readVersionNames(siteDir, options) {
61
- const versionFileContent = await readVersionsFile(siteDir, options.id);
62
- if (!versionFileContent && options.disableVersioning) {
63
- throw new Error(`Docs: using "disableVersioning: ${options.disableVersioning}" option on a non-versioned site does not make sense.`);
64
- }
65
- const versions = options.disableVersioning ? [] : versionFileContent !== null && versionFileContent !== void 0 ? versionFileContent : [];
66
- // We add the current version at the beginning, unless:
67
- // - user don't want to; or
68
- // - it's already been explicitly added to versions.json
69
- if (options.includeCurrentVersion &&
70
- !versions.includes(constants_1.CURRENT_VERSION_NAME)) {
71
- versions.unshift(constants_1.CURRENT_VERSION_NAME);
72
- }
73
- if (versions.length === 0) {
74
- throw new Error(`It is not possible to use docs without any version. Please check the configuration of these options: "includeCurrentVersion: ${options.includeCurrentVersion}", "disableVersioning: ${options.disableVersioning}".`);
75
- }
76
- return versions;
77
- }
78
- exports.readVersionNames = readVersionNames;
79
- function getDocsDirPathLocalized({ siteDir, locale, pluginId, versionName, }) {
80
- return (0, utils_1.getPluginI18nPath)({
81
- siteDir,
82
- locale,
83
- pluginName: 'docusaurus-plugin-content-docs',
84
- pluginId,
85
- subPaths: [
86
- versionName === constants_1.CURRENT_VERSION_NAME
87
- ? constants_1.CURRENT_VERSION_NAME
88
- : `version-${versionName}`,
89
- ],
90
- });
91
- }
92
- function getVersionMetadataPaths({ versionName, context, options, }) {
93
- const isCurrentVersion = versionName === constants_1.CURRENT_VERSION_NAME;
94
- const contentPathLocalized = getDocsDirPathLocalized({
95
- siteDir: context.siteDir,
96
- locale: context.i18n.currentLocale,
97
- pluginId: options.id,
98
- versionName,
99
- });
100
- if (isCurrentVersion) {
101
- return {
102
- contentPath: path_1.default.resolve(context.siteDir, options.path),
103
- contentPathLocalized,
104
- sidebarFilePath: (0, sidebars_1.resolveSidebarPathOption)(context.siteDir, options.sidebarPath),
105
- };
106
- }
107
- return {
108
- contentPath: path_1.default.join(getVersionedDocsDirPath(context.siteDir, options.id), `version-${versionName}`),
109
- contentPathLocalized,
110
- sidebarFilePath: path_1.default.join(getVersionedSidebarsDirPath(context.siteDir, options.id), `version-${versionName}-sidebars.json`),
111
- };
112
- }
113
- function getVersionEditUrls({ contentPath, contentPathLocalized, context: { siteDir, i18n }, options: { id, path: currentVersionPath, editUrl, editCurrentVersion }, }) {
114
- if (!editUrl) {
115
- return undefined;
116
- }
117
- // if the user is using the functional form of editUrl,
118
- // he has total freedom and we can't compute a "version edit url"
119
- if (typeof editUrl === 'function') {
120
- return undefined;
121
- }
122
- const editDirPath = editCurrentVersion ? currentVersionPath : contentPath;
123
- const editDirPathLocalized = editCurrentVersion
124
- ? getDocsDirPathLocalized({
125
- siteDir,
126
- locale: i18n.currentLocale,
127
- versionName: constants_1.CURRENT_VERSION_NAME,
128
- pluginId: id,
129
- })
130
- : contentPathLocalized;
131
- const versionPathSegment = (0, utils_1.posixPath)(path_1.default.relative(siteDir, path_1.default.resolve(siteDir, editDirPath)));
132
- const versionPathSegmentLocalized = (0, utils_1.posixPath)(path_1.default.relative(siteDir, path_1.default.resolve(siteDir, editDirPathLocalized)));
133
- const versionEditUrl = (0, utils_1.normalizeUrl)([editUrl, versionPathSegment]);
134
- const versionEditUrlLocalized = (0, utils_1.normalizeUrl)([
135
- editUrl,
136
- versionPathSegmentLocalized,
137
- ]);
138
- return {
139
- versionEditUrl,
140
- versionEditUrlLocalized,
141
- };
142
- }
143
- function getDefaultVersionBanner({ versionName, versionNames, lastVersionName, }) {
144
- // Current version: good, no banner
145
- if (versionName === lastVersionName) {
146
- return null;
147
- }
148
- // Upcoming versions: unreleased banner
149
- if (versionNames.indexOf(versionName) < versionNames.indexOf(lastVersionName)) {
150
- return 'unreleased';
151
- }
152
- // Older versions: display unmaintained banner
153
- return 'unmaintained';
154
- }
155
- exports.getDefaultVersionBanner = getDefaultVersionBanner;
156
- function getVersionBanner({ versionName, versionNames, lastVersionName, options, }) {
157
- var _a;
158
- const versionBannerOption = (_a = options.versions[versionName]) === null || _a === void 0 ? void 0 : _a.banner;
159
- if (versionBannerOption) {
160
- return versionBannerOption === 'none' ? null : versionBannerOption;
161
- }
162
- return getDefaultVersionBanner({
163
- versionName,
164
- versionNames,
165
- lastVersionName,
166
- });
167
- }
168
- exports.getVersionBanner = getVersionBanner;
169
- function getVersionBadge({ versionName, versionNames, options, }) {
170
- var _a;
171
- const versionBadgeOption = (_a = options.versions[versionName]) === null || _a === void 0 ? void 0 : _a.badge;
172
- // If site is not versioned or only one version is included
173
- // we don't show the version badge by default
174
- // See https://github.com/facebook/docusaurus/issues/3362
175
- const versionBadgeDefault = versionNames.length !== 1;
176
- return versionBadgeOption !== null && versionBadgeOption !== void 0 ? versionBadgeOption : versionBadgeDefault;
177
- }
178
- exports.getVersionBadge = getVersionBadge;
179
- function getVersionClassName({ versionName, options, }) {
180
- var _a;
181
- const versionClassNameOption = (_a = options.versions[versionName]) === null || _a === void 0 ? void 0 : _a.className;
182
- const versionClassNameDefault = `docs-version-${versionName}`;
183
- return versionClassNameOption !== null && versionClassNameOption !== void 0 ? versionClassNameOption : versionClassNameDefault;
184
- }
185
- function createVersionMetadata({ versionName, versionNames, lastVersionName, context, options, }) {
186
- var _a, _b, _c;
187
- const { sidebarFilePath, contentPath, contentPathLocalized } = getVersionMetadataPaths({ versionName, context, options });
188
- const isLast = versionName === lastVersionName;
189
- // retro-compatible values
190
- const defaultVersionLabel = versionName === constants_1.CURRENT_VERSION_NAME ? 'Next' : versionName;
191
- function getDefaultVersionPathPart() {
192
- if (isLast) {
193
- return '';
194
- }
195
- return versionName === constants_1.CURRENT_VERSION_NAME ? 'next' : versionName;
196
- }
197
- const defaultVersionPathPart = getDefaultVersionPathPart();
198
- const versionOptions = (_a = options.versions[versionName]) !== null && _a !== void 0 ? _a : {};
199
- const versionLabel = (_b = versionOptions.label) !== null && _b !== void 0 ? _b : defaultVersionLabel;
200
- const versionPathPart = (_c = versionOptions.path) !== null && _c !== void 0 ? _c : defaultVersionPathPart;
201
- const versionPath = (0, utils_1.normalizeUrl)([
202
- context.baseUrl,
203
- options.routeBasePath,
204
- versionPathPart,
205
- ]);
206
- const versionEditUrls = getVersionEditUrls({
207
- contentPath,
208
- contentPathLocalized,
209
- context,
210
- options,
211
- });
212
- // Because /docs/:route` should always be after `/docs/versionName/:route`.
213
- const routePriority = versionPathPart === '' ? -1 : undefined;
214
- // the path that will be used to refer the docs tags
215
- // example below will be using /docs/tags
216
- const tagsPath = (0, utils_1.normalizeUrl)([versionPath, options.tagsBasePath]);
217
- return {
218
- versionName,
219
- versionLabel,
220
- versionPath,
221
- tagsPath,
222
- versionEditUrl: versionEditUrls === null || versionEditUrls === void 0 ? void 0 : versionEditUrls.versionEditUrl,
223
- versionEditUrlLocalized: versionEditUrls === null || versionEditUrls === void 0 ? void 0 : versionEditUrls.versionEditUrlLocalized,
224
- versionBanner: getVersionBanner({
225
- versionName,
226
- versionNames,
227
- lastVersionName,
228
- options,
229
- }),
230
- versionBadge: getVersionBadge({ versionName, versionNames, options }),
231
- versionClassName: getVersionClassName({ versionName, options }),
232
- isLast,
233
- routePriority,
234
- sidebarFilePath,
235
- contentPath,
236
- contentPathLocalized,
237
- };
238
- }
239
- async function checkVersionMetadataPaths({ versionMetadata, context, }) {
240
- const { versionName, contentPath, sidebarFilePath } = versionMetadata;
241
- const { siteDir } = context;
242
- const isCurrentVersion = versionName === constants_1.CURRENT_VERSION_NAME;
243
- if (!(await fs_extra_1.default.pathExists(contentPath))) {
244
- throw new Error(`The docs folder does not exist for version "${versionName}". A docs folder is expected to be found at ${path_1.default.relative(siteDir, contentPath)}.`);
245
- }
246
- // If the current version defines a path to a sidebar file that does not
247
- // exist, we throw! Note: for versioned sidebars, the file may not exist (as
248
- // we prefer to not create it rather than to create an empty file)
249
- // See https://github.com/facebook/docusaurus/issues/3366
250
- // See https://github.com/facebook/docusaurus/pull/4775
251
- if (isCurrentVersion &&
252
- typeof sidebarFilePath === 'string' &&
253
- !(await fs_extra_1.default.pathExists(sidebarFilePath))) {
254
- throw new Error(`The path to the sidebar file does not exist at "${path_1.default.relative(siteDir, sidebarFilePath)}".
255
- Please set the docs "sidebarPath" field in your config file to:
256
- - a sidebars path that exists
257
- - false: to disable the sidebar
258
- - undefined: for Docusaurus to generate it automatically`);
259
- }
260
- }
261
- // TODO for retrocompatibility with existing behavior
262
- // We should make this configurable
263
- // "last version" is not a very good concept nor api surface
264
- function getDefaultLastVersionName(versionNames) {
265
- if (versionNames.length === 1) {
266
- return versionNames[0];
267
- }
268
- return versionNames.filter((versionName) => versionName !== constants_1.CURRENT_VERSION_NAME)[0];
269
- }
270
- function checkVersionsOptions(availableVersionNames, options) {
271
- const availableVersionNamesMsg = `Available version names are: ${availableVersionNames.join(', ')}`;
272
- if (options.lastVersion &&
273
- !availableVersionNames.includes(options.lastVersion)) {
274
- throw new Error(`Docs option lastVersion: ${options.lastVersion} is invalid. ${availableVersionNamesMsg}`);
275
- }
276
- const unknownVersionConfigNames = lodash_1.default.difference(Object.keys(options.versions), availableVersionNames);
277
- if (unknownVersionConfigNames.length > 0) {
278
- throw new Error(`Invalid docs option "versions": unknown versions (${unknownVersionConfigNames.join(',')}) found. ${availableVersionNamesMsg}`);
279
- }
280
- if (options.onlyIncludeVersions) {
281
- if (options.onlyIncludeVersions.length === 0) {
282
- throw new Error(`Invalid docs option "onlyIncludeVersions": an empty array is not allowed, at least one version is needed.`);
283
- }
284
- const unknownOnlyIncludeVersionNames = lodash_1.default.difference(options.onlyIncludeVersions, availableVersionNames);
285
- if (unknownOnlyIncludeVersionNames.length > 0) {
286
- throw new Error(`Invalid docs option "onlyIncludeVersions": unknown versions (${unknownOnlyIncludeVersionNames.join(',')}) found. ${availableVersionNamesMsg}`);
287
- }
288
- if (options.lastVersion &&
289
- !options.onlyIncludeVersions.includes(options.lastVersion)) {
290
- throw new Error(`Invalid docs option "lastVersion": if you use both the "onlyIncludeVersions" and "lastVersion" options, then "lastVersion" must be present in the provided "onlyIncludeVersions" array.`);
291
- }
292
- }
293
- }
294
- /**
295
- * Filter versions according to provided options.
296
- * Note: we preserve the order in which versions are provided;
297
- * the order of the onlyIncludeVersions array does not matter
298
- */
299
- function filterVersions(versionNamesUnfiltered, options) {
300
- if (options.onlyIncludeVersions) {
301
- return versionNamesUnfiltered.filter((name) => (options.onlyIncludeVersions || []).includes(name));
302
- }
303
- return versionNamesUnfiltered;
304
- }
305
- exports.filterVersions = filterVersions;
306
- async function readVersionsMetadata({ context, options, }) {
307
- var _a;
308
- const versionNamesUnfiltered = await readVersionNames(context.siteDir, options);
309
- checkVersionsOptions(versionNamesUnfiltered, options);
310
- const versionNames = filterVersions(versionNamesUnfiltered, options);
311
- const lastVersionName = (_a = options.lastVersion) !== null && _a !== void 0 ? _a : getDefaultLastVersionName(versionNames);
312
- const versionsMetadata = versionNames.map((versionName) => createVersionMetadata({
313
- versionName,
314
- versionNames,
315
- lastVersionName,
316
- context,
317
- options,
318
- }));
319
- await Promise.all(versionsMetadata.map((versionMetadata) => checkVersionMetadataPaths({ versionMetadata, context })));
320
- return versionsMetadata;
321
- }
322
- exports.readVersionsMetadata = readVersionsMetadata;
323
- // order matter!
324
- // Read in priority the localized path, then the unlocalized one
325
- // We want the localized doc to "override" the unlocalized one
326
- function getDocsDirPaths(versionMetadata) {
327
- return [versionMetadata.contentPathLocalized, versionMetadata.contentPath];
328
- }
329
- exports.getDocsDirPaths = getDocsDirPaths;