@docusaurus/plugin-content-docs 2.0.0-beta.1ec2c95e3 → 2.0.0-beta.21

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 (229) hide show
  1. package/lib/categoryGeneratedIndex.d.ts +12 -0
  2. package/lib/categoryGeneratedIndex.js +37 -0
  3. package/lib/cli.d.ts +3 -2
  4. package/lib/cli.js +58 -72
  5. package/lib/client/docsClientUtils.d.ts +9 -28
  6. package/lib/client/docsClientUtils.js +34 -43
  7. package/lib/client/index.d.ts +22 -0
  8. package/lib/client/index.js +59 -0
  9. package/lib/constants.d.ts +4 -0
  10. package/lib/constants.js +4 -1
  11. package/lib/docs.d.ts +32 -3
  12. package/lib/docs.js +164 -63
  13. package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
  14. package/lib/{docFrontMatter.js → frontMatter.js} +12 -3
  15. package/lib/globalData.d.ts +3 -3
  16. package/lib/globalData.js +35 -6
  17. package/lib/index.d.ts +3 -3
  18. package/lib/index.js +123 -149
  19. package/lib/lastUpdate.d.ts +4 -6
  20. package/lib/lastUpdate.js +22 -26
  21. package/lib/markdown/index.d.ts +3 -6
  22. package/lib/markdown/index.js +3 -3
  23. package/lib/markdown/linkify.d.ts +1 -1
  24. package/lib/markdown/linkify.js +6 -3
  25. package/lib/numberPrefix.d.ts +1 -1
  26. package/lib/numberPrefix.js +16 -21
  27. package/lib/options.d.ts +3 -5
  28. package/lib/options.js +54 -13
  29. package/lib/props.d.ts +7 -2
  30. package/lib/props.js +70 -14
  31. package/lib/routes.d.ts +29 -0
  32. package/lib/routes.js +96 -0
  33. package/lib/server-export.d.ts +9 -0
  34. package/lib/server-export.js +22 -0
  35. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +1 -6
  36. package/lib/sidebars/generator.js +209 -0
  37. package/lib/sidebars/index.d.ts +13 -0
  38. package/lib/sidebars/index.js +92 -0
  39. package/lib/sidebars/normalization.d.ts +13 -0
  40. package/lib/sidebars/normalization.js +55 -0
  41. package/lib/sidebars/postProcessor.d.ts +11 -0
  42. package/lib/sidebars/postProcessor.js +81 -0
  43. package/lib/sidebars/processor.d.ts +10 -0
  44. package/lib/sidebars/processor.js +79 -0
  45. package/lib/sidebars/types.d.ts +178 -0
  46. package/lib/{docFrontMatter.d.ts → sidebars/types.js} +2 -2
  47. package/lib/sidebars/utils.d.ts +54 -0
  48. package/lib/sidebars/utils.js +255 -0
  49. package/lib/sidebars/validation.d.ts +11 -0
  50. package/lib/sidebars/validation.js +142 -0
  51. package/lib/slug.d.ts +5 -4
  52. package/lib/slug.js +28 -18
  53. package/{src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js → lib/tags.d.ts} +3 -14
  54. package/lib/tags.js +21 -0
  55. package/lib/translations.d.ts +3 -3
  56. package/lib/translations.js +83 -93
  57. package/lib/types.d.ts +13 -184
  58. package/lib/versions/files.d.ts +51 -0
  59. package/lib/versions/files.js +143 -0
  60. package/lib/versions/index.d.ts +36 -0
  61. package/lib/versions/index.js +155 -0
  62. package/lib/versions/validation.d.ts +17 -0
  63. package/lib/versions/validation.js +71 -0
  64. package/package.json +34 -29
  65. package/src/categoryGeneratedIndex.ts +60 -0
  66. package/src/cli.ts +85 -114
  67. package/src/client/docsClientUtils.ts +44 -71
  68. package/src/client/index.ts +106 -0
  69. package/src/constants.ts +4 -2
  70. package/{types.d.ts → src/deps.d.ts} +1 -1
  71. package/src/docs.ts +228 -65
  72. package/src/{docFrontMatter.ts → frontMatter.ts} +20 -10
  73. package/src/globalData.ts +57 -7
  74. package/src/index.ts +179 -216
  75. package/src/lastUpdate.ts +26 -37
  76. package/src/markdown/index.ts +10 -16
  77. package/src/markdown/linkify.ts +7 -4
  78. package/src/numberPrefix.ts +19 -26
  79. package/src/options.ts +59 -18
  80. package/src/plugin-content-docs.d.ts +642 -89
  81. package/src/props.ts +103 -21
  82. package/src/routes.ts +159 -0
  83. package/src/server-export.ts +22 -0
  84. package/src/sidebars/README.md +10 -0
  85. package/src/sidebars/generator.ts +292 -0
  86. package/src/sidebars/index.ts +118 -0
  87. package/src/sidebars/normalization.ts +85 -0
  88. package/src/sidebars/postProcessor.ts +112 -0
  89. package/src/sidebars/processor.ts +123 -0
  90. package/src/sidebars/types.ts +275 -0
  91. package/src/sidebars/utils.ts +390 -0
  92. package/src/sidebars/validation.ts +178 -0
  93. package/src/slug.ts +41 -22
  94. package/src/tags.ts +20 -0
  95. package/src/translations.ts +129 -123
  96. package/src/types.ts +18 -249
  97. package/src/versions/files.ts +220 -0
  98. package/src/versions/index.ts +247 -0
  99. package/src/versions/validation.ts +115 -0
  100. package/lib/.tsbuildinfo +0 -1
  101. package/lib/sidebarItemsGenerator.js +0 -211
  102. package/lib/sidebars.d.ts +0 -43
  103. package/lib/sidebars.js +0 -320
  104. package/lib/theme/hooks/useDocs.d.ts +0 -20
  105. package/lib/theme/hooks/useDocs.js +0 -75
  106. package/lib/versions.d.ts +0 -16
  107. package/lib/versions.js +0 -319
  108. package/src/__tests__/__fixtures__/bad-id-site/docs/invalid-id.md +0 -5
  109. package/src/__tests__/__fixtures__/bad-slug-on-doc-home-site/docs/docWithSlug.md +0 -5
  110. package/src/__tests__/__fixtures__/empty-site/docusaurus.config.js +0 -16
  111. package/src/__tests__/__fixtures__/empty-site/sidebars.json +0 -1
  112. package/src/__tests__/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -34
  113. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -11
  114. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -11
  115. package/src/__tests__/__fixtures__/sidebars/sidebars-category.js +0 -44
  116. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -20
  117. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed.json +0 -21
  118. package/src/__tests__/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -10
  119. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -11
  120. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -11
  121. package/src/__tests__/__fixtures__/sidebars/sidebars-link.json +0 -11
  122. package/src/__tests__/__fixtures__/sidebars/sidebars-unknown-type.json +0 -14
  123. package/src/__tests__/__fixtures__/sidebars/sidebars-wrong-field.json +0 -20
  124. package/src/__tests__/__fixtures__/sidebars/sidebars.json +0 -20
  125. package/src/__tests__/__fixtures__/simple-site/docs/foo/bar.md +0 -69
  126. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +0 -70
  127. package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
  128. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -53
  129. package/src/__tests__/__fixtures__/simple-site/docs/ipsum.md +0 -5
  130. package/src/__tests__/__fixtures__/simple-site/docs/lorem.md +0 -6
  131. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +0 -5
  132. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +0 -5
  133. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +0 -5
  134. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +0 -5
  135. package/src/__tests__/__fixtures__/simple-site/docs/slugs/absoluteSlug.md +0 -5
  136. package/src/__tests__/__fixtures__/simple-site/docs/slugs/relativeSlug.md +0 -5
  137. package/src/__tests__/__fixtures__/simple-site/docs/slugs/resolvedSlug.md +0 -5
  138. package/src/__tests__/__fixtures__/simple-site/docs/slugs/tryToEscapeSlug.md +0 -5
  139. package/src/__tests__/__fixtures__/simple-site/docusaurus.config.js +0 -14
  140. package/src/__tests__/__fixtures__/simple-site/sidebars.json +0 -23
  141. package/src/__tests__/__fixtures__/simple-site/wrong-sidebars.json +0 -7
  142. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/0-getting-started.md +0 -3
  143. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/1-installation.md +0 -3
  144. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/00_api-overview.md +0 -3
  145. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/0 --- Client API.md +0 -1
  146. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/1 --- Server API.md +0 -1
  147. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/0. Plugin API.md +0 -1
  148. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/1. Theme API.md +0 -1
  149. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/_category_.yml +0 -1
  150. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/03_api-end.md +0 -3
  151. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/_category_.json +0 -3
  152. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/0-guide2.5.md +0 -8
  153. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/02-guide2.md +0 -7
  154. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/_category_.json +0 -3
  155. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/a-guide4.md +0 -7
  156. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/b-guide5.md +0 -7
  157. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/guide3.md +0 -8
  158. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/z-guide1.md +0 -8
  159. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars.js +0 -23
  160. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js +0 -16
  161. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +0 -7
  162. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-2.md +0 -8
  163. package/src/__tests__/__fixtures__/site-with-doc-label/docusaurus.config.js +0 -14
  164. package/src/__tests__/__fixtures__/site-with-doc-label/sidebars.json +0 -14
  165. package/src/__tests__/__fixtures__/versioned-site/community/team.md +0 -1
  166. package/src/__tests__/__fixtures__/versioned-site/community_sidebars.json +0 -3
  167. package/src/__tests__/__fixtures__/versioned-site/community_versioned_docs/version-1.0.0/team.md +0 -1
  168. package/src/__tests__/__fixtures__/versioned-site/community_versioned_sidebars/version-1.0.0-sidebars.json +0 -3
  169. package/src/__tests__/__fixtures__/versioned-site/community_versions.json +0 -1
  170. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +0 -4
  171. package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +0 -1
  172. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/absoluteSlug.md +0 -5
  173. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/relativeSlug.md +0 -5
  174. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/resolvedSlug.md +0 -5
  175. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/tryToEscapeSlug.md +0 -5
  176. package/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js +0 -18
  177. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  178. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md +0 -5
  179. package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  180. package/src/__tests__/__fixtures__/versioned-site/sidebars.json +0 -10
  181. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/bar.md +0 -4
  182. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/baz.md +0 -1
  183. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +0 -1
  184. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/foo/bar.md +0 -1
  185. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +0 -1
  186. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootAbsoluteSlug.md +0 -5
  187. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootRelativeSlug.md +0 -5
  188. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootResolvedSlug.md +0 -5
  189. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootTryToEscapeSlug.md +0 -5
  190. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/absoluteSlug.md +0 -5
  191. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/relativeSlug.md +0 -5
  192. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/resolvedSlug.md +0 -5
  193. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/tryToEscapeSlug.md +0 -5
  194. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.0-sidebars.json +0 -11
  195. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +0 -10
  196. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-withSlugs-sidebars.json +0 -5
  197. package/src/__tests__/__fixtures__/versioned-site/versions.json +0 -5
  198. package/src/__tests__/__snapshots__/cli.test.ts.snap +0 -90
  199. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -1916
  200. package/src/__tests__/__snapshots__/sidebars.test.ts.snap +0 -218
  201. package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -487
  202. package/src/__tests__/cli.test.ts +0 -333
  203. package/src/__tests__/docFrontMatter.test.ts +0 -244
  204. package/src/__tests__/docs.test.ts +0 -878
  205. package/src/__tests__/index.test.ts +0 -1871
  206. package/src/__tests__/lastUpdate.test.ts +0 -69
  207. package/src/__tests__/numberPrefix.test.ts +0 -199
  208. package/src/__tests__/options.test.ts +0 -231
  209. package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
  210. package/src/__tests__/sidebars.test.ts +0 -639
  211. package/src/__tests__/slug.test.ts +0 -109
  212. package/src/__tests__/translations.test.ts +0 -159
  213. package/src/__tests__/versions.test.ts +0 -741
  214. package/src/client/__tests__/docsClientUtils.test.ts +0 -371
  215. package/src/markdown/__tests__/__fixtures__/docs/doc-localized.md +0 -1
  216. package/src/markdown/__tests__/__fixtures__/docs/doc1.md +0 -13
  217. package/src/markdown/__tests__/__fixtures__/docs/doc2.md +0 -12
  218. package/src/markdown/__tests__/__fixtures__/docs/doc4.md +0 -19
  219. package/src/markdown/__tests__/__fixtures__/docs/doc5.md +0 -6
  220. package/src/markdown/__tests__/__fixtures__/docs/subdir/doc3.md +0 -3
  221. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/doc2.md +0 -7
  222. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/subdir/doc1.md +0 -3
  223. package/src/markdown/__tests__/__snapshots__/linkify.test.ts.snap +0 -82
  224. package/src/markdown/__tests__/linkify.test.ts +0 -190
  225. package/src/sidebarItemsGenerator.ts +0 -307
  226. package/src/sidebars.ts +0 -522
  227. package/src/theme/hooks/useDocs.ts +0 -103
  228. package/src/versions.ts +0 -572
  229. package/tsconfig.json +0 -9
package/lib/options.js CHANGED
@@ -1,21 +1,32 @@
1
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
+ */
2
8
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateOptions = exports.OptionsSchema = exports.DEFAULT_OPTIONS = void 0;
9
+ exports.validateOptions = exports.DEFAULT_OPTIONS = void 0;
4
10
  const tslib_1 = require("tslib");
11
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
5
12
  const utils_validation_1 = require("@docusaurus/utils-validation");
6
- const chalk_1 = tslib_1.__importDefault(require("chalk"));
13
+ const utils_1 = require("@docusaurus/utils");
7
14
  const remark_admonitions_1 = tslib_1.__importDefault(require("remark-admonitions"));
8
- const sidebarItemsGenerator_1 = require("./sidebarItemsGenerator");
15
+ const generator_1 = require("./sidebars/generator");
9
16
  const numberPrefix_1 = require("./numberPrefix");
10
17
  exports.DEFAULT_OPTIONS = {
11
18
  path: 'docs',
12
19
  routeBasePath: 'docs',
13
- homePageId: undefined,
20
+ tagsBasePath: 'tags',
14
21
  include: ['**/*.{md,mdx}'],
15
- sidebarItemsGenerator: sidebarItemsGenerator_1.DefaultSidebarItemsGenerator,
22
+ exclude: utils_1.GlobExcludeDefault,
23
+ sidebarItemsGenerator: generator_1.DefaultSidebarItemsGenerator,
16
24
  numberPrefixParser: numberPrefix_1.DefaultNumberPrefixParser,
17
25
  docLayoutComponent: '@theme/DocPage',
18
26
  docItemComponent: '@theme/DocItem',
27
+ docTagDocListComponent: '@theme/DocTagDocListPage',
28
+ docTagsListComponent: '@theme/DocTagsListPage',
29
+ docCategoryGeneratedIndexComponent: '@theme/DocCategoryGeneratedIndexPage',
19
30
  remarkPlugins: [],
20
31
  rehypePlugins: [],
21
32
  beforeDefaultRemarkPlugins: [],
@@ -29,16 +40,21 @@ exports.DEFAULT_OPTIONS = {
29
40
  versions: {},
30
41
  editCurrentVersion: false,
31
42
  editLocalizedFiles: false,
43
+ sidebarCollapsible: true,
44
+ sidebarCollapsed: true,
45
+ breadcrumbs: true,
32
46
  };
33
47
  const VersionOptionsSchema = utils_validation_1.Joi.object({
34
48
  path: utils_validation_1.Joi.string().allow('').optional(),
35
49
  label: utils_validation_1.Joi.string().optional(),
36
50
  banner: utils_validation_1.Joi.string().equal('none', 'unreleased', 'unmaintained').optional(),
51
+ badge: utils_validation_1.Joi.boolean().optional(),
52
+ className: utils_validation_1.Joi.string().optional(),
37
53
  });
38
54
  const VersionsOptionsSchema = utils_validation_1.Joi.object()
39
55
  .pattern(utils_validation_1.Joi.string().required(), VersionOptionsSchema)
40
56
  .default(exports.DEFAULT_OPTIONS.versions);
41
- exports.OptionsSchema = utils_validation_1.Joi.object({
57
+ const OptionsSchema = utils_validation_1.Joi.object({
42
58
  path: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.path),
43
59
  editUrl: utils_validation_1.Joi.alternatives().try(utils_validation_1.URISchema, utils_validation_1.Joi.function()),
44
60
  editCurrentVersion: utils_validation_1.Joi.boolean().default(exports.DEFAULT_OPTIONS.editCurrentVersion),
@@ -47,10 +63,17 @@ exports.OptionsSchema = utils_validation_1.Joi.object({
47
63
  // '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
48
64
  // .allow('') ""
49
65
  .default(exports.DEFAULT_OPTIONS.routeBasePath),
50
- homePageId: utils_validation_1.Joi.string().optional(),
66
+ tagsBasePath: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.tagsBasePath),
67
+ // @ts-expect-error: deprecated
68
+ homePageId: utils_validation_1.Joi.any().forbidden().messages({
69
+ 'any.unknown': 'The docs plugin option homePageId is not supported anymore. To make a doc the "home", please add "slug: /" in its front matter. See: https://docusaurus.io/docs/next/docs-introduction#home-page-docs',
70
+ }),
51
71
  include: utils_validation_1.Joi.array().items(utils_validation_1.Joi.string()).default(exports.DEFAULT_OPTIONS.include),
72
+ exclude: utils_validation_1.Joi.array().items(utils_validation_1.Joi.string()).default(exports.DEFAULT_OPTIONS.exclude),
52
73
  sidebarPath: utils_validation_1.Joi.alternatives().try(utils_validation_1.Joi.boolean().invalid(true), utils_validation_1.Joi.string()),
53
74
  sidebarItemsGenerator: utils_validation_1.Joi.function().default(() => exports.DEFAULT_OPTIONS.sidebarItemsGenerator),
75
+ sidebarCollapsible: utils_validation_1.Joi.boolean().default(exports.DEFAULT_OPTIONS.sidebarCollapsible),
76
+ sidebarCollapsed: utils_validation_1.Joi.boolean().default(exports.DEFAULT_OPTIONS.sidebarCollapsed),
54
77
  numberPrefixParser: utils_validation_1.Joi.alternatives()
55
78
  .try(utils_validation_1.Joi.function(),
56
79
  // Convert boolean values to functions
@@ -60,6 +83,9 @@ exports.OptionsSchema = utils_validation_1.Joi.object({
60
83
  .default(() => exports.DEFAULT_OPTIONS.numberPrefixParser),
61
84
  docLayoutComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.docLayoutComponent),
62
85
  docItemComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.docItemComponent),
86
+ docTagsListComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.docTagsListComponent),
87
+ docTagDocListComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.docTagDocListComponent),
88
+ docCategoryGeneratedIndexComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.docCategoryGeneratedIndexComponent),
63
89
  remarkPlugins: utils_validation_1.RemarkPluginsSchema.default(exports.DEFAULT_OPTIONS.remarkPlugins),
64
90
  rehypePlugins: utils_validation_1.RehypePluginsSchema.default(exports.DEFAULT_OPTIONS.rehypePlugins),
65
91
  beforeDefaultRemarkPlugins: utils_validation_1.RemarkPluginsSchema.default(exports.DEFAULT_OPTIONS.beforeDefaultRemarkPlugins),
@@ -74,14 +100,29 @@ exports.OptionsSchema = utils_validation_1.Joi.object({
74
100
  disableVersioning: utils_validation_1.Joi.bool().default(exports.DEFAULT_OPTIONS.disableVersioning),
75
101
  lastVersion: utils_validation_1.Joi.string().optional(),
76
102
  versions: VersionsOptionsSchema,
103
+ breadcrumbs: utils_validation_1.Joi.bool().default(exports.DEFAULT_OPTIONS.breadcrumbs),
77
104
  });
78
- function validateOptions({ validate, options, }) {
79
- // TODO remove homePageId before end of 2020
80
- // "slug: /" is better because the home doc can be different across versions
81
- if (options.homePageId) {
82
- console.log(chalk_1.default.red(`The docs plugin option homePageId=${options.homePageId} is deprecated. To make a doc the "home", prefer frontmatter: "slug: /"`));
105
+ function validateOptions({ validate, options: userOptions, }) {
106
+ let options = userOptions;
107
+ if (options.sidebarCollapsible === false) {
108
+ // When sidebarCollapsible=false and sidebarCollapsed=undefined, we don't
109
+ // want to have the inconsistency warning. We let options.sidebarCollapsible
110
+ // become the default value for options.sidebarCollapsed
111
+ if (typeof options.sidebarCollapsed === 'undefined') {
112
+ options = {
113
+ ...options,
114
+ sidebarCollapsed: false,
115
+ };
116
+ }
117
+ if (options.sidebarCollapsed) {
118
+ logger_1.default.warn `The docs plugin config is inconsistent. It does not make sense to use code=${'sidebarCollapsible: false'} and code=${'sidebarCollapsed: true'} at the same time. code=${'sidebarCollapsed: true'} will be ignored.`;
119
+ options = {
120
+ ...options,
121
+ sidebarCollapsed: false,
122
+ };
123
+ }
83
124
  }
84
- const normalizedOptions = validate(exports.OptionsSchema, options);
125
+ const normalizedOptions = validate(OptionsSchema, options);
85
126
  if (normalizedOptions.admonitions) {
86
127
  normalizedOptions.remarkPlugins = normalizedOptions.remarkPlugins.concat([
87
128
  [remark_admonitions_1.default, normalizedOptions.admonitions],
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 type { VersionTag } from './types';
8
+ import type { PropSidebars, PropVersionMetadata, PropTagDocList, DocMetadata, LoadedVersion } from '@docusaurus/plugin-content-docs';
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: DocMetadata[];
15
+ }): PropTagDocList;
package/lib/props.js CHANGED
@@ -6,30 +6,52 @@
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;
10
- const lodash_1 = require("lodash");
9
+ exports.toTagDocListProp = exports.toVersionMetadataProp = exports.toSidebarsProp = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
12
+ const docs_1 = require("./docs");
11
13
  function toSidebarsProp(loadedVersion) {
12
- const docsById = lodash_1.keyBy(loadedVersion.docs, (doc) => doc.id);
13
- const convertDocLink = (item) => {
14
- const docId = item.id;
14
+ const docsById = (0, docs_1.createDocsByIdIndex)(loadedVersion.docs);
15
+ function getDocById(docId) {
15
16
  const docMetadata = docsById[docId];
16
17
  if (!docMetadata) {
17
18
  throw new Error(`Invalid sidebars file. The document with id "${docId}" was used in the sidebar, but no document with this id could be found.
18
19
  Available document ids are:
19
20
  - ${Object.keys(docsById).sort().join('\n- ')}`);
20
21
  }
22
+ return docMetadata;
23
+ }
24
+ const convertDocLink = (item) => {
25
+ const docMetadata = getDocById(item.id);
21
26
  const { title, permalink, frontMatter: { sidebar_label: sidebarLabel }, } = docMetadata;
22
27
  return {
23
28
  type: 'link',
24
- label: sidebarLabel || item.label || title,
29
+ label: sidebarLabel ?? item.label ?? title,
25
30
  href: permalink,
26
- customProps: item.customProps,
31
+ className: item.className,
32
+ customProps: item.customProps ?? docMetadata.frontMatter.sidebar_custom_props,
33
+ docId: docMetadata.unversionedId,
27
34
  };
28
35
  };
29
- const normalizeItem = (item) => {
36
+ function getCategoryLinkHref(link) {
37
+ switch (link?.type) {
38
+ case 'doc':
39
+ return getDocById(link.id).permalink;
40
+ case 'generated-index':
41
+ return link.permalink;
42
+ default:
43
+ return undefined;
44
+ }
45
+ }
46
+ function convertCategory(item) {
47
+ const { link, ...rest } = item;
48
+ const href = getCategoryLinkHref(link);
49
+ return { ...rest, items: item.items.map(normalizeItem), ...(href && { href }) };
50
+ }
51
+ function normalizeItem(item) {
30
52
  switch (item.type) {
31
53
  case 'category':
32
- return { ...item, items: item.items.map(normalizeItem) };
54
+ return convertCategory(item);
33
55
  case 'ref':
34
56
  case 'doc':
35
57
  return convertDocLink(item);
@@ -37,22 +59,56 @@ Available document ids are:
37
59
  default:
38
60
  return item;
39
61
  }
40
- };
62
+ }
41
63
  // Transform the sidebar so that all sidebar item will be in the
42
64
  // form of 'link' or 'category' only.
43
65
  // This is what will be passed as props to the UI component.
44
- return lodash_1.mapValues(loadedVersion.sidebars, (items) => items.map(normalizeItem));
66
+ return lodash_1.default.mapValues(loadedVersion.sidebars, (items) => items.map(normalizeItem));
45
67
  }
46
68
  exports.toSidebarsProp = toSidebarsProp;
69
+ function toVersionDocsProp(loadedVersion) {
70
+ return Object.fromEntries(loadedVersion.docs.map((doc) => [
71
+ doc.unversionedId,
72
+ {
73
+ id: doc.unversionedId,
74
+ title: doc.title,
75
+ description: doc.description,
76
+ sidebar: doc.sidebar,
77
+ },
78
+ ]));
79
+ }
47
80
  function toVersionMetadataProp(pluginId, loadedVersion) {
48
81
  return {
49
82
  pluginId,
50
83
  version: loadedVersion.versionName,
51
- label: loadedVersion.versionLabel,
52
- banner: loadedVersion.versionBanner,
84
+ label: loadedVersion.label,
85
+ banner: loadedVersion.banner,
86
+ badge: loadedVersion.badge,
87
+ className: loadedVersion.className,
53
88
  isLast: loadedVersion.isLast,
54
89
  docsSidebars: toSidebarsProp(loadedVersion),
55
- permalinkToSidebar: loadedVersion.permalinkToSidebar,
90
+ docs: toVersionDocsProp(loadedVersion),
56
91
  };
57
92
  }
58
93
  exports.toVersionMetadataProp = toVersionMetadataProp;
94
+ function toTagDocListProp({ allTagsPath, tag, docs, }) {
95
+ function toDocListProp() {
96
+ const list = lodash_1.default.compact(tag.docIds.map((id) => docs.find((doc) => doc.id === id)));
97
+ // Sort docs by title
98
+ list.sort((doc1, doc2) => doc1.title.localeCompare(doc2.title));
99
+ return list.map((doc) => ({
100
+ id: doc.id,
101
+ title: doc.title,
102
+ description: doc.description,
103
+ permalink: doc.permalink,
104
+ }));
105
+ }
106
+ return {
107
+ label: tag.label,
108
+ permalink: tag.permalink,
109
+ allTagsPath,
110
+ count: tag.docIds.length,
111
+ items: toDocListProp(),
112
+ };
113
+ }
114
+ exports.toTagDocListProp = toTagDocListProp;
@@ -0,0 +1,29 @@
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 { PluginContentLoadedActions, RouteConfig } from '@docusaurus/types';
8
+ import type { FullVersion } from './types';
9
+ import type { DocMetadata } from '@docusaurus/plugin-content-docs';
10
+ export declare function createCategoryGeneratedIndexRoutes({ version, actions, docCategoryGeneratedIndexComponent, aliasedSource, }: {
11
+ version: FullVersion;
12
+ actions: PluginContentLoadedActions;
13
+ docCategoryGeneratedIndexComponent: string;
14
+ aliasedSource: (str: string) => string;
15
+ }): Promise<RouteConfig[]>;
16
+ export declare function createDocRoutes({ docs, actions, docItemComponent, }: {
17
+ docs: DocMetadata[];
18
+ actions: PluginContentLoadedActions;
19
+ docItemComponent: string;
20
+ }): Promise<RouteConfig[]>;
21
+ export declare function createVersionRoutes({ version, actions, docItemComponent, docLayoutComponent, docCategoryGeneratedIndexComponent, pluginId, aliasedSource, }: {
22
+ version: FullVersion;
23
+ actions: PluginContentLoadedActions;
24
+ docLayoutComponent: string;
25
+ docItemComponent: string;
26
+ docCategoryGeneratedIndexComponent: string;
27
+ pluginId: string;
28
+ aliasedSource: (str: string) => string;
29
+ }): Promise<void>;
package/lib/routes.js ADDED
@@ -0,0 +1,96 @@
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.createVersionRoutes = exports.createDocRoutes = exports.createCategoryGeneratedIndexRoutes = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
12
+ const utils_1 = require("@docusaurus/utils");
13
+ const props_1 = require("./props");
14
+ async function createCategoryGeneratedIndexRoutes({ version, actions, docCategoryGeneratedIndexComponent, aliasedSource, }) {
15
+ const slugs = (0, utils_1.createSlugger)();
16
+ async function createCategoryGeneratedIndexRoute(categoryGeneratedIndex) {
17
+ const { sidebar, ...prop } = categoryGeneratedIndex;
18
+ const propFileName = slugs.slug(`${version.path}-${categoryGeneratedIndex.sidebar}-category-${categoryGeneratedIndex.title}`);
19
+ const propData = await actions.createData(`${(0, utils_1.docuHash)(`category/${propFileName}`)}.json`, JSON.stringify(prop, null, 2));
20
+ return {
21
+ path: categoryGeneratedIndex.permalink,
22
+ component: docCategoryGeneratedIndexComponent,
23
+ exact: true,
24
+ modules: {
25
+ categoryGeneratedIndex: aliasedSource(propData),
26
+ },
27
+ // Same as doc, this sidebar route attribute permits to associate this
28
+ // subpage to the given sidebar
29
+ ...(sidebar && { sidebar }),
30
+ };
31
+ }
32
+ return Promise.all(version.categoryGeneratedIndices.map(createCategoryGeneratedIndexRoute));
33
+ }
34
+ exports.createCategoryGeneratedIndexRoutes = createCategoryGeneratedIndexRoutes;
35
+ async function createDocRoutes({ docs, actions, docItemComponent, }) {
36
+ return Promise.all(docs.map(async (metadataItem) => {
37
+ await actions.createData(
38
+ // Note that this created data path must be in sync with
39
+ // metadataPath provided to mdx-loader.
40
+ `${(0, utils_1.docuHash)(metadataItem.source)}.json`, JSON.stringify(metadataItem, null, 2));
41
+ const docRoute = {
42
+ path: metadataItem.permalink,
43
+ component: docItemComponent,
44
+ exact: true,
45
+ modules: {
46
+ content: metadataItem.source,
47
+ },
48
+ // Because the parent (DocPage) comp need to access it easily
49
+ // This permits to render the sidebar once without unmount/remount when
50
+ // navigating (and preserve sidebar state)
51
+ ...(metadataItem.sidebar && {
52
+ sidebar: metadataItem.sidebar,
53
+ }),
54
+ };
55
+ return docRoute;
56
+ }));
57
+ }
58
+ exports.createDocRoutes = createDocRoutes;
59
+ async function createVersionRoutes({ version, actions, docItemComponent, docLayoutComponent, docCategoryGeneratedIndexComponent, pluginId, aliasedSource, }) {
60
+ async function doCreateVersionRoutes() {
61
+ const versionMetadata = (0, props_1.toVersionMetadataProp)(pluginId, version);
62
+ const versionMetadataPropPath = await actions.createData(`${(0, utils_1.docuHash)(`version-${version.versionName}-metadata-prop`)}.json`, JSON.stringify(versionMetadata, null, 2));
63
+ async function createVersionSubRoutes() {
64
+ const [docRoutes, sidebarsRoutes] = await Promise.all([
65
+ createDocRoutes({ docs: version.docs, actions, docItemComponent }),
66
+ createCategoryGeneratedIndexRoutes({
67
+ version,
68
+ actions,
69
+ docCategoryGeneratedIndexComponent,
70
+ aliasedSource,
71
+ }),
72
+ ]);
73
+ const routes = [...docRoutes, ...sidebarsRoutes];
74
+ return routes.sort((a, b) => a.path.localeCompare(b.path));
75
+ }
76
+ actions.addRoute({
77
+ path: version.path,
78
+ // Allow matching /docs/* since this is the wrapping route
79
+ exact: false,
80
+ component: docLayoutComponent,
81
+ routes: await createVersionSubRoutes(),
82
+ modules: {
83
+ versionMetadata: aliasedSource(versionMetadataPropPath),
84
+ },
85
+ priority: version.routePriority,
86
+ });
87
+ }
88
+ try {
89
+ return await doCreateVersionRoutes();
90
+ }
91
+ catch (err) {
92
+ logger_1.default.error `Can't create version routes for version name=${version.versionName}`;
93
+ throw err;
94
+ }
95
+ }
96
+ exports.createVersionRoutes = createVersionRoutes;
@@ -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
+ export { CURRENT_VERSION_NAME, VERSIONED_DOCS_DIR, VERSIONED_SIDEBARS_DIR, VERSIONS_JSON_FILE, } from './constants';
8
+ export { filterVersions, getDefaultVersionBanner, getVersionBadge, getVersionBanner, } from './versions';
9
+ export { readVersionNames } from './versions/files';
@@ -0,0 +1,22 @@
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.readVersionNames = exports.getVersionBanner = exports.getVersionBadge = exports.getDefaultVersionBanner = exports.filterVersions = exports.VERSIONS_JSON_FILE = exports.VERSIONED_SIDEBARS_DIR = exports.VERSIONED_DOCS_DIR = exports.CURRENT_VERSION_NAME = void 0;
10
+ // APIs available to Node.js
11
+ var constants_1 = require("./constants");
12
+ Object.defineProperty(exports, "CURRENT_VERSION_NAME", { enumerable: true, get: function () { return constants_1.CURRENT_VERSION_NAME; } });
13
+ Object.defineProperty(exports, "VERSIONED_DOCS_DIR", { enumerable: true, get: function () { return constants_1.VERSIONED_DOCS_DIR; } });
14
+ Object.defineProperty(exports, "VERSIONED_SIDEBARS_DIR", { enumerable: true, get: function () { return constants_1.VERSIONED_SIDEBARS_DIR; } });
15
+ Object.defineProperty(exports, "VERSIONS_JSON_FILE", { enumerable: true, get: function () { return constants_1.VERSIONS_JSON_FILE; } });
16
+ var versions_1 = require("./versions");
17
+ Object.defineProperty(exports, "filterVersions", { enumerable: true, get: function () { return versions_1.filterVersions; } });
18
+ Object.defineProperty(exports, "getDefaultVersionBanner", { enumerable: true, get: function () { return versions_1.getDefaultVersionBanner; } });
19
+ Object.defineProperty(exports, "getVersionBadge", { enumerable: true, get: function () { return versions_1.getVersionBadge; } });
20
+ Object.defineProperty(exports, "getVersionBanner", { enumerable: true, get: function () { return versions_1.getVersionBanner; } });
21
+ var files_1 = require("./versions/files");
22
+ Object.defineProperty(exports, "readVersionNames", { enumerable: true, get: function () { return files_1.readVersionNames; } });
@@ -4,12 +4,7 @@
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
- export declare type CategoryMetadatasFile = {
11
- label?: string;
12
- position?: number;
13
- collapsed?: boolean;
14
- };
15
10
  export declare const DefaultSidebarItemsGenerator: SidebarItemsGenerator;
@@ -0,0 +1,209 @@
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 path_1 = tslib_1.__importDefault(require("path"));
12
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
13
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
14
+ const utils_1 = require("@docusaurus/utils");
15
+ const docs_1 = require("../docs");
16
+ const BreadcrumbSeparator = '/';
17
+ // Just an alias to the make code more explicit
18
+ function getLocalDocId(docId) {
19
+ return lodash_1.default.last(docId.split('/'));
20
+ }
21
+ exports.CategoryMetadataFilenameBase = '_category_';
22
+ exports.CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
23
+ // Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
24
+ const DefaultSidebarItemsGenerator = ({ numberPrefixParser, isCategoryIndex, docs: allDocs, item: { dirName: autogenDir }, categoriesMetadata, }) => {
25
+ const docsById = (0, docs_1.createDocsByIdIndex)(allDocs);
26
+ const findDoc = (docId) => docsById[docId];
27
+ const getDoc = (docId) => {
28
+ const doc = findDoc(docId);
29
+ if (!doc) {
30
+ throw new Error(`Can't find any doc with ID ${docId}.
31
+ Available doc IDs:
32
+ - ${Object.keys(docsById).join('\n- ')}`);
33
+ }
34
+ return doc;
35
+ };
36
+ /**
37
+ * Step 1. Extract the docs that are in the autogen dir.
38
+ */
39
+ function getAutogenDocs() {
40
+ function isInAutogeneratedDir(doc) {
41
+ return (
42
+ // Doc at the root of the autogenerated sidebar dir
43
+ doc.sourceDirName === autogenDir ||
44
+ // Autogen dir is . and doc is in subfolder
45
+ autogenDir === '.' ||
46
+ // Autogen dir is not . and doc is in subfolder
47
+ // "api/myDoc" startsWith "api/" (note "api2/myDoc" is not included)
48
+ doc.sourceDirName.startsWith((0, utils_1.addTrailingSlash)(autogenDir)));
49
+ }
50
+ const docs = allDocs.filter(isInAutogeneratedDir);
51
+ if (docs.length === 0) {
52
+ logger_1.default.warn `No docs found in path=${autogenDir}: can't auto-generate a sidebar.`;
53
+ }
54
+ return docs;
55
+ }
56
+ /**
57
+ * Step 2. Turn the linear file list into a tree structure.
58
+ */
59
+ function treeify(docs) {
60
+ // Get the category breadcrumb of a doc (relative to the dir of the
61
+ // autogenerated sidebar item)
62
+ // autogenDir=a/b and docDir=a/b/c/d => returns [c, d]
63
+ // autogenDir=a/b and docDir=a/b => returns []
64
+ // TODO: try to use path.relative()
65
+ function getRelativeBreadcrumb(doc) {
66
+ return autogenDir === doc.sourceDirName
67
+ ? []
68
+ : doc.sourceDirName
69
+ .replace((0, utils_1.addTrailingSlash)(autogenDir), '')
70
+ .split(BreadcrumbSeparator);
71
+ }
72
+ const treeRoot = {};
73
+ docs.forEach((doc) => {
74
+ const breadcrumb = getRelativeBreadcrumb(doc);
75
+ // We walk down the file's path to generate the fs structure
76
+ let currentDir = treeRoot;
77
+ breadcrumb.forEach((dir) => {
78
+ if (typeof currentDir[dir] === 'undefined') {
79
+ currentDir[dir] = {}; // Create new folder.
80
+ }
81
+ currentDir = currentDir[dir]; // Go into the subdirectory.
82
+ });
83
+ // We've walked through the path. Register the file in this directory.
84
+ currentDir[path_1.default.basename(doc.source)] = doc.id;
85
+ });
86
+ return treeRoot;
87
+ }
88
+ /**
89
+ * Step 3. Recursively transform the tree-like structure to sidebar items.
90
+ * (From a record to an array of items, akin to normalizing shorthand)
91
+ */
92
+ function generateSidebar(fsModel) {
93
+ function createDocItem(id, fullPath, fileName) {
94
+ const { sidebarPosition: position, frontMatter: { sidebar_label: label, sidebar_class_name: className }, } = getDoc(id);
95
+ return {
96
+ type: 'doc',
97
+ id,
98
+ position,
99
+ source: fileName,
100
+ // We don't want these fields to magically appear in the generated
101
+ // sidebar
102
+ ...(label !== undefined && { label }),
103
+ ...(className !== undefined && { className }),
104
+ };
105
+ }
106
+ function createCategoryItem(dir, fullPath, folderName) {
107
+ const categoryMetadata = categoriesMetadata[path_1.default.posix.join(autogenDir, fullPath)];
108
+ const allItems = Object.entries(dir).map(([key, content]) => dirToItem(content, key, `${fullPath}/${key}`));
109
+ // Try to match a doc inside the category folder,
110
+ // using the "local id" (myDoc) or "qualified id" (dirName/myDoc)
111
+ function findDocByLocalId(localId) {
112
+ return allItems.find((item) => item.type === 'doc' && getLocalDocId(item.id) === localId);
113
+ }
114
+ function findConventionalCategoryDocLink() {
115
+ return allItems.find((item) => {
116
+ if (item.type !== 'doc') {
117
+ return false;
118
+ }
119
+ const doc = getDoc(item.id);
120
+ return isCategoryIndex((0, docs_1.toCategoryIndexMatcherParam)(doc));
121
+ });
122
+ }
123
+ // In addition to the ID, this function also retrieves metadata of the
124
+ // linked doc that could be used as fallback values for category metadata
125
+ function getCategoryLinkedDocMetadata() {
126
+ const link = categoryMetadata?.link;
127
+ if (link !== undefined && link?.type !== 'doc') {
128
+ // If a link is explicitly specified, we won't apply conventions
129
+ return undefined;
130
+ }
131
+ const id = link
132
+ ? findDocByLocalId(link.id)?.id ?? getDoc(link.id).id
133
+ : findConventionalCategoryDocLink()?.id;
134
+ if (!id) {
135
+ return undefined;
136
+ }
137
+ const doc = getDoc(id);
138
+ return {
139
+ id,
140
+ position: doc.sidebarPosition,
141
+ label: doc.frontMatter.sidebar_label ?? doc.title,
142
+ customProps: doc.frontMatter.sidebar_custom_props,
143
+ className: doc.frontMatter.sidebar_class_name,
144
+ };
145
+ }
146
+ const categoryLinkedDoc = getCategoryLinkedDocMetadata();
147
+ const link = categoryLinkedDoc
148
+ ? {
149
+ type: 'doc',
150
+ id: categoryLinkedDoc.id, // We "remap" a potentially "local id" to a "qualified id"
151
+ }
152
+ : categoryMetadata?.link;
153
+ // If a doc is linked, remove it from the category subItems
154
+ const items = allItems.filter((item) => !(item.type === 'doc' && item.id === categoryLinkedDoc?.id));
155
+ const className = categoryMetadata?.className ?? categoryLinkedDoc?.className;
156
+ const customProps = categoryMetadata?.customProps ?? categoryLinkedDoc?.customProps;
157
+ const { filename, numberPrefix } = numberPrefixParser(folderName);
158
+ return {
159
+ type: 'category',
160
+ label: categoryMetadata?.label ?? categoryLinkedDoc?.label ?? filename,
161
+ collapsible: categoryMetadata?.collapsible,
162
+ collapsed: categoryMetadata?.collapsed,
163
+ position: categoryMetadata?.position ??
164
+ categoryLinkedDoc?.position ??
165
+ numberPrefix,
166
+ source: folderName,
167
+ ...(customProps !== undefined && { customProps }),
168
+ ...(className !== undefined && { className }),
169
+ items,
170
+ ...(link && { link }),
171
+ };
172
+ }
173
+ function dirToItem(dir, // The directory item to be transformed.
174
+ itemKey, // File/folder name; for categories, it's used to generate the next `relativePath`.
175
+ fullPath) {
176
+ return typeof dir === 'object'
177
+ ? createCategoryItem(dir, fullPath, itemKey)
178
+ : createDocItem(dir, fullPath, itemKey);
179
+ }
180
+ return Object.entries(fsModel).map(([key, content]) => dirToItem(content, key, key));
181
+ }
182
+ /**
183
+ * Step 4. Recursively sort the categories/docs + remove the "position"
184
+ * attribute from final output. Note: the "position" is only used to sort
185
+ * "inside" a sidebar slice. It is not used to sort across multiple
186
+ * consecutive sidebar slices (i.e. a whole category composed of multiple
187
+ * autogenerated items)
188
+ */
189
+ function sortItems(sidebarItems) {
190
+ const processedSidebarItems = sidebarItems.map((item) => {
191
+ if (item.type === 'category') {
192
+ return { ...item, items: sortItems(item.items) };
193
+ }
194
+ return item;
195
+ });
196
+ const sortedSidebarItems = lodash_1.default.sortBy(processedSidebarItems, [
197
+ 'position',
198
+ 'source',
199
+ ]);
200
+ return sortedSidebarItems.map(({ position, source, ...item }) => item);
201
+ }
202
+ // TODO: the whole code is designed for pipeline operator
203
+ const docs = getAutogenDocs();
204
+ const fsModel = treeify(docs);
205
+ const sidebarWithPosition = generateSidebar(fsModel);
206
+ const sortedSidebar = sortItems(sidebarWithPosition);
207
+ return sortedSidebar;
208
+ };
209
+ exports.DefaultSidebarItemsGenerator = DefaultSidebarItemsGenerator;