@docusaurus/plugin-content-docs 2.0.0-beta.8bda3b2db → 2.0.0-beta.9

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 (117) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/cli.d.ts +2 -2
  3. package/lib/cli.js +20 -24
  4. package/lib/client/docsClientUtils.d.ts +1 -4
  5. package/lib/client/docsClientUtils.js +12 -16
  6. package/lib/docFrontMatter.js +7 -3
  7. package/lib/docs.d.ts +4 -2
  8. package/lib/docs.js +77 -23
  9. package/lib/index.js +88 -94
  10. package/lib/lastUpdate.js +8 -8
  11. package/lib/markdown/index.d.ts +3 -6
  12. package/lib/markdown/index.js +3 -3
  13. package/lib/markdown/linkify.js +2 -2
  14. package/lib/options.d.ts +1 -1
  15. package/lib/options.js +39 -11
  16. package/lib/props.d.ts +7 -2
  17. package/lib/props.js +27 -4
  18. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +3 -1
  19. package/lib/sidebars/generator.js +174 -0
  20. package/lib/sidebars/index.d.ts +14 -0
  21. package/lib/sidebars/index.js +64 -0
  22. package/lib/sidebars/normalization.d.ts +9 -0
  23. package/lib/sidebars/normalization.js +58 -0
  24. package/lib/sidebars/processor.d.ts +16 -0
  25. package/lib/sidebars/processor.js +70 -0
  26. package/lib/sidebars/types.d.ts +87 -0
  27. package/lib/sidebars/types.js +13 -0
  28. package/lib/sidebars/utils.d.ts +22 -0
  29. package/lib/sidebars/utils.js +101 -0
  30. package/lib/sidebars/validation.d.ts +8 -0
  31. package/lib/sidebars/validation.js +102 -0
  32. package/lib/slug.js +4 -4
  33. package/lib/tags.d.ts +8 -0
  34. package/lib/tags.js +22 -0
  35. package/lib/theme/hooks/useDocs.js +24 -21
  36. package/lib/translations.d.ts +1 -1
  37. package/lib/translations.js +13 -13
  38. package/lib/types.d.ts +35 -58
  39. package/lib/versions.d.ts +1 -1
  40. package/lib/versions.js +75 -22
  41. package/package.json +15 -14
  42. package/src/__tests__/__fixtures__/simple-site/docs/_partials/somePartial.md +3 -0
  43. package/src/__tests__/__fixtures__/simple-site/docs/_partials/subfolder/somePartial.md +3 -0
  44. package/src/__tests__/__fixtures__/simple-site/docs/_somePartial.md +3 -0
  45. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +5 -0
  46. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +1 -0
  47. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +2 -0
  48. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +2 -0
  49. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +2 -0
  50. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +2 -0
  51. package/src/__tests__/__fixtures__/simple-site/sidebars.json +15 -1
  52. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +6 -0
  53. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/somePartial.md +3 -0
  54. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/subfolder/somePartial.md +3 -0
  55. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_somePartial.md +3 -0
  56. package/src/__tests__/__snapshots__/cli.test.ts.snap +33 -0
  57. package/src/__tests__/__snapshots__/docs.test.ts.snap +140 -0
  58. package/src/__tests__/__snapshots__/index.test.ts.snap +478 -60
  59. package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -3
  60. package/src/__tests__/cli.test.ts +14 -10
  61. package/src/__tests__/docFrontMatter.test.ts +163 -48
  62. package/src/__tests__/docs.test.ts +167 -21
  63. package/src/__tests__/index.test.ts +74 -30
  64. package/src/__tests__/lastUpdate.test.ts +3 -2
  65. package/src/__tests__/options.test.ts +46 -3
  66. package/src/__tests__/props.test.ts +62 -0
  67. package/src/__tests__/translations.test.ts +0 -1
  68. package/src/__tests__/versions.test.ts +88 -60
  69. package/src/cli.ts +27 -30
  70. package/src/client/__tests__/docsClientUtils.test.ts +4 -5
  71. package/src/client/docsClientUtils.ts +6 -27
  72. package/src/docFrontMatter.ts +8 -3
  73. package/src/docs.ts +92 -9
  74. package/src/index.ts +114 -121
  75. package/src/lastUpdate.ts +10 -6
  76. package/src/markdown/index.ts +8 -12
  77. package/src/numberPrefix.ts +4 -2
  78. package/src/options.ts +47 -17
  79. package/src/plugin-content-docs.d.ts +121 -34
  80. package/src/props.ts +42 -6
  81. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -0
  82. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -0
  83. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -0
  84. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category.js +0 -0
  85. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -0
  86. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed.json +0 -0
  87. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -0
  88. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -0
  89. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -0
  90. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -0
  91. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link.json +0 -0
  92. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-unknown-type.json +0 -0
  93. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-wrong-field.json +0 -0
  94. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars.json +0 -0
  95. package/src/{__tests__/__snapshots__/sidebars.test.ts.snap → sidebars/__tests__/__snapshots__/index.test.ts.snap} +21 -6
  96. package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +29 -7
  97. package/src/sidebars/__tests__/index.test.ts +202 -0
  98. package/src/sidebars/__tests__/processor.test.ts +148 -0
  99. package/src/sidebars/__tests__/utils.test.ts +395 -0
  100. package/src/sidebars/generator.ts +253 -0
  101. package/src/sidebars/index.ts +84 -0
  102. package/src/sidebars/normalization.ts +88 -0
  103. package/src/sidebars/processor.ts +124 -0
  104. package/src/sidebars/types.ts +156 -0
  105. package/src/sidebars/utils.ts +146 -0
  106. package/src/sidebars/validation.ts +124 -0
  107. package/src/tags.ts +21 -0
  108. package/src/theme/hooks/useDocs.ts +5 -1
  109. package/src/translations.ts +26 -36
  110. package/src/types.ts +48 -99
  111. package/src/versions.ts +109 -17
  112. package/lib/sidebarItemsGenerator.js +0 -211
  113. package/lib/sidebars.d.ts +0 -43
  114. package/lib/sidebars.js +0 -319
  115. package/src/__tests__/sidebars.test.ts +0 -639
  116. package/src/sidebarItemsGenerator.ts +0 -307
  117. package/src/sidebars.ts +0 -506
package/lib/options.js CHANGED
@@ -3,19 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateOptions = exports.OptionsSchema = exports.DEFAULT_OPTIONS = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const utils_validation_1 = require("@docusaurus/utils-validation");
6
- const chalk_1 = tslib_1.__importDefault(require("chalk"));
7
- const remark_admonitions_1 = tslib_1.__importDefault(require("remark-admonitions"));
8
- const sidebarItemsGenerator_1 = require("./sidebarItemsGenerator");
6
+ const utils_1 = require("@docusaurus/utils");
7
+ const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
8
+ const remark_admonitions_1 = (0, tslib_1.__importDefault)(require("remark-admonitions"));
9
+ const generator_1 = require("./sidebars/generator");
9
10
  const numberPrefix_1 = require("./numberPrefix");
10
11
  exports.DEFAULT_OPTIONS = {
11
12
  path: 'docs',
12
13
  routeBasePath: 'docs',
14
+ tagsBasePath: 'tags',
13
15
  homePageId: undefined,
14
16
  include: ['**/*.{md,mdx}'],
15
- sidebarItemsGenerator: sidebarItemsGenerator_1.DefaultSidebarItemsGenerator,
17
+ exclude: utils_1.GlobExcludeDefault,
18
+ sidebarItemsGenerator: generator_1.DefaultSidebarItemsGenerator,
16
19
  numberPrefixParser: numberPrefix_1.DefaultNumberPrefixParser,
17
20
  docLayoutComponent: '@theme/DocPage',
18
21
  docItemComponent: '@theme/DocItem',
22
+ docTagDocListComponent: '@theme/DocTagDocListPage',
23
+ docTagsListComponent: '@theme/DocTagsListPage',
19
24
  remarkPlugins: [],
20
25
  rehypePlugins: [],
21
26
  beforeDefaultRemarkPlugins: [],
@@ -23,17 +28,21 @@ exports.DEFAULT_OPTIONS = {
23
28
  showLastUpdateTime: false,
24
29
  showLastUpdateAuthor: false,
25
30
  admonitions: {},
26
- excludeNextVersionDocs: false,
27
31
  includeCurrentVersion: true,
28
32
  disableVersioning: false,
29
33
  lastVersion: undefined,
30
34
  versions: {},
31
35
  editCurrentVersion: false,
32
36
  editLocalizedFiles: false,
37
+ sidebarCollapsible: true,
38
+ sidebarCollapsed: true,
33
39
  };
34
40
  const VersionOptionsSchema = utils_validation_1.Joi.object({
35
41
  path: utils_validation_1.Joi.string().allow('').optional(),
36
42
  label: utils_validation_1.Joi.string().optional(),
43
+ banner: utils_validation_1.Joi.string().equal('none', 'unreleased', 'unmaintained').optional(),
44
+ badge: utils_validation_1.Joi.boolean().optional(),
45
+ className: utils_validation_1.Joi.string().optional(),
37
46
  });
38
47
  const VersionsOptionsSchema = utils_validation_1.Joi.object()
39
48
  .pattern(utils_validation_1.Joi.string().required(), VersionOptionsSchema)
@@ -47,10 +56,14 @@ exports.OptionsSchema = utils_validation_1.Joi.object({
47
56
  // '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
48
57
  // .allow('') ""
49
58
  .default(exports.DEFAULT_OPTIONS.routeBasePath),
59
+ tagsBasePath: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.tagsBasePath),
50
60
  homePageId: utils_validation_1.Joi.string().optional(),
51
61
  include: utils_validation_1.Joi.array().items(utils_validation_1.Joi.string()).default(exports.DEFAULT_OPTIONS.include),
62
+ exclude: utils_validation_1.Joi.array().items(utils_validation_1.Joi.string()).default(exports.DEFAULT_OPTIONS.exclude),
52
63
  sidebarPath: utils_validation_1.Joi.alternatives().try(utils_validation_1.Joi.boolean().invalid(true), utils_validation_1.Joi.string()),
53
64
  sidebarItemsGenerator: utils_validation_1.Joi.function().default(() => exports.DEFAULT_OPTIONS.sidebarItemsGenerator),
65
+ sidebarCollapsible: utils_validation_1.Joi.boolean().default(exports.DEFAULT_OPTIONS.sidebarCollapsible),
66
+ sidebarCollapsed: utils_validation_1.Joi.boolean().default(exports.DEFAULT_OPTIONS.sidebarCollapsed),
54
67
  numberPrefixParser: utils_validation_1.Joi.alternatives()
55
68
  .try(utils_validation_1.Joi.function(),
56
69
  // Convert boolean values to functions
@@ -60,6 +73,8 @@ exports.OptionsSchema = utils_validation_1.Joi.object({
60
73
  .default(() => exports.DEFAULT_OPTIONS.numberPrefixParser),
61
74
  docLayoutComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.docLayoutComponent),
62
75
  docItemComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.docItemComponent),
76
+ docTagsListComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.docTagsListComponent),
77
+ docTagDocListComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.docTagDocListComponent),
63
78
  remarkPlugins: utils_validation_1.RemarkPluginsSchema.default(exports.DEFAULT_OPTIONS.remarkPlugins),
64
79
  rehypePlugins: utils_validation_1.RehypePluginsSchema.default(exports.DEFAULT_OPTIONS.rehypePlugins),
65
80
  beforeDefaultRemarkPlugins: utils_validation_1.RemarkPluginsSchema.default(exports.DEFAULT_OPTIONS.beforeDefaultRemarkPlugins),
@@ -69,23 +84,36 @@ exports.OptionsSchema = utils_validation_1.Joi.object({
69
84
  .default(exports.DEFAULT_OPTIONS.admonitions),
70
85
  showLastUpdateTime: utils_validation_1.Joi.bool().default(exports.DEFAULT_OPTIONS.showLastUpdateTime),
71
86
  showLastUpdateAuthor: utils_validation_1.Joi.bool().default(exports.DEFAULT_OPTIONS.showLastUpdateAuthor),
72
- excludeNextVersionDocs: utils_validation_1.Joi.bool().default(exports.DEFAULT_OPTIONS.excludeNextVersionDocs),
73
87
  includeCurrentVersion: utils_validation_1.Joi.bool().default(exports.DEFAULT_OPTIONS.includeCurrentVersion),
74
88
  onlyIncludeVersions: utils_validation_1.Joi.array().items(utils_validation_1.Joi.string().required()).optional(),
75
89
  disableVersioning: utils_validation_1.Joi.bool().default(exports.DEFAULT_OPTIONS.disableVersioning),
76
90
  lastVersion: utils_validation_1.Joi.string().optional(),
77
91
  versions: VersionsOptionsSchema,
78
92
  });
79
- function validateOptions({ validate, options, }) {
93
+ function validateOptions({ validate, options: userOptions, }) {
94
+ let options = userOptions;
95
+ if (options.sidebarCollapsible === false) {
96
+ // When sidebarCollapsible=false and sidebarCollapsed=undefined, we don't want to have the inconsistency warning
97
+ // We let options.sidebarCollapsible become the default value for options.sidebarCollapsed
98
+ if (typeof options.sidebarCollapsed === 'undefined') {
99
+ options = {
100
+ ...options,
101
+ sidebarCollapsed: false,
102
+ };
103
+ }
104
+ if (options.sidebarCollapsed) {
105
+ console.warn(chalk_1.default.yellow('The docs plugin config is inconsistent. It does not make sense to use sidebarCollapsible=false and sidebarCollapsed=true at the same time. sidebarCollapsed=false will be ignored.'));
106
+ options = {
107
+ ...options,
108
+ sidebarCollapsed: false,
109
+ };
110
+ }
111
+ }
80
112
  // TODO remove homePageId before end of 2020
81
113
  // "slug: /" is better because the home doc can be different across versions
82
114
  if (options.homePageId) {
83
115
  console.log(chalk_1.default.red(`The docs plugin option homePageId=${options.homePageId} is deprecated. To make a doc the "home", prefer frontmatter: "slug: /"`));
84
116
  }
85
- if (typeof options.excludeNextVersionDocs !== 'undefined') {
86
- console.log(chalk_1.default.red(`The docs plugin option "excludeNextVersionDocs=${options.excludeNextVersionDocs}" is deprecated. Use the "includeCurrentVersion=${!options.excludeNextVersionDocs}" option instead!"`));
87
- options.includeCurrentVersion = !options.excludeNextVersionDocs;
88
- }
89
117
  const normalizedOptions = validate(exports.OptionsSchema, options);
90
118
  if (normalizedOptions.admonitions) {
91
119
  normalizedOptions.remarkPlugins = normalizedOptions.remarkPlugins.concat([
package/lib/props.d.ts CHANGED
@@ -4,7 +4,12 @@
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 { LoadedVersion } from './types';
8
- import { PropSidebars, PropVersionMetadata } from '@docusaurus/plugin-content-docs-types';
7
+ import { LoadedVersion, VersionTag, DocMetadata } from './types';
8
+ import type { PropSidebars, PropVersionMetadata, PropTagDocList } from '@docusaurus/plugin-content-docs-types';
9
9
  export declare function toSidebarsProp(loadedVersion: LoadedVersion): PropSidebars;
10
10
  export declare function toVersionMetadataProp(pluginId: string, loadedVersion: LoadedVersion): PropVersionMetadata;
11
+ export declare function toTagDocListProp({ allTagsPath, tag, docs, }: {
12
+ allTagsPath: string;
13
+ tag: VersionTag;
14
+ docs: Pick<DocMetadata, 'id' | 'title' | 'description' | 'permalink'>[];
15
+ }): PropTagDocList;
package/lib/props.js CHANGED
@@ -6,10 +6,10 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.toVersionMetadataProp = exports.toSidebarsProp = void 0;
9
+ exports.toTagDocListProp = exports.toVersionMetadataProp = exports.toSidebarsProp = void 0;
10
10
  const lodash_1 = require("lodash");
11
11
  function toSidebarsProp(loadedVersion) {
12
- const docsById = lodash_1.keyBy(loadedVersion.docs, (doc) => doc.id);
12
+ const docsById = (0, lodash_1.keyBy)(loadedVersion.docs, (doc) => doc.id);
13
13
  const convertDocLink = (item) => {
14
14
  const docId = item.id;
15
15
  const docMetadata = docsById[docId];
@@ -23,6 +23,7 @@ Available document ids are:
23
23
  type: 'link',
24
24
  label: sidebarLabel || item.label || title,
25
25
  href: permalink,
26
+ className: item.className,
26
27
  customProps: item.customProps,
27
28
  };
28
29
  };
@@ -41,7 +42,7 @@ Available document ids are:
41
42
  // Transform the sidebar so that all sidebar item will be in the
42
43
  // form of 'link' or 'category' only.
43
44
  // This is what will be passed as props to the UI component.
44
- return lodash_1.mapValues(loadedVersion.sidebars, (items) => items.map(normalizeItem));
45
+ return (0, lodash_1.mapValues)(loadedVersion.sidebars, (items) => items.map(normalizeItem));
45
46
  }
46
47
  exports.toSidebarsProp = toSidebarsProp;
47
48
  function toVersionMetadataProp(pluginId, loadedVersion) {
@@ -49,9 +50,31 @@ function toVersionMetadataProp(pluginId, loadedVersion) {
49
50
  pluginId,
50
51
  version: loadedVersion.versionName,
51
52
  label: loadedVersion.versionLabel,
53
+ banner: loadedVersion.versionBanner,
54
+ badge: loadedVersion.versionBadge,
55
+ className: loadedVersion.versionClassName,
52
56
  isLast: loadedVersion.isLast,
53
57
  docsSidebars: toSidebarsProp(loadedVersion),
54
- permalinkToSidebar: loadedVersion.permalinkToSidebar,
55
58
  };
56
59
  }
57
60
  exports.toVersionMetadataProp = toVersionMetadataProp;
61
+ function toTagDocListProp({ allTagsPath, tag, docs, }) {
62
+ function toDocListProp() {
63
+ const list = (0, lodash_1.compact)(tag.docIds.map((id) => docs.find((doc) => doc.id === id)));
64
+ // Sort docs by title
65
+ list.sort((doc1, doc2) => doc1.title.localeCompare(doc2.title));
66
+ return list.map((doc) => ({
67
+ id: doc.id,
68
+ title: doc.title,
69
+ description: doc.description,
70
+ permalink: doc.permalink,
71
+ }));
72
+ }
73
+ return {
74
+ name: tag.name,
75
+ permalink: tag.permalink,
76
+ docs: toDocListProp(),
77
+ allTagsPath,
78
+ };
79
+ }
80
+ exports.toTagDocListProp = toTagDocListProp;
@@ -4,12 +4,14 @@
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 { SidebarItemsGenerator } from './types';
7
+ import type { SidebarItemsGenerator } from './types';
8
8
  export declare const CategoryMetadataFilenameBase = "_category_";
9
9
  export declare const CategoryMetadataFilenamePattern = "_category_.{json,yml,yaml}";
10
10
  export declare type CategoryMetadatasFile = {
11
11
  label?: string;
12
12
  position?: number;
13
13
  collapsed?: boolean;
14
+ collapsible?: boolean;
15
+ className?: string;
14
16
  };
15
17
  export declare const DefaultSidebarItemsGenerator: SidebarItemsGenerator;
@@ -0,0 +1,174 @@
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.DefaultSidebarItemsGenerator = exports.CategoryMetadataFilenamePattern = exports.CategoryMetadataFilenameBase = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const lodash_1 = require("lodash");
12
+ const utils_1 = require("@docusaurus/utils");
13
+ const utils_validation_1 = require("@docusaurus/utils-validation");
14
+ const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
15
+ const path_1 = (0, tslib_1.__importDefault)(require("path"));
16
+ const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
17
+ const js_yaml_1 = (0, tslib_1.__importDefault)(require("js-yaml"));
18
+ const BreadcrumbSeparator = '/';
19
+ // To avoid possible name clashes with a folder of the same name as the ID
20
+ const docIdPrefix = '$doc$/';
21
+ exports.CategoryMetadataFilenameBase = '_category_';
22
+ exports.CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
23
+ const CategoryMetadatasFileSchema = utils_validation_1.Joi.object({
24
+ label: utils_validation_1.Joi.string(),
25
+ position: utils_validation_1.Joi.number(),
26
+ collapsed: utils_validation_1.Joi.boolean(),
27
+ collapsible: utils_validation_1.Joi.boolean(),
28
+ className: utils_validation_1.Joi.string(),
29
+ });
30
+ // TODO I now believe we should read all the category metadata files ahead of time: we may need this metadata to customize docs metadata
31
+ // Example use-case being able to disable number prefix parsing at the folder level, or customize the default route path segment for an intermediate directory...
32
+ // TODO later if there is `CategoryFolder/index.md`, we may want to read the metadata as yaml on it
33
+ // see https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
34
+ async function readCategoryMetadatasFile(categoryDirPath) {
35
+ var _a, _b;
36
+ async function tryReadFile(fileNameWithExtension, parse) {
37
+ // Simpler to use only posix paths for mocking file metadatas in tests
38
+ const filePath = (0, utils_1.posixPath)(path_1.default.join(categoryDirPath, fileNameWithExtension));
39
+ if (await fs_extra_1.default.pathExists(filePath)) {
40
+ const contentString = await fs_extra_1.default.readFile(filePath, { encoding: 'utf8' });
41
+ const unsafeContent = parse(contentString);
42
+ try {
43
+ return utils_validation_1.Joi.attempt(unsafeContent, CategoryMetadatasFileSchema);
44
+ }
45
+ catch (e) {
46
+ console.error(chalk_1.default.red(`The docs sidebar category metadata file looks invalid!\nPath: ${filePath}`));
47
+ throw e;
48
+ }
49
+ }
50
+ return null;
51
+ }
52
+ return ((_b = (_a = (await tryReadFile(`${exports.CategoryMetadataFilenameBase}.json`, JSON.parse))) !== null && _a !== void 0 ? _a : (await tryReadFile(`${exports.CategoryMetadataFilenameBase}.yml`, js_yaml_1.default.load))) !== null && _b !== void 0 ? _b :
53
+ // eslint-disable-next-line no-return-await
54
+ (await tryReadFile(`${exports.CategoryMetadataFilenameBase}.yaml`, js_yaml_1.default.load)));
55
+ }
56
+ // Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
57
+ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, docs: allDocs, options, item: { dirName: autogenDir }, version, }) => {
58
+ /**
59
+ * Step 1. Extract the docs that are in the autogen dir.
60
+ */
61
+ function getAutogenDocs() {
62
+ function isInAutogeneratedDir(doc) {
63
+ return (
64
+ // Doc at the root of the autogenerated sidebar dir
65
+ doc.sourceDirName === autogenDir ||
66
+ // autogen dir is . and doc is in subfolder
67
+ autogenDir === '.' ||
68
+ // autogen dir is not . and doc is in subfolder
69
+ // "api/myDoc" startsWith "api/" (note "api2/myDoc" is not included)
70
+ doc.sourceDirName.startsWith((0, utils_1.addTrailingSlash)(autogenDir)));
71
+ }
72
+ const docs = allDocs.filter(isInAutogeneratedDir);
73
+ if (docs.length === 0) {
74
+ console.warn(chalk_1.default.yellow(`No docs found in dir ${autogenDir}: can't auto-generate a sidebar.`));
75
+ }
76
+ return docs;
77
+ }
78
+ /**
79
+ * Step 2. Turn the linear file list into a tree structure.
80
+ */
81
+ function treeify(docs) {
82
+ // Get the category breadcrumb of a doc (relative to the dir of the autogenerated sidebar item)
83
+ // autogenDir=a/b and docDir=a/b/c/d => returns [c, d]
84
+ // autogenDir=a/b and docDir=a/b => returns []
85
+ // TODO: try to use path.relative()
86
+ function getRelativeBreadcrumb(doc) {
87
+ return autogenDir === doc.sourceDirName
88
+ ? []
89
+ : doc.sourceDirName
90
+ .replace((0, utils_1.addTrailingSlash)(autogenDir), '')
91
+ .split(BreadcrumbSeparator);
92
+ }
93
+ const treeRoot = {};
94
+ docs.forEach((doc) => {
95
+ const breadcrumb = getRelativeBreadcrumb(doc);
96
+ let currentDir = treeRoot; // We walk down the file's path to generate the fs structure
97
+ // eslint-disable-next-line no-restricted-syntax
98
+ for (const dir of breadcrumb) {
99
+ if (typeof currentDir[dir] === 'undefined') {
100
+ currentDir[dir] = {}; // Create new folder.
101
+ }
102
+ currentDir = currentDir[dir]; // Go into the subdirectory.
103
+ }
104
+ currentDir[`${docIdPrefix}${doc.id}`] = null; // We've walked through the file path. Register the file in this directory.
105
+ });
106
+ return treeRoot;
107
+ }
108
+ /**
109
+ * Step 3. Recursively transform the tree-like file structure to sidebar items.
110
+ * (From a record to an array of items, akin to normalizing shorthand)
111
+ */
112
+ function generateSidebar(fsModel) {
113
+ const docsById = (0, lodash_1.keyBy)(allDocs, (doc) => doc.id);
114
+ function createDocItem(id) {
115
+ const { sidebarPosition: position, frontMatter: { sidebar_label: label, sidebar_class_name: className }, } = docsById[id];
116
+ return {
117
+ type: 'doc',
118
+ id,
119
+ position,
120
+ // We don't want these fields to magically appear in the generated sidebar
121
+ ...(label !== undefined && { label }),
122
+ ...(className !== undefined && { className }),
123
+ };
124
+ }
125
+ async function createCategoryItem(dir, fullPath, folderName) {
126
+ var _a, _b, _c, _d;
127
+ const categoryPath = path_1.default.join(version.contentPath, autogenDir, fullPath);
128
+ const categoryMetadatas = await readCategoryMetadatasFile(categoryPath);
129
+ const className = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.className;
130
+ const { filename, numberPrefix } = numberPrefixParser(folderName);
131
+ return {
132
+ type: 'category',
133
+ label: (_a = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.label) !== null && _a !== void 0 ? _a : filename,
134
+ collapsible: (_b = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.collapsible) !== null && _b !== void 0 ? _b : options.sidebarCollapsible,
135
+ collapsed: (_c = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.collapsed) !== null && _c !== void 0 ? _c : options.sidebarCollapsed,
136
+ position: (_d = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.position) !== null && _d !== void 0 ? _d : numberPrefix,
137
+ ...(className !== undefined && { className }),
138
+ items: await Promise.all(Object.entries(dir).map(([key, content]) => dirToItem(content, key, `${fullPath}/${key}`))),
139
+ };
140
+ }
141
+ async function dirToItem(dir, // The directory item to be transformed.
142
+ itemKey, // For docs, it's the doc ID; for categories, it's used to generate the next `relativePath`.
143
+ fullPath) {
144
+ return dir
145
+ ? createCategoryItem(dir, fullPath, itemKey)
146
+ : createDocItem(itemKey.substring(docIdPrefix.length));
147
+ }
148
+ return Promise.all(Object.entries(fsModel).map(([key, content]) => dirToItem(content, key, key)));
149
+ }
150
+ /**
151
+ * Step 4. Recursively sort the categories/docs + remove the "position" attribute from final output.
152
+ * Note: the "position" is only used to sort "inside" a sidebar slice. It is not
153
+ * used to sort across multiple consecutive sidebar slices (ie a whole Category
154
+ * composed of multiple autogenerated items)
155
+ */
156
+ function sortItems(sidebarItems) {
157
+ const processedSidebarItems = sidebarItems.map((item) => {
158
+ if (item.type === 'category') {
159
+ return { ...item, items: sortItems(item.items) };
160
+ }
161
+ return item;
162
+ });
163
+ const sortedSidebarItems = (0, lodash_1.sortBy)(processedSidebarItems, (item) => item.position);
164
+ return sortedSidebarItems.map(({ position, ...item }) => item);
165
+ }
166
+ // TODO: the whole code is designed for pipeline operator
167
+ // return getAutogenDocs() |> treeify |> await generateSidebar(^) |> sortItems;
168
+ const docs = getAutogenDocs();
169
+ const fsModel = treeify(docs);
170
+ const sidebarWithPosition = await generateSidebar(fsModel);
171
+ const sortedSidebar = sortItems(sidebarWithPosition);
172
+ return sortedSidebar;
173
+ };
174
+ exports.DefaultSidebarItemsGenerator = DefaultSidebarItemsGenerator;
@@ -0,0 +1,14 @@
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 { SidebarsConfig, Sidebars, NormalizedSidebars } from './types';
8
+ import type { PluginOptions } from '../types';
9
+ import { SidebarProcessorProps } from './processor';
10
+ export declare const DefaultSidebars: SidebarsConfig;
11
+ export declare const DisabledSidebars: SidebarsConfig;
12
+ export declare function resolveSidebarPathOption(siteDir: string, sidebarPathOption: PluginOptions['sidebarPath']): PluginOptions['sidebarPath'];
13
+ export declare function loadUnprocessedSidebars(sidebarFilePath: string | false | undefined, options: SidebarProcessorProps['options']): NormalizedSidebars;
14
+ export declare function loadSidebars(sidebarFilePath: string | false | undefined, options: SidebarProcessorProps): Promise<Sidebars>;
@@ -0,0 +1,64 @@
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.loadSidebars = exports.loadUnprocessedSidebars = exports.resolveSidebarPathOption = exports.DisabledSidebars = exports.DefaultSidebars = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
12
+ const import_fresh_1 = (0, tslib_1.__importDefault)(require("import-fresh"));
13
+ const validation_1 = require("./validation");
14
+ const normalization_1 = require("./normalization");
15
+ const processor_1 = require("./processor");
16
+ const path_1 = (0, tslib_1.__importDefault)(require("path"));
17
+ exports.DefaultSidebars = {
18
+ defaultSidebar: [
19
+ {
20
+ type: 'autogenerated',
21
+ dirName: '.',
22
+ },
23
+ ],
24
+ };
25
+ exports.DisabledSidebars = {};
26
+ // If a path is provided, make it absolute
27
+ // use this before loadSidebars()
28
+ function resolveSidebarPathOption(siteDir, sidebarPathOption) {
29
+ return sidebarPathOption
30
+ ? path_1.default.resolve(siteDir, sidebarPathOption)
31
+ : sidebarPathOption;
32
+ }
33
+ exports.resolveSidebarPathOption = resolveSidebarPathOption;
34
+ function loadSidebarFile(sidebarFilePath) {
35
+ // false => no sidebars
36
+ if (sidebarFilePath === false) {
37
+ return exports.DisabledSidebars;
38
+ }
39
+ // undefined => defaults to autogenerated sidebars
40
+ if (typeof sidebarFilePath === 'undefined') {
41
+ return exports.DefaultSidebars;
42
+ }
43
+ // Non-existent sidebars file: no sidebars
44
+ // Note: this edge case can happen on versioned docs, not current version
45
+ // We avoid creating empty versioned sidebars file with the CLI
46
+ if (!fs_extra_1.default.existsSync(sidebarFilePath)) {
47
+ return exports.DisabledSidebars;
48
+ }
49
+ // We don't want sidebars to be cached because of hot reloading.
50
+ return (0, import_fresh_1.default)(sidebarFilePath);
51
+ }
52
+ function loadUnprocessedSidebars(sidebarFilePath, options) {
53
+ const sidebarsConfig = loadSidebarFile(sidebarFilePath);
54
+ (0, validation_1.validateSidebars)(sidebarsConfig);
55
+ const normalizedSidebars = (0, normalization_1.normalizeSidebars)(sidebarsConfig, options);
56
+ return normalizedSidebars;
57
+ }
58
+ exports.loadUnprocessedSidebars = loadUnprocessedSidebars;
59
+ // Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
60
+ async function loadSidebars(sidebarFilePath, options) {
61
+ const unprocessedSidebars = loadUnprocessedSidebars(sidebarFilePath, options.options);
62
+ return (0, processor_1.processSidebars)(unprocessedSidebars, options);
63
+ }
64
+ exports.loadSidebars = loadSidebars;
@@ -0,0 +1,9 @@
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 { SidebarOptions } from '../types';
8
+ import { NormalizedSidebars, SidebarsConfig } from './types';
9
+ export declare function normalizeSidebars(sidebars: SidebarsConfig, options: SidebarOptions): NormalizedSidebars;
@@ -0,0 +1,58 @@
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.normalizeSidebars = void 0;
10
+ const types_1 = require("./types");
11
+ const lodash_1 = require("lodash");
12
+ function normalizeCategoriesShorthand(sidebar, options) {
13
+ return Object.entries(sidebar).map(([label, items]) => ({
14
+ type: 'category',
15
+ collapsed: options.sidebarCollapsed,
16
+ collapsible: options.sidebarCollapsible,
17
+ label,
18
+ items,
19
+ }));
20
+ }
21
+ /**
22
+ * Normalizes recursively item and all its children. Ensures that at the end
23
+ * each item will be an object with the corresponding type.
24
+ */
25
+ function normalizeItem(item, options) {
26
+ var _a, _b;
27
+ if (typeof item === 'string') {
28
+ return [
29
+ {
30
+ type: 'doc',
31
+ id: item,
32
+ },
33
+ ];
34
+ }
35
+ if ((0, types_1.isCategoriesShorthand)(item)) {
36
+ return normalizeCategoriesShorthand(item, options).flatMap((subitem) => normalizeItem(subitem, options));
37
+ }
38
+ return item.type === 'category'
39
+ ? [
40
+ {
41
+ ...item,
42
+ items: item.items.flatMap((subItem) => normalizeItem(subItem, options)),
43
+ collapsible: (_a = item.collapsible) !== null && _a !== void 0 ? _a : options.sidebarCollapsible,
44
+ collapsed: (_b = item.collapsed) !== null && _b !== void 0 ? _b : options.sidebarCollapsed,
45
+ },
46
+ ]
47
+ : [item];
48
+ }
49
+ function normalizeSidebar(sidebar, options) {
50
+ const normalizedSidebar = Array.isArray(sidebar)
51
+ ? sidebar
52
+ : normalizeCategoriesShorthand(sidebar, options);
53
+ return normalizedSidebar.flatMap((subitem) => normalizeItem(subitem, options));
54
+ }
55
+ function normalizeSidebars(sidebars, options) {
56
+ return (0, lodash_1.mapValues)(sidebars, (subitem) => normalizeSidebar(subitem, options));
57
+ }
58
+ exports.normalizeSidebars = normalizeSidebars;
@@ -0,0 +1,16 @@
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 { NumberPrefixParser, DocMetadataBase, VersionMetadata, SidebarOptions } from '../types';
8
+ import type { Sidebars, NormalizedSidebars, SidebarItemsGeneratorOption } from './types';
9
+ export declare type SidebarProcessorProps = {
10
+ sidebarItemsGenerator: SidebarItemsGeneratorOption;
11
+ numberPrefixParser: NumberPrefixParser;
12
+ docs: DocMetadataBase[];
13
+ version: VersionMetadata;
14
+ options: SidebarOptions;
15
+ };
16
+ export declare function processSidebars(unprocessedSidebars: NormalizedSidebars, props: SidebarProcessorProps): Promise<Sidebars>;
@@ -0,0 +1,70 @@
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.processSidebars = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const utils_1 = require("./utils");
12
+ const generator_1 = require("./generator");
13
+ const lodash_1 = require("lodash");
14
+ const combine_promises_1 = (0, tslib_1.__importDefault)(require("combine-promises"));
15
+ function toSidebarItemsGeneratorDoc(doc) {
16
+ return (0, lodash_1.pick)(doc, [
17
+ 'id',
18
+ 'frontMatter',
19
+ 'source',
20
+ 'sourceDirName',
21
+ 'sidebarPosition',
22
+ ]);
23
+ }
24
+ function toSidebarItemsGeneratorVersion(version) {
25
+ return (0, lodash_1.pick)(version, ['versionName', 'contentPath']);
26
+ }
27
+ // Handle the generation of autogenerated sidebar items and other post-processing checks
28
+ async function processSidebar(unprocessedSidebar, { sidebarItemsGenerator, numberPrefixParser, docs, version, options, }) {
29
+ // Just a minor lazy transformation optimization
30
+ const getSidebarItemsGeneratorDocsAndVersion = (0, lodash_1.memoize)(() => ({
31
+ docs: docs.map(toSidebarItemsGeneratorDoc),
32
+ version: toSidebarItemsGeneratorVersion(version),
33
+ }));
34
+ async function handleAutoGeneratedItems(item) {
35
+ if (item.type === 'category') {
36
+ return [
37
+ {
38
+ ...item,
39
+ items: (await Promise.all(item.items.map(handleAutoGeneratedItems))).flat(),
40
+ },
41
+ ];
42
+ }
43
+ if (item.type === 'autogenerated') {
44
+ return sidebarItemsGenerator({
45
+ item,
46
+ numberPrefixParser,
47
+ defaultSidebarItemsGenerator: generator_1.DefaultSidebarItemsGenerator,
48
+ ...getSidebarItemsGeneratorDocsAndVersion(),
49
+ options,
50
+ });
51
+ }
52
+ return [item];
53
+ }
54
+ const processedSidebar = (await Promise.all(unprocessedSidebar.map(handleAutoGeneratedItems))).flat();
55
+ const fixSidebarItemInconsistencies = (item) => {
56
+ // A non-collapsible category can't be collapsed!
57
+ if (item.type === 'category' && !item.collapsible && item.collapsed) {
58
+ return {
59
+ ...item,
60
+ collapsed: false,
61
+ };
62
+ }
63
+ return item;
64
+ };
65
+ return (0, utils_1.transformSidebarItems)(processedSidebar, fixSidebarItemInconsistencies);
66
+ }
67
+ async function processSidebars(unprocessedSidebars, props) {
68
+ return (0, combine_promises_1.default)((0, lodash_1.mapValues)(unprocessedSidebars, (unprocessedSidebar) => processSidebar(unprocessedSidebar, props)));
69
+ }
70
+ exports.processSidebars = processSidebars;