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

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 (214) hide show
  1. package/lib/categoryGeneratedIndex.d.ts +12 -0
  2. package/lib/categoryGeneratedIndex.js +35 -0
  3. package/lib/cli.d.ts +2 -2
  4. package/lib/cli.js +37 -51
  5. package/lib/client/docsClientUtils.d.ts +6 -26
  6. package/lib/client/docsClientUtils.js +28 -34
  7. package/lib/{theme/hooks/useDocs.d.ts → client/index.d.ts} +4 -3
  8. package/lib/{theme/hooks/useDocs.js → client/index.js} +28 -25
  9. package/lib/docs.d.ts +31 -4
  10. package/lib/docs.js +160 -54
  11. package/{src/__tests__/__fixtures__/simple-site/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
  12. package/lib/{docFrontMatter.js → frontMatter.js} +13 -6
  13. package/lib/globalData.d.ts +2 -2
  14. package/lib/globalData.js +32 -3
  15. package/lib/index.d.ts +4 -3
  16. package/lib/index.js +123 -136
  17. package/lib/lastUpdate.d.ts +4 -6
  18. package/lib/lastUpdate.js +22 -26
  19. package/lib/markdown/index.d.ts +3 -6
  20. package/lib/markdown/index.js +3 -3
  21. package/lib/markdown/linkify.d.ts +1 -1
  22. package/lib/markdown/linkify.js +7 -3
  23. package/lib/numberPrefix.d.ts +1 -1
  24. package/lib/numberPrefix.js +16 -21
  25. package/lib/options.d.ts +3 -5
  26. package/lib/options.js +55 -19
  27. package/lib/props.d.ts +7 -2
  28. package/lib/props.js +70 -14
  29. package/lib/routes.d.ts +28 -0
  30. package/lib/routes.js +110 -0
  31. package/lib/server-export.d.ts +8 -0
  32. package/lib/server-export.js +23 -0
  33. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +1 -6
  34. package/lib/sidebars/generator.js +209 -0
  35. package/lib/sidebars/index.d.ts +13 -0
  36. package/lib/sidebars/index.js +94 -0
  37. package/lib/sidebars/normalization.d.ts +13 -0
  38. package/lib/sidebars/normalization.js +55 -0
  39. package/lib/sidebars/postProcessor.d.ts +8 -0
  40. package/lib/sidebars/postProcessor.js +65 -0
  41. package/lib/sidebars/processor.d.ts +10 -0
  42. package/lib/sidebars/processor.js +79 -0
  43. package/lib/sidebars/types.d.ts +174 -0
  44. package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js → lib/sidebars/types.js} +2 -10
  45. package/lib/sidebars/utils.d.ts +54 -0
  46. package/lib/sidebars/utils.js +255 -0
  47. package/lib/sidebars/validation.d.ts +11 -0
  48. package/lib/sidebars/validation.js +138 -0
  49. package/lib/slug.d.ts +6 -4
  50. package/lib/slug.js +29 -19
  51. package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js → lib/tags.d.ts} +2 -8
  52. package/lib/tags.js +21 -0
  53. package/lib/translations.d.ts +2 -2
  54. package/lib/translations.js +89 -49
  55. package/lib/types.d.ts +60 -130
  56. package/lib/versions.d.ts +29 -4
  57. package/lib/versions.js +134 -97
  58. package/package.json +30 -26
  59. package/src/categoryGeneratedIndex.ts +55 -0
  60. package/src/cli.ts +47 -63
  61. package/src/client/docsClientUtils.ts +38 -73
  62. package/src/{theme/hooks/useDocs.ts → client/index.ts} +16 -11
  63. package/{types.d.ts → src/deps.d.ts} +1 -1
  64. package/src/docs.ts +212 -46
  65. package/src/{docFrontMatter.ts → frontMatter.ts} +21 -26
  66. package/src/globalData.ts +53 -3
  67. package/src/index.ts +168 -178
  68. package/src/lastUpdate.ts +26 -33
  69. package/src/markdown/index.ts +10 -16
  70. package/src/markdown/linkify.ts +6 -2
  71. package/src/numberPrefix.ts +19 -26
  72. package/src/options.ts +60 -32
  73. package/src/plugin-content-docs.d.ts +263 -40
  74. package/src/props.ts +105 -21
  75. package/src/routes.ts +185 -0
  76. package/src/server-export.ts +24 -0
  77. package/src/sidebars/README.md +9 -0
  78. package/src/sidebars/generator.ts +292 -0
  79. package/src/sidebars/index.ts +120 -0
  80. package/src/sidebars/normalization.ts +85 -0
  81. package/src/sidebars/postProcessor.ts +89 -0
  82. package/src/sidebars/processor.ts +120 -0
  83. package/src/sidebars/types.ts +274 -0
  84. package/src/sidebars/utils.ts +388 -0
  85. package/src/sidebars/validation.ts +174 -0
  86. package/src/slug.ts +40 -23
  87. package/src/tags.ts +19 -0
  88. package/src/translations.ts +124 -66
  89. package/src/types.ts +67 -187
  90. package/src/versions.ts +205 -110
  91. package/lib/.tsbuildinfo +0 -4717
  92. package/lib/docFrontMatter.d.ts +0 -21
  93. package/lib/sidebarItemsGenerator.js +0 -211
  94. package/lib/sidebars.d.ts +0 -42
  95. package/lib/sidebars.js +0 -309
  96. package/src/__tests__/__fixtures__/bad-id-site/docs/invalid-id.md +0 -5
  97. package/src/__tests__/__fixtures__/bad-slug-on-doc-home-site/docs/docWithSlug.md +0 -5
  98. package/src/__tests__/__fixtures__/empty-site/docusaurus.config.js +0 -16
  99. package/src/__tests__/__fixtures__/empty-site/sidebars.json +0 -1
  100. package/src/__tests__/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -34
  101. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -11
  102. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -11
  103. package/src/__tests__/__fixtures__/sidebars/sidebars-category.js +0 -44
  104. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -20
  105. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed.json +0 -21
  106. package/src/__tests__/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -10
  107. package/src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -20
  108. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -11
  109. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -11
  110. package/src/__tests__/__fixtures__/sidebars/sidebars-link.json +0 -11
  111. package/src/__tests__/__fixtures__/sidebars/sidebars-unknown-type.json +0 -14
  112. package/src/__tests__/__fixtures__/sidebars/sidebars-wrong-field.json +0 -20
  113. package/src/__tests__/__fixtures__/sidebars/sidebars.json +0 -20
  114. package/src/__tests__/__fixtures__/simple-site/docs/foo/bar.md +0 -69
  115. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +0 -67
  116. package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
  117. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -52
  118. package/src/__tests__/__fixtures__/simple-site/docs/ipsum.md +0 -5
  119. package/src/__tests__/__fixtures__/simple-site/docs/lorem.md +0 -6
  120. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +0 -5
  121. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +0 -5
  122. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +0 -5
  123. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +0 -5
  124. package/src/__tests__/__fixtures__/simple-site/docs/slugs/absoluteSlug.md +0 -5
  125. package/src/__tests__/__fixtures__/simple-site/docs/slugs/relativeSlug.md +0 -5
  126. package/src/__tests__/__fixtures__/simple-site/docs/slugs/resolvedSlug.md +0 -5
  127. package/src/__tests__/__fixtures__/simple-site/docs/slugs/tryToEscapeSlug.md +0 -5
  128. package/src/__tests__/__fixtures__/simple-site/sidebars.json +0 -23
  129. package/src/__tests__/__fixtures__/simple-site/wrong-sidebars.json +0 -7
  130. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/0-getting-started.md +0 -3
  131. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/1-installation.md +0 -3
  132. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/00_api-overview.md +0 -3
  133. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/0 --- Client API.md +0 -1
  134. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/1 --- Server API.md +0 -1
  135. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/0. Plugin API.md +0 -1
  136. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/1. Theme API.md +0 -1
  137. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/_category_.yml +0 -1
  138. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/03_api-end.md +0 -3
  139. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/_category_.json +0 -3
  140. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/0-guide2.5.md +0 -8
  141. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/02-guide2.md +0 -7
  142. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/_category_.json +0 -3
  143. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/a-guide4.md +0 -7
  144. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/b-guide5.md +0 -7
  145. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/guide3.md +0 -8
  146. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/z-guide1.md +0 -8
  147. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars.js +0 -23
  148. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +0 -7
  149. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-2.md +0 -8
  150. package/src/__tests__/__fixtures__/site-with-doc-label/docusaurus.config.js +0 -14
  151. package/src/__tests__/__fixtures__/site-with-doc-label/sidebars.json +0 -14
  152. package/src/__tests__/__fixtures__/versioned-site/community/team.md +0 -1
  153. package/src/__tests__/__fixtures__/versioned-site/community_sidebars.json +0 -3
  154. package/src/__tests__/__fixtures__/versioned-site/community_versioned_docs/version-1.0.0/team.md +0 -1
  155. package/src/__tests__/__fixtures__/versioned-site/community_versioned_sidebars/version-1.0.0-sidebars.json +0 -3
  156. package/src/__tests__/__fixtures__/versioned-site/community_versions.json +0 -1
  157. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +0 -4
  158. package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +0 -1
  159. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/absoluteSlug.md +0 -5
  160. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/relativeSlug.md +0 -5
  161. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/resolvedSlug.md +0 -5
  162. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/tryToEscapeSlug.md +0 -5
  163. package/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js +0 -18
  164. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  165. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md +0 -5
  166. package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  167. package/src/__tests__/__fixtures__/versioned-site/sidebars.json +0 -10
  168. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/bar.md +0 -4
  169. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/baz.md +0 -1
  170. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +0 -1
  171. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/foo/bar.md +0 -1
  172. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +0 -1
  173. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootAbsoluteSlug.md +0 -5
  174. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootRelativeSlug.md +0 -5
  175. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootResolvedSlug.md +0 -5
  176. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootTryToEscapeSlug.md +0 -5
  177. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/absoluteSlug.md +0 -5
  178. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/relativeSlug.md +0 -5
  179. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/resolvedSlug.md +0 -5
  180. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/tryToEscapeSlug.md +0 -5
  181. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.0-sidebars.json +0 -11
  182. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +0 -10
  183. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-withSlugs-sidebars.json +0 -5
  184. package/src/__tests__/__fixtures__/versioned-site/versions.json +0 -5
  185. package/src/__tests__/__snapshots__/cli.test.ts.snap +0 -90
  186. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -1907
  187. package/src/__tests__/__snapshots__/sidebars.test.ts.snap +0 -218
  188. package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -487
  189. package/src/__tests__/cli.test.ts +0 -333
  190. package/src/__tests__/docFrontMatter.test.ts +0 -204
  191. package/src/__tests__/docs.test.ts +0 -875
  192. package/src/__tests__/index.test.ts +0 -1831
  193. package/src/__tests__/lastUpdate.test.ts +0 -68
  194. package/src/__tests__/numberPrefix.test.ts +0 -199
  195. package/src/__tests__/options.test.ts +0 -232
  196. package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
  197. package/src/__tests__/sidebars.test.ts +0 -638
  198. package/src/__tests__/slug.test.ts +0 -109
  199. package/src/__tests__/translations.test.ts +0 -159
  200. package/src/__tests__/versions.test.ts +0 -718
  201. package/src/client/__tests__/docsClientUtils.test.ts +0 -372
  202. package/src/markdown/__tests__/__fixtures__/docs/doc-localized.md +0 -1
  203. package/src/markdown/__tests__/__fixtures__/docs/doc1.md +0 -13
  204. package/src/markdown/__tests__/__fixtures__/docs/doc2.md +0 -12
  205. package/src/markdown/__tests__/__fixtures__/docs/doc4.md +0 -19
  206. package/src/markdown/__tests__/__fixtures__/docs/doc5.md +0 -6
  207. package/src/markdown/__tests__/__fixtures__/docs/subdir/doc3.md +0 -3
  208. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/doc2.md +0 -7
  209. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/subdir/doc1.md +0 -3
  210. package/src/markdown/__tests__/__snapshots__/linkify.test.ts.snap +0 -82
  211. package/src/markdown/__tests__/linkify.test.ts +0 -190
  212. package/src/sidebarItemsGenerator.ts +0 -307
  213. package/src/sidebars.ts +0 -489
  214. package/tsconfig.json +0 -9
@@ -0,0 +1,120 @@
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
+ // use this before loadSidebars()
36
+ export function resolveSidebarPathOption(
37
+ siteDir: string,
38
+ sidebarPathOption: PluginOptions['sidebarPath'],
39
+ ): PluginOptions['sidebarPath'] {
40
+ return sidebarPathOption
41
+ ? path.resolve(siteDir, sidebarPathOption)
42
+ : sidebarPathOption;
43
+ }
44
+
45
+ async function readCategoriesMetadata(contentPath: string) {
46
+ const categoryFiles = await Globby('**/_category_.{json,yml,yaml}', {
47
+ cwd: contentPath,
48
+ });
49
+ const categoryToFile = _.groupBy(categoryFiles, path.dirname);
50
+ return combinePromises(
51
+ _.mapValues(categoryToFile, async (files, folder) => {
52
+ const filePath = files[0]!;
53
+ if (files.length > 1) {
54
+ logger.warn`There are more than one category metadata files for path=${folder}: ${files.join(
55
+ ', ',
56
+ )}. The behavior is undetermined.`;
57
+ }
58
+ const content = await fs.readFile(
59
+ path.join(contentPath, filePath),
60
+ 'utf-8',
61
+ );
62
+ try {
63
+ return validateCategoryMetadataFile(Yaml.load(content));
64
+ } catch (err) {
65
+ logger.error`The docs sidebar category metadata file path=${filePath} looks invalid!`;
66
+ throw err;
67
+ }
68
+ }),
69
+ );
70
+ }
71
+
72
+ export async function loadSidebarsFileUnsafe(
73
+ sidebarFilePath: string | false | undefined,
74
+ ): Promise<SidebarsConfig> {
75
+ // false => no sidebars
76
+ if (sidebarFilePath === false) {
77
+ return DisabledSidebars;
78
+ }
79
+
80
+ // undefined => defaults to autogenerated sidebars
81
+ if (typeof sidebarFilePath === 'undefined') {
82
+ return DefaultSidebars;
83
+ }
84
+
85
+ // Non-existent sidebars file: no sidebars
86
+ // Note: this edge case can happen on versioned docs, not current version
87
+ // We avoid creating empty versioned sidebars file with the CLI
88
+ if (!(await fs.pathExists(sidebarFilePath))) {
89
+ return DisabledSidebars;
90
+ }
91
+
92
+ // We don't want sidebars to be cached because of hot reloading.
93
+ return importFresh(sidebarFilePath);
94
+ }
95
+
96
+ // Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
97
+ export async function loadSidebars(
98
+ sidebarFilePath: string | false | undefined,
99
+ options: SidebarProcessorParams,
100
+ ): Promise<Sidebars> {
101
+ try {
102
+ const sidebarsConfig = await loadSidebarsFileUnsafe(sidebarFilePath);
103
+ const normalizedSidebars = normalizeSidebars(sidebarsConfig);
104
+ validateSidebars(normalizedSidebars);
105
+ const categoriesMetadata = await readCategoriesMetadata(
106
+ options.version.contentPath,
107
+ );
108
+ const processedSidebars = await processSidebars(
109
+ normalizedSidebars,
110
+ categoriesMetadata,
111
+ options,
112
+ );
113
+ return postProcessSidebars(processedSidebars, options);
114
+ } catch (err) {
115
+ logger.error`Sidebars file at path=${
116
+ sidebarFilePath as string
117
+ } failed to be loaded.`;
118
+ throw err;
119
+ }
120
+ }
@@ -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 path=${'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.versionPath, 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,120 @@
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 {DocMetadataBase, VersionMetadata} from '../types';
9
+ import type {
10
+ NormalizedSidebarItem,
11
+ NormalizedSidebar,
12
+ NormalizedSidebars,
13
+ SidebarItemsGeneratorDoc,
14
+ SidebarItemsGeneratorVersion,
15
+ SidebarItemAutogenerated,
16
+ ProcessedSidebarItem,
17
+ ProcessedSidebar,
18
+ ProcessedSidebars,
19
+ SidebarProcessorParams,
20
+ CategoryMetadataFile,
21
+ } from './types';
22
+ import {DefaultSidebarItemsGenerator} from './generator';
23
+ import {validateSidebars} from './validation';
24
+ import _ from 'lodash';
25
+ import combinePromises from 'combine-promises';
26
+ import {isCategoryIndex} from '../docs';
27
+
28
+ function toSidebarItemsGeneratorDoc(
29
+ doc: DocMetadataBase,
30
+ ): SidebarItemsGeneratorDoc {
31
+ return _.pick(doc, [
32
+ 'id',
33
+ 'unversionedId',
34
+ 'title',
35
+ 'frontMatter',
36
+ 'source',
37
+ 'sourceDirName',
38
+ 'sidebarPosition',
39
+ ]);
40
+ }
41
+
42
+ function toSidebarItemsGeneratorVersion(
43
+ version: VersionMetadata,
44
+ ): SidebarItemsGeneratorVersion {
45
+ return _.pick(version, ['versionName', 'contentPath']);
46
+ }
47
+
48
+ // Handle the generation of autogenerated sidebar items and other
49
+ // post-processing checks
50
+ async function processSidebar(
51
+ unprocessedSidebar: NormalizedSidebar,
52
+ categoriesMetadata: {[filePath: string]: CategoryMetadataFile},
53
+ params: SidebarProcessorParams,
54
+ ): Promise<ProcessedSidebar> {
55
+ const {sidebarItemsGenerator, numberPrefixParser, docs, version} = params;
56
+
57
+ // Just a minor lazy transformation optimization
58
+ const getSidebarItemsGeneratorDocsAndVersion = _.memoize(() => ({
59
+ docs: docs.map(toSidebarItemsGeneratorDoc),
60
+ version: toSidebarItemsGeneratorVersion(version),
61
+ }));
62
+
63
+ async function processAutoGeneratedItem(
64
+ item: SidebarItemAutogenerated,
65
+ ): Promise<ProcessedSidebarItem[]> {
66
+ const generatedItems = await sidebarItemsGenerator({
67
+ item,
68
+ numberPrefixParser,
69
+ defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
70
+ isCategoryIndex,
71
+ ...getSidebarItemsGeneratorDocsAndVersion(),
72
+ categoriesMetadata,
73
+ });
74
+ // Process again... weird but sidebar item generated might generate some
75
+ // auto-generated items?
76
+ // TODO repeatedly process & unwrap autogenerated items until there are no
77
+ // more autogenerated items, or when loop count (e.g. 10) is reached
78
+ return processItems(generatedItems);
79
+ }
80
+
81
+ async function processItem(
82
+ item: NormalizedSidebarItem,
83
+ ): Promise<ProcessedSidebarItem[]> {
84
+ if (item.type === 'category') {
85
+ return [
86
+ {
87
+ ...item,
88
+ items: (await Promise.all(item.items.map(processItem))).flat(),
89
+ },
90
+ ];
91
+ }
92
+ if (item.type === 'autogenerated') {
93
+ return processAutoGeneratedItem(item);
94
+ }
95
+ return [item];
96
+ }
97
+
98
+ async function processItems(
99
+ items: NormalizedSidebarItem[],
100
+ ): Promise<ProcessedSidebarItem[]> {
101
+ return (await Promise.all(items.map(processItem))).flat();
102
+ }
103
+
104
+ const processedSidebar = await processItems(unprocessedSidebar);
105
+ return processedSidebar;
106
+ }
107
+
108
+ export async function processSidebars(
109
+ unprocessedSidebars: NormalizedSidebars,
110
+ categoriesMetadata: {[filePath: string]: CategoryMetadataFile},
111
+ params: SidebarProcessorParams,
112
+ ): Promise<ProcessedSidebars> {
113
+ const processedSidebars = await combinePromises(
114
+ _.mapValues(unprocessedSidebars, (unprocessedSidebar) =>
115
+ processSidebar(unprocessedSidebar, categoriesMetadata, params),
116
+ ),
117
+ );
118
+ validateSidebars(processedSidebars);
119
+ return processedSidebars;
120
+ }
@@ -0,0 +1,274 @@
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 {DocMetadataBase, VersionMetadata} from '../types';
10
+ import type {
11
+ NumberPrefixParser,
12
+ SidebarOptions,
13
+ CategoryIndexMatcher,
14
+ } from '@docusaurus/plugin-content-docs';
15
+ import type {Slugger} from '@docusaurus/utils';
16
+
17
+ // Makes all properties visible when hovering over the type
18
+ type Expand<T extends {[x: string]: unknown}> = {[P in keyof T]: T[P]};
19
+
20
+ export type SidebarItemBase = {
21
+ className?: string;
22
+ customProps?: {[key: string]: unknown};
23
+ };
24
+
25
+ export type SidebarItemDoc = SidebarItemBase & {
26
+ type: 'doc' | 'ref';
27
+ label?: string;
28
+ id: string;
29
+ };
30
+
31
+ export type SidebarItemHtml = SidebarItemBase & {
32
+ type: 'html';
33
+ value: string;
34
+ defaultStyle?: boolean;
35
+ };
36
+
37
+ export type SidebarItemLink = SidebarItemBase & {
38
+ type: 'link';
39
+ href: string;
40
+ label: string;
41
+ };
42
+
43
+ export type SidebarItemAutogenerated = SidebarItemBase & {
44
+ type: 'autogenerated';
45
+ dirName: string;
46
+ };
47
+
48
+ type SidebarItemCategoryBase = SidebarItemBase & {
49
+ type: 'category';
50
+ label: string;
51
+ collapsed: boolean;
52
+ collapsible: boolean;
53
+ };
54
+
55
+ export type SidebarItemCategoryLinkDoc = {type: 'doc'; id: string};
56
+
57
+ export type SidebarItemCategoryLinkGeneratedIndexConfig = {
58
+ type: 'generated-index';
59
+ slug?: string;
60
+ title?: string;
61
+ description?: string;
62
+ image?: string;
63
+ keywords?: string | readonly string[];
64
+ };
65
+ export type SidebarItemCategoryLinkGeneratedIndex = {
66
+ type: 'generated-index';
67
+ slug: string;
68
+ permalink: string;
69
+ title?: string;
70
+ description?: string;
71
+ image?: string;
72
+ keywords?: string | readonly string[];
73
+ };
74
+
75
+ export type SidebarItemCategoryLinkConfig =
76
+ | SidebarItemCategoryLinkDoc
77
+ | SidebarItemCategoryLinkGeneratedIndexConfig;
78
+
79
+ export type SidebarItemCategoryLink =
80
+ | SidebarItemCategoryLinkDoc
81
+ | SidebarItemCategoryLinkGeneratedIndex;
82
+
83
+ // The user-given configuration in sidebars.js, before normalization
84
+ export type SidebarItemCategoryConfig = Expand<
85
+ Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
86
+ items: SidebarCategoriesShorthand | SidebarItemConfig[];
87
+ link?: SidebarItemCategoryLinkConfig;
88
+ }
89
+ >;
90
+
91
+ export type SidebarCategoriesShorthand = {
92
+ [sidebarCategory: string]: SidebarCategoriesShorthand | SidebarItemConfig[];
93
+ };
94
+
95
+ export type SidebarItemConfig =
96
+ | SidebarItemDoc
97
+ | SidebarItemHtml
98
+ | SidebarItemLink
99
+ | SidebarItemAutogenerated
100
+ | SidebarItemCategoryConfig
101
+ | string
102
+ | SidebarCategoriesShorthand;
103
+
104
+ export type SidebarConfig = SidebarCategoriesShorthand | SidebarItemConfig[];
105
+ export type SidebarsConfig = {
106
+ [sidebarId: string]: SidebarConfig;
107
+ };
108
+
109
+ // Normalized but still has 'autogenerated', which will be handled in processing
110
+ export type NormalizedSidebarItemCategory = Expand<
111
+ Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
112
+ items: NormalizedSidebarItem[];
113
+ link?: SidebarItemCategoryLinkConfig;
114
+ }
115
+ >;
116
+
117
+ export type NormalizedSidebarItem =
118
+ | SidebarItemDoc
119
+ | SidebarItemHtml
120
+ | SidebarItemLink
121
+ | NormalizedSidebarItemCategory
122
+ | SidebarItemAutogenerated;
123
+
124
+ export type NormalizedSidebar = NormalizedSidebarItem[];
125
+ export type NormalizedSidebars = {
126
+ [sidebarId: string]: NormalizedSidebar;
127
+ };
128
+
129
+ export type ProcessedSidebarItemCategory = Expand<
130
+ Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
131
+ items: ProcessedSidebarItem[];
132
+ link?: SidebarItemCategoryLinkConfig;
133
+ }
134
+ >;
135
+ export type ProcessedSidebarItem =
136
+ | SidebarItemDoc
137
+ | SidebarItemHtml
138
+ | SidebarItemLink
139
+ | ProcessedSidebarItemCategory;
140
+ export type ProcessedSidebar = ProcessedSidebarItem[];
141
+ export type ProcessedSidebars = {
142
+ [sidebarId: string]: ProcessedSidebar;
143
+ };
144
+
145
+ export type SidebarItemCategory = Expand<
146
+ SidebarItemCategoryBase & {
147
+ items: SidebarItem[];
148
+ link?: SidebarItemCategoryLink;
149
+ }
150
+ >;
151
+
152
+ export type SidebarItemCategoryWithLink = Required<SidebarItemCategory, 'link'>;
153
+
154
+ export type SidebarItemCategoryWithGeneratedIndex =
155
+ SidebarItemCategoryWithLink & {link: SidebarItemCategoryLinkGeneratedIndex};
156
+
157
+ export type SidebarItem =
158
+ | SidebarItemDoc
159
+ | SidebarItemHtml
160
+ | SidebarItemLink
161
+ | SidebarItemCategory;
162
+
163
+ // A sidebar item that is part of the previous/next ordered navigation
164
+ export type SidebarNavigationItem =
165
+ | SidebarItemDoc
166
+ | SidebarItemCategoryWithLink;
167
+
168
+ export type Sidebar = SidebarItem[];
169
+ export type SidebarItemType = SidebarItem['type'];
170
+ export type Sidebars = {
171
+ [sidebarId: string]: Sidebar;
172
+ };
173
+
174
+ // Doc links have been resolved to URLs, ready to be passed to the theme
175
+ export type PropSidebarItemCategory = Expand<
176
+ SidebarItemCategoryBase & {
177
+ items: PropSidebarItem[];
178
+ href?: string;
179
+ }
180
+ >;
181
+
182
+ // we may want to use a union type in props instead of this generic link?
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 PropVersionDoc = {
203
+ id: string;
204
+ title: string;
205
+ description?: string;
206
+ sidebar?: string;
207
+ };
208
+ export type PropVersionDocs = {
209
+ [docId: string]: PropVersionDoc;
210
+ };
211
+
212
+ export type CategoryMetadataFile = {
213
+ label?: string;
214
+ position?: number;
215
+ collapsed?: boolean;
216
+ collapsible?: boolean;
217
+ className?: string;
218
+ link?: SidebarItemCategoryLinkConfig | null;
219
+ customProps?: {[key: string]: unknown};
220
+
221
+ // TODO should we allow "items" here? how would this work? would an
222
+ // "autogenerated" type be allowed?
223
+ // This mkdocs plugin do something like that: https://github.com/lukasgeiter/mkdocs-awesome-pages-plugin/
224
+ // cf comment: https://github.com/facebook/docusaurus/issues/3464#issuecomment-784765199
225
+ };
226
+
227
+ // Reduce API surface for options.sidebarItemsGenerator
228
+ // The user-provided generator fn should receive only a subset of metadata
229
+ // A change to any of these metadata can be considered as a breaking change
230
+ export type SidebarItemsGeneratorDoc = Pick<
231
+ DocMetadataBase,
232
+ | 'id'
233
+ | 'unversionedId'
234
+ | 'title'
235
+ | 'frontMatter'
236
+ | 'source'
237
+ | 'sourceDirName'
238
+ | 'sidebarPosition'
239
+ >;
240
+ export type SidebarItemsGeneratorVersion = Pick<
241
+ VersionMetadata,
242
+ 'versionName' | 'contentPath'
243
+ >;
244
+
245
+ export type SidebarItemsGeneratorArgs = {
246
+ item: SidebarItemAutogenerated;
247
+ version: SidebarItemsGeneratorVersion;
248
+ docs: SidebarItemsGeneratorDoc[];
249
+ numberPrefixParser: NumberPrefixParser;
250
+ isCategoryIndex: CategoryIndexMatcher;
251
+ categoriesMetadata: {[filePath: string]: CategoryMetadataFile};
252
+ };
253
+ export type SidebarItemsGenerator = (
254
+ generatorArgs: SidebarItemsGeneratorArgs,
255
+ ) => Promise<NormalizedSidebar>;
256
+
257
+ // Also inject the default generator to conveniently wrap/enhance/sort the
258
+ // default sidebar gen logic
259
+ // see https://github.com/facebook/docusaurus/issues/4640#issuecomment-822292320
260
+ export type SidebarItemsGeneratorOptionArgs = {
261
+ defaultSidebarItemsGenerator: SidebarItemsGenerator;
262
+ } & SidebarItemsGeneratorArgs;
263
+ export type SidebarItemsGeneratorOption = (
264
+ generatorArgs: SidebarItemsGeneratorOptionArgs,
265
+ ) => Promise<NormalizedSidebarItem[]>;
266
+
267
+ export type SidebarProcessorParams = {
268
+ sidebarItemsGenerator: SidebarItemsGeneratorOption;
269
+ numberPrefixParser: NumberPrefixParser;
270
+ docs: DocMetadataBase[];
271
+ version: VersionMetadata;
272
+ categoryLabelSlugger: Slugger;
273
+ sidebarOptions: SidebarOptions;
274
+ };