@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
package/src/types.ts CHANGED
@@ -7,13 +7,14 @@
7
7
 
8
8
  /// <reference types="@docusaurus/module-type-aliases" />
9
9
 
10
- import type {Sidebars} from './sidebars/types';
11
- import type {Tag, FrontMatterTag} from '@docusaurus/utils';
10
+ import type {BrokenMarkdownLink, Tag} from '@docusaurus/utils';
12
11
  import type {
13
- BrokenMarkdownLink as IBrokenMarkdownLink,
14
- ContentPaths,
15
- } from '@docusaurus/utils/lib/markdownLinks';
16
- import type {VersionBanner} from '@docusaurus/plugin-content-docs';
12
+ VersionMetadata,
13
+ LastUpdateData,
14
+ LoadedVersion,
15
+ CategoryGeneratedIndexMetadata,
16
+ } from '@docusaurus/plugin-content-docs';
17
+ import type {SidebarsUtils} from './sidebars/utils';
17
18
 
18
19
  export type DocFile = {
19
20
  contentPath: string; // /!\ may be localized
@@ -23,121 +24,28 @@ export type DocFile = {
23
24
  lastUpdate: LastUpdateData;
24
25
  };
25
26
 
26
- export type VersionMetadata = ContentPaths & {
27
- versionName: string; // 1.0.0
28
- versionLabel: string; // Version 1.0.0
29
- versionPath: string; // /baseUrl/docs/1.0.0
30
- tagsPath: string;
31
- versionEditUrl?: string | undefined;
32
- versionEditUrlLocalized?: string | undefined;
33
- versionBanner: VersionBanner | null;
34
- versionBadge: boolean;
35
- versionClassName: string;
36
- isLast: boolean;
37
- sidebarFilePath: string | false | undefined; // versioned_sidebars/1.0.0.json
38
- routePriority: number | undefined; // -1 for the latest docs
39
- };
40
-
41
- export type LastUpdateData = {
42
- lastUpdatedAt?: number;
43
- formattedLastUpdatedAt?: string;
44
- lastUpdatedBy?: string;
45
- };
46
-
47
- export type DocFrontMatter = {
48
- // Front matter uses snake case
49
- id?: string;
50
- title?: string;
51
- tags?: FrontMatterTag[];
52
- hide_title?: boolean;
53
- hide_table_of_contents?: boolean;
54
- keywords?: string[];
55
- image?: string;
56
- description?: string;
57
- slug?: string;
58
- sidebar_label?: string;
59
- sidebar_position?: number;
60
- sidebar_class_name?: string;
61
- sidebar_custom_props?: Record<string, unknown>;
62
- displayed_sidebar?: string | null;
63
- pagination_label?: string;
64
- custom_edit_url?: string | null;
65
- parse_number_prefixes?: boolean;
66
- toc_min_heading_level?: number;
67
- toc_max_heading_level?: number;
68
- pagination_next?: string | null;
69
- pagination_prev?: string | null;
70
- };
71
-
72
- export type DocMetadataBase = LastUpdateData & {
73
- id: string; // TODO legacy versioned id => try to remove
74
- unversionedId: string; // TODO new unversioned id => try to rename to "id"
75
- version: string;
76
- title: string;
77
- description: string;
78
- source: string; // @site aliased posix source => "@site/docs/folder/subFolder/subSubFolder/myDoc.md"
79
- sourceDirName: string; // posix path relative to the versioned docs folder (can be ".") => "folder/subFolder/subSubFolder"
80
- slug: string;
81
- permalink: string;
82
- sidebarPosition?: number;
83
- editUrl?: string | null;
84
- tags: Tag[];
85
- frontMatter: DocFrontMatter & Record<string, unknown>;
86
- };
87
-
88
- export type DocNavLink = {
89
- title: string;
90
- permalink: string;
91
- };
92
-
93
- export type DocMetadata = DocMetadataBase & {
94
- sidebar?: string;
95
- previous?: DocNavLink;
96
- next?: DocNavLink;
97
- };
98
-
99
- export type CategoryGeneratedIndexMetadata = {
100
- title: string;
101
- description?: string;
102
- slug: string;
103
- permalink: string;
104
- sidebar: string;
105
- previous?: DocNavLink;
106
- next?: DocNavLink;
107
- image?: string;
108
- keywords?: string | readonly string[];
109
- };
110
-
111
27
  export type SourceToPermalink = {
112
28
  [source: string]: string;
113
29
  };
114
30
 
115
- export type VersionTag = {
116
- name: string; // normalized name/label of the tag
117
- docIds: string[]; // all doc ids having this tag
118
- permalink: string; // pathname of the tag
31
+ export type VersionTag = Tag & {
32
+ /** All doc ids having this tag. */
33
+ docIds: string[];
119
34
  };
120
35
  export type VersionTags = {
121
- [key: string]: VersionTag;
36
+ [permalink: string]: VersionTag;
122
37
  };
123
38
 
124
- export type LoadedVersion = VersionMetadata & {
125
- versionPath: string;
126
- mainDocId: string;
127
- docs: DocMetadata[];
128
- sidebars: Sidebars;
39
+ export type FullVersion = LoadedVersion & {
40
+ sidebarsUtils: SidebarsUtils;
129
41
  categoryGeneratedIndices: CategoryGeneratedIndexMetadata[];
130
42
  };
131
43
 
132
- export type LoadedContent = {
133
- loadedVersions: LoadedVersion[];
134
- };
135
-
136
- export type BrokenMarkdownLink = IBrokenMarkdownLink<VersionMetadata>;
44
+ export type DocBrokenMarkdownLink = BrokenMarkdownLink<VersionMetadata>;
137
45
 
138
46
  export type DocsMarkdownOption = {
139
47
  versionsMetadata: VersionMetadata[];
140
48
  siteDir: string;
141
49
  sourceToPermalink: SourceToPermalink;
142
- onBrokenMarkdownLink: (brokenMarkdownLink: BrokenMarkdownLink) => void;
50
+ onBrokenMarkdownLink: (brokenMarkdownLink: DocBrokenMarkdownLink) => void;
143
51
  };
@@ -0,0 +1,220 @@
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 path from 'path';
9
+ import fs from 'fs-extra';
10
+ import {
11
+ VERSIONS_JSON_FILE,
12
+ VERSIONED_DOCS_DIR,
13
+ VERSIONED_SIDEBARS_DIR,
14
+ CURRENT_VERSION_NAME,
15
+ } from '../constants';
16
+ import {validateVersionNames} from './validation';
17
+ import {getPluginI18nPath, DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
18
+ import type {
19
+ PluginOptions,
20
+ VersionMetadata,
21
+ } from '@docusaurus/plugin-content-docs';
22
+ import type {VersionContext} from './index';
23
+
24
+ /** Add a prefix like `community_version-1.0.0`. No-op for default instance. */
25
+ function addPluginIdPrefix(fileOrDir: string, pluginId: string): string {
26
+ return pluginId === DEFAULT_PLUGIN_ID
27
+ ? fileOrDir
28
+ : `${pluginId}_${fileOrDir}`;
29
+ }
30
+
31
+ /** `[siteDir]/community_versioned_docs/version-1.0.0` */
32
+ export function getVersionDocsDirPath(
33
+ siteDir: string,
34
+ pluginId: string,
35
+ versionName: string,
36
+ ): string {
37
+ return path.join(
38
+ siteDir,
39
+ addPluginIdPrefix(VERSIONED_DOCS_DIR, pluginId),
40
+ `version-${versionName}`,
41
+ );
42
+ }
43
+
44
+ /** `[siteDir]/community_versioned_sidebars/version-1.0.0-sidebars.json` */
45
+ export function getVersionSidebarsPath(
46
+ siteDir: string,
47
+ pluginId: string,
48
+ versionName: string,
49
+ ): string {
50
+ return path.join(
51
+ siteDir,
52
+ addPluginIdPrefix(VERSIONED_SIDEBARS_DIR, pluginId),
53
+ `version-${versionName}-sidebars.json`,
54
+ );
55
+ }
56
+
57
+ export function getDocsDirPathLocalized({
58
+ siteDir,
59
+ locale,
60
+ pluginId,
61
+ versionName,
62
+ }: {
63
+ siteDir: string;
64
+ locale: string;
65
+ pluginId: string;
66
+ versionName: string;
67
+ }): string {
68
+ return getPluginI18nPath({
69
+ siteDir,
70
+ locale,
71
+ pluginName: 'docusaurus-plugin-content-docs',
72
+ pluginId,
73
+ subPaths: [
74
+ versionName === CURRENT_VERSION_NAME
75
+ ? CURRENT_VERSION_NAME
76
+ : `version-${versionName}`,
77
+ ],
78
+ });
79
+ }
80
+
81
+ /** `community` => `[siteDir]/community_versions.json` */
82
+ export function getVersionsFilePath(siteDir: string, pluginId: string): string {
83
+ return path.join(siteDir, addPluginIdPrefix(VERSIONS_JSON_FILE, pluginId));
84
+ }
85
+
86
+ /**
87
+ * Reads the plugin's respective `versions.json` file, and returns its content.
88
+ *
89
+ * @throws Throws if validation fails, i.e. `versions.json` doesn't contain an
90
+ * array of valid version names.
91
+ */
92
+ async function readVersionsFile(
93
+ siteDir: string,
94
+ pluginId: string,
95
+ ): Promise<string[] | null> {
96
+ const versionsFilePath = getVersionsFilePath(siteDir, pluginId);
97
+ if (await fs.pathExists(versionsFilePath)) {
98
+ const content = await fs.readJSON(versionsFilePath);
99
+ validateVersionNames(content);
100
+ return content;
101
+ }
102
+ return null;
103
+ }
104
+
105
+ /**
106
+ * Reads the `versions.json` file, and returns an ordered list of version names.
107
+ *
108
+ * - If `disableVersioning` is turned on, it will return `["current"]` (requires
109
+ * `includeCurrentVersion` to be true);
110
+ * - If `includeCurrentVersion` is turned on, "current" will be inserted at the
111
+ * beginning, if not already there.
112
+ *
113
+ * You need to use {@link filterVersions} after this.
114
+ *
115
+ * @throws Throws an error if `disableVersioning: true` but `versions.json`
116
+ * doesn't exist (i.e. site is not versioned)
117
+ * @throws Throws an error if versions list is empty (empty `versions.json` or
118
+ * `disableVersioning` is true, and not including current version)
119
+ */
120
+ export async function readVersionNames(
121
+ siteDir: string,
122
+ options: PluginOptions,
123
+ ): Promise<string[]> {
124
+ const versionFileContent = await readVersionsFile(siteDir, options.id);
125
+
126
+ if (!versionFileContent && options.disableVersioning) {
127
+ throw new Error(
128
+ `Docs: using "disableVersioning: true" option on a non-versioned site does not make sense.`,
129
+ );
130
+ }
131
+
132
+ const versions = options.disableVersioning ? [] : versionFileContent ?? [];
133
+
134
+ // We add the current version at the beginning, unless:
135
+ // - user don't want to; or
136
+ // - it's already been explicitly added to versions.json
137
+ if (
138
+ options.includeCurrentVersion &&
139
+ !versions.includes(CURRENT_VERSION_NAME)
140
+ ) {
141
+ versions.unshift(CURRENT_VERSION_NAME);
142
+ }
143
+
144
+ if (versions.length === 0) {
145
+ throw new Error(
146
+ `It is not possible to use docs without any version. No version is included because you have requested to not include ${path.resolve(
147
+ options.path,
148
+ )} through "includeCurrentVersion: false", while ${
149
+ options.disableVersioning
150
+ ? 'versioning is disabled with "disableVersioning: true"'
151
+ : `the versions file is empty/non-existent`
152
+ }.`,
153
+ );
154
+ }
155
+
156
+ return versions;
157
+ }
158
+
159
+ /**
160
+ * Gets the path-related version metadata.
161
+ *
162
+ * @throws Throws if the resolved docs folder or sidebars file doesn't exist.
163
+ * Does not throw if a versioned sidebar is missing (since we don't create empty
164
+ * files).
165
+ */
166
+ export async function getVersionMetadataPaths({
167
+ versionName,
168
+ context,
169
+ options,
170
+ }: VersionContext): Promise<
171
+ Pick<
172
+ VersionMetadata,
173
+ 'contentPath' | 'contentPathLocalized' | 'sidebarFilePath'
174
+ >
175
+ > {
176
+ const isCurrent = versionName === CURRENT_VERSION_NAME;
177
+ const contentPathLocalized = getDocsDirPathLocalized({
178
+ siteDir: context.siteDir,
179
+ locale: context.i18n.currentLocale,
180
+ pluginId: options.id,
181
+ versionName,
182
+ });
183
+ const contentPath = isCurrent
184
+ ? path.resolve(context.siteDir, options.path)
185
+ : getVersionDocsDirPath(context.siteDir, options.id, versionName);
186
+ const sidebarFilePath = isCurrent
187
+ ? options.sidebarPath
188
+ : getVersionSidebarsPath(context.siteDir, options.id, versionName);
189
+
190
+ if (!(await fs.pathExists(contentPath))) {
191
+ throw new Error(
192
+ `The docs folder does not exist for version "${versionName}". A docs folder is expected to be found at ${path.relative(
193
+ context.siteDir,
194
+ contentPath,
195
+ )}.`,
196
+ );
197
+ }
198
+
199
+ // If the current version defines a path to a sidebar file that does not
200
+ // exist, we throw! Note: for versioned sidebars, the file may not exist (as
201
+ // we prefer to not create it rather than to create an empty file)
202
+ // See https://github.com/facebook/docusaurus/issues/3366
203
+ // See https://github.com/facebook/docusaurus/pull/4775
204
+ if (
205
+ versionName === CURRENT_VERSION_NAME &&
206
+ typeof sidebarFilePath === 'string' &&
207
+ !(await fs.pathExists(sidebarFilePath))
208
+ ) {
209
+ throw new Error(`The path to the sidebar file does not exist at "${path.relative(
210
+ context.siteDir,
211
+ sidebarFilePath,
212
+ )}".
213
+ Please set the docs "sidebarPath" field in your config file to:
214
+ - a sidebars path that exists
215
+ - false: to disable the sidebar
216
+ - undefined: for Docusaurus to generate it automatically`);
217
+ }
218
+
219
+ return {contentPath, contentPathLocalized, sidebarFilePath};
220
+ }
@@ -0,0 +1,247 @@
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 path from 'path';
9
+ import {CURRENT_VERSION_NAME} from '../constants';
10
+ import {normalizeUrl, posixPath} from '@docusaurus/utils';
11
+ import {validateVersionsOptions} from './validation';
12
+ import {
13
+ getDocsDirPathLocalized,
14
+ getVersionMetadataPaths,
15
+ readVersionNames,
16
+ } from './files';
17
+ import type {
18
+ PluginOptions,
19
+ VersionBanner,
20
+ VersionMetadata,
21
+ } from '@docusaurus/plugin-content-docs';
22
+ import type {LoadContext} from '@docusaurus/types';
23
+
24
+ export type VersionContext = {
25
+ /** The version name to get banner of. */
26
+ versionName: string;
27
+ /** All versions, ordered from newest to oldest. */
28
+ versionNames: string[];
29
+ lastVersionName: string;
30
+ context: LoadContext;
31
+ options: PluginOptions;
32
+ };
33
+
34
+ function getVersionEditUrls({
35
+ contentPath,
36
+ contentPathLocalized,
37
+ context,
38
+ options,
39
+ }: Pick<VersionMetadata, 'contentPath' | 'contentPathLocalized'> & {
40
+ context: LoadContext;
41
+ options: PluginOptions;
42
+ }): Pick<VersionMetadata, 'editUrl' | 'editUrlLocalized'> {
43
+ // If the user is using the functional form of editUrl,
44
+ // she has total freedom and we can't compute a "version edit url"
45
+ if (!options.editUrl || typeof options.editUrl === 'function') {
46
+ return {editUrl: undefined, editUrlLocalized: undefined};
47
+ }
48
+
49
+ const editDirPath = options.editCurrentVersion ? options.path : contentPath;
50
+ const editDirPathLocalized = options.editCurrentVersion
51
+ ? getDocsDirPathLocalized({
52
+ siteDir: context.siteDir,
53
+ locale: context.i18n.currentLocale,
54
+ versionName: CURRENT_VERSION_NAME,
55
+ pluginId: options.id,
56
+ })
57
+ : contentPathLocalized;
58
+
59
+ const versionPathSegment = posixPath(
60
+ path.relative(context.siteDir, path.resolve(context.siteDir, editDirPath)),
61
+ );
62
+ const versionPathSegmentLocalized = posixPath(
63
+ path.relative(
64
+ context.siteDir,
65
+ path.resolve(context.siteDir, editDirPathLocalized),
66
+ ),
67
+ );
68
+
69
+ const editUrl = normalizeUrl([options.editUrl, versionPathSegment]);
70
+
71
+ const editUrlLocalized = normalizeUrl([
72
+ options.editUrl,
73
+ versionPathSegmentLocalized,
74
+ ]);
75
+
76
+ return {editUrl, editUrlLocalized};
77
+ }
78
+
79
+ /**
80
+ * The default version banner depends on the version's relative position to the
81
+ * latest version. More recent ones are "unreleased", and older ones are
82
+ * "unmaintained".
83
+ */
84
+ export function getDefaultVersionBanner({
85
+ versionName,
86
+ versionNames,
87
+ lastVersionName,
88
+ }: VersionContext): VersionBanner | null {
89
+ // Current version: good, no banner
90
+ if (versionName === lastVersionName) {
91
+ return null;
92
+ }
93
+ // Upcoming versions: unreleased banner
94
+ if (
95
+ versionNames.indexOf(versionName) < versionNames.indexOf(lastVersionName)
96
+ ) {
97
+ return 'unreleased';
98
+ }
99
+ // Older versions: display unmaintained banner
100
+ return 'unmaintained';
101
+ }
102
+
103
+ export function getVersionBanner(
104
+ context: VersionContext,
105
+ ): VersionMetadata['banner'] {
106
+ const {versionName, options} = context;
107
+ const versionBannerOption = options.versions[versionName]?.banner;
108
+ if (versionBannerOption) {
109
+ return versionBannerOption === 'none' ? null : versionBannerOption;
110
+ }
111
+ return getDefaultVersionBanner(context);
112
+ }
113
+
114
+ export function getVersionBadge({
115
+ versionName,
116
+ versionNames,
117
+ options,
118
+ }: VersionContext): VersionMetadata['badge'] {
119
+ // If site is not versioned or only one version is included
120
+ // we don't show the version badge by default
121
+ // See https://github.com/facebook/docusaurus/issues/3362
122
+ const defaultVersionBadge = versionNames.length !== 1;
123
+ return options.versions[versionName]?.badge ?? defaultVersionBadge;
124
+ }
125
+
126
+ function getVersionClassName({
127
+ versionName,
128
+ options,
129
+ }: VersionContext): VersionMetadata['className'] {
130
+ const defaultVersionClassName = `docs-version-${versionName}`;
131
+ return options.versions[versionName]?.className ?? defaultVersionClassName;
132
+ }
133
+
134
+ function getVersionLabel({
135
+ versionName,
136
+ options,
137
+ }: VersionContext): VersionMetadata['label'] {
138
+ const defaultVersionLabel =
139
+ versionName === CURRENT_VERSION_NAME ? 'Next' : versionName;
140
+ return options.versions[versionName]?.label ?? defaultVersionLabel;
141
+ }
142
+
143
+ function getVersionPathPart({
144
+ versionName,
145
+ options,
146
+ lastVersionName,
147
+ }: VersionContext): string {
148
+ function getDefaultVersionPathPart() {
149
+ if (versionName === lastVersionName) {
150
+ return '';
151
+ }
152
+ return versionName === CURRENT_VERSION_NAME ? 'next' : versionName;
153
+ }
154
+ return options.versions[versionName]?.path ?? getDefaultVersionPathPart();
155
+ }
156
+
157
+ async function createVersionMetadata(
158
+ context: VersionContext,
159
+ ): Promise<VersionMetadata> {
160
+ const {versionName, lastVersionName, options, context: loadContext} = context;
161
+ const {sidebarFilePath, contentPath, contentPathLocalized} =
162
+ await getVersionMetadataPaths(context);
163
+ const versionPathPart = getVersionPathPart(context);
164
+
165
+ const routePath = normalizeUrl([
166
+ loadContext.baseUrl,
167
+ options.routeBasePath,
168
+ versionPathPart,
169
+ ]);
170
+
171
+ const versionEditUrls = getVersionEditUrls({
172
+ contentPath,
173
+ contentPathLocalized,
174
+ context: loadContext,
175
+ options,
176
+ });
177
+
178
+ return {
179
+ versionName,
180
+ label: getVersionLabel(context),
181
+ banner: getVersionBanner(context),
182
+ badge: getVersionBadge(context),
183
+ className: getVersionClassName(context),
184
+ path: routePath,
185
+ tagsPath: normalizeUrl([routePath, options.tagsBasePath]),
186
+ ...versionEditUrls,
187
+ isLast: versionName === lastVersionName,
188
+ routePriority: versionPathPart === '' ? -1 : undefined,
189
+ sidebarFilePath,
190
+ contentPath,
191
+ contentPathLocalized,
192
+ };
193
+ }
194
+
195
+ /**
196
+ * Filter versions according to provided options (i.e. `onlyIncludeVersions`).
197
+ *
198
+ * Note: we preserve the order in which versions are provided; the order of the
199
+ * `onlyIncludeVersions` array does not matter
200
+ */
201
+ export function filterVersions(
202
+ versionNamesUnfiltered: string[],
203
+ options: PluginOptions,
204
+ ): string[] {
205
+ if (options.onlyIncludeVersions) {
206
+ return versionNamesUnfiltered.filter((name) =>
207
+ options.onlyIncludeVersions!.includes(name),
208
+ );
209
+ }
210
+ return versionNamesUnfiltered;
211
+ }
212
+
213
+ function getLastVersionName({
214
+ versionNames,
215
+ options,
216
+ }: Pick<VersionContext, 'versionNames' | 'options'>) {
217
+ return (
218
+ options.lastVersion ??
219
+ versionNames.find((name) => name !== CURRENT_VERSION_NAME) ??
220
+ CURRENT_VERSION_NAME
221
+ );
222
+ }
223
+
224
+ export async function readVersionsMetadata({
225
+ context,
226
+ options,
227
+ }: {
228
+ context: LoadContext;
229
+ options: PluginOptions;
230
+ }): Promise<VersionMetadata[]> {
231
+ const allVersionNames = await readVersionNames(context.siteDir, options);
232
+ validateVersionsOptions(allVersionNames, options);
233
+ const versionNames = filterVersions(allVersionNames, options);
234
+ const lastVersionName = getLastVersionName({versionNames, options});
235
+ const versionsMetadata = await Promise.all(
236
+ versionNames.map((versionName) =>
237
+ createVersionMetadata({
238
+ versionName,
239
+ versionNames,
240
+ lastVersionName,
241
+ context,
242
+ options,
243
+ }),
244
+ ),
245
+ );
246
+ return versionsMetadata;
247
+ }