@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
@@ -1,639 +0,0 @@
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 path from 'path';
9
- import {
10
- loadSidebars,
11
- collectSidebarDocItems,
12
- collectSidebarsDocIds,
13
- createSidebarsUtils,
14
- collectSidebarCategories,
15
- collectSidebarLinks,
16
- transformSidebarItems,
17
- processSidebars,
18
- DefaultSidebars,
19
- DisabledSidebars,
20
- } from '../sidebars';
21
- import {
22
- Sidebar,
23
- SidebarItem,
24
- SidebarItemsGenerator,
25
- Sidebars,
26
- UnprocessedSidebars,
27
- } from '../types';
28
- import {DefaultSidebarItemsGenerator} from '../sidebarItemsGenerator';
29
-
30
- /* eslint-disable global-require, import/no-dynamic-require */
31
-
32
- describe('loadSidebars', () => {
33
- const fixtureDir = path.join(__dirname, '__fixtures__', 'sidebars');
34
- test('sidebars with known sidebar item type', async () => {
35
- const sidebarPath = path.join(fixtureDir, 'sidebars.json');
36
- const result = loadSidebars(sidebarPath);
37
- expect(result).toMatchSnapshot();
38
- });
39
-
40
- test('sidebars with deep level of category', async () => {
41
- const sidebarPath = path.join(fixtureDir, 'sidebars-category.js');
42
- const result = loadSidebars(sidebarPath);
43
- expect(result).toMatchSnapshot();
44
- });
45
-
46
- test('sidebars shorthand and longform lead to exact same sidebar', async () => {
47
- const sidebarPath1 = path.join(fixtureDir, 'sidebars-category.js');
48
- const sidebarPath2 = path.join(
49
- fixtureDir,
50
- 'sidebars-category-shorthand.js',
51
- );
52
- const sidebar1 = loadSidebars(sidebarPath1);
53
- const sidebar2 = loadSidebars(sidebarPath2);
54
- expect(sidebar1).toEqual(sidebar2);
55
- });
56
-
57
- test('sidebars with category but category.items is not an array', async () => {
58
- const sidebarPath = path.join(
59
- fixtureDir,
60
- 'sidebars-category-wrong-items.json',
61
- );
62
- expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
63
- `"Error loading {\\"type\\":\\"category\\",\\"label\\":\\"Category Label\\",\\"items\\":\\"doc1\\"}: \\"items\\" must be an array."`,
64
- );
65
- });
66
-
67
- test('sidebars with category but category label is not a string', async () => {
68
- const sidebarPath = path.join(
69
- fixtureDir,
70
- 'sidebars-category-wrong-label.json',
71
- );
72
- expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
73
- `"Error loading {\\"type\\":\\"category\\",\\"label\\":true,\\"items\\":[\\"doc1\\"]}: \\"label\\" must be a string."`,
74
- );
75
- });
76
-
77
- test('sidebars item doc but id is not a string', async () => {
78
- const sidebarPath = path.join(
79
- fixtureDir,
80
- 'sidebars-doc-id-not-string.json',
81
- );
82
- expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
83
- `"Error loading {\\"type\\":\\"doc\\",\\"id\\":[\\"doc1\\"]}: \\"id\\" must be a string."`,
84
- );
85
- });
86
-
87
- test('sidebars with first level not a category', async () => {
88
- const sidebarPath = path.join(
89
- fixtureDir,
90
- 'sidebars-first-level-not-category.js',
91
- );
92
- const result = loadSidebars(sidebarPath);
93
- expect(result).toMatchSnapshot();
94
- });
95
-
96
- test('sidebars link', async () => {
97
- const sidebarPath = path.join(fixtureDir, 'sidebars-link.json');
98
- const result = loadSidebars(sidebarPath);
99
- expect(result).toMatchSnapshot();
100
- });
101
-
102
- test('sidebars link wrong label', async () => {
103
- const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-label.json');
104
- expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
105
- `"Error loading {\\"type\\":\\"link\\",\\"label\\":false,\\"href\\":\\"https://github.com\\"}: \\"label\\" must be a string."`,
106
- );
107
- });
108
-
109
- test('sidebars link wrong href', async () => {
110
- const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-href.json');
111
- expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
112
- `"Error loading {\\"type\\":\\"link\\",\\"label\\":\\"GitHub\\",\\"href\\":[\\"example.com\\"]}: \\"href\\" must be a string."`,
113
- );
114
- });
115
-
116
- test('sidebars with unknown sidebar item type', async () => {
117
- const sidebarPath = path.join(fixtureDir, 'sidebars-unknown-type.json');
118
- expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(`
119
- "Unknown sidebar item type \\"superman\\". Sidebar item is {\\"type\\":\\"superman\\"}.
120
- "
121
- `);
122
- });
123
-
124
- test('sidebars with known sidebar item type but wrong field', async () => {
125
- const sidebarPath = path.join(fixtureDir, 'sidebars-wrong-field.json');
126
- expect(() => loadSidebars(sidebarPath)).toThrowErrorMatchingInlineSnapshot(
127
- `"Unknown sidebar item keys: href. Item: {\\"type\\":\\"category\\",\\"label\\":\\"category\\",\\"href\\":\\"https://github.com\\"}"`,
128
- );
129
- });
130
-
131
- test('unexisting path', () => {
132
- expect(loadSidebars('badpath')).toEqual(DisabledSidebars);
133
- });
134
-
135
- test('undefined path', () => {
136
- expect(loadSidebars(undefined)).toEqual(DefaultSidebars);
137
- });
138
-
139
- test('literal false path', () => {
140
- expect(loadSidebars(false)).toEqual(DisabledSidebars);
141
- });
142
-
143
- test('sidebars with category.collapsed property', async () => {
144
- const sidebarPath = path.join(fixtureDir, 'sidebars-collapsed.json');
145
- const result = loadSidebars(sidebarPath);
146
- expect(result).toMatchSnapshot();
147
- });
148
-
149
- test('sidebars with category.collapsed property at first level', async () => {
150
- const sidebarPath = path.join(
151
- fixtureDir,
152
- 'sidebars-collapsed-first-level.json',
153
- );
154
- const result = loadSidebars(sidebarPath);
155
- expect(result).toMatchSnapshot();
156
- });
157
- });
158
-
159
- describe('collectSidebarDocItems', () => {
160
- test('can collect docs', async () => {
161
- const sidebar: Sidebar = [
162
- {
163
- type: 'category',
164
- collapsed: false,
165
- label: 'Category1',
166
- items: [
167
- {
168
- type: 'category',
169
- collapsed: false,
170
- label: 'Subcategory 1',
171
- items: [{type: 'doc', id: 'doc1'}],
172
- },
173
- {
174
- type: 'category',
175
- collapsed: false,
176
- label: 'Subcategory 2',
177
- items: [
178
- {type: 'doc', id: 'doc2'},
179
- {
180
- type: 'category',
181
- collapsed: false,
182
- label: 'Sub sub category 1',
183
- items: [{type: 'doc', id: 'doc3'}],
184
- },
185
- ],
186
- },
187
- ],
188
- },
189
- {
190
- type: 'category',
191
- collapsed: false,
192
- label: 'Category2',
193
- items: [
194
- {type: 'doc', id: 'doc4'},
195
- {type: 'doc', id: 'doc5'},
196
- ],
197
- },
198
- ];
199
-
200
- expect(collectSidebarDocItems(sidebar).map((doc) => doc.id)).toEqual([
201
- 'doc1',
202
- 'doc2',
203
- 'doc3',
204
- 'doc4',
205
- 'doc5',
206
- ]);
207
- });
208
- });
209
-
210
- describe('collectSidebarCategories', () => {
211
- test('can collect categories', async () => {
212
- const sidebar: Sidebar = [
213
- {
214
- type: 'category',
215
- collapsed: false,
216
- label: 'Category1',
217
- items: [
218
- {
219
- type: 'category',
220
- collapsed: false,
221
- label: 'Subcategory 1',
222
- items: [{type: 'doc', id: 'doc1'}],
223
- },
224
- {
225
- type: 'category',
226
- collapsed: false,
227
- label: 'Subcategory 2',
228
- items: [
229
- {type: 'doc', id: 'doc2'},
230
- {
231
- type: 'category',
232
- collapsed: false,
233
- label: 'Sub sub category 1',
234
- items: [{type: 'doc', id: 'doc3'}],
235
- },
236
- ],
237
- },
238
- ],
239
- },
240
- {
241
- type: 'category',
242
- collapsed: false,
243
- label: 'Category2',
244
- items: [
245
- {type: 'doc', id: 'doc4'},
246
- {type: 'doc', id: 'doc5'},
247
- ],
248
- },
249
- ];
250
-
251
- expect(
252
- collectSidebarCategories(sidebar).map((category) => category.label),
253
- ).toEqual([
254
- 'Category1',
255
- 'Subcategory 1',
256
- 'Subcategory 2',
257
- 'Sub sub category 1',
258
- 'Category2',
259
- ]);
260
- });
261
- });
262
-
263
- describe('collectSidebarLinks', () => {
264
- test('can collect links', async () => {
265
- const sidebar: Sidebar = [
266
- {
267
- type: 'category',
268
- collapsed: false,
269
- label: 'Category1',
270
- items: [
271
- {
272
- type: 'link',
273
- href: 'https://google.com',
274
- label: 'Google',
275
- },
276
- {
277
- type: 'category',
278
- collapsed: false,
279
- label: 'Subcategory 2',
280
- items: [
281
- {
282
- type: 'link',
283
- href: 'https://facebook.com',
284
- label: 'Facebook',
285
- },
286
- ],
287
- },
288
- ],
289
- },
290
- ];
291
-
292
- expect(collectSidebarLinks(sidebar).map((link) => link.href)).toEqual([
293
- 'https://google.com',
294
- 'https://facebook.com',
295
- ]);
296
- });
297
- });
298
-
299
- describe('collectSidebarsDocIds', () => {
300
- test('can collect sidebars doc items', async () => {
301
- const sidebar1: Sidebar = [
302
- {
303
- type: 'category',
304
- collapsed: false,
305
- label: 'Category1',
306
- items: [
307
- {
308
- type: 'category',
309
- collapsed: false,
310
- label: 'Subcategory 1',
311
- items: [{type: 'doc', id: 'doc1'}],
312
- },
313
- {type: 'doc', id: 'doc2'},
314
- ],
315
- },
316
- ];
317
-
318
- const sidebar2: Sidebar = [
319
- {
320
- type: 'category',
321
- collapsed: false,
322
- label: 'Category2',
323
- items: [
324
- {type: 'doc', id: 'doc3'},
325
- {type: 'doc', id: 'doc4'},
326
- ],
327
- },
328
- ];
329
-
330
- const sidebar3: Sidebar = [
331
- {type: 'doc', id: 'doc5'},
332
- {type: 'doc', id: 'doc6'},
333
- ];
334
- expect(collectSidebarsDocIds({sidebar1, sidebar2, sidebar3})).toEqual({
335
- sidebar1: ['doc1', 'doc2'],
336
- sidebar2: ['doc3', 'doc4'],
337
- sidebar3: ['doc5', 'doc6'],
338
- });
339
- });
340
- });
341
-
342
- describe('transformSidebarItems', () => {
343
- test('can transform sidebar items', async () => {
344
- const sidebar: Sidebar = [
345
- {
346
- type: 'category',
347
- collapsed: false,
348
- label: 'Category1',
349
- items: [
350
- {
351
- type: 'category',
352
- collapsed: false,
353
- label: 'Subcategory 1',
354
- items: [{type: 'doc', id: 'doc1'}],
355
- customProps: {fakeProp: false},
356
- },
357
- {
358
- type: 'category',
359
- collapsed: false,
360
- label: 'Subcategory 2',
361
- items: [
362
- {type: 'doc', id: 'doc2'},
363
- {
364
- type: 'category',
365
- collapsed: false,
366
- label: 'Sub sub category 1',
367
- items: [
368
- {type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
369
- ],
370
- },
371
- ],
372
- },
373
- ],
374
- },
375
- {
376
- type: 'category',
377
- collapsed: false,
378
- label: 'Category2',
379
- items: [
380
- {type: 'doc', id: 'doc4'},
381
- {type: 'doc', id: 'doc5'},
382
- ],
383
- },
384
- ];
385
-
386
- expect(
387
- transformSidebarItems(sidebar, (item) => {
388
- if (item.type === 'category') {
389
- return {...item, label: `MODIFIED LABEL: ${item.label}`};
390
- }
391
- return item;
392
- }),
393
- ).toEqual([
394
- {
395
- type: 'category',
396
- collapsed: false,
397
- label: 'MODIFIED LABEL: Category1',
398
- items: [
399
- {
400
- type: 'category',
401
- collapsed: false,
402
- label: 'MODIFIED LABEL: Subcategory 1',
403
- items: [{type: 'doc', id: 'doc1'}],
404
- customProps: {fakeProp: false},
405
- },
406
- {
407
- type: 'category',
408
- collapsed: false,
409
- label: 'MODIFIED LABEL: Subcategory 2',
410
- items: [
411
- {type: 'doc', id: 'doc2'},
412
- {
413
- type: 'category',
414
- collapsed: false,
415
- label: 'MODIFIED LABEL: Sub sub category 1',
416
- items: [
417
- {type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
418
- ],
419
- },
420
- ],
421
- },
422
- ],
423
- },
424
- {
425
- type: 'category',
426
- collapsed: false,
427
- label: 'MODIFIED LABEL: Category2',
428
- items: [
429
- {type: 'doc', id: 'doc4'},
430
- {type: 'doc', id: 'doc5'},
431
- ],
432
- },
433
- ]);
434
- });
435
- });
436
-
437
- describe('processSidebars', () => {
438
- const StaticGeneratedSidebarSlice: SidebarItem[] = [
439
- {type: 'doc', id: 'doc-generated-id-1'},
440
- {type: 'doc', id: 'doc-generated-id-2'},
441
- ];
442
-
443
- const StaticSidebarItemsGenerator: SidebarItemsGenerator = jest.fn(
444
- async () => {
445
- return StaticGeneratedSidebarSlice;
446
- },
447
- );
448
-
449
- async function testProcessSidebars(unprocessedSidebars: UnprocessedSidebars) {
450
- return processSidebars({
451
- sidebarItemsGenerator: StaticSidebarItemsGenerator,
452
- unprocessedSidebars,
453
- docs: [],
454
- // @ts-expect-error: useless for this test
455
- version: {},
456
- });
457
- }
458
-
459
- test('let sidebars without autogenerated items untouched', async () => {
460
- const unprocessedSidebars: UnprocessedSidebars = {
461
- someSidebar: [
462
- {type: 'doc', id: 'doc1'},
463
- {
464
- type: 'category',
465
- collapsed: false,
466
- items: [{type: 'doc', id: 'doc2'}],
467
- label: 'Category',
468
- },
469
- {type: 'link', href: 'https://facebook.com', label: 'FB'},
470
- ],
471
- secondSidebar: [
472
- {type: 'doc', id: 'doc3'},
473
- {type: 'link', href: 'https://instagram.com', label: 'IG'},
474
- {
475
- type: 'category',
476
- collapsed: false,
477
- items: [{type: 'doc', id: 'doc4'}],
478
- label: 'Category',
479
- },
480
- ],
481
- };
482
-
483
- const processedSidebar = await testProcessSidebars(unprocessedSidebars);
484
- expect(processedSidebar).toEqual(unprocessedSidebars);
485
- });
486
-
487
- test('replace autogenerated items by generated sidebars slices', async () => {
488
- const unprocessedSidebars: UnprocessedSidebars = {
489
- someSidebar: [
490
- {type: 'doc', id: 'doc1'},
491
- {
492
- type: 'category',
493
- collapsed: false,
494
- items: [
495
- {type: 'doc', id: 'doc2'},
496
- {type: 'autogenerated', dirName: 'dir1'},
497
- ],
498
- label: 'Category',
499
- },
500
- {type: 'link', href: 'https://facebook.com', label: 'FB'},
501
- ],
502
- secondSidebar: [
503
- {type: 'doc', id: 'doc3'},
504
- {type: 'autogenerated', dirName: 'dir2'},
505
- {type: 'link', href: 'https://instagram.com', label: 'IG'},
506
- {type: 'autogenerated', dirName: 'dir3'},
507
- {
508
- type: 'category',
509
- collapsed: false,
510
- items: [{type: 'doc', id: 'doc4'}],
511
- label: 'Category',
512
- },
513
- ],
514
- };
515
-
516
- const processedSidebar = await testProcessSidebars(unprocessedSidebars);
517
-
518
- expect(StaticSidebarItemsGenerator).toHaveBeenCalledTimes(3);
519
- expect(StaticSidebarItemsGenerator).toHaveBeenCalledWith({
520
- defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
521
- item: {type: 'autogenerated', dirName: 'dir1'},
522
- docs: [],
523
- version: {},
524
- });
525
- expect(StaticSidebarItemsGenerator).toHaveBeenCalledWith({
526
- defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
527
- item: {type: 'autogenerated', dirName: 'dir2'},
528
- docs: [],
529
- version: {},
530
- });
531
- expect(StaticSidebarItemsGenerator).toHaveBeenCalledWith({
532
- defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
533
- item: {type: 'autogenerated', dirName: 'dir3'},
534
- docs: [],
535
- version: {},
536
- });
537
-
538
- expect(processedSidebar).toEqual({
539
- someSidebar: [
540
- {type: 'doc', id: 'doc1'},
541
- {
542
- type: 'category',
543
- collapsed: false,
544
- items: [{type: 'doc', id: 'doc2'}, ...StaticGeneratedSidebarSlice],
545
- label: 'Category',
546
- },
547
- {type: 'link', href: 'https://facebook.com', label: 'FB'},
548
- ],
549
- secondSidebar: [
550
- {type: 'doc', id: 'doc3'},
551
- ...StaticGeneratedSidebarSlice,
552
- {type: 'link', href: 'https://instagram.com', label: 'IG'},
553
- ...StaticGeneratedSidebarSlice,
554
- {
555
- type: 'category',
556
- collapsed: false,
557
- items: [{type: 'doc', id: 'doc4'}],
558
- label: 'Category',
559
- },
560
- ],
561
- } as Sidebars);
562
- });
563
- });
564
-
565
- describe('createSidebarsUtils', () => {
566
- const sidebar1: Sidebar = [
567
- {
568
- type: 'category',
569
- collapsed: false,
570
- label: 'Category1',
571
- items: [
572
- {
573
- type: 'category',
574
- collapsed: false,
575
- label: 'Subcategory 1',
576
- items: [{type: 'doc', id: 'doc1'}],
577
- },
578
- {type: 'doc', id: 'doc2'},
579
- ],
580
- },
581
- ];
582
-
583
- const sidebar2: Sidebar = [
584
- {
585
- type: 'category',
586
- collapsed: false,
587
- label: 'Category2',
588
- items: [
589
- {type: 'doc', id: 'doc3'},
590
- {type: 'doc', id: 'doc4'},
591
- ],
592
- },
593
- ];
594
-
595
- const sidebars: Sidebars = {sidebar1, sidebar2};
596
-
597
- const {
598
- getFirstDocIdOfFirstSidebar,
599
- getSidebarNameByDocId,
600
- getDocNavigation,
601
- } = createSidebarsUtils(sidebars);
602
-
603
- test('getSidebarNameByDocId', async () => {
604
- expect(getFirstDocIdOfFirstSidebar()).toEqual('doc1');
605
- });
606
-
607
- test('getSidebarNameByDocId', async () => {
608
- expect(getSidebarNameByDocId('doc1')).toEqual('sidebar1');
609
- expect(getSidebarNameByDocId('doc2')).toEqual('sidebar1');
610
- expect(getSidebarNameByDocId('doc3')).toEqual('sidebar2');
611
- expect(getSidebarNameByDocId('doc4')).toEqual('sidebar2');
612
- expect(getSidebarNameByDocId('doc5')).toEqual(undefined);
613
- expect(getSidebarNameByDocId('doc6')).toEqual(undefined);
614
- });
615
-
616
- test('getDocNavigation', async () => {
617
- expect(getDocNavigation('doc1')).toEqual({
618
- sidebarName: 'sidebar1',
619
- previousId: undefined,
620
- nextId: 'doc2',
621
- });
622
- expect(getDocNavigation('doc2')).toEqual({
623
- sidebarName: 'sidebar1',
624
- previousId: 'doc1',
625
- nextId: undefined,
626
- });
627
-
628
- expect(getDocNavigation('doc3')).toEqual({
629
- sidebarName: 'sidebar2',
630
- previousId: undefined,
631
- nextId: 'doc4',
632
- });
633
- expect(getDocNavigation('doc4')).toEqual({
634
- sidebarName: 'sidebar2',
635
- previousId: 'doc3',
636
- nextId: undefined,
637
- });
638
- });
639
- });