@docusaurus/plugin-content-docs 2.0.0-beta.8e9b829d9 → 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 (103) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/cli.d.ts +1 -1
  3. package/lib/cli.js +18 -23
  4. package/lib/client/docsClientUtils.d.ts +0 -3
  5. package/lib/client/docsClientUtils.js +10 -7
  6. package/lib/docFrontMatter.js +6 -2
  7. package/lib/docs.d.ts +3 -1
  8. package/lib/docs.js +76 -25
  9. package/lib/index.js +70 -77
  10. package/lib/lastUpdate.js +4 -4
  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.js +12 -4
  15. package/lib/props.d.ts +7 -2
  16. package/lib/props.js +26 -3
  17. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +2 -1
  18. package/lib/sidebars/generator.js +174 -0
  19. package/lib/sidebars/index.d.ts +14 -0
  20. package/lib/sidebars/index.js +64 -0
  21. package/lib/sidebars/normalization.d.ts +9 -0
  22. package/lib/sidebars/normalization.js +58 -0
  23. package/lib/sidebars/processor.d.ts +16 -0
  24. package/lib/sidebars/processor.js +70 -0
  25. package/lib/sidebars/types.d.ts +87 -0
  26. package/lib/sidebars/types.js +13 -0
  27. package/lib/sidebars/utils.d.ts +22 -0
  28. package/lib/sidebars/utils.js +101 -0
  29. package/lib/sidebars/validation.d.ts +8 -0
  30. package/lib/sidebars/validation.js +102 -0
  31. package/lib/slug.js +4 -4
  32. package/lib/tags.d.ts +8 -0
  33. package/lib/tags.js +22 -0
  34. package/lib/theme/hooks/useDocs.js +21 -21
  35. package/lib/translations.d.ts +1 -1
  36. package/lib/translations.js +13 -13
  37. package/lib/types.d.ts +29 -61
  38. package/lib/versions.d.ts +1 -1
  39. package/lib/versions.js +40 -20
  40. package/package.json +15 -14
  41. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +5 -0
  42. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +1 -0
  43. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +2 -0
  44. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +2 -0
  45. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +2 -0
  46. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +2 -0
  47. package/src/__tests__/__fixtures__/simple-site/sidebars.json +15 -1
  48. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +6 -0
  49. package/src/__tests__/__snapshots__/cli.test.ts.snap +28 -0
  50. package/src/__tests__/__snapshots__/docs.test.ts.snap +140 -0
  51. package/src/__tests__/__snapshots__/index.test.ts.snap +426 -25
  52. package/src/__tests__/docFrontMatter.test.ts +160 -45
  53. package/src/__tests__/docs.test.ts +167 -21
  54. package/src/__tests__/index.test.ts +53 -27
  55. package/src/__tests__/options.test.ts +4 -1
  56. package/src/__tests__/props.test.ts +62 -0
  57. package/src/__tests__/versions.test.ts +68 -63
  58. package/src/cli.ts +23 -30
  59. package/src/client/docsClientUtils.ts +1 -12
  60. package/src/docFrontMatter.ts +7 -2
  61. package/src/docs.ts +88 -9
  62. package/src/index.ts +77 -91
  63. package/src/markdown/index.ts +8 -12
  64. package/src/numberPrefix.ts +4 -2
  65. package/src/options.ts +13 -1
  66. package/src/plugin-content-docs.d.ts +107 -32
  67. package/src/props.ts +41 -5
  68. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -0
  69. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -0
  70. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -0
  71. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category.js +0 -0
  72. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -0
  73. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed.json +0 -0
  74. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -0
  75. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -0
  76. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -0
  77. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -0
  78. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link.json +0 -0
  79. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-unknown-type.json +0 -0
  80. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-wrong-field.json +0 -0
  81. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars.json +0 -0
  82. package/src/{__tests__/__snapshots__/sidebars.test.ts.snap → sidebars/__tests__/__snapshots__/index.test.ts.snap} +6 -6
  83. package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +2 -2
  84. package/src/sidebars/__tests__/index.test.ts +202 -0
  85. package/src/sidebars/__tests__/processor.test.ts +148 -0
  86. package/src/sidebars/__tests__/utils.test.ts +395 -0
  87. package/src/sidebars/generator.ts +253 -0
  88. package/src/sidebars/index.ts +84 -0
  89. package/src/sidebars/normalization.ts +88 -0
  90. package/src/sidebars/processor.ts +124 -0
  91. package/src/sidebars/types.ts +156 -0
  92. package/src/sidebars/utils.ts +146 -0
  93. package/src/sidebars/validation.ts +124 -0
  94. package/src/tags.ts +21 -0
  95. package/src/translations.ts +26 -36
  96. package/src/types.ts +35 -101
  97. package/src/versions.ts +51 -21
  98. package/lib/sidebarItemsGenerator.js +0 -215
  99. package/lib/sidebars.d.ts +0 -45
  100. package/lib/sidebars.js +0 -354
  101. package/src/__tests__/sidebars.test.ts +0 -746
  102. package/src/sidebarItemsGenerator.ts +0 -315
  103. package/src/sidebars.ts +0 -589
@@ -1,6 +1,6 @@
1
1
  // Jest Snapshot v1, https://goo.gl/fbAQLP
2
2
 
3
- exports[`loadSidebars sidebars link 1`] = `
3
+ exports[`loadUnprocessedSidebars sidebars link 1`] = `
4
4
  Object {
5
5
  "docs": Array [
6
6
  Object {
@@ -20,7 +20,7 @@ Object {
20
20
  }
21
21
  `;
22
22
 
23
- exports[`loadSidebars sidebars with category.collapsed property 1`] = `
23
+ exports[`loadUnprocessedSidebars sidebars with category.collapsed property 1`] = `
24
24
  Object {
25
25
  "docs": Array [
26
26
  Object {
@@ -67,7 +67,7 @@ Object {
67
67
  }
68
68
  `;
69
69
 
70
- exports[`loadSidebars sidebars with category.collapsed property at first level 1`] = `
70
+ exports[`loadUnprocessedSidebars sidebars with category.collapsed property at first level 1`] = `
71
71
  Object {
72
72
  "docs": Array [
73
73
  Object {
@@ -98,7 +98,7 @@ Object {
98
98
  }
99
99
  `;
100
100
 
101
- exports[`loadSidebars sidebars with deep level of category 1`] = `
101
+ exports[`loadUnprocessedSidebars sidebars with deep level of category 1`] = `
102
102
  Object {
103
103
  "docs": Array [
104
104
  Object {
@@ -165,7 +165,7 @@ Object {
165
165
  }
166
166
  `;
167
167
 
168
- exports[`loadSidebars sidebars with first level not a category 1`] = `
168
+ exports[`loadUnprocessedSidebars sidebars with first level not a category 1`] = `
169
169
  Object {
170
170
  "docs": Array [
171
171
  Object {
@@ -188,7 +188,7 @@ Object {
188
188
  }
189
189
  `;
190
190
 
191
- exports[`loadSidebars sidebars with known sidebar item type 1`] = `
191
+ exports[`loadUnprocessedSidebars sidebars with known sidebar item type 1`] = `
192
192
  Object {
193
193
  "docs": Array [
194
194
  Object {
@@ -8,10 +8,10 @@
8
8
  import {
9
9
  CategoryMetadatasFile,
10
10
  DefaultSidebarItemsGenerator,
11
- } from '../sidebarItemsGenerator';
11
+ } from '../generator';
12
12
  import {Sidebar, SidebarItemsGenerator} from '../types';
13
13
  import fs from 'fs-extra';
14
- import {DefaultNumberPrefixParser} from '../numberPrefix';
14
+ import {DefaultNumberPrefixParser} from '../../numberPrefix';
15
15
 
16
16
  describe('DefaultSidebarItemsGenerator', () => {
17
17
  function testDefaultSidebarItemsGenerator(
@@ -0,0 +1,202 @@
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
+ loadUnprocessedSidebars,
11
+ DefaultSidebars,
12
+ DisabledSidebars,
13
+ } from '../index';
14
+ import type {SidebarOptions} from '../../types';
15
+
16
+ describe('loadUnprocessedSidebars', () => {
17
+ const fixtureDir = path.join(__dirname, '__fixtures__', 'sidebars');
18
+ const options: SidebarOptions = {
19
+ sidebarCollapsed: true,
20
+ sidebarCollapsible: true,
21
+ };
22
+ test('sidebars with known sidebar item type', async () => {
23
+ const sidebarPath = path.join(fixtureDir, 'sidebars.json');
24
+ const result = loadUnprocessedSidebars(sidebarPath, options);
25
+ expect(result).toMatchSnapshot();
26
+ });
27
+
28
+ test('sidebars with deep level of category', async () => {
29
+ const sidebarPath = path.join(fixtureDir, 'sidebars-category.js');
30
+ const result = loadUnprocessedSidebars(sidebarPath, options);
31
+ expect(result).toMatchSnapshot();
32
+ });
33
+
34
+ test('sidebars shorthand and longform lead to exact same sidebar', async () => {
35
+ const sidebarPath1 = path.join(fixtureDir, 'sidebars-category.js');
36
+ const sidebarPath2 = path.join(
37
+ fixtureDir,
38
+ 'sidebars-category-shorthand.js',
39
+ );
40
+ const sidebar1 = loadUnprocessedSidebars(sidebarPath1, options);
41
+ const sidebar2 = loadUnprocessedSidebars(sidebarPath2, options);
42
+ expect(sidebar1).toEqual(sidebar2);
43
+ });
44
+
45
+ test('sidebars with category but category.items is not an array', async () => {
46
+ const sidebarPath = path.join(
47
+ fixtureDir,
48
+ 'sidebars-category-wrong-items.json',
49
+ );
50
+ expect(() => loadUnprocessedSidebars(sidebarPath, options))
51
+ .toThrowErrorMatchingInlineSnapshot(`
52
+ "{
53
+ \\"type\\": \\"category\\",
54
+ \\"label\\": \\"Category Label\\",
55
+ \\"items\\" [1]: \\"doc1\\"
56
+ }
57
+ 
58
+ [1] \\"items\\" must be an array"
59
+ `);
60
+ });
61
+
62
+ test('sidebars with category but category label is not a string', async () => {
63
+ const sidebarPath = path.join(
64
+ fixtureDir,
65
+ 'sidebars-category-wrong-label.json',
66
+ );
67
+ expect(() => loadUnprocessedSidebars(sidebarPath, options))
68
+ .toThrowErrorMatchingInlineSnapshot(`
69
+ "{
70
+ \\"type\\": \\"category\\",
71
+ \\"items\\": [
72
+ \\"doc1\\"
73
+ ],
74
+ \\"label\\" [1]: true
75
+ }
76
+ 
77
+ [1] \\"label\\" must be a string"
78
+ `);
79
+ });
80
+
81
+ test('sidebars item doc but id is not a string', async () => {
82
+ const sidebarPath = path.join(
83
+ fixtureDir,
84
+ 'sidebars-doc-id-not-string.json',
85
+ );
86
+ expect(() => loadUnprocessedSidebars(sidebarPath, options))
87
+ .toThrowErrorMatchingInlineSnapshot(`
88
+ "{
89
+ \\"type\\": \\"doc\\",
90
+ \\"id\\" [1]: [
91
+ \\"doc1\\"
92
+ ]
93
+ }
94
+ 
95
+ [1] \\"id\\" must be a string"
96
+ `);
97
+ });
98
+
99
+ test('sidebars with first level not a category', async () => {
100
+ const sidebarPath = path.join(
101
+ fixtureDir,
102
+ 'sidebars-first-level-not-category.js',
103
+ );
104
+ const result = loadUnprocessedSidebars(sidebarPath, options);
105
+ expect(result).toMatchSnapshot();
106
+ });
107
+
108
+ test('sidebars link', async () => {
109
+ const sidebarPath = path.join(fixtureDir, 'sidebars-link.json');
110
+ const result = loadUnprocessedSidebars(sidebarPath, options);
111
+ expect(result).toMatchSnapshot();
112
+ });
113
+
114
+ test('sidebars link wrong label', async () => {
115
+ const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-label.json');
116
+ expect(() => loadUnprocessedSidebars(sidebarPath, options))
117
+ .toThrowErrorMatchingInlineSnapshot(`
118
+ "{
119
+ \\"type\\": \\"link\\",
120
+ \\"href\\": \\"https://github.com\\",
121
+ \\"label\\" [1]: false
122
+ }
123
+ 
124
+ [1] \\"label\\" must be a string"
125
+ `);
126
+ });
127
+
128
+ test('sidebars link wrong href', async () => {
129
+ const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-href.json');
130
+ expect(() => loadUnprocessedSidebars(sidebarPath, options))
131
+ .toThrowErrorMatchingInlineSnapshot(`
132
+ "{
133
+ \\"type\\": \\"link\\",
134
+ \\"label\\": \\"GitHub\\",
135
+ \\"href\\" [1]: [
136
+ \\"example.com\\"
137
+ ]
138
+ }
139
+ 
140
+ [1] \\"href\\" contains an invalid value"
141
+ `);
142
+ });
143
+
144
+ test('sidebars with unknown sidebar item type', async () => {
145
+ const sidebarPath = path.join(fixtureDir, 'sidebars-unknown-type.json');
146
+ expect(() => loadUnprocessedSidebars(sidebarPath, options))
147
+ .toThrowErrorMatchingInlineSnapshot(`
148
+ "{
149
+ \\"type\\": \\"superman\\",
150
+ \\"undefined\\" [1]: -- missing --
151
+ }
152
+ 
153
+ [1] Unknown sidebar item type \\"superman\\"."
154
+ `);
155
+ });
156
+
157
+ test('sidebars with known sidebar item type but wrong field', async () => {
158
+ const sidebarPath = path.join(fixtureDir, 'sidebars-wrong-field.json');
159
+ expect(() => loadUnprocessedSidebars(sidebarPath, options))
160
+ .toThrowErrorMatchingInlineSnapshot(`
161
+ "{
162
+ \\"type\\": \\"category\\",
163
+ \\"label\\": \\"category\\",
164
+ \\"href\\": \\"https://github.com\\",
165
+ \\"items\\" [1]: -- missing --
166
+ }
167
+ 
168
+ [1] \\"items\\" is required"
169
+ `);
170
+ });
171
+
172
+ test('unexisting path', () => {
173
+ expect(loadUnprocessedSidebars('badpath', options)).toEqual(
174
+ DisabledSidebars,
175
+ );
176
+ });
177
+
178
+ test('undefined path', () => {
179
+ expect(loadUnprocessedSidebars(undefined, options)).toEqual(
180
+ DefaultSidebars,
181
+ );
182
+ });
183
+
184
+ test('literal false path', () => {
185
+ expect(loadUnprocessedSidebars(false, options)).toEqual(DisabledSidebars);
186
+ });
187
+
188
+ test('sidebars with category.collapsed property', async () => {
189
+ const sidebarPath = path.join(fixtureDir, 'sidebars-collapsed.json');
190
+ const result = loadUnprocessedSidebars(sidebarPath, options);
191
+ expect(result).toMatchSnapshot();
192
+ });
193
+
194
+ test('sidebars with category.collapsed property at first level', async () => {
195
+ const sidebarPath = path.join(
196
+ fixtureDir,
197
+ 'sidebars-collapsed-first-level.json',
198
+ );
199
+ const result = loadUnprocessedSidebars(sidebarPath, options);
200
+ expect(result).toMatchSnapshot();
201
+ });
202
+ });
@@ -0,0 +1,148 @@
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 {processSidebars} from '../processor';
9
+ import type {
10
+ SidebarItem,
11
+ SidebarItemsGenerator,
12
+ Sidebars,
13
+ NormalizedSidebars,
14
+ } from '../types';
15
+ import {DefaultSidebarItemsGenerator} from '../generator';
16
+
17
+ describe('processSidebars', () => {
18
+ const StaticGeneratedSidebarSlice: SidebarItem[] = [
19
+ {type: 'doc', id: 'doc-generated-id-1'},
20
+ {type: 'doc', id: 'doc-generated-id-2'},
21
+ ];
22
+
23
+ const StaticSidebarItemsGenerator: SidebarItemsGenerator = jest.fn(
24
+ async () => {
25
+ return StaticGeneratedSidebarSlice;
26
+ },
27
+ );
28
+
29
+ async function testProcessSidebars(unprocessedSidebars: NormalizedSidebars) {
30
+ return processSidebars(unprocessedSidebars, {
31
+ sidebarItemsGenerator: StaticSidebarItemsGenerator,
32
+ docs: [],
33
+ // @ts-expect-error: useless for this test
34
+ version: {},
35
+ });
36
+ }
37
+
38
+ test('let sidebars without autogenerated items untouched', async () => {
39
+ const unprocessedSidebars: NormalizedSidebars = {
40
+ someSidebar: [
41
+ {type: 'doc', id: 'doc1'},
42
+ {
43
+ type: 'category',
44
+ collapsed: false,
45
+ collapsible: true,
46
+ items: [{type: 'doc', id: 'doc2'}],
47
+ label: 'Category',
48
+ },
49
+ {type: 'link', href: 'https://facebook.com', label: 'FB'},
50
+ ],
51
+ secondSidebar: [
52
+ {type: 'doc', id: 'doc3'},
53
+ {type: 'link', href: 'https://instagram.com', label: 'IG'},
54
+ {
55
+ type: 'category',
56
+ collapsed: false,
57
+ collapsible: true,
58
+ items: [{type: 'doc', id: 'doc4'}],
59
+ label: 'Category',
60
+ },
61
+ ],
62
+ };
63
+
64
+ const processedSidebar = await testProcessSidebars(unprocessedSidebars);
65
+ expect(processedSidebar).toEqual(unprocessedSidebars);
66
+ });
67
+
68
+ test('replace autogenerated items by generated sidebars slices', async () => {
69
+ const unprocessedSidebars: NormalizedSidebars = {
70
+ someSidebar: [
71
+ {type: 'doc', id: 'doc1'},
72
+ {
73
+ type: 'category',
74
+ collapsed: false,
75
+ collapsible: true,
76
+ items: [
77
+ {type: 'doc', id: 'doc2'},
78
+ {type: 'autogenerated', dirName: 'dir1'},
79
+ ],
80
+ label: 'Category',
81
+ },
82
+ {type: 'link', href: 'https://facebook.com', label: 'FB'},
83
+ ],
84
+ secondSidebar: [
85
+ {type: 'doc', id: 'doc3'},
86
+ {type: 'autogenerated', dirName: 'dir2'},
87
+ {type: 'link', href: 'https://instagram.com', label: 'IG'},
88
+ {type: 'autogenerated', dirName: 'dir3'},
89
+ {
90
+ type: 'category',
91
+ collapsed: false,
92
+ collapsible: true,
93
+ items: [{type: 'doc', id: 'doc4'}],
94
+ label: 'Category',
95
+ },
96
+ ],
97
+ };
98
+
99
+ const processedSidebar = await testProcessSidebars(unprocessedSidebars);
100
+
101
+ expect(StaticSidebarItemsGenerator).toHaveBeenCalledTimes(3);
102
+ expect(StaticSidebarItemsGenerator).toHaveBeenCalledWith({
103
+ defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
104
+ item: {type: 'autogenerated', dirName: 'dir1'},
105
+ docs: [],
106
+ version: {},
107
+ });
108
+ expect(StaticSidebarItemsGenerator).toHaveBeenCalledWith({
109
+ defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
110
+ item: {type: 'autogenerated', dirName: 'dir2'},
111
+ docs: [],
112
+ version: {},
113
+ });
114
+ expect(StaticSidebarItemsGenerator).toHaveBeenCalledWith({
115
+ defaultSidebarItemsGenerator: DefaultSidebarItemsGenerator,
116
+ item: {type: 'autogenerated', dirName: 'dir3'},
117
+ docs: [],
118
+ version: {},
119
+ });
120
+
121
+ expect(processedSidebar).toEqual({
122
+ someSidebar: [
123
+ {type: 'doc', id: 'doc1'},
124
+ {
125
+ type: 'category',
126
+ collapsed: false,
127
+ collapsible: true,
128
+ items: [{type: 'doc', id: 'doc2'}, ...StaticGeneratedSidebarSlice],
129
+ label: 'Category',
130
+ },
131
+ {type: 'link', href: 'https://facebook.com', label: 'FB'},
132
+ ],
133
+ secondSidebar: [
134
+ {type: 'doc', id: 'doc3'},
135
+ ...StaticGeneratedSidebarSlice,
136
+ {type: 'link', href: 'https://instagram.com', label: 'IG'},
137
+ ...StaticGeneratedSidebarSlice,
138
+ {
139
+ type: 'category',
140
+ collapsed: false,
141
+ collapsible: true,
142
+ items: [{type: 'doc', id: 'doc4'}],
143
+ label: 'Category',
144
+ },
145
+ ],
146
+ } as Sidebars);
147
+ });
148
+ });