@docusaurus/plugin-content-docs 2.0.0-beta.12faed89d → 2.0.0-beta.13

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 (138) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/categoryGeneratedIndex.d.ts +12 -0
  3. package/lib/categoryGeneratedIndex.js +37 -0
  4. package/lib/cli.d.ts +2 -2
  5. package/lib/cli.js +12 -34
  6. package/lib/client/docsClientUtils.d.ts +0 -3
  7. package/lib/client/docsClientUtils.js +19 -22
  8. package/lib/docFrontMatter.d.ts +1 -1
  9. package/lib/docFrontMatter.js +7 -3
  10. package/lib/docs.d.ts +25 -3
  11. package/lib/docs.js +125 -41
  12. package/lib/globalData.d.ts +1 -1
  13. package/lib/index.d.ts +1 -1
  14. package/lib/index.js +100 -131
  15. package/lib/lastUpdate.js +8 -9
  16. package/lib/markdown/index.d.ts +3 -6
  17. package/lib/markdown/index.js +3 -3
  18. package/lib/markdown/linkify.js +2 -2
  19. package/lib/numberPrefix.d.ts +1 -1
  20. package/lib/options.d.ts +3 -3
  21. package/lib/options.js +48 -11
  22. package/lib/props.d.ts +7 -2
  23. package/lib/props.js +60 -8
  24. package/lib/routes.d.ts +27 -0
  25. package/lib/routes.js +105 -0
  26. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +5 -2
  27. package/lib/sidebars/generator.js +216 -0
  28. package/lib/sidebars/index.d.ts +15 -0
  29. package/lib/sidebars/index.js +73 -0
  30. package/lib/sidebars/normalization.d.ts +14 -0
  31. package/lib/sidebars/normalization.js +77 -0
  32. package/lib/sidebars/processor.d.ts +18 -0
  33. package/lib/sidebars/processor.js +85 -0
  34. package/lib/sidebars/types.d.ts +127 -0
  35. package/lib/sidebars/types.js +8 -0
  36. package/lib/sidebars/utils.d.ts +35 -0
  37. package/lib/sidebars/utils.js +228 -0
  38. package/lib/sidebars/validation.d.ts +10 -0
  39. package/lib/sidebars/validation.js +138 -0
  40. package/lib/slug.d.ts +4 -3
  41. package/lib/slug.js +27 -15
  42. package/lib/tags.d.ts +8 -0
  43. package/lib/tags.js +20 -0
  44. package/lib/theme/hooks/useDocs.js +21 -21
  45. package/lib/translations.d.ts +2 -2
  46. package/lib/translations.js +71 -29
  47. package/lib/types.d.ts +52 -63
  48. package/lib/versions.d.ts +3 -3
  49. package/lib/versions.js +41 -22
  50. package/package.json +20 -20
  51. package/src/__tests__/__fixtures__/simple-site/docs/_partials/somePartial.md +3 -0
  52. package/src/__tests__/__fixtures__/simple-site/docs/_partials/subfolder/somePartial.md +3 -0
  53. package/src/__tests__/__fixtures__/simple-site/docs/_somePartial.md +3 -0
  54. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +5 -0
  55. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +2 -0
  56. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +2 -0
  57. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +2 -0
  58. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +2 -0
  59. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +2 -0
  60. package/src/__tests__/__fixtures__/simple-site/sidebars.json +15 -1
  61. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +1 -0
  62. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +6 -0
  63. package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +3 -0
  64. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +3 -0
  65. package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +3 -0
  66. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +3 -0
  67. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/somePartial.md +3 -0
  68. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/subfolder/somePartial.md +3 -0
  69. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_somePartial.md +3 -0
  70. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +3 -0
  71. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +2 -2
  72. package/src/__tests__/__snapshots__/cli.test.ts.snap +48 -73
  73. package/src/__tests__/__snapshots__/docs.test.ts.snap +140 -0
  74. package/src/__tests__/__snapshots__/index.test.ts.snap +745 -97
  75. package/src/__tests__/__snapshots__/translations.test.ts.snap +45 -15
  76. package/src/__tests__/cli.test.ts +15 -11
  77. package/src/__tests__/docFrontMatter.test.ts +160 -45
  78. package/src/__tests__/docs.test.ts +311 -150
  79. package/src/__tests__/index.test.ts +108 -66
  80. package/src/__tests__/lastUpdate.test.ts +1 -1
  81. package/src/__tests__/options.test.ts +48 -3
  82. package/src/__tests__/props.test.ts +62 -0
  83. package/src/__tests__/slug.test.ts +127 -20
  84. package/src/__tests__/translations.test.ts +7 -1
  85. package/src/__tests__/versions.test.ts +73 -70
  86. package/src/categoryGeneratedIndex.ts +57 -0
  87. package/src/cli.ts +8 -41
  88. package/src/client/docsClientUtils.ts +14 -26
  89. package/{types.d.ts → src/deps.d.ts} +0 -0
  90. package/src/docFrontMatter.ts +9 -4
  91. package/src/docs.ts +158 -32
  92. package/src/globalData.ts +6 -1
  93. package/src/index.ts +125 -169
  94. package/src/lastUpdate.ts +10 -13
  95. package/src/markdown/index.ts +8 -12
  96. package/src/numberPrefix.ts +5 -3
  97. package/src/options.ts +59 -14
  98. package/src/plugin-content-docs.d.ts +173 -40
  99. package/src/props.ts +90 -15
  100. package/src/routes.ts +173 -0
  101. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -0
  102. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -0
  103. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -0
  104. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category.js +0 -0
  105. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -0
  106. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed.json +0 -0
  107. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -0
  108. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -0
  109. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -0
  110. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -0
  111. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link.json +0 -0
  112. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-unknown-type.json +0 -0
  113. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-wrong-field.json +0 -0
  114. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars.json +0 -0
  115. package/src/{__tests__/__snapshots__/sidebars.test.ts.snap → sidebars/__tests__/__snapshots__/index.test.ts.snap} +36 -6
  116. package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +143 -18
  117. package/src/sidebars/__tests__/index.test.ts +204 -0
  118. package/src/sidebars/__tests__/processor.test.ts +237 -0
  119. package/src/sidebars/__tests__/utils.test.ts +695 -0
  120. package/src/sidebars/__tests__/validation.test.ts +105 -0
  121. package/src/sidebars/generator.ts +310 -0
  122. package/src/sidebars/index.ts +94 -0
  123. package/src/sidebars/normalization.ts +112 -0
  124. package/src/sidebars/processor.ts +154 -0
  125. package/src/sidebars/types.ts +211 -0
  126. package/src/sidebars/utils.ts +329 -0
  127. package/src/sidebars/validation.ts +168 -0
  128. package/src/slug.ts +32 -17
  129. package/src/tags.ts +19 -0
  130. package/src/translations.ts +103 -47
  131. package/src/types.ts +64 -107
  132. package/src/versions.ts +59 -25
  133. package/lib/sidebarItemsGenerator.js +0 -211
  134. package/lib/sidebars.d.ts +0 -43
  135. package/lib/sidebars.js +0 -320
  136. package/src/__tests__/sidebars.test.ts +0 -639
  137. package/src/sidebarItemsGenerator.ts +0 -307
  138. package/src/sidebars.ts +0 -522
@@ -1,10 +1,11 @@
1
1
  // Jest Snapshot v1, https://goo.gl/fbAQLP
2
2
 
3
- exports[`loadSidebars sidebars link 1`] = `
3
+ exports[`loadNormalizedSidebars sidebars link 1`] = `
4
4
  Object {
5
5
  "docs": Array [
6
6
  Object {
7
7
  "collapsed": true,
8
+ "collapsible": true,
8
9
  "items": Array [
9
10
  Object {
10
11
  "href": "https://github.com",
@@ -13,20 +14,23 @@ Object {
13
14
  },
14
15
  ],
15
16
  "label": "Test",
17
+ "link": undefined,
16
18
  "type": "category",
17
19
  },
18
20
  ],
19
21
  }
20
22
  `;
21
23
 
22
- exports[`loadSidebars sidebars with category.collapsed property 1`] = `
24
+ exports[`loadNormalizedSidebars sidebars with category.collapsed property 1`] = `
23
25
  Object {
24
26
  "docs": Array [
25
27
  Object {
26
28
  "collapsed": true,
29
+ "collapsible": true,
27
30
  "items": Array [
28
31
  Object {
29
32
  "collapsed": false,
33
+ "collapsible": true,
30
34
  "items": Array [
31
35
  Object {
32
36
  "id": "doc1",
@@ -34,17 +38,21 @@ Object {
34
38
  },
35
39
  ],
36
40
  "label": "Introduction",
41
+ "link": undefined,
37
42
  "type": "category",
38
43
  },
39
44
  ],
40
45
  "label": "Test",
46
+ "link": undefined,
41
47
  "type": "category",
42
48
  },
43
49
  Object {
44
50
  "collapsed": true,
51
+ "collapsible": true,
45
52
  "items": Array [
46
53
  Object {
47
54
  "collapsed": false,
55
+ "collapsible": true,
48
56
  "items": Array [
49
57
  Object {
50
58
  "id": "doc2",
@@ -52,21 +60,24 @@ Object {
52
60
  },
53
61
  ],
54
62
  "label": "Powering MDX",
63
+ "link": undefined,
55
64
  "type": "category",
56
65
  },
57
66
  ],
58
67
  "label": "Reference",
68
+ "link": undefined,
59
69
  "type": "category",
60
70
  },
61
71
  ],
62
72
  }
63
73
  `;
64
74
 
65
- exports[`loadSidebars sidebars with category.collapsed property at first level 1`] = `
75
+ exports[`loadNormalizedSidebars sidebars with category.collapsed property at first level 1`] = `
66
76
  Object {
67
77
  "docs": Array [
68
78
  Object {
69
79
  "collapsed": false,
80
+ "collapsible": true,
70
81
  "items": Array [
71
82
  Object {
72
83
  "id": "doc1",
@@ -74,10 +85,12 @@ Object {
74
85
  },
75
86
  ],
76
87
  "label": "Introduction",
88
+ "link": undefined,
77
89
  "type": "category",
78
90
  },
79
91
  Object {
80
92
  "collapsed": false,
93
+ "collapsible": true,
81
94
  "items": Array [
82
95
  Object {
83
96
  "id": "doc2",
@@ -85,17 +98,19 @@ Object {
85
98
  },
86
99
  ],
87
100
  "label": "Powering MDX",
101
+ "link": undefined,
88
102
  "type": "category",
89
103
  },
90
104
  ],
91
105
  }
92
106
  `;
93
107
 
94
- exports[`loadSidebars sidebars with deep level of category 1`] = `
108
+ exports[`loadNormalizedSidebars sidebars with deep level of category 1`] = `
95
109
  Object {
96
110
  "docs": Array [
97
111
  Object {
98
112
  "collapsed": true,
113
+ "collapsible": true,
99
114
  "items": Array [
100
115
  Object {
101
116
  "id": "a",
@@ -103,9 +118,11 @@ Object {
103
118
  },
104
119
  Object {
105
120
  "collapsed": true,
121
+ "collapsible": true,
106
122
  "items": Array [
107
123
  Object {
108
124
  "collapsed": true,
125
+ "collapsible": true,
109
126
  "items": Array [
110
127
  Object {
111
128
  "id": "c",
@@ -113,6 +130,7 @@ Object {
113
130
  },
114
131
  Object {
115
132
  "collapsed": true,
133
+ "collapsible": true,
116
134
  "items": Array [
117
135
  Object {
118
136
  "id": "d",
@@ -120,6 +138,7 @@ Object {
120
138
  },
121
139
  Object {
122
140
  "collapsed": true,
141
+ "collapsible": true,
123
142
  "items": Array [
124
143
  Object {
125
144
  "id": "e",
@@ -127,14 +146,17 @@ Object {
127
146
  },
128
147
  ],
129
148
  "label": "deeper more more",
149
+ "link": undefined,
130
150
  "type": "category",
131
151
  },
132
152
  ],
133
153
  "label": "level 4",
154
+ "link": undefined,
134
155
  "type": "category",
135
156
  },
136
157
  ],
137
158
  "label": "level 3",
159
+ "link": undefined,
138
160
  "type": "category",
139
161
  },
140
162
  Object {
@@ -143,21 +165,24 @@ Object {
143
165
  },
144
166
  ],
145
167
  "label": "level 2",
168
+ "link": undefined,
146
169
  "type": "category",
147
170
  },
148
171
  ],
149
172
  "label": "level 1",
173
+ "link": undefined,
150
174
  "type": "category",
151
175
  },
152
176
  ],
153
177
  }
154
178
  `;
155
179
 
156
- exports[`loadSidebars sidebars with first level not a category 1`] = `
180
+ exports[`loadNormalizedSidebars sidebars with first level not a category 1`] = `
157
181
  Object {
158
182
  "docs": Array [
159
183
  Object {
160
184
  "collapsed": true,
185
+ "collapsible": true,
161
186
  "items": Array [
162
187
  Object {
163
188
  "id": "greeting",
@@ -165,6 +190,7 @@ Object {
165
190
  },
166
191
  ],
167
192
  "label": "Getting Started",
193
+ "link": undefined,
168
194
  "type": "category",
169
195
  },
170
196
  Object {
@@ -175,11 +201,12 @@ Object {
175
201
  }
176
202
  `;
177
203
 
178
- exports[`loadSidebars sidebars with known sidebar item type 1`] = `
204
+ exports[`loadNormalizedSidebars sidebars with known sidebar item type 1`] = `
179
205
  Object {
180
206
  "docs": Array [
181
207
  Object {
182
208
  "collapsed": true,
209
+ "collapsible": true,
183
210
  "items": Array [
184
211
  Object {
185
212
  "id": "foo/bar",
@@ -200,10 +227,12 @@ Object {
200
227
  },
201
228
  ],
202
229
  "label": "Test",
230
+ "link": undefined,
203
231
  "type": "category",
204
232
  },
205
233
  Object {
206
234
  "collapsed": true,
235
+ "collapsible": true,
207
236
  "items": Array [
208
237
  Object {
209
238
  "id": "hello",
@@ -211,6 +240,7 @@ Object {
211
240
  },
212
241
  ],
213
242
  "label": "Guides",
243
+ "link": undefined,
214
244
  "type": "category",
215
245
  },
216
246
  ],
@@ -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 '../numberPrefix';
11
+ import {DefaultNumberPrefixParser} from '../../numberPrefix';
16
12
 
17
13
  describe('DefaultSidebarItemsGenerator', () => {
18
14
  function testDefaultSidebarItemsGenerator(
19
- options: Partial<Parameters<SidebarItemsGenerator>[0]>,
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
- ...options,
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<CategoryMetadatasFile>>,
37
+ categoryMetadataFiles: Record<string, Partial<CategoryMetadataFile>>,
38
38
  ) {
39
- jest.spyOn(fs, 'pathExists').mockImplementation((metadataFilePath) => {
40
- return typeof categoryMetadataFiles[metadataFilePath] !== 'undefined';
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
- return JSON.stringify(categoryMetadataFiles[metadataFilePath]);
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: DefaultCategoryCollapsedValue,
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: DefaultCategoryCollapsedValue,
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\\" [1]: \\"doc1\\"
60
+ }
61
+ 
62
+ [1] \\"items\\" must be an array"
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\\" [1]: true
79
+ }
80
+ 
81
+ [1] \\"label\\" must be a string"
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\\" [1]: [
95
+ \\"doc1\\"
96
+ ]
97
+ }
98
+ 
99
+ [1] \\"id\\" must be a string"
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\\" [1]: false
126
+ }
127
+ 
128
+ [1] \\"label\\" must be a string"
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\\" [1]: [
140
+ \\"example.com\\"
141
+ ]
142
+ }
143
+ 
144
+ [1] \\"href\\" contains an invalid value"
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
+ \\"undefined\\" [1]: -- missing --
155
+ }
156
+ 
157
+ [1] Unknown sidebar item type \\"superman\\"."
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
+ \\"items\\" [1]: -- missing --
170
+ }
171
+ 
172
+ [1] \\"items\\" is required"
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
+ });