@docusaurus/plugin-content-docs 0.0.0-4071 → 0.0.0-4075

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 (64) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/cli.d.ts +1 -1
  3. package/lib/cli.js +12 -16
  4. package/lib/index.js +12 -16
  5. package/lib/options.js +2 -2
  6. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +1 -1
  7. package/lib/sidebars/generator.js +174 -0
  8. package/lib/sidebars/index.d.ts +14 -0
  9. package/lib/sidebars/index.js +64 -0
  10. package/lib/sidebars/normalization.d.ts +9 -0
  11. package/lib/sidebars/normalization.js +58 -0
  12. package/lib/sidebars/processor.d.ts +16 -0
  13. package/lib/sidebars/processor.js +70 -0
  14. package/lib/sidebars/types.d.ts +87 -0
  15. package/lib/sidebars/types.js +13 -0
  16. package/lib/sidebars/utils.d.ts +22 -0
  17. package/lib/sidebars/utils.js +101 -0
  18. package/lib/sidebars/validation.d.ts +8 -0
  19. package/lib/sidebars/validation.js +102 -0
  20. package/lib/translations.d.ts +1 -1
  21. package/lib/translations.js +9 -9
  22. package/lib/types.d.ts +1 -58
  23. package/package.json +10 -9
  24. package/src/__tests__/index.test.ts +4 -5
  25. package/src/__tests__/options.test.ts +1 -1
  26. package/src/cli.ts +23 -29
  27. package/src/index.ts +19 -27
  28. package/src/options.ts +1 -1
  29. package/src/plugin-content-docs.d.ts +6 -24
  30. package/src/props.ts +3 -5
  31. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -0
  32. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -0
  33. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -0
  34. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category.js +0 -0
  35. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -0
  36. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed.json +0 -0
  37. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -0
  38. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -0
  39. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -0
  40. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -0
  41. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link.json +0 -0
  42. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-unknown-type.json +0 -0
  43. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-wrong-field.json +0 -0
  44. package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars.json +0 -0
  45. package/src/{__tests__/__snapshots__/sidebars.test.ts.snap → sidebars/__tests__/__snapshots__/index.test.ts.snap} +6 -6
  46. package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +2 -2
  47. package/src/sidebars/__tests__/index.test.ts +202 -0
  48. package/src/sidebars/__tests__/processor.test.ts +148 -0
  49. package/src/sidebars/__tests__/utils.test.ts +395 -0
  50. package/src/sidebars/generator.ts +253 -0
  51. package/src/sidebars/index.ts +84 -0
  52. package/src/sidebars/normalization.ts +88 -0
  53. package/src/sidebars/processor.ts +124 -0
  54. package/src/sidebars/types.ts +156 -0
  55. package/src/sidebars/utils.ts +146 -0
  56. package/src/sidebars/validation.ts +124 -0
  57. package/src/translations.ts +7 -12
  58. package/src/types.ts +1 -94
  59. package/lib/sidebarItemsGenerator.js +0 -222
  60. package/lib/sidebars.d.ts +0 -45
  61. package/lib/sidebars.js +0 -365
  62. package/src/__tests__/sidebars.test.ts +0 -741
  63. package/src/sidebarItemsGenerator.ts +0 -322
  64. package/src/sidebars.ts +0 -609
package/lib/sidebars.js DELETED
@@ -1,365 +0,0 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) Facebook, Inc. and its affiliates.
4
- *
5
- * This source code is licensed under the MIT license found in the
6
- * LICENSE file in the root directory of this source tree.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.createSidebarsUtils = exports.collectSidebarsDocIds = exports.transformSidebarItems = exports.collectSidebarLinks = exports.collectSidebarCategories = exports.collectSidebarDocItems = exports.processSidebars = exports.processSidebar = exports.fixSidebarItemInconsistencies = exports.toSidebarItemsGeneratorVersion = exports.toSidebarItemsGeneratorDoc = exports.loadSidebars = exports.resolveSidebarPathOption = exports.DisabledSidebars = exports.DefaultSidebars = void 0;
10
- const tslib_1 = require("tslib");
11
- const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
12
- const import_fresh_1 = (0, tslib_1.__importDefault)(require("import-fresh"));
13
- const lodash_1 = require("lodash");
14
- const utils_1 = require("@docusaurus/utils");
15
- const combine_promises_1 = (0, tslib_1.__importDefault)(require("combine-promises"));
16
- const sidebarItemsGenerator_1 = require("./sidebarItemsGenerator");
17
- const path_1 = (0, tslib_1.__importDefault)(require("path"));
18
- function isCategoryShorthand(item) {
19
- return typeof item !== 'string' && !item.type;
20
- }
21
- /**
22
- * Convert {category1: [item1,item2]} shorthand syntax to long-form syntax
23
- */
24
- function normalizeCategoryShorthand(sidebar, options) {
25
- return Object.entries(sidebar).map(([label, items]) => ({
26
- type: 'category',
27
- collapsed: options.sidebarCollapsed,
28
- collapsible: options.sidebarCollapsible,
29
- label,
30
- items,
31
- }));
32
- }
33
- /**
34
- * Check that item contains only allowed keys.
35
- */
36
- function assertItem(item, keys) {
37
- const unknownKeys = Object.keys(item).filter((key) => !keys.includes(key) && key !== 'type');
38
- if (unknownKeys.length) {
39
- throw new Error(`Unknown sidebar item keys: ${unknownKeys}. Item: ${JSON.stringify(item)}`);
40
- }
41
- }
42
- function assertIsCategory(item) {
43
- assertItem(item, [
44
- 'items',
45
- 'label',
46
- 'collapsed',
47
- 'collapsible',
48
- 'className',
49
- 'customProps',
50
- ]);
51
- if (typeof item.label !== 'string') {
52
- throw new Error(`Error loading ${JSON.stringify(item)}: "label" must be a string.`);
53
- }
54
- if (!Array.isArray(item.items)) {
55
- throw new Error(`Error loading ${JSON.stringify(item)}: "items" must be an array.`);
56
- }
57
- // "collapsed" is an optional property
58
- if (typeof item.collapsed !== 'undefined' &&
59
- typeof item.collapsed !== 'boolean') {
60
- throw new Error(`Error loading ${JSON.stringify(item)}: "collapsed" must be a boolean.`);
61
- }
62
- if (typeof item.collapsible !== 'undefined' &&
63
- typeof item.collapsible !== 'boolean') {
64
- throw new Error(`Error loading ${JSON.stringify(item)}: "collapsible" must be a boolean.`);
65
- }
66
- if (typeof item.className !== 'undefined' &&
67
- typeof item.className !== 'string') {
68
- throw new Error(`Error loading ${JSON.stringify(item)}: "className" must be a string.`);
69
- }
70
- }
71
- function assertIsAutogenerated(item) {
72
- assertItem(item, ['dirName', 'customProps']);
73
- if (typeof item.dirName !== 'string') {
74
- throw new Error(`Error loading ${JSON.stringify(item)}: "dirName" must be a string.`);
75
- }
76
- if (item.dirName.startsWith('/') || item.dirName.endsWith('/')) {
77
- throw new Error(`Error loading ${JSON.stringify(item)}: "dirName" must be a dir path relative to the docs folder root, and should not start or end with slash`);
78
- }
79
- }
80
- function assertIsDoc(item) {
81
- assertItem(item, ['id', 'label', 'className', 'customProps']);
82
- if (typeof item.id !== 'string') {
83
- throw new Error(`Error loading ${JSON.stringify(item)}: "id" must be a string.`);
84
- }
85
- if (typeof item.label !== 'undefined' && typeof item.label !== 'string') {
86
- throw new Error(`Error loading ${JSON.stringify(item)}: "label" must be a string.`);
87
- }
88
- if (typeof item.className !== 'undefined' &&
89
- typeof item.className !== 'string') {
90
- throw new Error(`Error loading ${JSON.stringify(item)}: "className" must be a string.`);
91
- }
92
- }
93
- function assertIsLink(item) {
94
- assertItem(item, ['href', 'label', 'className', 'customProps']);
95
- if (typeof item.href !== 'string') {
96
- throw new Error(`Error loading ${JSON.stringify(item)}: "href" must be a string.`);
97
- }
98
- if (typeof item.label !== 'string') {
99
- throw new Error(`Error loading ${JSON.stringify(item)}: "label" must be a string.`);
100
- }
101
- if (typeof item.className !== 'undefined' &&
102
- typeof item.className !== 'string') {
103
- throw new Error(`Error loading ${JSON.stringify(item)}: "className" must be a string.`);
104
- }
105
- }
106
- /**
107
- * Normalizes recursively item and all its children. Ensures that at the end
108
- * each item will be an object with the corresponding type.
109
- */
110
- function normalizeItem(item, options) {
111
- var _a, _b;
112
- if (typeof item === 'string') {
113
- return [
114
- {
115
- type: 'doc',
116
- id: item,
117
- },
118
- ];
119
- }
120
- if (isCategoryShorthand(item)) {
121
- return (0, lodash_1.flatMap)(normalizeCategoryShorthand(item, options), (subitem) => normalizeItem(subitem, options));
122
- }
123
- switch (item.type) {
124
- case 'category':
125
- assertIsCategory(item);
126
- return [
127
- {
128
- ...item,
129
- items: (0, lodash_1.flatMap)(item.items, (subItem) => normalizeItem(subItem, options)),
130
- collapsible: (_a = item.collapsible) !== null && _a !== void 0 ? _a : options.sidebarCollapsible,
131
- collapsed: (_b = item.collapsed) !== null && _b !== void 0 ? _b : options.sidebarCollapsed,
132
- },
133
- ];
134
- case 'autogenerated':
135
- assertIsAutogenerated(item);
136
- return [item];
137
- case 'link':
138
- assertIsLink(item);
139
- return [item];
140
- case 'ref':
141
- case 'doc':
142
- assertIsDoc(item);
143
- return [item];
144
- default: {
145
- const extraMigrationError = item.type === 'subcategory'
146
- ? 'Docusaurus v2: "subcategory" has been renamed as "category".'
147
- : '';
148
- throw new Error(`Unknown sidebar item type "${item.type}". Sidebar item is ${JSON.stringify(item)}.\n${extraMigrationError}`);
149
- }
150
- }
151
- }
152
- function normalizeSidebar(sidebar, options) {
153
- const normalizedSidebar = Array.isArray(sidebar)
154
- ? sidebar
155
- : normalizeCategoryShorthand(sidebar, options);
156
- return (0, lodash_1.flatMap)(normalizedSidebar, (subitem) => normalizeItem(subitem, options));
157
- }
158
- function normalizeSidebars(sidebars, options) {
159
- return (0, lodash_1.mapValues)(sidebars, (subitem) => normalizeSidebar(subitem, options));
160
- }
161
- exports.DefaultSidebars = {
162
- defaultSidebar: [
163
- {
164
- type: 'autogenerated',
165
- dirName: '.',
166
- },
167
- ],
168
- };
169
- exports.DisabledSidebars = {};
170
- // If a path is provided, make it absolute
171
- // use this before loadSidebars()
172
- function resolveSidebarPathOption(siteDir, sidebarPathOption) {
173
- return sidebarPathOption
174
- ? path_1.default.resolve(siteDir, sidebarPathOption)
175
- : sidebarPathOption;
176
- }
177
- exports.resolveSidebarPathOption = resolveSidebarPathOption;
178
- // TODO refactor: make async
179
- // Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
180
- function loadSidebars(sidebarFilePath, options) {
181
- // false => no sidebars
182
- if (sidebarFilePath === false) {
183
- return exports.DisabledSidebars;
184
- }
185
- // undefined => defaults to autogenerated sidebars
186
- if (typeof sidebarFilePath === 'undefined') {
187
- return exports.DefaultSidebars;
188
- }
189
- // unexisting sidebars file: no sidebars
190
- // Note: this edge case can happen on versioned docs, not current version
191
- // We avoid creating empty versioned sidebars file with the CLI
192
- if (!fs_extra_1.default.existsSync(sidebarFilePath)) {
193
- return exports.DisabledSidebars;
194
- }
195
- // We don't want sidebars to be cached because of hot reloading.
196
- const sidebarJson = (0, import_fresh_1.default)(sidebarFilePath);
197
- return normalizeSidebars(sidebarJson, options);
198
- }
199
- exports.loadSidebars = loadSidebars;
200
- function toSidebarItemsGeneratorDoc(doc) {
201
- return (0, lodash_1.pick)(doc, [
202
- 'id',
203
- 'frontMatter',
204
- 'source',
205
- 'sourceDirName',
206
- 'sidebarPosition',
207
- ]);
208
- }
209
- exports.toSidebarItemsGeneratorDoc = toSidebarItemsGeneratorDoc;
210
- function toSidebarItemsGeneratorVersion(version) {
211
- return (0, lodash_1.pick)(version, ['versionName', 'contentPath']);
212
- }
213
- exports.toSidebarItemsGeneratorVersion = toSidebarItemsGeneratorVersion;
214
- function fixSidebarItemInconsistencies(item) {
215
- function fixCategoryInconsistencies(category) {
216
- // A non-collapsible category can't be collapsed!
217
- if (!category.collapsible && category.collapsed) {
218
- return {
219
- ...category,
220
- collapsed: false,
221
- };
222
- }
223
- return category;
224
- }
225
- if (item.type === 'category') {
226
- return {
227
- ...fixCategoryInconsistencies(item),
228
- items: item.items.map(fixSidebarItemInconsistencies),
229
- };
230
- }
231
- return item;
232
- }
233
- exports.fixSidebarItemInconsistencies = fixSidebarItemInconsistencies;
234
- // Handle the generation of autogenerated sidebar items and other post-processing checks
235
- async function processSidebar({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebar, docs, version, options, }) {
236
- // Just a minor lazy transformation optimization
237
- const getSidebarItemsGeneratorDocsAndVersion = (0, lodash_1.memoize)(() => ({
238
- docs: docs.map(toSidebarItemsGeneratorDoc),
239
- version: toSidebarItemsGeneratorVersion(version),
240
- }));
241
- async function handleAutoGeneratedItems(item) {
242
- if (item.type === 'category') {
243
- return [
244
- {
245
- ...item,
246
- items: (await Promise.all(item.items.map(handleAutoGeneratedItems))).flat(),
247
- },
248
- ];
249
- }
250
- if (item.type === 'autogenerated') {
251
- return sidebarItemsGenerator({
252
- item,
253
- numberPrefixParser,
254
- defaultSidebarItemsGenerator: sidebarItemsGenerator_1.DefaultSidebarItemsGenerator,
255
- ...getSidebarItemsGeneratorDocsAndVersion(),
256
- options,
257
- });
258
- }
259
- return [item];
260
- }
261
- const processedSidebar = (await Promise.all(unprocessedSidebar.map(handleAutoGeneratedItems))).flat();
262
- return processedSidebar.map(fixSidebarItemInconsistencies);
263
- }
264
- exports.processSidebar = processSidebar;
265
- async function processSidebars({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebars, docs, version, options, }) {
266
- return (0, combine_promises_1.default)((0, lodash_1.mapValues)(unprocessedSidebars, (unprocessedSidebar) => processSidebar({
267
- sidebarItemsGenerator,
268
- numberPrefixParser,
269
- unprocessedSidebar,
270
- docs,
271
- version,
272
- options,
273
- })));
274
- }
275
- exports.processSidebars = processSidebars;
276
- function collectSidebarItemsOfType(type, sidebar) {
277
- function collectRecursive(item) {
278
- const currentItemsCollected = item.type === type ? [item] : [];
279
- const childItemsCollected = item.type === 'category' ? (0, lodash_1.flatten)(item.items.map(collectRecursive)) : [];
280
- return [...currentItemsCollected, ...childItemsCollected];
281
- }
282
- return (0, lodash_1.flatten)(sidebar.map(collectRecursive));
283
- }
284
- function collectSidebarDocItems(sidebar) {
285
- return collectSidebarItemsOfType('doc', sidebar);
286
- }
287
- exports.collectSidebarDocItems = collectSidebarDocItems;
288
- function collectSidebarCategories(sidebar) {
289
- return collectSidebarItemsOfType('category', sidebar);
290
- }
291
- exports.collectSidebarCategories = collectSidebarCategories;
292
- function collectSidebarLinks(sidebar) {
293
- return collectSidebarItemsOfType('link', sidebar);
294
- }
295
- exports.collectSidebarLinks = collectSidebarLinks;
296
- function transformSidebarItems(sidebar, updateFn) {
297
- function transformRecursive(item) {
298
- if (item.type === 'category') {
299
- return updateFn({
300
- ...item,
301
- items: item.items.map(transformRecursive),
302
- });
303
- }
304
- return updateFn(item);
305
- }
306
- return sidebar.map(transformRecursive);
307
- }
308
- exports.transformSidebarItems = transformSidebarItems;
309
- function collectSidebarsDocIds(sidebars) {
310
- return (0, lodash_1.mapValues)(sidebars, (sidebar) => {
311
- return collectSidebarDocItems(sidebar).map((docItem) => docItem.id);
312
- });
313
- }
314
- exports.collectSidebarsDocIds = collectSidebarsDocIds;
315
- function createSidebarsUtils(sidebars) {
316
- const sidebarNameToDocIds = collectSidebarsDocIds(sidebars);
317
- function getFirstDocIdOfFirstSidebar() {
318
- var _a;
319
- return (_a = Object.values(sidebarNameToDocIds)[0]) === null || _a === void 0 ? void 0 : _a[0];
320
- }
321
- function getSidebarNameByDocId(docId) {
322
- // TODO lookup speed can be optimized
323
- const entry = Object.entries(sidebarNameToDocIds).find(([_sidebarName, docIds]) => docIds.includes(docId));
324
- return entry === null || entry === void 0 ? void 0 : entry[0];
325
- }
326
- function getDocNavigation(docId) {
327
- const sidebarName = getSidebarNameByDocId(docId);
328
- if (sidebarName) {
329
- const docIds = sidebarNameToDocIds[sidebarName];
330
- const currentIndex = docIds.indexOf(docId);
331
- const { previous, next } = (0, utils_1.getElementsAround)(docIds, currentIndex);
332
- return {
333
- sidebarName,
334
- previousId: previous,
335
- nextId: next,
336
- };
337
- }
338
- else {
339
- return {
340
- sidebarName: undefined,
341
- previousId: undefined,
342
- nextId: undefined,
343
- };
344
- }
345
- }
346
- function checkSidebarsDocIds(validDocIds, sidebarFilePath) {
347
- const allSidebarDocIds = (0, lodash_1.flatten)(Object.values(sidebarNameToDocIds));
348
- const invalidSidebarDocIds = (0, lodash_1.difference)(allSidebarDocIds, validDocIds);
349
- if (invalidSidebarDocIds.length > 0) {
350
- throw new Error(`Invalid sidebar file at "${(0, utils_1.toMessageRelativeFilePath)(sidebarFilePath)}".
351
- These sidebar document ids do not exist:
352
- - ${invalidSidebarDocIds.sort().join('\n- ')}
353
-
354
- Available document ids are:
355
- - ${validDocIds.sort().join('\n- ')}`);
356
- }
357
- }
358
- return {
359
- getFirstDocIdOfFirstSidebar,
360
- getSidebarNameByDocId,
361
- getDocNavigation,
362
- checkSidebarsDocIds,
363
- };
364
- }
365
- exports.createSidebarsUtils = createSidebarsUtils;