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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/cli.d.ts +2 -2
  3. package/lib/cli.js +20 -24
  4. package/lib/client/docsClientUtils.d.ts +1 -4
  5. package/lib/client/docsClientUtils.js +12 -16
  6. package/lib/docFrontMatter.js +7 -3
  7. package/lib/docs.d.ts +4 -2
  8. package/lib/docs.js +77 -23
  9. package/lib/index.js +88 -94
  10. package/lib/lastUpdate.js +8 -8
  11. package/lib/markdown/index.d.ts +3 -6
  12. package/lib/markdown/index.js +3 -3
  13. package/lib/markdown/linkify.js +2 -2
  14. package/lib/options.d.ts +1 -1
  15. package/lib/options.js +39 -11
  16. package/lib/props.d.ts +7 -2
  17. package/lib/props.js +27 -4
  18. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +3 -1
  19. package/lib/sidebars/generator.js +174 -0
  20. package/lib/sidebars/index.d.ts +14 -0
  21. package/lib/sidebars/index.js +64 -0
  22. package/lib/sidebars/normalization.d.ts +9 -0
  23. package/lib/sidebars/normalization.js +58 -0
  24. package/lib/sidebars/processor.d.ts +16 -0
  25. package/lib/sidebars/processor.js +70 -0
  26. package/lib/sidebars/types.d.ts +87 -0
  27. package/lib/sidebars/types.js +13 -0
  28. package/lib/sidebars/utils.d.ts +22 -0
  29. package/lib/sidebars/utils.js +101 -0
  30. package/lib/sidebars/validation.d.ts +8 -0
  31. package/lib/sidebars/validation.js +102 -0
  32. package/lib/slug.js +4 -4
  33. package/lib/tags.d.ts +8 -0
  34. package/lib/tags.js +22 -0
  35. package/lib/theme/hooks/useDocs.js +24 -21
  36. package/lib/translations.d.ts +1 -1
  37. package/lib/translations.js +13 -13
  38. package/lib/types.d.ts +35 -58
  39. package/lib/versions.d.ts +1 -1
  40. package/lib/versions.js +75 -22
  41. package/package.json +15 -14
  42. package/src/__tests__/__fixtures__/simple-site/docs/_partials/somePartial.md +3 -0
  43. package/src/__tests__/__fixtures__/simple-site/docs/_partials/subfolder/somePartial.md +3 -0
  44. package/src/__tests__/__fixtures__/simple-site/docs/_somePartial.md +3 -0
  45. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +5 -0
  46. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +1 -0
  47. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +2 -0
  48. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +2 -0
  49. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +2 -0
  50. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +2 -0
  51. package/src/__tests__/__fixtures__/simple-site/sidebars.json +15 -1
  52. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +6 -0
  53. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/somePartial.md +3 -0
  54. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/subfolder/somePartial.md +3 -0
  55. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_somePartial.md +3 -0
  56. package/src/__tests__/__snapshots__/cli.test.ts.snap +33 -0
  57. package/src/__tests__/__snapshots__/docs.test.ts.snap +140 -0
  58. package/src/__tests__/__snapshots__/index.test.ts.snap +478 -60
  59. package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -3
  60. package/src/__tests__/cli.test.ts +14 -10
  61. package/src/__tests__/docFrontMatter.test.ts +163 -48
  62. package/src/__tests__/docs.test.ts +167 -21
  63. package/src/__tests__/index.test.ts +74 -30
  64. package/src/__tests__/lastUpdate.test.ts +3 -2
  65. package/src/__tests__/options.test.ts +46 -3
  66. package/src/__tests__/props.test.ts +62 -0
  67. package/src/__tests__/translations.test.ts +0 -1
  68. package/src/__tests__/versions.test.ts +88 -60
  69. package/src/cli.ts +27 -30
  70. package/src/client/__tests__/docsClientUtils.test.ts +4 -5
  71. package/src/client/docsClientUtils.ts +6 -27
  72. package/src/docFrontMatter.ts +8 -3
  73. package/src/docs.ts +92 -9
  74. package/src/index.ts +114 -121
  75. package/src/lastUpdate.ts +10 -6
  76. package/src/markdown/index.ts +8 -12
  77. package/src/numberPrefix.ts +4 -2
  78. package/src/options.ts +47 -17
  79. package/src/plugin-content-docs.d.ts +121 -34
  80. package/src/props.ts +42 -6
  81. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -0
  82. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -0
  83. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -0
  84. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category.js +0 -0
  85. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -0
  86. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed.json +0 -0
  87. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -0
  88. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -0
  89. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -0
  90. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -0
  91. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link.json +0 -0
  92. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-unknown-type.json +0 -0
  93. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-wrong-field.json +0 -0
  94. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars.json +0 -0
  95. package/src/{__tests__/__snapshots__/sidebars.test.ts.snap → sidebars/__tests__/__snapshots__/index.test.ts.snap} +21 -6
  96. package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +29 -7
  97. package/src/sidebars/__tests__/index.test.ts +202 -0
  98. package/src/sidebars/__tests__/processor.test.ts +148 -0
  99. package/src/sidebars/__tests__/utils.test.ts +395 -0
  100. package/src/sidebars/generator.ts +253 -0
  101. package/src/sidebars/index.ts +84 -0
  102. package/src/sidebars/normalization.ts +88 -0
  103. package/src/sidebars/processor.ts +124 -0
  104. package/src/sidebars/types.ts +156 -0
  105. package/src/sidebars/utils.ts +146 -0
  106. package/src/sidebars/validation.ts +124 -0
  107. package/src/tags.ts +21 -0
  108. package/src/theme/hooks/useDocs.ts +5 -1
  109. package/src/translations.ts +26 -36
  110. package/src/types.ts +48 -99
  111. package/src/versions.ts +109 -17
  112. package/lib/sidebarItemsGenerator.js +0 -211
  113. package/lib/sidebars.d.ts +0 -43
  114. package/lib/sidebars.js +0 -319
  115. package/src/__tests__/sidebars.test.ts +0 -639
  116. package/src/sidebarItemsGenerator.ts +0 -307
  117. package/src/sidebars.ts +0 -506
@@ -0,0 +1,395 @@
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 {
9
+ createSidebarsUtils,
10
+ collectSidebarDocItems,
11
+ collectSidebarCategories,
12
+ collectSidebarLinks,
13
+ transformSidebarItems,
14
+ collectSidebarsDocIds,
15
+ } from '../utils';
16
+ import type {Sidebar, Sidebars} from '../types';
17
+
18
+ describe('createSidebarsUtils', () => {
19
+ const sidebar1: Sidebar = [
20
+ {
21
+ type: 'category',
22
+ collapsed: false,
23
+ collapsible: true,
24
+ label: 'Category1',
25
+ items: [
26
+ {
27
+ type: 'category',
28
+ collapsed: false,
29
+ collapsible: true,
30
+ label: 'Subcategory 1',
31
+ items: [{type: 'doc', id: 'doc1'}],
32
+ },
33
+ {type: 'doc', id: 'doc2'},
34
+ ],
35
+ },
36
+ ];
37
+
38
+ const sidebar2: Sidebar = [
39
+ {
40
+ type: 'category',
41
+ collapsed: false,
42
+ collapsible: true,
43
+ label: 'Category2',
44
+ items: [
45
+ {type: 'doc', id: 'doc3'},
46
+ {type: 'doc', id: 'doc4'},
47
+ ],
48
+ },
49
+ ];
50
+
51
+ const sidebars: Sidebars = {sidebar1, sidebar2};
52
+
53
+ const {getFirstDocIdOfFirstSidebar, getSidebarNameByDocId, getDocNavigation} =
54
+ createSidebarsUtils(sidebars);
55
+
56
+ test('getSidebarNameByDocId', async () => {
57
+ expect(getFirstDocIdOfFirstSidebar()).toEqual('doc1');
58
+ });
59
+
60
+ test('getSidebarNameByDocId', async () => {
61
+ expect(getSidebarNameByDocId('doc1')).toEqual('sidebar1');
62
+ expect(getSidebarNameByDocId('doc2')).toEqual('sidebar1');
63
+ expect(getSidebarNameByDocId('doc3')).toEqual('sidebar2');
64
+ expect(getSidebarNameByDocId('doc4')).toEqual('sidebar2');
65
+ expect(getSidebarNameByDocId('doc5')).toEqual(undefined);
66
+ expect(getSidebarNameByDocId('doc6')).toEqual(undefined);
67
+ });
68
+
69
+ test('getDocNavigation', async () => {
70
+ expect(getDocNavigation('doc1')).toEqual({
71
+ sidebarName: 'sidebar1',
72
+ previousId: undefined,
73
+ nextId: 'doc2',
74
+ });
75
+ expect(getDocNavigation('doc2')).toEqual({
76
+ sidebarName: 'sidebar1',
77
+ previousId: 'doc1',
78
+ nextId: undefined,
79
+ });
80
+
81
+ expect(getDocNavigation('doc3')).toEqual({
82
+ sidebarName: 'sidebar2',
83
+ previousId: undefined,
84
+ nextId: 'doc4',
85
+ });
86
+ expect(getDocNavigation('doc4')).toEqual({
87
+ sidebarName: 'sidebar2',
88
+ previousId: 'doc3',
89
+ nextId: undefined,
90
+ });
91
+ });
92
+ });
93
+
94
+ describe('collectSidebarDocItems', () => {
95
+ test('can collect docs', async () => {
96
+ const sidebar: Sidebar = [
97
+ {
98
+ type: 'category',
99
+ collapsed: false,
100
+ collapsible: true,
101
+ label: 'Category1',
102
+ items: [
103
+ {
104
+ type: 'category',
105
+ collapsed: false,
106
+ collapsible: true,
107
+ label: 'Subcategory 1',
108
+ items: [{type: 'doc', id: 'doc1'}],
109
+ },
110
+ {
111
+ type: 'category',
112
+ collapsed: false,
113
+ collapsible: true,
114
+ label: 'Subcategory 2',
115
+ items: [
116
+ {type: 'doc', id: 'doc2'},
117
+ {
118
+ type: 'category',
119
+ collapsed: false,
120
+ collapsible: true,
121
+ label: 'Sub sub category 1',
122
+ items: [{type: 'doc', id: 'doc3'}],
123
+ },
124
+ ],
125
+ },
126
+ ],
127
+ },
128
+ {
129
+ type: 'category',
130
+ collapsed: false,
131
+ collapsible: true,
132
+ label: 'Category2',
133
+ items: [
134
+ {type: 'doc', id: 'doc4'},
135
+ {type: 'doc', id: 'doc5'},
136
+ ],
137
+ },
138
+ ];
139
+
140
+ expect(collectSidebarDocItems(sidebar).map((doc) => doc.id)).toEqual([
141
+ 'doc1',
142
+ 'doc2',
143
+ 'doc3',
144
+ 'doc4',
145
+ 'doc5',
146
+ ]);
147
+ });
148
+ });
149
+
150
+ describe('collectSidebarCategories', () => {
151
+ test('can collect categories', async () => {
152
+ const sidebar: Sidebar = [
153
+ {
154
+ type: 'category',
155
+ collapsed: false,
156
+ collapsible: true,
157
+ label: 'Category1',
158
+ items: [
159
+ {
160
+ type: 'category',
161
+ collapsed: false,
162
+ collapsible: true,
163
+ label: 'Subcategory 1',
164
+ items: [{type: 'doc', id: 'doc1'}],
165
+ },
166
+ {
167
+ type: 'category',
168
+ collapsed: false,
169
+ collapsible: true,
170
+ label: 'Subcategory 2',
171
+ items: [
172
+ {type: 'doc', id: 'doc2'},
173
+ {
174
+ type: 'category',
175
+ collapsed: false,
176
+ collapsible: true,
177
+ label: 'Sub sub category 1',
178
+ items: [{type: 'doc', id: 'doc3'}],
179
+ },
180
+ ],
181
+ },
182
+ ],
183
+ },
184
+ {
185
+ type: 'category',
186
+ collapsed: false,
187
+ collapsible: true,
188
+ label: 'Category2',
189
+ items: [
190
+ {type: 'doc', id: 'doc4'},
191
+ {type: 'doc', id: 'doc5'},
192
+ ],
193
+ },
194
+ ];
195
+
196
+ expect(
197
+ collectSidebarCategories(sidebar).map((category) => category.label),
198
+ ).toEqual([
199
+ 'Category1',
200
+ 'Subcategory 1',
201
+ 'Subcategory 2',
202
+ 'Sub sub category 1',
203
+ 'Category2',
204
+ ]);
205
+ });
206
+ });
207
+
208
+ describe('collectSidebarLinks', () => {
209
+ test('can collect links', async () => {
210
+ const sidebar: Sidebar = [
211
+ {
212
+ type: 'category',
213
+ collapsed: false,
214
+ collapsible: true,
215
+ label: 'Category1',
216
+ items: [
217
+ {
218
+ type: 'link',
219
+ href: 'https://google.com',
220
+ label: 'Google',
221
+ },
222
+ {
223
+ type: 'category',
224
+ collapsed: false,
225
+ collapsible: true,
226
+ label: 'Subcategory 2',
227
+ items: [
228
+ {
229
+ type: 'link',
230
+ href: 'https://facebook.com',
231
+ label: 'Facebook',
232
+ },
233
+ ],
234
+ },
235
+ ],
236
+ },
237
+ ];
238
+
239
+ expect(collectSidebarLinks(sidebar).map((link) => link.href)).toEqual([
240
+ 'https://google.com',
241
+ 'https://facebook.com',
242
+ ]);
243
+ });
244
+ });
245
+
246
+ describe('collectSidebarsDocIds', () => {
247
+ test('can collect sidebars doc items', async () => {
248
+ const sidebar1: Sidebar = [
249
+ {
250
+ type: 'category',
251
+ collapsed: false,
252
+ collapsible: true,
253
+ label: 'Category1',
254
+ items: [
255
+ {
256
+ type: 'category',
257
+ collapsed: false,
258
+ collapsible: true,
259
+ label: 'Subcategory 1',
260
+ items: [{type: 'doc', id: 'doc1'}],
261
+ },
262
+ {type: 'doc', id: 'doc2'},
263
+ ],
264
+ },
265
+ ];
266
+
267
+ const sidebar2: Sidebar = [
268
+ {
269
+ type: 'category',
270
+ collapsed: false,
271
+ collapsible: true,
272
+ label: 'Category2',
273
+ items: [
274
+ {type: 'doc', id: 'doc3'},
275
+ {type: 'doc', id: 'doc4'},
276
+ ],
277
+ },
278
+ ];
279
+
280
+ const sidebar3: Sidebar = [
281
+ {type: 'doc', id: 'doc5'},
282
+ {type: 'doc', id: 'doc6'},
283
+ ];
284
+ expect(collectSidebarsDocIds({sidebar1, sidebar2, sidebar3})).toEqual({
285
+ sidebar1: ['doc1', 'doc2'],
286
+ sidebar2: ['doc3', 'doc4'],
287
+ sidebar3: ['doc5', 'doc6'],
288
+ });
289
+ });
290
+ });
291
+
292
+ describe('transformSidebarItems', () => {
293
+ test('can transform sidebar items', async () => {
294
+ const sidebar: Sidebar = [
295
+ {
296
+ type: 'category',
297
+ collapsed: false,
298
+ collapsible: true,
299
+ label: 'Category1',
300
+ items: [
301
+ {
302
+ type: 'category',
303
+ collapsed: false,
304
+ collapsible: true,
305
+ label: 'Subcategory 1',
306
+ items: [{type: 'doc', id: 'doc1'}],
307
+ customProps: {fakeProp: false},
308
+ },
309
+ {
310
+ type: 'category',
311
+ collapsed: false,
312
+ collapsible: true,
313
+ label: 'Subcategory 2',
314
+ items: [
315
+ {type: 'doc', id: 'doc2'},
316
+ {
317
+ type: 'category',
318
+ collapsed: false,
319
+ collapsible: true,
320
+ label: 'Sub sub category 1',
321
+ items: [
322
+ {type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
323
+ ],
324
+ },
325
+ ],
326
+ },
327
+ ],
328
+ },
329
+ {
330
+ type: 'category',
331
+ collapsed: false,
332
+ collapsible: true,
333
+ label: 'Category2',
334
+ items: [
335
+ {type: 'doc', id: 'doc4'},
336
+ {type: 'doc', id: 'doc5'},
337
+ ],
338
+ },
339
+ ];
340
+
341
+ expect(
342
+ transformSidebarItems(sidebar, (item) => {
343
+ if (item.type === 'category') {
344
+ return {...item, label: `MODIFIED LABEL: ${item.label}`};
345
+ }
346
+ return item;
347
+ }),
348
+ ).toEqual([
349
+ {
350
+ type: 'category',
351
+ collapsed: false,
352
+ collapsible: true,
353
+ label: 'MODIFIED LABEL: Category1',
354
+ items: [
355
+ {
356
+ type: 'category',
357
+ collapsed: false,
358
+ collapsible: true,
359
+ label: 'MODIFIED LABEL: Subcategory 1',
360
+ items: [{type: 'doc', id: 'doc1'}],
361
+ customProps: {fakeProp: false},
362
+ },
363
+ {
364
+ type: 'category',
365
+ collapsed: false,
366
+ collapsible: true,
367
+ label: 'MODIFIED LABEL: Subcategory 2',
368
+ items: [
369
+ {type: 'doc', id: 'doc2'},
370
+ {
371
+ type: 'category',
372
+ collapsed: false,
373
+ collapsible: true,
374
+ label: 'MODIFIED LABEL: Sub sub category 1',
375
+ items: [
376
+ {type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
377
+ ],
378
+ },
379
+ ],
380
+ },
381
+ ],
382
+ },
383
+ {
384
+ type: 'category',
385
+ collapsed: false,
386
+ collapsible: true,
387
+ label: 'MODIFIED LABEL: Category2',
388
+ items: [
389
+ {type: 'doc', id: 'doc4'},
390
+ {type: 'doc', id: 'doc5'},
391
+ ],
392
+ },
393
+ ]);
394
+ });
395
+ });
@@ -0,0 +1,253 @@
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
+ SidebarItem,
10
+ SidebarItemDoc,
11
+ SidebarItemCategory,
12
+ SidebarItemsGenerator,
13
+ SidebarItemsGeneratorDoc,
14
+ } from './types';
15
+ import {keyBy, sortBy} from 'lodash';
16
+ import {addTrailingSlash, posixPath} from '@docusaurus/utils';
17
+ import {Joi} from '@docusaurus/utils-validation';
18
+ import chalk from 'chalk';
19
+ import path from 'path';
20
+ import fs from 'fs-extra';
21
+ import Yaml from 'js-yaml';
22
+
23
+ const BreadcrumbSeparator = '/';
24
+ // To avoid possible name clashes with a folder of the same name as the ID
25
+ const docIdPrefix = '$doc$/';
26
+
27
+ export const CategoryMetadataFilenameBase = '_category_';
28
+ export const CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
29
+
30
+ export type CategoryMetadatasFile = {
31
+ label?: string;
32
+ position?: number;
33
+ collapsed?: boolean;
34
+ collapsible?: boolean;
35
+ className?: string;
36
+
37
+ // TODO should we allow "items" here? how would this work? would an "autogenerated" type be allowed?
38
+ // This mkdocs plugin do something like that: https://github.com/lukasgeiter/mkdocs-awesome-pages-plugin/
39
+ // cf comment: https://github.com/facebook/docusaurus/issues/3464#issuecomment-784765199
40
+ };
41
+
42
+ type WithPosition<T> = T & {position?: number};
43
+
44
+ /**
45
+ * A representation of the fs structure. For each object entry:
46
+ * If it's a folder, the key is the directory name, and value is the directory content;
47
+ * If it's a doc file, the key is the doc id prefixed with '$doc$/', and value is null
48
+ */
49
+ type Dir = {
50
+ [item: string]: Dir | null;
51
+ };
52
+
53
+ const CategoryMetadatasFileSchema = Joi.object<CategoryMetadatasFile>({
54
+ label: Joi.string(),
55
+ position: Joi.number(),
56
+ collapsed: Joi.boolean(),
57
+ collapsible: Joi.boolean(),
58
+ className: Joi.string(),
59
+ });
60
+
61
+ // TODO I now believe we should read all the category metadata files ahead of time: we may need this metadata to customize docs metadata
62
+ // Example use-case being able to disable number prefix parsing at the folder level, or customize the default route path segment for an intermediate directory...
63
+ // TODO later if there is `CategoryFolder/index.md`, we may want to read the metadata as yaml on it
64
+ // see https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
65
+ async function readCategoryMetadatasFile(
66
+ categoryDirPath: string,
67
+ ): Promise<CategoryMetadatasFile | null> {
68
+ async function tryReadFile(
69
+ fileNameWithExtension: string,
70
+ parse: (content: string) => unknown,
71
+ ): Promise<CategoryMetadatasFile | null> {
72
+ // Simpler to use only posix paths for mocking file metadatas in tests
73
+ const filePath = posixPath(
74
+ path.join(categoryDirPath, fileNameWithExtension),
75
+ );
76
+ if (await fs.pathExists(filePath)) {
77
+ const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
78
+ const unsafeContent = parse(contentString);
79
+ try {
80
+ return Joi.attempt(unsafeContent, CategoryMetadatasFileSchema);
81
+ } catch (e) {
82
+ console.error(
83
+ chalk.red(
84
+ `The docs sidebar category metadata file looks invalid!\nPath: ${filePath}`,
85
+ ),
86
+ );
87
+ throw e;
88
+ }
89
+ }
90
+ return null;
91
+ }
92
+
93
+ return (
94
+ (await tryReadFile(`${CategoryMetadataFilenameBase}.json`, JSON.parse)) ??
95
+ (await tryReadFile(`${CategoryMetadataFilenameBase}.yml`, Yaml.load)) ??
96
+ // eslint-disable-next-line no-return-await
97
+ (await tryReadFile(`${CategoryMetadataFilenameBase}.yaml`, Yaml.load))
98
+ );
99
+ }
100
+
101
+ // Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
102
+ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
103
+ numberPrefixParser,
104
+ docs: allDocs,
105
+ options,
106
+ item: {dirName: autogenDir},
107
+ version,
108
+ }) => {
109
+ /**
110
+ * Step 1. Extract the docs that are in the autogen dir.
111
+ */
112
+ function getAutogenDocs(): SidebarItemsGeneratorDoc[] {
113
+ function isInAutogeneratedDir(doc: SidebarItemsGeneratorDoc) {
114
+ return (
115
+ // Doc at the root of the autogenerated sidebar dir
116
+ doc.sourceDirName === autogenDir ||
117
+ // autogen dir is . and doc is in subfolder
118
+ autogenDir === '.' ||
119
+ // autogen dir is not . and doc is in subfolder
120
+ // "api/myDoc" startsWith "api/" (note "api2/myDoc" is not included)
121
+ doc.sourceDirName.startsWith(addTrailingSlash(autogenDir))
122
+ );
123
+ }
124
+ const docs = allDocs.filter(isInAutogeneratedDir);
125
+
126
+ if (docs.length === 0) {
127
+ console.warn(
128
+ chalk.yellow(
129
+ `No docs found in dir ${autogenDir}: can't auto-generate a sidebar.`,
130
+ ),
131
+ );
132
+ }
133
+ return docs;
134
+ }
135
+
136
+ /**
137
+ * Step 2. Turn the linear file list into a tree structure.
138
+ */
139
+ function treeify(docs: SidebarItemsGeneratorDoc[]): Dir {
140
+ // Get the category breadcrumb of a doc (relative to the dir of the autogenerated sidebar item)
141
+ // autogenDir=a/b and docDir=a/b/c/d => returns [c, d]
142
+ // autogenDir=a/b and docDir=a/b => returns []
143
+ // TODO: try to use path.relative()
144
+ function getRelativeBreadcrumb(doc: SidebarItemsGeneratorDoc): string[] {
145
+ return autogenDir === doc.sourceDirName
146
+ ? []
147
+ : doc.sourceDirName
148
+ .replace(addTrailingSlash(autogenDir), '')
149
+ .split(BreadcrumbSeparator);
150
+ }
151
+ const treeRoot: Dir = {};
152
+ docs.forEach((doc) => {
153
+ const breadcrumb = getRelativeBreadcrumb(doc);
154
+ let currentDir = treeRoot; // We walk down the file's path to generate the fs structure
155
+ // eslint-disable-next-line no-restricted-syntax
156
+ for (const dir of breadcrumb) {
157
+ if (typeof currentDir[dir] === 'undefined') {
158
+ currentDir[dir] = {}; // Create new folder.
159
+ }
160
+ currentDir = currentDir[dir]!; // Go into the subdirectory.
161
+ }
162
+ currentDir[`${docIdPrefix}${doc.id}`] = null; // We've walked through the file path. Register the file in this directory.
163
+ });
164
+ return treeRoot;
165
+ }
166
+
167
+ /**
168
+ * Step 3. Recursively transform the tree-like file structure to sidebar items.
169
+ * (From a record to an array of items, akin to normalizing shorthand)
170
+ */
171
+ function generateSidebar(fsModel: Dir): Promise<WithPosition<SidebarItem>[]> {
172
+ const docsById = keyBy(allDocs, (doc) => doc.id);
173
+ function createDocItem(id: string): WithPosition<SidebarItemDoc> {
174
+ const {
175
+ sidebarPosition: position,
176
+ frontMatter: {sidebar_label: label, sidebar_class_name: className},
177
+ } = docsById[id];
178
+ return {
179
+ type: 'doc',
180
+ id,
181
+ position,
182
+ // We don't want these fields to magically appear in the generated sidebar
183
+ ...(label !== undefined && {label}),
184
+ ...(className !== undefined && {className}),
185
+ };
186
+ }
187
+ async function createCategoryItem(
188
+ dir: Dir,
189
+ fullPath: string,
190
+ folderName: string,
191
+ ): Promise<WithPosition<SidebarItemCategory>> {
192
+ const categoryPath = path.join(version.contentPath, autogenDir, fullPath);
193
+ const categoryMetadatas = await readCategoryMetadatasFile(categoryPath);
194
+ const className = categoryMetadatas?.className;
195
+ const {filename, numberPrefix} = numberPrefixParser(folderName);
196
+ return {
197
+ type: 'category',
198
+ label: categoryMetadatas?.label ?? filename,
199
+ collapsible:
200
+ categoryMetadatas?.collapsible ?? options.sidebarCollapsible,
201
+ collapsed: categoryMetadatas?.collapsed ?? options.sidebarCollapsed,
202
+ position: categoryMetadatas?.position ?? numberPrefix,
203
+ ...(className !== undefined && {className}),
204
+ items: await Promise.all(
205
+ Object.entries(dir).map(([key, content]) =>
206
+ dirToItem(content, key, `${fullPath}/${key}`),
207
+ ),
208
+ ),
209
+ };
210
+ }
211
+ async function dirToItem(
212
+ dir: Dir | null, // The directory item to be transformed.
213
+ itemKey: string, // For docs, it's the doc ID; for categories, it's used to generate the next `relativePath`.
214
+ fullPath: string, // `dir`'s full path relative to the autogen dir.
215
+ ): Promise<WithPosition<SidebarItem>> {
216
+ return dir
217
+ ? createCategoryItem(dir, fullPath, itemKey)
218
+ : createDocItem(itemKey.substring(docIdPrefix.length));
219
+ }
220
+ return Promise.all(
221
+ Object.entries(fsModel).map(([key, content]) =>
222
+ dirToItem(content, key, key),
223
+ ),
224
+ );
225
+ }
226
+
227
+ /**
228
+ * Step 4. Recursively sort the categories/docs + remove the "position" attribute from final output.
229
+ * Note: the "position" is only used to sort "inside" a sidebar slice. It is not
230
+ * used to sort across multiple consecutive sidebar slices (ie a whole Category
231
+ * composed of multiple autogenerated items)
232
+ */
233
+ function sortItems(sidebarItems: WithPosition<SidebarItem>[]): SidebarItem[] {
234
+ const processedSidebarItems = sidebarItems.map((item) => {
235
+ if (item.type === 'category') {
236
+ return {...item, items: sortItems(item.items)};
237
+ }
238
+ return item;
239
+ });
240
+ const sortedSidebarItems = sortBy(
241
+ processedSidebarItems,
242
+ (item) => item.position,
243
+ );
244
+ return sortedSidebarItems.map(({position, ...item}) => item);
245
+ }
246
+ // TODO: the whole code is designed for pipeline operator
247
+ // return getAutogenDocs() |> treeify |> await generateSidebar(^) |> sortItems;
248
+ const docs = getAutogenDocs();
249
+ const fsModel = treeify(docs);
250
+ const sidebarWithPosition = await generateSidebar(fsModel);
251
+ const sortedSidebar = sortItems(sidebarWithPosition);
252
+ return sortedSidebar;
253
+ };