@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
@@ -0,0 +1,118 @@
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 fs from 'fs-extra';
9
+ import path from 'path';
10
+ import _ from 'lodash';
11
+ import logger from '@docusaurus/logger';
12
+ import {Globby} from '@docusaurus/utils';
13
+ import Yaml from 'js-yaml';
14
+ import combinePromises from 'combine-promises';
15
+ import importFresh from 'import-fresh';
16
+ import {validateSidebars, validateCategoryMetadataFile} from './validation';
17
+ import {normalizeSidebars} from './normalization';
18
+ import {processSidebars} from './processor';
19
+ import {postProcessSidebars} from './postProcessor';
20
+ import type {PluginOptions} from '@docusaurus/plugin-content-docs';
21
+ import type {SidebarsConfig, Sidebars, SidebarProcessorParams} from './types';
22
+
23
+ export const DefaultSidebars: SidebarsConfig = {
24
+ defaultSidebar: [
25
+ {
26
+ type: 'autogenerated',
27
+ dirName: '.',
28
+ },
29
+ ],
30
+ };
31
+
32
+ export const DisabledSidebars: SidebarsConfig = {};
33
+
34
+ // If a path is provided, make it absolute
35
+ export function resolveSidebarPathOption(
36
+ siteDir: string,
37
+ sidebarPathOption: PluginOptions['sidebarPath'],
38
+ ): PluginOptions['sidebarPath'] {
39
+ return sidebarPathOption
40
+ ? path.resolve(siteDir, sidebarPathOption)
41
+ : sidebarPathOption;
42
+ }
43
+
44
+ async function readCategoriesMetadata(contentPath: string) {
45
+ const categoryFiles = await Globby('**/_category_.{json,yml,yaml}', {
46
+ cwd: contentPath,
47
+ });
48
+ const categoryToFile = _.groupBy(categoryFiles, path.dirname);
49
+ return combinePromises(
50
+ _.mapValues(categoryToFile, async (files, folder) => {
51
+ const filePath = files[0]!;
52
+ if (files.length > 1) {
53
+ logger.warn`There are more than one category metadata files for path=${folder}: ${files.join(
54
+ ', ',
55
+ )}. The behavior is undetermined.`;
56
+ }
57
+ const content = await fs.readFile(
58
+ path.join(contentPath, filePath),
59
+ 'utf-8',
60
+ );
61
+ try {
62
+ return validateCategoryMetadataFile(Yaml.load(content));
63
+ } catch (err) {
64
+ logger.error`The docs sidebar category metadata file path=${filePath} looks invalid!`;
65
+ throw err;
66
+ }
67
+ }),
68
+ );
69
+ }
70
+
71
+ export async function loadSidebarsFileUnsafe(
72
+ sidebarFilePath: string | false | undefined,
73
+ ): Promise<SidebarsConfig> {
74
+ // false => no sidebars
75
+ if (sidebarFilePath === false) {
76
+ return DisabledSidebars;
77
+ }
78
+
79
+ // undefined => defaults to autogenerated sidebars
80
+ if (typeof sidebarFilePath === 'undefined') {
81
+ return DefaultSidebars;
82
+ }
83
+
84
+ // Non-existent sidebars file: no sidebars
85
+ // Note: this edge case can happen on versioned docs, not current version
86
+ // We avoid creating empty versioned sidebars file with the CLI
87
+ if (!(await fs.pathExists(sidebarFilePath))) {
88
+ return DisabledSidebars;
89
+ }
90
+
91
+ // We don't want sidebars to be cached because of hot reloading.
92
+ return importFresh(sidebarFilePath);
93
+ }
94
+
95
+ export async function loadSidebars(
96
+ sidebarFilePath: string | false | undefined,
97
+ options: SidebarProcessorParams,
98
+ ): Promise<Sidebars> {
99
+ try {
100
+ const sidebarsConfig = await loadSidebarsFileUnsafe(sidebarFilePath);
101
+ const normalizedSidebars = normalizeSidebars(sidebarsConfig);
102
+ validateSidebars(normalizedSidebars);
103
+ const categoriesMetadata = await readCategoriesMetadata(
104
+ options.version.contentPath,
105
+ );
106
+ const processedSidebars = await processSidebars(
107
+ normalizedSidebars,
108
+ categoriesMetadata,
109
+ options,
110
+ );
111
+ return postProcessSidebars(processedSidebars, options);
112
+ } catch (err) {
113
+ logger.error`Sidebars file at path=${
114
+ sidebarFilePath as string
115
+ } failed to be loaded.`;
116
+ throw err;
117
+ }
118
+ }
@@ -0,0 +1,85 @@
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 _ from 'lodash';
9
+ import logger from '@docusaurus/logger';
10
+ import {isCategoriesShorthand} from './utils';
11
+ import type {
12
+ NormalizedSidebarItem,
13
+ NormalizedSidebar,
14
+ NormalizedSidebars,
15
+ SidebarCategoriesShorthand,
16
+ SidebarItemCategoryConfig,
17
+ SidebarItemConfig,
18
+ SidebarConfig,
19
+ SidebarsConfig,
20
+ NormalizedSidebarItemCategory,
21
+ } from './types';
22
+
23
+ function normalizeCategoriesShorthand(
24
+ sidebar: SidebarCategoriesShorthand,
25
+ ): SidebarItemCategoryConfig[] {
26
+ return Object.entries(sidebar).map(([label, items]) => ({
27
+ type: 'category',
28
+ label,
29
+ items,
30
+ }));
31
+ }
32
+
33
+ /**
34
+ * Normalizes recursively item and all its children. Ensures that at the end
35
+ * each item will be an object with the corresponding type.
36
+ */
37
+ export function normalizeItem(
38
+ item: SidebarItemConfig,
39
+ ): NormalizedSidebarItem[] {
40
+ if (typeof item === 'string') {
41
+ return [{type: 'doc', id: item}];
42
+ }
43
+ if (isCategoriesShorthand(item)) {
44
+ // This will never throw anyways
45
+ return normalizeSidebar(item, 'sidebar items slice');
46
+ }
47
+ if (item.type === 'category') {
48
+ const normalizedCategory: NormalizedSidebarItemCategory = {
49
+ ...item,
50
+ items: normalizeSidebar(
51
+ item.items,
52
+ logger.interpolate`code=${'items'} of the category name=${item.label}`,
53
+ ),
54
+ };
55
+ return [normalizedCategory];
56
+ }
57
+ return [item];
58
+ }
59
+
60
+ function normalizeSidebar(
61
+ sidebar: SidebarConfig,
62
+ place: string,
63
+ ): NormalizedSidebar {
64
+ if (!Array.isArray(sidebar) && !isCategoriesShorthand(sidebar)) {
65
+ throw new Error(
66
+ logger.interpolate`Invalid sidebar items collection code=${JSON.stringify(
67
+ sidebar,
68
+ )} in ${place}: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a code=${'type'} property). See url=${'https://docusaurus.io/docs/sidebar/items'} for all valid syntaxes.`,
69
+ );
70
+ }
71
+
72
+ const normalizedSidebar = Array.isArray(sidebar)
73
+ ? sidebar
74
+ : normalizeCategoriesShorthand(sidebar);
75
+
76
+ return normalizedSidebar.flatMap((subItem) => normalizeItem(subItem));
77
+ }
78
+
79
+ export function normalizeSidebars(
80
+ sidebars: SidebarsConfig,
81
+ ): NormalizedSidebars {
82
+ return _.mapValues(sidebars, (sidebar, id) =>
83
+ normalizeSidebar(sidebar, logger.interpolate`sidebar name=${id}`),
84
+ );
85
+ }
@@ -0,0 +1,112 @@
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 _ from 'lodash';
9
+ import {normalizeUrl} from '@docusaurus/utils';
10
+ import {getDocIds} from '../docs';
11
+ import type {
12
+ SidebarItem,
13
+ Sidebars,
14
+ SidebarProcessorParams,
15
+ ProcessedSidebarItemCategory,
16
+ ProcessedSidebarItem,
17
+ ProcessedSidebars,
18
+ SidebarItemCategoryLink,
19
+ } from './types';
20
+
21
+ export type SidebarPostProcessorParams = SidebarProcessorParams & {
22
+ draftIds: Set<string>;
23
+ };
24
+
25
+ function normalizeCategoryLink(
26
+ category: ProcessedSidebarItemCategory,
27
+ params: SidebarPostProcessorParams,
28
+ ): SidebarItemCategoryLink | undefined {
29
+ if (category.link?.type === 'doc' && params.draftIds.has(category.link.id)) {
30
+ return undefined;
31
+ }
32
+ if (category.link?.type === 'generated-index') {
33
+ // Default slug logic can be improved
34
+ const getDefaultSlug = () =>
35
+ `/category/${params.categoryLabelSlugger.slug(category.label)}`;
36
+ const slug = category.link.slug ?? getDefaultSlug();
37
+ const permalink = normalizeUrl([params.version.path, slug]);
38
+ return {
39
+ ...category.link,
40
+ slug,
41
+ permalink,
42
+ };
43
+ }
44
+ return category.link;
45
+ }
46
+
47
+ function postProcessSidebarItem(
48
+ item: ProcessedSidebarItem,
49
+ params: SidebarPostProcessorParams,
50
+ ): SidebarItem | null {
51
+ if (item.type === 'category') {
52
+ // Fail-fast if there's actually no subitems, no because all subitems are
53
+ // drafts. This is likely a configuration mistake.
54
+ if (item.items.length === 0 && !item.link) {
55
+ throw new Error(
56
+ `Sidebar category ${item.label} has neither any subitem nor a link. This makes this item not able to link to anything.`,
57
+ );
58
+ }
59
+ const category = {
60
+ ...item,
61
+ collapsed: item.collapsed ?? params.sidebarOptions.sidebarCollapsed,
62
+ collapsible: item.collapsible ?? params.sidebarOptions.sidebarCollapsible,
63
+ link: normalizeCategoryLink(item, params),
64
+ items: item.items
65
+ .map((subItem) => postProcessSidebarItem(subItem, params))
66
+ .filter((v): v is SidebarItem => Boolean(v)),
67
+ };
68
+ // If the current category doesn't have subitems, we render a normal link
69
+ // instead.
70
+ if (category.items.length === 0) {
71
+ // Doesn't make sense to render an empty generated index page, so we
72
+ // filter the entire category out as well.
73
+ if (
74
+ !category.link ||
75
+ category.link.type === 'generated-index' ||
76
+ params.draftIds.has(category.link.id)
77
+ ) {
78
+ return null;
79
+ }
80
+ return {
81
+ type: 'doc',
82
+ label: category.label,
83
+ id: category.link.id,
84
+ };
85
+ }
86
+ // A non-collapsible category can't be collapsed!
87
+ if (!category.collapsible) {
88
+ category.collapsed = false;
89
+ }
90
+ return category;
91
+ }
92
+ if (
93
+ (item.type === 'doc' || item.type === 'ref') &&
94
+ params.draftIds.has(item.id)
95
+ ) {
96
+ return null;
97
+ }
98
+ return item;
99
+ }
100
+
101
+ export function postProcessSidebars(
102
+ sidebars: ProcessedSidebars,
103
+ params: SidebarProcessorParams,
104
+ ): Sidebars {
105
+ const draftIds = new Set(params.drafts.flatMap(getDocIds));
106
+
107
+ return _.mapValues(sidebars, (sidebar) =>
108
+ sidebar
109
+ .map((item) => postProcessSidebarItem(item, {...params, draftIds}))
110
+ .filter((v): v is SidebarItem => Boolean(v)),
111
+ );
112
+ }
@@ -0,0 +1,123 @@
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 _ from 'lodash';
9
+ import combinePromises from 'combine-promises';
10
+ import {DefaultSidebarItemsGenerator} from './generator';
11
+ import {validateSidebars} from './validation';
12
+ import {isCategoryIndex} from '../docs';
13
+ import type {
14
+ DocMetadataBase,
15
+ VersionMetadata,
16
+ } from '@docusaurus/plugin-content-docs';
17
+ import type {
18
+ NormalizedSidebarItem,
19
+ NormalizedSidebar,
20
+ NormalizedSidebars,
21
+ SidebarItemsGeneratorDoc,
22
+ SidebarItemsGeneratorVersion,
23
+ SidebarItemAutogenerated,
24
+ ProcessedSidebarItem,
25
+ ProcessedSidebar,
26
+ ProcessedSidebars,
27
+ SidebarProcessorParams,
28
+ CategoryMetadataFile,
29
+ } from './types';
30
+
31
+ function toSidebarItemsGeneratorDoc(
32
+ doc: DocMetadataBase,
33
+ ): SidebarItemsGeneratorDoc {
34
+ return _.pick(doc, [
35
+ 'id',
36
+ 'unversionedId',
37
+ 'title',
38
+ 'frontMatter',
39
+ 'source',
40
+ 'sourceDirName',
41
+ 'sidebarPosition',
42
+ ]);
43
+ }
44
+
45
+ function toSidebarItemsGeneratorVersion(
46
+ version: VersionMetadata,
47
+ ): SidebarItemsGeneratorVersion {
48
+ return _.pick(version, ['versionName', 'contentPath']);
49
+ }
50
+
51
+ // Handle the generation of autogenerated sidebar items and other
52
+ // post-processing checks
53
+ async function processSidebar(
54
+ unprocessedSidebar: NormalizedSidebar,
55
+ categoriesMetadata: {[filePath: string]: CategoryMetadataFile},
56
+ params: SidebarProcessorParams,
57
+ ): Promise<ProcessedSidebar> {
58
+ const {sidebarItemsGenerator, numberPrefixParser, docs, version} = params;
59
+
60
+ // Just a minor lazy transformation optimization
61
+ const getSidebarItemsGeneratorDocsAndVersion = _.memoize(() => ({
62
+ docs: docs.map(toSidebarItemsGeneratorDoc),
63
+ version: toSidebarItemsGeneratorVersion(version),
64
+ }));
65
+
66
+ async function processAutoGeneratedItem(
67
+ item: SidebarItemAutogenerated,
68
+ ): Promise<ProcessedSidebarItem[]> {
69
+ const generatedItems = await sidebarItemsGenerator({
70
+ item,
71
+ numberPrefixParser,
72
+ defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
73
+ isCategoryIndex,
74
+ ...getSidebarItemsGeneratorDocsAndVersion(),
75
+ categoriesMetadata,
76
+ });
77
+ // Process again... weird but sidebar item generated might generate some
78
+ // auto-generated items?
79
+ // TODO repeatedly process & unwrap autogenerated items until there are no
80
+ // more autogenerated items, or when loop count (e.g. 10) is reached
81
+ return processItems(generatedItems);
82
+ }
83
+
84
+ async function processItem(
85
+ item: NormalizedSidebarItem,
86
+ ): Promise<ProcessedSidebarItem[]> {
87
+ if (item.type === 'category') {
88
+ return [
89
+ {
90
+ ...item,
91
+ items: (await Promise.all(item.items.map(processItem))).flat(),
92
+ },
93
+ ];
94
+ }
95
+ if (item.type === 'autogenerated') {
96
+ return processAutoGeneratedItem(item);
97
+ }
98
+ return [item];
99
+ }
100
+
101
+ async function processItems(
102
+ items: NormalizedSidebarItem[],
103
+ ): Promise<ProcessedSidebarItem[]> {
104
+ return (await Promise.all(items.map(processItem))).flat();
105
+ }
106
+
107
+ const processedSidebar = await processItems(unprocessedSidebar);
108
+ return processedSidebar;
109
+ }
110
+
111
+ export async function processSidebars(
112
+ unprocessedSidebars: NormalizedSidebars,
113
+ categoriesMetadata: {[filePath: string]: CategoryMetadataFile},
114
+ params: SidebarProcessorParams,
115
+ ): Promise<ProcessedSidebars> {
116
+ const processedSidebars = await combinePromises(
117
+ _.mapValues(unprocessedSidebars, (unprocessedSidebar) =>
118
+ processSidebar(unprocessedSidebar, categoriesMetadata, params),
119
+ ),
120
+ );
121
+ validateSidebars(processedSidebars);
122
+ return processedSidebars;
123
+ }
@@ -0,0 +1,275 @@
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 type {Optional, Required} from 'utility-types';
9
+ import type {
10
+ NumberPrefixParser,
11
+ SidebarOptions,
12
+ CategoryIndexMatcher,
13
+ DocMetadataBase,
14
+ VersionMetadata,
15
+ } from '@docusaurus/plugin-content-docs';
16
+ import type {Slugger} from '@docusaurus/utils';
17
+
18
+ // Makes all properties visible when hovering over the type
19
+ type Expand<T extends {[x: string]: unknown}> = {[P in keyof T]: T[P]};
20
+
21
+ export type SidebarItemBase = {
22
+ className?: string;
23
+ customProps?: {[key: string]: unknown};
24
+ };
25
+
26
+ export type SidebarItemDoc = SidebarItemBase & {
27
+ type: 'doc' | 'ref';
28
+ label?: string;
29
+ id: string;
30
+ };
31
+
32
+ export type SidebarItemHtml = SidebarItemBase & {
33
+ type: 'html';
34
+ value: string;
35
+ defaultStyle?: boolean;
36
+ };
37
+
38
+ export type SidebarItemLink = SidebarItemBase & {
39
+ type: 'link';
40
+ href: string;
41
+ label: string;
42
+ };
43
+
44
+ export type SidebarItemAutogenerated = SidebarItemBase & {
45
+ type: 'autogenerated';
46
+ dirName: string;
47
+ };
48
+
49
+ type SidebarItemCategoryBase = SidebarItemBase & {
50
+ type: 'category';
51
+ label: string;
52
+ collapsed: boolean;
53
+ collapsible: boolean;
54
+ };
55
+
56
+ export type SidebarItemCategoryLinkDoc = {type: 'doc'; id: string};
57
+
58
+ export type SidebarItemCategoryLinkGeneratedIndexConfig = {
59
+ type: 'generated-index';
60
+ slug?: string;
61
+ title?: string;
62
+ description?: string;
63
+ image?: string;
64
+ keywords?: string | readonly string[];
65
+ };
66
+ export type SidebarItemCategoryLinkGeneratedIndex = {
67
+ type: 'generated-index';
68
+ slug: string;
69
+ permalink: string;
70
+ title?: string;
71
+ description?: string;
72
+ image?: string;
73
+ keywords?: string | readonly string[];
74
+ };
75
+
76
+ export type SidebarItemCategoryLinkConfig =
77
+ | SidebarItemCategoryLinkDoc
78
+ | SidebarItemCategoryLinkGeneratedIndexConfig;
79
+
80
+ export type SidebarItemCategoryLink =
81
+ | SidebarItemCategoryLinkDoc
82
+ | SidebarItemCategoryLinkGeneratedIndex;
83
+
84
+ // The user-given configuration in sidebars.js, before normalization
85
+ export type SidebarItemCategoryConfig = Expand<
86
+ Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
87
+ items: SidebarCategoriesShorthand | SidebarItemConfig[];
88
+ link?: SidebarItemCategoryLinkConfig;
89
+ }
90
+ >;
91
+
92
+ export type SidebarCategoriesShorthand = {
93
+ [sidebarCategory: string]: SidebarCategoriesShorthand | SidebarItemConfig[];
94
+ };
95
+
96
+ export type SidebarItemConfig =
97
+ | SidebarItemDoc
98
+ | SidebarItemHtml
99
+ | SidebarItemLink
100
+ | SidebarItemAutogenerated
101
+ | SidebarItemCategoryConfig
102
+ | string
103
+ | SidebarCategoriesShorthand;
104
+
105
+ export type SidebarConfig = SidebarCategoriesShorthand | SidebarItemConfig[];
106
+ export type SidebarsConfig = {
107
+ [sidebarId: string]: SidebarConfig;
108
+ };
109
+
110
+ // Normalized but still has 'autogenerated', which will be handled in processing
111
+ export type NormalizedSidebarItemCategory = Expand<
112
+ Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
113
+ items: NormalizedSidebarItem[];
114
+ link?: SidebarItemCategoryLinkConfig;
115
+ }
116
+ >;
117
+
118
+ export type NormalizedSidebarItem =
119
+ | SidebarItemDoc
120
+ | SidebarItemHtml
121
+ | SidebarItemLink
122
+ | NormalizedSidebarItemCategory
123
+ | SidebarItemAutogenerated;
124
+
125
+ export type NormalizedSidebar = NormalizedSidebarItem[];
126
+ export type NormalizedSidebars = {
127
+ [sidebarId: string]: NormalizedSidebar;
128
+ };
129
+
130
+ export type ProcessedSidebarItemCategory = Expand<
131
+ Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
132
+ items: ProcessedSidebarItem[];
133
+ link?: SidebarItemCategoryLinkConfig;
134
+ }
135
+ >;
136
+ export type ProcessedSidebarItem =
137
+ | SidebarItemDoc
138
+ | SidebarItemHtml
139
+ | SidebarItemLink
140
+ | ProcessedSidebarItemCategory;
141
+ export type ProcessedSidebar = ProcessedSidebarItem[];
142
+ export type ProcessedSidebars = {
143
+ [sidebarId: string]: ProcessedSidebar;
144
+ };
145
+
146
+ export type SidebarItemCategory = Expand<
147
+ SidebarItemCategoryBase & {
148
+ items: SidebarItem[];
149
+ link?: SidebarItemCategoryLink;
150
+ }
151
+ >;
152
+
153
+ export type SidebarItemCategoryWithLink = Required<SidebarItemCategory, 'link'>;
154
+
155
+ export type SidebarItemCategoryWithGeneratedIndex =
156
+ SidebarItemCategoryWithLink & {link: SidebarItemCategoryLinkGeneratedIndex};
157
+
158
+ export type SidebarItem =
159
+ | SidebarItemDoc
160
+ | SidebarItemHtml
161
+ | SidebarItemLink
162
+ | SidebarItemCategory;
163
+
164
+ // A sidebar item that is part of the previous/next ordered navigation
165
+ export type SidebarNavigationItem =
166
+ | SidebarItemDoc
167
+ | SidebarItemCategoryWithLink;
168
+
169
+ export type Sidebar = SidebarItem[];
170
+ export type SidebarItemType = SidebarItem['type'];
171
+ export type Sidebars = {
172
+ [sidebarId: string]: Sidebar;
173
+ };
174
+
175
+ // Doc links have been resolved to URLs, ready to be passed to the theme
176
+ export type PropSidebarItemCategory = Expand<
177
+ SidebarItemCategoryBase & {
178
+ items: PropSidebarItem[];
179
+ href?: string;
180
+ }
181
+ >;
182
+
183
+ export type PropSidebarItemLink = SidebarItemLink & {
184
+ docId?: string;
185
+ };
186
+
187
+ export type PropSidebarItemHtml = SidebarItemHtml;
188
+
189
+ export type PropSidebarItem =
190
+ | PropSidebarItemLink
191
+ | PropSidebarItemCategory
192
+ | PropSidebarItemHtml;
193
+ export type PropSidebar = PropSidebarItem[];
194
+ export type PropSidebars = {
195
+ [sidebarId: string]: PropSidebar;
196
+ };
197
+
198
+ export type PropSidebarBreadcrumbsItem =
199
+ | PropSidebarItemLink
200
+ | PropSidebarItemCategory;
201
+
202
+ export type CategoryMetadataFile = {
203
+ label?: string;
204
+ position?: number;
205
+ collapsed?: boolean;
206
+ collapsible?: boolean;
207
+ className?: string;
208
+ link?: SidebarItemCategoryLinkConfig | null;
209
+ customProps?: {[key: string]: unknown};
210
+
211
+ // TODO should we allow "items" here? how would this work? would an
212
+ // "autogenerated" type be allowed?
213
+ // This mkdocs plugin do something like that: https://github.com/lukasgeiter/mkdocs-awesome-pages-plugin/
214
+ // cf comment: https://github.com/facebook/docusaurus/issues/3464#issuecomment-784765199
215
+ };
216
+
217
+ // Reduce API surface for options.sidebarItemsGenerator
218
+ // The user-provided generator fn should receive only a subset of metadata
219
+ // A change to any of these metadata can be considered as a breaking change
220
+ export type SidebarItemsGeneratorDoc = Pick<
221
+ DocMetadataBase,
222
+ | 'id'
223
+ | 'unversionedId'
224
+ | 'title'
225
+ | 'frontMatter'
226
+ | 'source'
227
+ | 'sourceDirName'
228
+ | 'sidebarPosition'
229
+ >;
230
+ export type SidebarItemsGeneratorVersion = Pick<
231
+ VersionMetadata,
232
+ 'versionName' | 'contentPath'
233
+ >;
234
+
235
+ export type SidebarItemsGeneratorArgs = {
236
+ /** The sidebar item with type "autogenerated" to be transformed. */
237
+ item: SidebarItemAutogenerated;
238
+ /** Useful metadata for the version this sidebar belongs to. */
239
+ version: SidebarItemsGeneratorVersion;
240
+ /** All the docs of that version (unfiltered). */
241
+ docs: SidebarItemsGeneratorDoc[];
242
+ /** Number prefix parser configured for this plugin. */
243
+ numberPrefixParser: NumberPrefixParser;
244
+ /** The default category index matcher which you can override. */
245
+ isCategoryIndex: CategoryIndexMatcher;
246
+ /**
247
+ * Key is the path relative to the doc content directory, value is the
248
+ * category metadata file's content.
249
+ */
250
+ categoriesMetadata: {[filePath: string]: CategoryMetadataFile};
251
+ };
252
+ export type SidebarItemsGenerator = (
253
+ generatorArgs: SidebarItemsGeneratorArgs,
254
+ ) => NormalizedSidebar | Promise<NormalizedSidebar>;
255
+
256
+ export type SidebarItemsGeneratorOption = (
257
+ generatorArgs: {
258
+ /**
259
+ * Useful to re-use/enhance the default sidebar generation logic from
260
+ * Docusaurus.
261
+ * @see https://github.com/facebook/docusaurus/issues/4640#issuecomment-822292320
262
+ */
263
+ defaultSidebarItemsGenerator: SidebarItemsGenerator;
264
+ } & SidebarItemsGeneratorArgs,
265
+ ) => NormalizedSidebar | Promise<NormalizedSidebar>;
266
+
267
+ export type SidebarProcessorParams = {
268
+ sidebarItemsGenerator: SidebarItemsGeneratorOption;
269
+ numberPrefixParser: NumberPrefixParser;
270
+ docs: DocMetadataBase[];
271
+ drafts: DocMetadataBase[];
272
+ version: VersionMetadata;
273
+ categoryLabelSlugger: Slugger;
274
+ sidebarOptions: SidebarOptions;
275
+ };