@docusaurus/plugin-content-docs 2.0.0-beta.1decd6f80 → 2.0.0-beta.20

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 (230) 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 +65 -69
  5. package/lib/client/docsClientUtils.d.ts +9 -28
  6. package/lib/client/docsClientUtils.js +34 -42
  7. package/lib/client/index.d.ts +22 -0
  8. package/lib/client/index.js +72 -0
  9. package/lib/constants.d.ts +4 -0
  10. package/lib/constants.js +4 -1
  11. package/lib/docs.d.ts +33 -4
  12. package/lib/docs.js +173 -62
  13. package/{src/__tests__/__fixtures__/site-with-doc-label/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
  14. package/lib/{docFrontMatter.js → frontMatter.js} +16 -6
  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 +135 -140
  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 +7 -4
  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 +55 -19
  29. package/lib/props.d.ts +7 -2
  30. package/lib/props.js +72 -15
  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 +8 -0
  42. package/lib/sidebars/postProcessor.js +65 -0
  43. package/lib/sidebars/processor.d.ts +10 -0
  44. package/lib/sidebars/processor.js +90 -0
  45. package/lib/sidebars/types.d.ts +178 -0
  46. package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js → lib/sidebars/types.js} +2 -10
  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 +138 -0
  51. package/lib/slug.d.ts +5 -4
  52. package/lib/slug.js +29 -19
  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 +84 -94
  57. package/lib/types.d.ts +13 -171
  58. package/lib/versions/files.d.ts +44 -0
  59. package/lib/versions/files.js +142 -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 +32 -26
  65. package/src/categoryGeneratedIndex.ts +60 -0
  66. package/src/cli.ts +90 -109
  67. package/src/client/docsClientUtils.ts +47 -84
  68. package/src/client/index.ts +103 -0
  69. package/src/constants.ts +4 -2
  70. package/{types.d.ts → src/deps.d.ts} +1 -1
  71. package/src/docs.ts +244 -63
  72. package/src/frontMatter.ts +51 -0
  73. package/src/globalData.ts +57 -7
  74. package/src/index.ts +186 -190
  75. package/src/lastUpdate.ts +26 -33
  76. package/src/markdown/index.ts +10 -16
  77. package/src/markdown/linkify.ts +8 -5
  78. package/src/numberPrefix.ts +19 -26
  79. package/src/options.ts +60 -32
  80. package/src/plugin-content-docs.d.ts +632 -75
  81. package/src/props.ts +109 -22
  82. package/src/routes.ts +159 -0
  83. package/src/server-export.ts +22 -0
  84. package/src/sidebars/README.md +9 -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 +89 -0
  89. package/src/sidebars/processor.ts +139 -0
  90. package/src/sidebars/types.ts +275 -0
  91. package/src/sidebars/utils.ts +391 -0
  92. package/src/sidebars/validation.ts +174 -0
  93. package/src/slug.ts +42 -23
  94. package/src/tags.ts +20 -0
  95. package/src/translations.ts +124 -117
  96. package/src/types.ts +18 -231
  97. package/src/versions/files.ts +220 -0
  98. package/src/versions/index.ts +247 -0
  99. package/src/versions/validation.ts +113 -0
  100. package/lib/.tsbuildinfo +0 -4673
  101. package/lib/docFrontMatter.d.ts +0 -21
  102. package/lib/sidebarItemsGenerator.js +0 -211
  103. package/lib/sidebars.d.ts +0 -42
  104. package/lib/sidebars.js +0 -309
  105. package/lib/theme/hooks/useDocs.d.ts +0 -20
  106. package/lib/theme/hooks/useDocs.js +0 -72
  107. package/lib/versions.d.ts +0 -16
  108. package/lib/versions.js +0 -287
  109. package/src/__tests__/__fixtures__/bad-id-site/docs/invalid-id.md +0 -5
  110. package/src/__tests__/__fixtures__/bad-slug-on-doc-home-site/docs/docWithSlug.md +0 -5
  111. package/src/__tests__/__fixtures__/empty-site/docusaurus.config.js +0 -16
  112. package/src/__tests__/__fixtures__/empty-site/sidebars.json +0 -1
  113. package/src/__tests__/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -34
  114. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -11
  115. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -11
  116. package/src/__tests__/__fixtures__/sidebars/sidebars-category.js +0 -44
  117. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -20
  118. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed.json +0 -21
  119. package/src/__tests__/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -10
  120. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -11
  121. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -11
  122. package/src/__tests__/__fixtures__/sidebars/sidebars-link.json +0 -11
  123. package/src/__tests__/__fixtures__/sidebars/sidebars-unknown-type.json +0 -14
  124. package/src/__tests__/__fixtures__/sidebars/sidebars-wrong-field.json +0 -20
  125. package/src/__tests__/__fixtures__/sidebars/sidebars.json +0 -20
  126. package/src/__tests__/__fixtures__/simple-site/docs/foo/bar.md +0 -69
  127. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +0 -67
  128. package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
  129. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -52
  130. package/src/__tests__/__fixtures__/simple-site/docs/ipsum.md +0 -5
  131. package/src/__tests__/__fixtures__/simple-site/docs/lorem.md +0 -6
  132. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +0 -5
  133. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +0 -5
  134. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +0 -5
  135. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +0 -5
  136. package/src/__tests__/__fixtures__/simple-site/docs/slugs/absoluteSlug.md +0 -5
  137. package/src/__tests__/__fixtures__/simple-site/docs/slugs/relativeSlug.md +0 -5
  138. package/src/__tests__/__fixtures__/simple-site/docs/slugs/resolvedSlug.md +0 -5
  139. package/src/__tests__/__fixtures__/simple-site/docs/slugs/tryToEscapeSlug.md +0 -5
  140. package/src/__tests__/__fixtures__/simple-site/docusaurus.config.js +0 -14
  141. package/src/__tests__/__fixtures__/simple-site/sidebars.json +0 -23
  142. package/src/__tests__/__fixtures__/simple-site/wrong-sidebars.json +0 -7
  143. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/0-getting-started.md +0 -3
  144. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/1-installation.md +0 -3
  145. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/00_api-overview.md +0 -3
  146. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/0 --- Client API.md +0 -1
  147. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/1 --- Server API.md +0 -1
  148. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/0. Plugin API.md +0 -1
  149. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/1. Theme API.md +0 -1
  150. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/_category_.yml +0 -1
  151. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/03_api-end.md +0 -3
  152. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/_category_.json +0 -3
  153. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/0-guide2.5.md +0 -8
  154. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/02-guide2.md +0 -7
  155. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/_category_.json +0 -3
  156. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/a-guide4.md +0 -7
  157. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/b-guide5.md +0 -7
  158. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/guide3.md +0 -8
  159. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/z-guide1.md +0 -8
  160. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js +0 -14
  161. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars.js +0 -23
  162. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +0 -7
  163. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-2.md +0 -8
  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 -1907
  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 -204
  204. package/src/__tests__/docs.test.ts +0 -875
  205. package/src/__tests__/index.test.ts +0 -1831
  206. package/src/__tests__/lastUpdate.test.ts +0 -68
  207. package/src/__tests__/numberPrefix.test.ts +0 -199
  208. package/src/__tests__/options.test.ts +0 -232
  209. package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
  210. package/src/__tests__/sidebars.test.ts +0 -638
  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 -718
  214. package/src/client/__tests__/docsClientUtils.test.ts +0 -372
  215. package/src/docFrontMatter.ts +0 -53
  216. package/src/markdown/__tests__/__fixtures__/docs/doc-localized.md +0 -1
  217. package/src/markdown/__tests__/__fixtures__/docs/doc1.md +0 -13
  218. package/src/markdown/__tests__/__fixtures__/docs/doc2.md +0 -12
  219. package/src/markdown/__tests__/__fixtures__/docs/doc4.md +0 -19
  220. package/src/markdown/__tests__/__fixtures__/docs/doc5.md +0 -6
  221. package/src/markdown/__tests__/__fixtures__/docs/subdir/doc3.md +0 -3
  222. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/doc2.md +0 -7
  223. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/subdir/doc1.md +0 -3
  224. package/src/markdown/__tests__/__snapshots__/linkify.test.ts.snap +0 -82
  225. package/src/markdown/__tests__/linkify.test.ts +0 -190
  226. package/src/sidebarItemsGenerator.ts +0 -307
  227. package/src/sidebars.ts +0 -489
  228. package/src/theme/hooks/useDocs.ts +0 -99
  229. package/src/versions.ts +0 -511
  230. 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 importFresh from 'import-fresh';
10
+ import type {SidebarsConfig, Sidebars, SidebarProcessorParams} from './types';
11
+ import {validateSidebars, validateCategoryMetadataFile} from './validation';
12
+ import {normalizeSidebars} from './normalization';
13
+ import {processSidebars} from './processor';
14
+ import {postProcessSidebars} from './postProcessor';
15
+ import path from 'path';
16
+ import {Globby} from '@docusaurus/utils';
17
+ import logger from '@docusaurus/logger';
18
+ import type {PluginOptions} from '@docusaurus/plugin-content-docs';
19
+ import Yaml from 'js-yaml';
20
+ import _ from 'lodash';
21
+ import combinePromises from 'combine-promises';
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 type {
9
+ NormalizedSidebarItem,
10
+ NormalizedSidebar,
11
+ NormalizedSidebars,
12
+ SidebarCategoriesShorthand,
13
+ SidebarItemCategoryConfig,
14
+ SidebarItemConfig,
15
+ SidebarConfig,
16
+ SidebarsConfig,
17
+ NormalizedSidebarItemCategory,
18
+ } from './types';
19
+ import {isCategoriesShorthand} from './utils';
20
+ import _ from 'lodash';
21
+ import logger from '@docusaurus/logger';
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,89 @@
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 {normalizeUrl} from '@docusaurus/utils';
9
+ import type {
10
+ SidebarItem,
11
+ Sidebars,
12
+ SidebarProcessorParams,
13
+ ProcessedSidebarItemCategory,
14
+ ProcessedSidebarItem,
15
+ ProcessedSidebars,
16
+ SidebarItemCategoryLink,
17
+ } from './types';
18
+ import _ from 'lodash';
19
+
20
+ function normalizeCategoryLink(
21
+ category: ProcessedSidebarItemCategory,
22
+ params: SidebarProcessorParams,
23
+ ): SidebarItemCategoryLink | undefined {
24
+ if (category.link?.type === 'generated-index') {
25
+ // Default slug logic can be improved
26
+ const getDefaultSlug = () =>
27
+ `/category/${params.categoryLabelSlugger.slug(category.label)}`;
28
+ const slug = category.link.slug ?? getDefaultSlug();
29
+ const permalink = normalizeUrl([params.version.path, slug]);
30
+ return {
31
+ ...category.link,
32
+ slug,
33
+ permalink,
34
+ };
35
+ }
36
+ return category.link;
37
+ }
38
+
39
+ function postProcessSidebarItem(
40
+ item: ProcessedSidebarItem,
41
+ params: SidebarProcessorParams,
42
+ ): SidebarItem {
43
+ if (item.type === 'category') {
44
+ const category = {
45
+ ...item,
46
+ collapsed: item.collapsed ?? params.sidebarOptions.sidebarCollapsed,
47
+ collapsible: item.collapsible ?? params.sidebarOptions.sidebarCollapsible,
48
+ link: normalizeCategoryLink(item, params),
49
+ items: item.items.map((subItem) =>
50
+ postProcessSidebarItem(subItem, params),
51
+ ),
52
+ };
53
+ // If the current category doesn't have subitems, we render a normal link
54
+ // instead.
55
+ if (category.items.length === 0) {
56
+ if (!category.link) {
57
+ throw new Error(
58
+ `Sidebar category ${item.label} has neither any subitem nor a link. This makes this item not able to link to anything.`,
59
+ );
60
+ }
61
+ return category.link.type === 'doc'
62
+ ? {
63
+ type: 'doc',
64
+ label: category.label,
65
+ id: category.link.id,
66
+ }
67
+ : {
68
+ type: 'link',
69
+ label: category.label,
70
+ href: category.link.permalink,
71
+ };
72
+ }
73
+ // A non-collapsible category can't be collapsed!
74
+ if (category.collapsible === false) {
75
+ category.collapsed = false;
76
+ }
77
+ return category;
78
+ }
79
+ return item;
80
+ }
81
+
82
+ export function postProcessSidebars(
83
+ sidebars: ProcessedSidebars,
84
+ params: SidebarProcessorParams,
85
+ ): Sidebars {
86
+ return _.mapValues(sidebars, (sidebar) =>
87
+ sidebar.map((item) => postProcessSidebarItem(item, params)),
88
+ );
89
+ }
@@ -0,0 +1,139 @@
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 {
9
+ DocMetadataBase,
10
+ VersionMetadata,
11
+ } from '@docusaurus/plugin-content-docs';
12
+ import type {
13
+ NormalizedSidebarItem,
14
+ NormalizedSidebar,
15
+ NormalizedSidebars,
16
+ SidebarItemsGeneratorDoc,
17
+ SidebarItemsGeneratorVersion,
18
+ SidebarItemAutogenerated,
19
+ ProcessedSidebarItem,
20
+ ProcessedSidebar,
21
+ ProcessedSidebars,
22
+ SidebarProcessorParams,
23
+ CategoryMetadataFile,
24
+ } from './types';
25
+ import {DefaultSidebarItemsGenerator} from './generator';
26
+ import {validateSidebars} from './validation';
27
+ import _ from 'lodash';
28
+ import combinePromises from 'combine-promises';
29
+ import {getDocIds, isCategoryIndex} from '../docs';
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, drafts, version} =
59
+ params;
60
+
61
+ // Just a minor lazy transformation optimization
62
+ const getSidebarItemsGeneratorDocsAndVersion = _.memoize(() => ({
63
+ docs: docs.map(toSidebarItemsGeneratorDoc),
64
+ version: toSidebarItemsGeneratorVersion(version),
65
+ }));
66
+
67
+ async function processAutoGeneratedItem(
68
+ item: SidebarItemAutogenerated,
69
+ ): Promise<ProcessedSidebarItem[]> {
70
+ const generatedItems = await sidebarItemsGenerator({
71
+ item,
72
+ numberPrefixParser,
73
+ defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
74
+ isCategoryIndex,
75
+ ...getSidebarItemsGeneratorDocsAndVersion(),
76
+ categoriesMetadata,
77
+ });
78
+ // Process again... weird but sidebar item generated might generate some
79
+ // auto-generated items?
80
+ // TODO repeatedly process & unwrap autogenerated items until there are no
81
+ // more autogenerated items, or when loop count (e.g. 10) is reached
82
+ return processItems(generatedItems);
83
+ }
84
+
85
+ const draftIds = new Set(drafts.flatMap(getDocIds));
86
+
87
+ const isDraftItem = (item: NormalizedSidebarItem): boolean => {
88
+ if (item.type === 'doc' || item.type === 'ref') {
89
+ return draftIds.has(item.id);
90
+ }
91
+ // If a category only contains draft items, it should be filtered entirely.
92
+ if (item.type === 'category') {
93
+ return item.items.every(isDraftItem);
94
+ }
95
+ return false;
96
+ };
97
+
98
+ async function processItem(
99
+ item: NormalizedSidebarItem,
100
+ ): Promise<ProcessedSidebarItem[]> {
101
+ if (item.type === 'category') {
102
+ return [
103
+ {
104
+ ...item,
105
+ items: await processItems(item.items),
106
+ },
107
+ ];
108
+ }
109
+ if (item.type === 'autogenerated') {
110
+ return processAutoGeneratedItem(item);
111
+ }
112
+ return [item];
113
+ }
114
+
115
+ async function processItems(
116
+ items: NormalizedSidebarItem[],
117
+ ): Promise<ProcessedSidebarItem[]> {
118
+ return (
119
+ await Promise.all(items.filter((i) => !isDraftItem(i)).map(processItem))
120
+ ).flat();
121
+ }
122
+
123
+ const processedSidebar = await processItems(unprocessedSidebar);
124
+ return processedSidebar;
125
+ }
126
+
127
+ export async function processSidebars(
128
+ unprocessedSidebars: NormalizedSidebars,
129
+ categoriesMetadata: {[filePath: string]: CategoryMetadataFile},
130
+ params: SidebarProcessorParams,
131
+ ): Promise<ProcessedSidebars> {
132
+ const processedSidebars = await combinePromises(
133
+ _.mapValues(unprocessedSidebars, (unprocessedSidebar) =>
134
+ processSidebar(unprocessedSidebar, categoriesMetadata, params),
135
+ ),
136
+ );
137
+ validateSidebars(processedSidebars);
138
+ return processedSidebars;
139
+ }
@@ -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
+ ) => 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
+ ) => Promise<NormalizedSidebarItem[]>;
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
+ };