@docusaurus/plugin-content-docs 2.0.0-beta.14 → 2.0.0-beta.15

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 (66) hide show
  1. package/lib/categoryGeneratedIndex.d.ts +2 -2
  2. package/lib/categoryGeneratedIndex.js +2 -0
  3. package/lib/cli.d.ts +1 -1
  4. package/lib/client/docsClientUtils.d.ts +3 -22
  5. package/lib/client/docsClientUtils.js +5 -1
  6. package/lib/{theme/hooks/useDocs.d.ts → client/globalDataHooks.d.ts} +1 -2
  7. package/lib/{theme/hooks/useDocs.js → client/globalDataHooks.js} +2 -1
  8. package/lib/client/index.d.ts +7 -0
  9. package/lib/client/index.js +10 -0
  10. package/lib/docFrontMatter.js +1 -0
  11. package/lib/docs.d.ts +19 -6
  12. package/lib/docs.js +37 -17
  13. package/lib/globalData.d.ts +5 -1
  14. package/lib/globalData.js +34 -2
  15. package/lib/index.d.ts +3 -2
  16. package/lib/index.js +2 -8
  17. package/lib/lastUpdate.js +2 -2
  18. package/lib/markdown/index.d.ts +1 -1
  19. package/lib/markdown/linkify.d.ts +1 -1
  20. package/lib/numberPrefix.d.ts +1 -1
  21. package/lib/options.d.ts +1 -1
  22. package/lib/routes.d.ts +4 -3
  23. package/lib/routes.js +6 -3
  24. package/lib/sidebars/generator.js +12 -14
  25. package/lib/sidebars/index.d.ts +3 -2
  26. package/lib/sidebars/processor.d.ts +3 -2
  27. package/lib/sidebars/processor.js +2 -0
  28. package/lib/sidebars/types.d.ts +8 -3
  29. package/lib/sidebars/utils.d.ts +12 -4
  30. package/lib/sidebars/utils.js +48 -3
  31. package/lib/sidebars/validation.d.ts +1 -1
  32. package/lib/sidebars/validation.js +4 -0
  33. package/lib/slug.d.ts +5 -4
  34. package/lib/slug.js +8 -7
  35. package/lib/translations.js +1 -1
  36. package/lib/types.d.ts +7 -78
  37. package/lib/versions.d.ts +3 -2
  38. package/lib/versions.js +27 -32
  39. package/package.json +14 -12
  40. package/src/categoryGeneratedIndex.ts +5 -3
  41. package/src/cli.ts +4 -1
  42. package/src/client/docsClientUtils.ts +22 -35
  43. package/src/{theme/hooks/useDocs.ts → client/globalDataHooks.ts} +6 -2
  44. package/src/client/index.ts +8 -0
  45. package/src/docFrontMatter.ts +2 -1
  46. package/src/docs.ts +62 -29
  47. package/src/globalData.ts +49 -3
  48. package/src/index.ts +9 -15
  49. package/src/lastUpdate.ts +2 -2
  50. package/src/markdown/index.ts +1 -1
  51. package/src/markdown/linkify.ts +1 -1
  52. package/src/numberPrefix.ts +1 -1
  53. package/src/options.ts +1 -1
  54. package/src/plugin-content-docs.d.ts +128 -18
  55. package/src/routes.ts +19 -5
  56. package/src/sidebars/generator.ts +25 -20
  57. package/src/sidebars/index.ts +3 -2
  58. package/src/sidebars/normalization.ts +2 -1
  59. package/src/sidebars/processor.ts +8 -7
  60. package/src/sidebars/types.ts +9 -5
  61. package/src/sidebars/utils.ts +76 -8
  62. package/src/sidebars/validation.ts +6 -1
  63. package/src/slug.ts +15 -11
  64. package/src/translations.ts +2 -2
  65. package/src/types.ts +12 -98
  66. package/src/versions.ts +51 -47
package/src/versions.ts CHANGED
@@ -7,19 +7,19 @@
7
7
 
8
8
  import path from 'path';
9
9
  import fs from 'fs-extra';
10
- import type {
11
- PluginOptions,
12
- VersionBanner,
13
- VersionMetadata,
14
- VersionOptions,
15
- VersionsOptions,
16
- } from './types';
10
+ import type {VersionMetadata} from './types';
17
11
  import {
18
12
  VERSIONS_JSON_FILE,
19
13
  VERSIONED_DOCS_DIR,
20
14
  VERSIONED_SIDEBARS_DIR,
21
15
  CURRENT_VERSION_NAME,
22
16
  } from './constants';
17
+ import type {
18
+ PluginOptions,
19
+ VersionBanner,
20
+ VersionOptions,
21
+ VersionsOptions,
22
+ } from '@docusaurus/plugin-content-docs';
23
23
 
24
24
  import type {LoadContext} from '@docusaurus/types';
25
25
  import {
@@ -87,11 +87,13 @@ function ensureValidVersionArray(
87
87
  versionArray.forEach(ensureValidVersionString);
88
88
  }
89
89
 
90
- // TODO not easy to make async due to many deps
91
- function readVersionsFile(siteDir: string, pluginId: string): string[] | null {
90
+ async function readVersionsFile(
91
+ siteDir: string,
92
+ pluginId: string,
93
+ ): Promise<string[] | null> {
92
94
  const versionsFilePath = getVersionsFilePath(siteDir, pluginId);
93
- if (fs.existsSync(versionsFilePath)) {
94
- const content = JSON.parse(fs.readFileSync(versionsFilePath, 'utf8'));
95
+ if (await fs.pathExists(versionsFilePath)) {
96
+ const content = JSON.parse(await fs.readFile(versionsFilePath, 'utf8'));
95
97
  ensureValidVersionArray(content);
96
98
  return content;
97
99
  } else {
@@ -99,27 +101,26 @@ function readVersionsFile(siteDir: string, pluginId: string): string[] | null {
99
101
  }
100
102
  }
101
103
 
102
- // TODO not easy to make async due to many deps
103
- function readVersionNames(
104
+ async function readVersionNames(
104
105
  siteDir: string,
105
106
  options: Pick<
106
107
  PluginOptions,
107
108
  'id' | 'disableVersioning' | 'includeCurrentVersion'
108
109
  >,
109
- ): string[] {
110
- const versionFileContent = readVersionsFile(siteDir, options.id);
110
+ ): Promise<string[]> {
111
+ const versionFileContent = await readVersionsFile(siteDir, options.id);
111
112
 
112
113
  if (!versionFileContent && options.disableVersioning) {
113
114
  throw new Error(
114
- `Docs: using "disableVersioning=${options.disableVersioning}" option on a non-versioned site does not make sense.`,
115
+ `Docs: using "disableVersioning: ${options.disableVersioning}" option on a non-versioned site does not make sense.`,
115
116
  );
116
117
  }
117
118
 
118
119
  const versions = options.disableVersioning ? [] : versionFileContent ?? [];
119
120
 
120
- // We add the current version at the beginning, unless
121
- // - user don't want to
122
- // - it's been explicitly added to versions.json
121
+ // We add the current version at the beginning, unless:
122
+ // - user don't want to; or
123
+ // - it's already been explicitly added to versions.json
123
124
  if (
124
125
  options.includeCurrentVersion &&
125
126
  !versions.includes(CURRENT_VERSION_NAME)
@@ -129,7 +130,7 @@ function readVersionNames(
129
130
 
130
131
  if (versions.length === 0) {
131
132
  throw new Error(
132
- `It is not possible to use docs without any version. Please check the configuration of these options: "includeCurrentVersion=${options.includeCurrentVersion}", "disableVersioning=${options.disableVersioning}".`,
133
+ `It is not possible to use docs without any version. Please check the configuration of these options: "includeCurrentVersion: ${options.includeCurrentVersion}", "disableVersioning: ${options.disableVersioning}".`,
133
134
  );
134
135
  }
135
136
 
@@ -174,13 +175,6 @@ function getVersionMetadataPaths({
174
175
  > {
175
176
  const isCurrentVersion = versionName === CURRENT_VERSION_NAME;
176
177
 
177
- const contentPath = isCurrentVersion
178
- ? path.resolve(context.siteDir, options.path)
179
- : path.join(
180
- getVersionedDocsDirPath(context.siteDir, options.id),
181
- `version-${versionName}`,
182
- );
183
-
184
178
  const contentPathLocalized = getDocsDirPathLocalized({
185
179
  siteDir: context.siteDir,
186
180
  locale: context.i18n.currentLocale,
@@ -188,21 +182,27 @@ function getVersionMetadataPaths({
188
182
  versionName,
189
183
  });
190
184
 
191
- function getSidebarFilePath() {
192
- if (isCurrentVersion) {
193
- return resolveSidebarPathOption(context.siteDir, options.sidebarPath);
194
- } else {
195
- return path.join(
196
- getVersionedSidebarsDirPath(context.siteDir, options.id),
197
- `version-${versionName}-sidebars.json`,
198
- );
199
- }
185
+ if (isCurrentVersion) {
186
+ return {
187
+ contentPath: path.resolve(context.siteDir, options.path),
188
+ contentPathLocalized,
189
+ sidebarFilePath: resolveSidebarPathOption(
190
+ context.siteDir,
191
+ options.sidebarPath,
192
+ ),
193
+ };
200
194
  }
201
195
 
202
196
  return {
203
- contentPath,
197
+ contentPath: path.join(
198
+ getVersionedDocsDirPath(context.siteDir, options.id),
199
+ `version-${versionName}`,
200
+ ),
204
201
  contentPathLocalized,
205
- sidebarFilePath: getSidebarFilePath(),
202
+ sidebarFilePath: path.join(
203
+ getVersionedSidebarsDirPath(context.siteDir, options.id),
204
+ `version-${versionName}-sidebars.json`,
205
+ ),
206
206
  };
207
207
  }
208
208
 
@@ -459,7 +459,7 @@ function checkVersionMetadataPaths({
459
459
  Please set the docs "sidebarPath" field in your config file to:
460
460
  - a sidebars path that exists
461
461
  - false: to disable the sidebar
462
- - undefined: for Docusaurus generates it automatically`);
462
+ - undefined: for Docusaurus to generate it automatically`);
463
463
  }
464
464
  }
465
465
 
@@ -488,7 +488,7 @@ function checkVersionsOptions(
488
488
  !availableVersionNames.includes(options.lastVersion)
489
489
  ) {
490
490
  throw new Error(
491
- `Docs option lastVersion=${options.lastVersion} is invalid. ${availableVersionNamesMsg}`,
491
+ `Docs option lastVersion: ${options.lastVersion} is invalid. ${availableVersionNamesMsg}`,
492
492
  );
493
493
  }
494
494
  const unknownVersionConfigNames = difference(
@@ -531,9 +531,11 @@ function checkVersionsOptions(
531
531
  }
532
532
  }
533
533
 
534
- // Filter versions according to provided options
535
- // Note: we preserve the order in which versions are provided
536
- // the order of the onlyIncludeVersions array does not matter
534
+ /**
535
+ * Filter versions according to provided options.
536
+ * Note: we preserve the order in which versions are provided;
537
+ * the order of the onlyIncludeVersions array does not matter
538
+ */
537
539
  function filterVersions(
538
540
  versionNamesUnfiltered: string[],
539
541
  options: Pick<PluginOptions, 'onlyIncludeVersions'>,
@@ -547,8 +549,7 @@ function filterVersions(
547
549
  }
548
550
  }
549
551
 
550
- // TODO make this async (requires plugin init to be async)
551
- export function readVersionsMetadata({
552
+ export async function readVersionsMetadata({
552
553
  context,
553
554
  options,
554
555
  }: {
@@ -568,8 +569,11 @@ export function readVersionsMetadata({
568
569
  | 'editUrl'
569
570
  | 'editCurrentVersion'
570
571
  >;
571
- }): VersionMetadata[] {
572
- const versionNamesUnfiltered = readVersionNames(context.siteDir, options);
572
+ }): Promise<VersionMetadata[]> {
573
+ const versionNamesUnfiltered = await readVersionNames(
574
+ context.siteDir,
575
+ options,
576
+ );
573
577
 
574
578
  checkVersionsOptions(versionNamesUnfiltered, options);
575
579