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

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 (69) hide show
  1. package/lib/cli.d.ts +1 -1
  2. package/lib/cli.js +18 -14
  3. package/lib/client/docsClientUtils.js +2 -2
  4. package/lib/client/index.d.ts +13 -1
  5. package/lib/client/index.js +66 -1
  6. package/lib/docFrontMatter.js +1 -0
  7. package/lib/docs.d.ts +2 -1
  8. package/lib/docs.js +23 -22
  9. package/lib/globalData.js +3 -2
  10. package/lib/index.js +11 -6
  11. package/lib/lastUpdate.js +14 -27
  12. package/lib/numberPrefix.js +7 -6
  13. package/lib/options.js +5 -2
  14. package/lib/props.js +15 -10
  15. package/lib/routes.js +6 -4
  16. package/lib/server-export.d.ts +8 -0
  17. package/lib/server-export.js +23 -0
  18. package/lib/sidebars/generator.d.ts +1 -9
  19. package/lib/sidebars/generator.js +26 -50
  20. package/lib/sidebars/index.d.ts +2 -5
  21. package/lib/sidebars/index.js +35 -20
  22. package/lib/sidebars/normalization.d.ts +2 -3
  23. package/lib/sidebars/normalization.js +17 -39
  24. package/lib/sidebars/postProcessor.d.ts +8 -0
  25. package/lib/sidebars/postProcessor.js +72 -0
  26. package/lib/sidebars/processor.d.ts +2 -13
  27. package/lib/sidebars/processor.js +25 -33
  28. package/lib/sidebars/types.d.ts +44 -10
  29. package/lib/sidebars/utils.js +53 -61
  30. package/lib/sidebars/validation.d.ts +2 -3
  31. package/lib/sidebars/validation.js +25 -30
  32. package/lib/slug.js +6 -8
  33. package/lib/tags.js +3 -2
  34. package/lib/translations.js +18 -17
  35. package/lib/types.d.ts +3 -6
  36. package/lib/versions.d.ts +25 -1
  37. package/lib/versions.js +25 -29
  38. package/package.json +19 -18
  39. package/src/cli.ts +25 -16
  40. package/src/client/docsClientUtils.ts +2 -2
  41. package/src/client/index.ts +97 -1
  42. package/src/docFrontMatter.ts +1 -0
  43. package/src/docs.ts +25 -20
  44. package/src/globalData.ts +2 -2
  45. package/src/index.ts +17 -7
  46. package/src/lastUpdate.ts +12 -33
  47. package/src/numberPrefix.ts +7 -6
  48. package/src/options.ts +5 -2
  49. package/src/plugin-content-docs.d.ts +16 -60
  50. package/src/props.ts +17 -12
  51. package/src/routes.ts +6 -4
  52. package/src/server-export.ts +24 -0
  53. package/src/sidebars/README.md +9 -0
  54. package/src/sidebars/generator.ts +50 -94
  55. package/src/sidebars/index.ts +50 -32
  56. package/src/sidebars/normalization.ts +22 -50
  57. package/src/sidebars/postProcessor.ts +94 -0
  58. package/src/sidebars/processor.ts +37 -66
  59. package/src/sidebars/types.ts +68 -10
  60. package/src/sidebars/utils.ts +63 -68
  61. package/src/sidebars/validation.ts +53 -53
  62. package/src/slug.ts +9 -10
  63. package/src/tags.ts +2 -2
  64. package/src/translations.ts +19 -16
  65. package/src/types.ts +3 -10
  66. package/src/versions.ts +30 -34
  67. package/lib/client/globalDataHooks.d.ts +0 -19
  68. package/lib/client/globalDataHooks.js +0 -76
  69. package/src/client/globalDataHooks.ts +0 -107
package/src/versions.ts CHANGED
@@ -28,16 +28,14 @@ import {
28
28
  posixPath,
29
29
  DEFAULT_PLUGIN_ID,
30
30
  } from '@docusaurus/utils';
31
- import {difference} from 'lodash';
31
+ import _ from 'lodash';
32
32
  import {resolveSidebarPathOption} from './sidebars';
33
33
 
34
34
  // retro-compatibility: no prefix for the default plugin id
35
35
  function addPluginIdPrefix(fileOrDir: string, pluginId: string): string {
36
- if (pluginId === DEFAULT_PLUGIN_ID) {
37
- return fileOrDir;
38
- } else {
39
- return `${pluginId}_${fileOrDir}`;
40
- }
36
+ return pluginId === DEFAULT_PLUGIN_ID
37
+ ? fileOrDir
38
+ : `${pluginId}_${fileOrDir}`;
41
39
  }
42
40
 
43
41
  export function getVersionedDocsDirPath(
@@ -87,7 +85,7 @@ function ensureValidVersionArray(
87
85
  versionArray.forEach(ensureValidVersionString);
88
86
  }
89
87
 
90
- async function readVersionsFile(
88
+ export async function readVersionsFile(
91
89
  siteDir: string,
92
90
  pluginId: string,
93
91
  ): Promise<string[] | null> {
@@ -96,12 +94,11 @@ async function readVersionsFile(
96
94
  const content = JSON.parse(await fs.readFile(versionsFilePath, 'utf8'));
97
95
  ensureValidVersionArray(content);
98
96
  return content;
99
- } else {
100
- return null;
101
97
  }
98
+ return null;
102
99
  }
103
100
 
104
- async function readVersionNames(
101
+ export async function readVersionNames(
105
102
  siteDir: string,
106
103
  options: Pick<
107
104
  PluginOptions,
@@ -260,7 +257,7 @@ function getVersionEditUrls({
260
257
  };
261
258
  }
262
259
 
263
- function getDefaultVersionBanner({
260
+ export function getDefaultVersionBanner({
264
261
  versionName,
265
262
  versionNames,
266
263
  lastVersionName,
@@ -274,18 +271,16 @@ function getDefaultVersionBanner({
274
271
  return null;
275
272
  }
276
273
  // Upcoming versions: unreleased banner
277
- else if (
274
+ if (
278
275
  versionNames.indexOf(versionName) < versionNames.indexOf(lastVersionName)
279
276
  ) {
280
277
  return 'unreleased';
281
278
  }
282
279
  // Older versions: display unmaintained banner
283
- else {
284
- return 'unmaintained';
285
- }
280
+ return 'unmaintained';
286
281
  }
287
282
 
288
- function getVersionBanner({
283
+ export function getVersionBanner({
289
284
  versionName,
290
285
  versionNames,
291
286
  lastVersionName,
@@ -307,7 +302,7 @@ function getVersionBanner({
307
302
  });
308
303
  }
309
304
 
310
- function getVersionBadge({
305
+ export function getVersionBadge({
311
306
  versionName,
312
307
  versionNames,
313
308
  options,
@@ -423,7 +418,7 @@ function createVersionMetadata({
423
418
  };
424
419
  }
425
420
 
426
- function checkVersionMetadataPaths({
421
+ async function checkVersionMetadataPaths({
427
422
  versionMetadata,
428
423
  context,
429
424
  }: {
@@ -434,7 +429,7 @@ function checkVersionMetadataPaths({
434
429
  const {siteDir} = context;
435
430
  const isCurrentVersion = versionName === CURRENT_VERSION_NAME;
436
431
 
437
- if (!fs.existsSync(contentPath)) {
432
+ if (!(await fs.pathExists(contentPath))) {
438
433
  throw new Error(
439
434
  `The docs folder does not exist for version "${versionName}". A docs folder is expected to be found at ${path.relative(
440
435
  siteDir,
@@ -443,14 +438,15 @@ function checkVersionMetadataPaths({
443
438
  );
444
439
  }
445
440
 
446
- // If the current version defines a path to a sidebar file that does not exist, we throw!
447
- // Note: for versioned sidebars, the file may not exist (as we prefer to not create it rather than to create an empty file)
441
+ // If the current version defines a path to a sidebar file that does not
442
+ // exist, we throw! Note: for versioned sidebars, the file may not exist (as
443
+ // we prefer to not create it rather than to create an empty file)
448
444
  // See https://github.com/facebook/docusaurus/issues/3366
449
445
  // See https://github.com/facebook/docusaurus/pull/4775
450
446
  if (
451
447
  isCurrentVersion &&
452
448
  typeof sidebarFilePath === 'string' &&
453
- !fs.existsSync(sidebarFilePath)
449
+ !(await fs.pathExists(sidebarFilePath))
454
450
  ) {
455
451
  throw new Error(`The path to the sidebar file does not exist at "${path.relative(
456
452
  siteDir,
@@ -469,11 +465,10 @@ Please set the docs "sidebarPath" field in your config file to:
469
465
  function getDefaultLastVersionName(versionNames: string[]) {
470
466
  if (versionNames.length === 1) {
471
467
  return versionNames[0];
472
- } else {
473
- return versionNames.filter(
474
- (versionName) => versionName !== CURRENT_VERSION_NAME,
475
- )[0];
476
468
  }
469
+ return versionNames.filter(
470
+ (versionName) => versionName !== CURRENT_VERSION_NAME,
471
+ )[0];
477
472
  }
478
473
 
479
474
  function checkVersionsOptions(
@@ -491,7 +486,7 @@ function checkVersionsOptions(
491
486
  `Docs option lastVersion: ${options.lastVersion} is invalid. ${availableVersionNamesMsg}`,
492
487
  );
493
488
  }
494
- const unknownVersionConfigNames = difference(
489
+ const unknownVersionConfigNames = _.difference(
495
490
  Object.keys(options.versions),
496
491
  availableVersionNames,
497
492
  );
@@ -509,7 +504,7 @@ function checkVersionsOptions(
509
504
  `Invalid docs option "onlyIncludeVersions": an empty array is not allowed, at least one version is needed.`,
510
505
  );
511
506
  }
512
- const unknownOnlyIncludeVersionNames = difference(
507
+ const unknownOnlyIncludeVersionNames = _.difference(
513
508
  options.onlyIncludeVersions,
514
509
  availableVersionNames,
515
510
  );
@@ -536,17 +531,16 @@ function checkVersionsOptions(
536
531
  * Note: we preserve the order in which versions are provided;
537
532
  * the order of the onlyIncludeVersions array does not matter
538
533
  */
539
- function filterVersions(
534
+ export function filterVersions(
540
535
  versionNamesUnfiltered: string[],
541
536
  options: Pick<PluginOptions, 'onlyIncludeVersions'>,
542
- ) {
537
+ ): string[] {
543
538
  if (options.onlyIncludeVersions) {
544
539
  return versionNamesUnfiltered.filter((name) =>
545
540
  (options.onlyIncludeVersions || []).includes(name),
546
541
  );
547
- } else {
548
- return versionNamesUnfiltered;
549
542
  }
543
+ return versionNamesUnfiltered;
550
544
  }
551
545
 
552
546
  export async function readVersionsMetadata({
@@ -591,8 +585,10 @@ export async function readVersionsMetadata({
591
585
  options,
592
586
  }),
593
587
  );
594
- versionsMetadata.forEach((versionMetadata) =>
595
- checkVersionMetadataPaths({versionMetadata, context}),
588
+ await Promise.all(
589
+ versionsMetadata.map((versionMetadata) =>
590
+ checkVersionMetadataPaths({versionMetadata, context}),
591
+ ),
596
592
  );
597
593
  return versionsMetadata;
598
594
  }
@@ -1,19 +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 { GlobalPluginData, GlobalVersion, ActivePlugin, ActiveDocContext, DocVersionSuggestions, GetActivePluginOptions } from '@docusaurus/plugin-content-docs/client';
8
- export declare const useAllDocsData: () => Record<string, GlobalPluginData>;
9
- export declare const useDocsData: (pluginId: string | undefined) => GlobalPluginData;
10
- export declare const useActivePlugin: (options?: GetActivePluginOptions) => ActivePlugin | undefined;
11
- export declare const useActivePluginAndVersion: (options?: GetActivePluginOptions) => {
12
- activePlugin: ActivePlugin;
13
- activeVersion: GlobalVersion | undefined;
14
- } | undefined;
15
- export declare const useVersions: (pluginId: string | undefined) => GlobalVersion[];
16
- export declare const useLatestVersion: (pluginId: string | undefined) => GlobalVersion;
17
- export declare const useActiveVersion: (pluginId: string | undefined) => GlobalVersion | undefined;
18
- export declare const useActiveDocContext: (pluginId: string | undefined) => ActiveDocContext;
19
- export declare const useDocVersionSuggestions: (pluginId: string | undefined) => DocVersionSuggestions;
@@ -1,76 +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.useDocVersionSuggestions = exports.useActiveDocContext = exports.useActiveVersion = exports.useLatestVersion = exports.useVersions = exports.useActivePluginAndVersion = exports.useActivePlugin = exports.useDocsData = exports.useAllDocsData = void 0;
10
- const tslib_1 = require("tslib");
11
- const router_1 = require("@docusaurus/router");
12
- const useGlobalData_1 = (0, tslib_1.__importStar)(require("@docusaurus/useGlobalData"));
13
- const docsClientUtils_1 = require("./docsClientUtils");
14
- // Important to use a constant object to avoid React useEffect executions etc...,
15
- // see https://github.com/facebook/docusaurus/issues/5089
16
- const StableEmptyObject = {};
17
- // Not using useAllPluginInstancesData() because in blog-only mode, docs hooks are still used by the theme
18
- // We need a fail-safe fallback when the docs plugin is not in use
19
- const useAllDocsData = () => { var _a;
20
- // useAllPluginInstancesData('docusaurus-plugin-content-docs');
21
- return (_a = (0, useGlobalData_1.default)()['docusaurus-plugin-content-docs']) !== null && _a !== void 0 ? _a : StableEmptyObject; };
22
- exports.useAllDocsData = useAllDocsData;
23
- const useDocsData = (pluginId) => (0, useGlobalData_1.usePluginData)('docusaurus-plugin-content-docs', pluginId);
24
- exports.useDocsData = useDocsData;
25
- // TODO this feature should be provided by docusaurus core
26
- const useActivePlugin = (options = {}) => {
27
- const data = (0, exports.useAllDocsData)();
28
- const { pathname } = (0, router_1.useLocation)();
29
- return (0, docsClientUtils_1.getActivePlugin)(data, pathname, options);
30
- };
31
- exports.useActivePlugin = useActivePlugin;
32
- const useActivePluginAndVersion = (options = {}) => {
33
- const activePlugin = (0, exports.useActivePlugin)(options);
34
- const { pathname } = (0, router_1.useLocation)();
35
- if (activePlugin) {
36
- const activeVersion = (0, docsClientUtils_1.getActiveVersion)(activePlugin.pluginData, pathname);
37
- return {
38
- activePlugin,
39
- activeVersion,
40
- };
41
- }
42
- return undefined;
43
- };
44
- exports.useActivePluginAndVersion = useActivePluginAndVersion;
45
- // versions are returned ordered (most recent first)
46
- const useVersions = (pluginId) => {
47
- const data = (0, exports.useDocsData)(pluginId);
48
- return data.versions;
49
- };
50
- exports.useVersions = useVersions;
51
- const useLatestVersion = (pluginId) => {
52
- const data = (0, exports.useDocsData)(pluginId);
53
- return (0, docsClientUtils_1.getLatestVersion)(data);
54
- };
55
- exports.useLatestVersion = useLatestVersion;
56
- // Note: return undefined on doc-unrelated pages,
57
- // because there's no version currently considered as active
58
- const useActiveVersion = (pluginId) => {
59
- const data = (0, exports.useDocsData)(pluginId);
60
- const { pathname } = (0, router_1.useLocation)();
61
- return (0, docsClientUtils_1.getActiveVersion)(data, pathname);
62
- };
63
- exports.useActiveVersion = useActiveVersion;
64
- const useActiveDocContext = (pluginId) => {
65
- const data = (0, exports.useDocsData)(pluginId);
66
- const { pathname } = (0, router_1.useLocation)();
67
- return (0, docsClientUtils_1.getActiveDocContext)(data, pathname);
68
- };
69
- exports.useActiveDocContext = useActiveDocContext;
70
- // Useful to say "hey, you are not on the latest docs version, please switch"
71
- const useDocVersionSuggestions = (pluginId) => {
72
- const data = (0, exports.useDocsData)(pluginId);
73
- const { pathname } = (0, router_1.useLocation)();
74
- return (0, docsClientUtils_1.getDocVersionSuggestions)(data, pathname);
75
- };
76
- exports.useDocVersionSuggestions = useDocVersionSuggestions;
@@ -1,107 +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
-
8
- import {useLocation} from '@docusaurus/router';
9
- import useGlobalData, {
10
- // useAllPluginInstancesData,
11
- usePluginData,
12
- } from '@docusaurus/useGlobalData';
13
-
14
- import {
15
- getActivePlugin,
16
- getLatestVersion,
17
- getActiveVersion,
18
- getActiveDocContext,
19
- getDocVersionSuggestions,
20
- } from './docsClientUtils';
21
- import type {
22
- GlobalPluginData,
23
- GlobalVersion,
24
- ActivePlugin,
25
- ActiveDocContext,
26
- DocVersionSuggestions,
27
- GetActivePluginOptions,
28
- } from '@docusaurus/plugin-content-docs/client';
29
-
30
- // Important to use a constant object to avoid React useEffect executions etc...,
31
- // see https://github.com/facebook/docusaurus/issues/5089
32
- const StableEmptyObject = {};
33
-
34
- // Not using useAllPluginInstancesData() because in blog-only mode, docs hooks are still used by the theme
35
- // We need a fail-safe fallback when the docs plugin is not in use
36
- export const useAllDocsData = (): Record<string, GlobalPluginData> =>
37
- // useAllPluginInstancesData('docusaurus-plugin-content-docs');
38
- useGlobalData()['docusaurus-plugin-content-docs'] ?? StableEmptyObject;
39
-
40
- export const useDocsData = (pluginId: string | undefined): GlobalPluginData =>
41
- usePluginData('docusaurus-plugin-content-docs', pluginId) as GlobalPluginData;
42
-
43
- // TODO this feature should be provided by docusaurus core
44
- export const useActivePlugin = (
45
- options: GetActivePluginOptions = {},
46
- ): ActivePlugin | undefined => {
47
- const data = useAllDocsData();
48
- const {pathname} = useLocation();
49
- return getActivePlugin(data, pathname, options);
50
- };
51
-
52
- export const useActivePluginAndVersion = (
53
- options: GetActivePluginOptions = {},
54
- ):
55
- | undefined
56
- | {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined} => {
57
- const activePlugin = useActivePlugin(options);
58
- const {pathname} = useLocation();
59
- if (activePlugin) {
60
- const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
61
- return {
62
- activePlugin,
63
- activeVersion,
64
- };
65
- }
66
- return undefined;
67
- };
68
-
69
- // versions are returned ordered (most recent first)
70
- export const useVersions = (pluginId: string | undefined): GlobalVersion[] => {
71
- const data = useDocsData(pluginId);
72
- return data.versions;
73
- };
74
-
75
- export const useLatestVersion = (
76
- pluginId: string | undefined,
77
- ): GlobalVersion => {
78
- const data = useDocsData(pluginId);
79
- return getLatestVersion(data);
80
- };
81
-
82
- // Note: return undefined on doc-unrelated pages,
83
- // because there's no version currently considered as active
84
- export const useActiveVersion = (
85
- pluginId: string | undefined,
86
- ): GlobalVersion | undefined => {
87
- const data = useDocsData(pluginId);
88
- const {pathname} = useLocation();
89
- return getActiveVersion(data, pathname);
90
- };
91
-
92
- export const useActiveDocContext = (
93
- pluginId: string | undefined,
94
- ): ActiveDocContext => {
95
- const data = useDocsData(pluginId);
96
- const {pathname} = useLocation();
97
- return getActiveDocContext(data, pathname);
98
- };
99
-
100
- // Useful to say "hey, you are not on the latest docs version, please switch"
101
- export const useDocVersionSuggestions = (
102
- pluginId: string | undefined,
103
- ): DocVersionSuggestions => {
104
- const data = useDocsData(pluginId);
105
- const {pathname} = useLocation();
106
- return getDocVersionSuggestions(data, pathname);
107
- };