@docusaurus/plugin-content-docs 2.0.0-beta.1 → 2.0.0-beta.10
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.
- package/lib/.tsbuildinfo +1 -1
- package/lib/categoryGeneratedIndex.d.ts +12 -0
- package/lib/categoryGeneratedIndex.js +37 -0
- package/lib/cli.d.ts +2 -2
- package/lib/cli.js +12 -34
- package/lib/client/docsClientUtils.d.ts +1 -4
- package/lib/client/docsClientUtils.js +21 -31
- package/lib/docFrontMatter.d.ts +1 -1
- package/lib/docFrontMatter.js +10 -6
- package/lib/docs.d.ts +25 -3
- package/lib/docs.js +125 -38
- package/lib/globalData.d.ts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +104 -138
- package/lib/lastUpdate.js +9 -10
- package/lib/markdown/index.d.ts +3 -6
- package/lib/markdown/index.js +3 -3
- package/lib/markdown/linkify.js +2 -2
- package/lib/numberPrefix.d.ts +1 -1
- package/lib/options.d.ts +3 -3
- package/lib/options.js +49 -17
- package/lib/props.d.ts +7 -2
- package/lib/props.js +61 -9
- package/lib/routes.d.ts +27 -0
- package/lib/routes.js +105 -0
- package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +5 -2
- package/lib/sidebars/generator.js +216 -0
- package/lib/sidebars/index.d.ts +15 -0
- package/lib/sidebars/index.js +73 -0
- package/lib/sidebars/normalization.d.ts +14 -0
- package/lib/sidebars/normalization.js +77 -0
- package/lib/sidebars/processor.d.ts +18 -0
- package/lib/sidebars/processor.js +85 -0
- package/lib/sidebars/types.d.ts +127 -0
- package/lib/sidebars/types.js +8 -0
- package/lib/sidebars/utils.d.ts +35 -0
- package/lib/sidebars/utils.js +228 -0
- package/lib/sidebars/validation.d.ts +10 -0
- package/lib/sidebars/validation.js +138 -0
- package/lib/slug.d.ts +4 -3
- package/lib/slug.js +27 -15
- package/lib/tags.d.ts +8 -0
- package/lib/tags.js +20 -0
- package/lib/theme/hooks/useDocs.js +24 -21
- package/lib/translations.d.ts +2 -2
- package/lib/translations.js +71 -29
- package/lib/types.d.ts +52 -62
- package/lib/versions.d.ts +3 -3
- package/lib/versions.js +76 -24
- package/package.json +22 -20
- package/src/__tests__/__fixtures__/simple-site/docs/_partials/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/simple-site/docs/_partials/subfolder/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/simple-site/docs/_somePartial.md +3 -0
- package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +5 -0
- package/src/__tests__/__fixtures__/simple-site/docs/hello.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/sidebars.json +15 -1
- package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +1 -0
- package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +6 -0
- package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/subfolder/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_somePartial.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +2 -2
- package/src/__tests__/__snapshots__/cli.test.ts.snap +48 -73
- package/src/__tests__/__snapshots__/docs.test.ts.snap +140 -0
- package/src/__tests__/__snapshots__/index.test.ts.snap +753 -112
- package/src/__tests__/__snapshots__/translations.test.ts.snap +45 -18
- package/src/__tests__/cli.test.ts +15 -11
- package/src/__tests__/docFrontMatter.test.ts +195 -40
- package/src/__tests__/docs.test.ts +311 -150
- package/src/__tests__/index.test.ts +112 -69
- package/src/__tests__/lastUpdate.test.ts +3 -2
- package/src/__tests__/options.test.ts +48 -4
- package/src/__tests__/props.test.ts +62 -0
- package/src/__tests__/slug.test.ts +127 -20
- package/src/__tests__/translations.test.ts +7 -2
- package/src/__tests__/versions.test.ts +93 -67
- package/src/categoryGeneratedIndex.ts +57 -0
- package/src/cli.ts +8 -41
- package/src/client/__tests__/docsClientUtils.test.ts +4 -5
- package/src/client/docsClientUtils.ts +19 -41
- package/{types.d.ts → src/deps.d.ts} +0 -0
- package/src/docFrontMatter.ts +13 -7
- package/src/docs.ts +158 -29
- package/src/globalData.ts +6 -1
- package/src/index.ts +134 -179
- package/src/lastUpdate.ts +10 -9
- package/src/markdown/index.ts +8 -12
- package/src/numberPrefix.ts +5 -3
- package/src/options.ts +59 -28
- package/src/plugin-content-docs.d.ts +179 -35
- package/src/props.ts +91 -16
- package/src/routes.ts +173 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category.js +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-unknown-type.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-wrong-field.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars.json +0 -0
- package/src/{__tests__/__snapshots__/sidebars.test.ts.snap → sidebars/__tests__/__snapshots__/index.test.ts.snap} +36 -6
- package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +143 -18
- package/src/sidebars/__tests__/index.test.ts +204 -0
- package/src/sidebars/__tests__/processor.test.ts +237 -0
- package/src/sidebars/__tests__/utils.test.ts +695 -0
- package/src/sidebars/__tests__/validation.test.ts +105 -0
- package/src/sidebars/generator.ts +310 -0
- package/src/sidebars/index.ts +94 -0
- package/src/sidebars/normalization.ts +112 -0
- package/src/sidebars/processor.ts +154 -0
- package/src/sidebars/types.ts +211 -0
- package/src/sidebars/utils.ts +329 -0
- package/src/sidebars/validation.ts +168 -0
- package/src/slug.ts +32 -17
- package/src/tags.ts +19 -0
- package/src/theme/hooks/useDocs.ts +5 -1
- package/src/translations.ts +103 -47
- package/src/types.ts +67 -105
- package/src/versions.ts +117 -21
- package/lib/sidebarItemsGenerator.js +0 -211
- package/lib/sidebars.d.ts +0 -43
- package/lib/sidebars.js +0 -319
- package/src/__tests__/sidebars.test.ts +0 -639
- package/src/sidebarItemsGenerator.ts +0 -307
- package/src/sidebars.ts +0 -506
package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts}
RENAMED
|
@@ -5,18 +5,14 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
CategoryMetadatasFile,
|
|
10
|
-
DefaultSidebarItemsGenerator,
|
|
11
|
-
} from '../sidebarItemsGenerator';
|
|
12
|
-
import {DefaultCategoryCollapsedValue} from '../sidebars';
|
|
8
|
+
import {CategoryMetadataFile, DefaultSidebarItemsGenerator} from '../generator';
|
|
13
9
|
import {Sidebar, SidebarItemsGenerator} from '../types';
|
|
14
10
|
import fs from 'fs-extra';
|
|
15
|
-
import {DefaultNumberPrefixParser} from '
|
|
11
|
+
import {DefaultNumberPrefixParser} from '../../numberPrefix';
|
|
16
12
|
|
|
17
13
|
describe('DefaultSidebarItemsGenerator', () => {
|
|
18
14
|
function testDefaultSidebarItemsGenerator(
|
|
19
|
-
|
|
15
|
+
params: Partial<Parameters<SidebarItemsGenerator>[0]>,
|
|
20
16
|
) {
|
|
21
17
|
return DefaultSidebarItemsGenerator({
|
|
22
18
|
numberPrefixParser: DefaultNumberPrefixParser,
|
|
@@ -29,21 +25,27 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
29
25
|
contentPath: 'docs',
|
|
30
26
|
},
|
|
31
27
|
docs: [],
|
|
32
|
-
|
|
28
|
+
options: {
|
|
29
|
+
sidebarCollapsed: true,
|
|
30
|
+
sidebarCollapsible: true,
|
|
31
|
+
},
|
|
32
|
+
...params,
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
function mockCategoryMetadataFiles(
|
|
37
|
-
categoryMetadataFiles: Record<string, Partial<
|
|
37
|
+
categoryMetadataFiles: Record<string, Partial<CategoryMetadataFile>>,
|
|
38
38
|
) {
|
|
39
|
-
jest
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
jest
|
|
40
|
+
.spyOn(fs, 'pathExists')
|
|
41
|
+
.mockImplementation(
|
|
42
|
+
(metadataFilePath) =>
|
|
43
|
+
typeof categoryMetadataFiles[metadataFilePath] !== 'undefined',
|
|
44
|
+
);
|
|
42
45
|
jest.spyOn(fs, 'readFile').mockImplementation(
|
|
43
46
|
// @ts-expect-error: annoying TS error due to overrides
|
|
44
|
-
async (metadataFilePath: string) =>
|
|
45
|
-
|
|
46
|
-
},
|
|
47
|
+
async (metadataFilePath: string) =>
|
|
48
|
+
JSON.stringify(categoryMetadataFiles[metadataFilePath]),
|
|
47
49
|
);
|
|
48
50
|
}
|
|
49
51
|
|
|
@@ -110,6 +112,10 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
110
112
|
frontMatter: {},
|
|
111
113
|
},
|
|
112
114
|
],
|
|
115
|
+
options: {
|
|
116
|
+
sidebarCollapsed: true,
|
|
117
|
+
sidebarCollapsible: true,
|
|
118
|
+
},
|
|
113
119
|
});
|
|
114
120
|
|
|
115
121
|
expect(sidebarSlice).toEqual([
|
|
@@ -123,9 +129,15 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
123
129
|
|
|
124
130
|
test('generates complex nested sidebar', async () => {
|
|
125
131
|
mockCategoryMetadataFiles({
|
|
126
|
-
'02-Guides/_category_.json': {collapsed: false},
|
|
132
|
+
'02-Guides/_category_.json': {collapsed: false} as CategoryMetadataFile,
|
|
127
133
|
'02-Guides/01-SubGuides/_category_.yml': {
|
|
128
134
|
label: 'SubGuides (metadata file label)',
|
|
135
|
+
link: {
|
|
136
|
+
type: 'generated-index',
|
|
137
|
+
slug: 'subguides-generated-index-slug',
|
|
138
|
+
title: 'subguides-title',
|
|
139
|
+
description: 'subguides-description',
|
|
140
|
+
},
|
|
129
141
|
},
|
|
130
142
|
});
|
|
131
143
|
|
|
@@ -147,6 +159,13 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
147
159
|
sidebarPosition: 1,
|
|
148
160
|
frontMatter: {},
|
|
149
161
|
},
|
|
162
|
+
{
|
|
163
|
+
id: 'tutorials-index',
|
|
164
|
+
source: 'index.md',
|
|
165
|
+
sourceDirName: '01-Tutorials',
|
|
166
|
+
sidebarPosition: 2,
|
|
167
|
+
frontMatter: {},
|
|
168
|
+
},
|
|
150
169
|
{
|
|
151
170
|
id: 'tutorial2',
|
|
152
171
|
source: 'tutorial2.md',
|
|
@@ -161,6 +180,12 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
161
180
|
sidebarPosition: 1,
|
|
162
181
|
frontMatter: {},
|
|
163
182
|
},
|
|
183
|
+
{
|
|
184
|
+
id: 'guides-index',
|
|
185
|
+
source: '02-Guides.md', // TODO should we allow to just use "Guides.md" to have an index?
|
|
186
|
+
sourceDirName: '02-Guides',
|
|
187
|
+
frontMatter: {},
|
|
188
|
+
},
|
|
164
189
|
{
|
|
165
190
|
id: 'guide2',
|
|
166
191
|
source: 'guide2.md',
|
|
@@ -190,6 +215,10 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
190
215
|
frontMatter: {},
|
|
191
216
|
},
|
|
192
217
|
],
|
|
218
|
+
options: {
|
|
219
|
+
sidebarCollapsed: true,
|
|
220
|
+
sidebarCollapsible: true,
|
|
221
|
+
},
|
|
193
222
|
});
|
|
194
223
|
|
|
195
224
|
expect(sidebarSlice).toEqual([
|
|
@@ -197,7 +226,12 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
197
226
|
{
|
|
198
227
|
type: 'category',
|
|
199
228
|
label: 'Tutorials',
|
|
200
|
-
collapsed:
|
|
229
|
+
collapsed: true,
|
|
230
|
+
collapsible: true,
|
|
231
|
+
link: {
|
|
232
|
+
type: 'doc',
|
|
233
|
+
id: 'tutorials-index',
|
|
234
|
+
},
|
|
201
235
|
items: [
|
|
202
236
|
{type: 'doc', id: 'tutorial1'},
|
|
203
237
|
{type: 'doc', id: 'tutorial2'},
|
|
@@ -207,13 +241,25 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
207
241
|
type: 'category',
|
|
208
242
|
label: 'Guides',
|
|
209
243
|
collapsed: false,
|
|
244
|
+
collapsible: true,
|
|
245
|
+
link: {
|
|
246
|
+
type: 'doc',
|
|
247
|
+
id: 'guides-index',
|
|
248
|
+
},
|
|
210
249
|
items: [
|
|
211
250
|
{type: 'doc', id: 'guide1'},
|
|
212
251
|
{
|
|
213
252
|
type: 'category',
|
|
214
253
|
label: 'SubGuides (metadata file label)',
|
|
215
|
-
collapsed:
|
|
254
|
+
collapsed: true,
|
|
255
|
+
collapsible: true,
|
|
216
256
|
items: [{type: 'doc', id: 'nested-guide'}],
|
|
257
|
+
link: {
|
|
258
|
+
type: 'generated-index',
|
|
259
|
+
slug: 'subguides-generated-index-slug',
|
|
260
|
+
title: 'subguides-title',
|
|
261
|
+
description: 'subguides-description',
|
|
262
|
+
},
|
|
217
263
|
},
|
|
218
264
|
{type: 'doc', id: 'guide2'},
|
|
219
265
|
],
|
|
@@ -233,6 +279,7 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
233
279
|
'subfolder/subsubfolder/subsubsubfolder3/_category_.json': {
|
|
234
280
|
position: 1,
|
|
235
281
|
label: 'subsubsubfolder3 (_category_.json label)',
|
|
282
|
+
collapsible: false,
|
|
236
283
|
collapsed: false,
|
|
237
284
|
},
|
|
238
285
|
});
|
|
@@ -305,6 +352,10 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
305
352
|
frontMatter: {},
|
|
306
353
|
},
|
|
307
354
|
],
|
|
355
|
+
options: {
|
|
356
|
+
sidebarCollapsed: true,
|
|
357
|
+
sidebarCollapsible: true,
|
|
358
|
+
},
|
|
308
359
|
});
|
|
309
360
|
|
|
310
361
|
expect(sidebarSlice).toEqual([
|
|
@@ -312,6 +363,7 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
312
363
|
type: 'category',
|
|
313
364
|
label: 'subsubsubfolder3 (_category_.json label)',
|
|
314
365
|
collapsed: false,
|
|
366
|
+
collapsible: false,
|
|
315
367
|
items: [
|
|
316
368
|
{type: 'doc', id: 'doc8'},
|
|
317
369
|
{type: 'doc', id: 'doc7'},
|
|
@@ -321,6 +373,7 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
321
373
|
type: 'category',
|
|
322
374
|
label: 'subsubsubfolder2 (_category_.yml label)',
|
|
323
375
|
collapsed: true,
|
|
376
|
+
collapsible: true,
|
|
324
377
|
items: [{type: 'doc', id: 'doc6'}],
|
|
325
378
|
},
|
|
326
379
|
{type: 'doc', id: 'doc1'},
|
|
@@ -329,8 +382,80 @@ describe('DefaultSidebarItemsGenerator', () => {
|
|
|
329
382
|
type: 'category',
|
|
330
383
|
label: 'subsubsubfolder',
|
|
331
384
|
collapsed: true,
|
|
385
|
+
collapsible: true,
|
|
332
386
|
items: [{type: 'doc', id: 'doc5'}],
|
|
333
387
|
},
|
|
334
388
|
] as Sidebar);
|
|
335
389
|
});
|
|
390
|
+
|
|
391
|
+
test('uses explicit link over the index/readme.{md,mdx} naming convention', async () => {
|
|
392
|
+
mockCategoryMetadataFiles({
|
|
393
|
+
'Category/_category_.yml': {
|
|
394
|
+
label: 'Category label',
|
|
395
|
+
link: {
|
|
396
|
+
type: 'doc',
|
|
397
|
+
id: 'doc3', // Using a "local doc id" ("doc1" instead of "parent/doc1") on purpose
|
|
398
|
+
},
|
|
399
|
+
},
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
const sidebarSlice = await DefaultSidebarItemsGenerator({
|
|
403
|
+
numberPrefixParser: DefaultNumberPrefixParser,
|
|
404
|
+
item: {
|
|
405
|
+
type: 'autogenerated',
|
|
406
|
+
dirName: '.',
|
|
407
|
+
},
|
|
408
|
+
version: {
|
|
409
|
+
versionName: 'current',
|
|
410
|
+
contentPath: '',
|
|
411
|
+
},
|
|
412
|
+
docs: [
|
|
413
|
+
{
|
|
414
|
+
id: 'parent/doc1',
|
|
415
|
+
source: 'index.md',
|
|
416
|
+
sourceDirName: 'Category',
|
|
417
|
+
frontMatter: {},
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
id: 'parent/doc2',
|
|
421
|
+
source: 'index.md',
|
|
422
|
+
sourceDirName: 'Category',
|
|
423
|
+
frontMatter: {},
|
|
424
|
+
},
|
|
425
|
+
{
|
|
426
|
+
id: 'parent/doc3',
|
|
427
|
+
source: 'doc3.md',
|
|
428
|
+
sourceDirName: 'Category',
|
|
429
|
+
frontMatter: {},
|
|
430
|
+
},
|
|
431
|
+
],
|
|
432
|
+
options: {
|
|
433
|
+
sidebarCollapsed: true,
|
|
434
|
+
sidebarCollapsible: true,
|
|
435
|
+
},
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
expect(sidebarSlice).toEqual([
|
|
439
|
+
{
|
|
440
|
+
type: 'category',
|
|
441
|
+
label: 'Category label',
|
|
442
|
+
collapsed: true,
|
|
443
|
+
collapsible: true,
|
|
444
|
+
link: {
|
|
445
|
+
id: 'parent/doc3',
|
|
446
|
+
type: 'doc',
|
|
447
|
+
},
|
|
448
|
+
items: [
|
|
449
|
+
{
|
|
450
|
+
id: 'parent/doc1',
|
|
451
|
+
type: 'doc',
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
id: 'parent/doc2',
|
|
455
|
+
type: 'doc',
|
|
456
|
+
},
|
|
457
|
+
],
|
|
458
|
+
},
|
|
459
|
+
] as Sidebar);
|
|
460
|
+
});
|
|
336
461
|
});
|
|
@@ -0,0 +1,204 @@
|
|
|
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
|
+
loadNormalizedSidebars,
|
|
11
|
+
DefaultSidebars,
|
|
12
|
+
DisabledSidebars,
|
|
13
|
+
} from '../index';
|
|
14
|
+
import type {NormalizeSidebarsParams, VersionMetadata} from '../../types';
|
|
15
|
+
|
|
16
|
+
describe('loadNormalizedSidebars', () => {
|
|
17
|
+
const fixtureDir = path.join(__dirname, '__fixtures__', 'sidebars');
|
|
18
|
+
const options: NormalizeSidebarsParams = {
|
|
19
|
+
sidebarCollapsed: true,
|
|
20
|
+
sidebarCollapsible: true,
|
|
21
|
+
version: {
|
|
22
|
+
versionName: 'version',
|
|
23
|
+
versionPath: 'versionPath',
|
|
24
|
+
} as VersionMetadata,
|
|
25
|
+
};
|
|
26
|
+
test('sidebars with known sidebar item type', async () => {
|
|
27
|
+
const sidebarPath = path.join(fixtureDir, 'sidebars.json');
|
|
28
|
+
const result = loadNormalizedSidebars(sidebarPath, options);
|
|
29
|
+
expect(result).toMatchSnapshot();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('sidebars with deep level of category', async () => {
|
|
33
|
+
const sidebarPath = path.join(fixtureDir, 'sidebars-category.js');
|
|
34
|
+
const result = loadNormalizedSidebars(sidebarPath, options);
|
|
35
|
+
expect(result).toMatchSnapshot();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('sidebars shorthand and longform lead to exact same sidebar', async () => {
|
|
39
|
+
const sidebarPath1 = path.join(fixtureDir, 'sidebars-category.js');
|
|
40
|
+
const sidebarPath2 = path.join(
|
|
41
|
+
fixtureDir,
|
|
42
|
+
'sidebars-category-shorthand.js',
|
|
43
|
+
);
|
|
44
|
+
const sidebar1 = loadNormalizedSidebars(sidebarPath1, options);
|
|
45
|
+
const sidebar2 = loadNormalizedSidebars(sidebarPath2, options);
|
|
46
|
+
expect(sidebar1).toEqual(sidebar2);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('sidebars with category but category.items is not an array', async () => {
|
|
50
|
+
const sidebarPath = path.join(
|
|
51
|
+
fixtureDir,
|
|
52
|
+
'sidebars-category-wrong-items.json',
|
|
53
|
+
);
|
|
54
|
+
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
|
55
|
+
.toThrowErrorMatchingInlineSnapshot(`
|
|
56
|
+
"{
|
|
57
|
+
\\"type\\": \\"category\\",
|
|
58
|
+
\\"label\\": \\"Category Label\\",
|
|
59
|
+
\\"items\\" [31m[1][0m: \\"doc1\\"
|
|
60
|
+
}
|
|
61
|
+
[31m
|
|
62
|
+
[1] \\"items\\" must be an array[0m"
|
|
63
|
+
`);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('sidebars with category but category label is not a string', async () => {
|
|
67
|
+
const sidebarPath = path.join(
|
|
68
|
+
fixtureDir,
|
|
69
|
+
'sidebars-category-wrong-label.json',
|
|
70
|
+
);
|
|
71
|
+
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
|
72
|
+
.toThrowErrorMatchingInlineSnapshot(`
|
|
73
|
+
"{
|
|
74
|
+
\\"type\\": \\"category\\",
|
|
75
|
+
\\"items\\": [
|
|
76
|
+
\\"doc1\\"
|
|
77
|
+
],
|
|
78
|
+
\\"label\\" [31m[1][0m: true
|
|
79
|
+
}
|
|
80
|
+
[31m
|
|
81
|
+
[1] \\"label\\" must be a string[0m"
|
|
82
|
+
`);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('sidebars item doc but id is not a string', async () => {
|
|
86
|
+
const sidebarPath = path.join(
|
|
87
|
+
fixtureDir,
|
|
88
|
+
'sidebars-doc-id-not-string.json',
|
|
89
|
+
);
|
|
90
|
+
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
|
91
|
+
.toThrowErrorMatchingInlineSnapshot(`
|
|
92
|
+
"{
|
|
93
|
+
\\"type\\": \\"doc\\",
|
|
94
|
+
\\"id\\" [31m[1][0m: [
|
|
95
|
+
\\"doc1\\"
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
[31m
|
|
99
|
+
[1] \\"id\\" must be a string[0m"
|
|
100
|
+
`);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('sidebars with first level not a category', async () => {
|
|
104
|
+
const sidebarPath = path.join(
|
|
105
|
+
fixtureDir,
|
|
106
|
+
'sidebars-first-level-not-category.js',
|
|
107
|
+
);
|
|
108
|
+
const result = loadNormalizedSidebars(sidebarPath, options);
|
|
109
|
+
expect(result).toMatchSnapshot();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('sidebars link', async () => {
|
|
113
|
+
const sidebarPath = path.join(fixtureDir, 'sidebars-link.json');
|
|
114
|
+
const result = loadNormalizedSidebars(sidebarPath, options);
|
|
115
|
+
expect(result).toMatchSnapshot();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('sidebars link wrong label', async () => {
|
|
119
|
+
const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-label.json');
|
|
120
|
+
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
|
121
|
+
.toThrowErrorMatchingInlineSnapshot(`
|
|
122
|
+
"{
|
|
123
|
+
\\"type\\": \\"link\\",
|
|
124
|
+
\\"href\\": \\"https://github.com\\",
|
|
125
|
+
\\"label\\" [31m[1][0m: false
|
|
126
|
+
}
|
|
127
|
+
[31m
|
|
128
|
+
[1] \\"label\\" must be a string[0m"
|
|
129
|
+
`);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('sidebars link wrong href', async () => {
|
|
133
|
+
const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-href.json');
|
|
134
|
+
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
|
135
|
+
.toThrowErrorMatchingInlineSnapshot(`
|
|
136
|
+
"{
|
|
137
|
+
\\"type\\": \\"link\\",
|
|
138
|
+
\\"label\\": \\"GitHub\\",
|
|
139
|
+
\\"href\\" [31m[1][0m: [
|
|
140
|
+
\\"example.com\\"
|
|
141
|
+
]
|
|
142
|
+
}
|
|
143
|
+
[31m
|
|
144
|
+
[1] \\"href\\" contains an invalid value[0m"
|
|
145
|
+
`);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('sidebars with unknown sidebar item type', async () => {
|
|
149
|
+
const sidebarPath = path.join(fixtureDir, 'sidebars-unknown-type.json');
|
|
150
|
+
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
|
151
|
+
.toThrowErrorMatchingInlineSnapshot(`
|
|
152
|
+
"{
|
|
153
|
+
\\"type\\": \\"superman\\",
|
|
154
|
+
[41m\\"undefined\\"[0m[31m [1]: -- missing --[0m
|
|
155
|
+
}
|
|
156
|
+
[31m
|
|
157
|
+
[1] Unknown sidebar item type \\"superman\\".[0m"
|
|
158
|
+
`);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('sidebars with known sidebar item type but wrong field', async () => {
|
|
162
|
+
const sidebarPath = path.join(fixtureDir, 'sidebars-wrong-field.json');
|
|
163
|
+
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
|
164
|
+
.toThrowErrorMatchingInlineSnapshot(`
|
|
165
|
+
"{
|
|
166
|
+
\\"type\\": \\"category\\",
|
|
167
|
+
\\"label\\": \\"category\\",
|
|
168
|
+
\\"href\\": \\"https://github.com\\",
|
|
169
|
+
[41m\\"items\\"[0m[31m [1]: -- missing --[0m
|
|
170
|
+
}
|
|
171
|
+
[31m
|
|
172
|
+
[1] \\"items\\" is required[0m"
|
|
173
|
+
`);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('unexisting path', () => {
|
|
177
|
+
expect(loadNormalizedSidebars('badpath', options)).toEqual(
|
|
178
|
+
DisabledSidebars,
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('undefined path', () => {
|
|
183
|
+
expect(loadNormalizedSidebars(undefined, options)).toEqual(DefaultSidebars);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test('literal false path', () => {
|
|
187
|
+
expect(loadNormalizedSidebars(false, options)).toEqual(DisabledSidebars);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('sidebars with category.collapsed property', async () => {
|
|
191
|
+
const sidebarPath = path.join(fixtureDir, 'sidebars-collapsed.json');
|
|
192
|
+
const result = loadNormalizedSidebars(sidebarPath, options);
|
|
193
|
+
expect(result).toMatchSnapshot();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test('sidebars with category.collapsed property at first level', async () => {
|
|
197
|
+
const sidebarPath = path.join(
|
|
198
|
+
fixtureDir,
|
|
199
|
+
'sidebars-collapsed-first-level.json',
|
|
200
|
+
);
|
|
201
|
+
const result = loadNormalizedSidebars(sidebarPath, options);
|
|
202
|
+
expect(result).toMatchSnapshot();
|
|
203
|
+
});
|
|
204
|
+
});
|