@docusaurus/plugin-content-docs 0.0.0-4547 → 0.0.0-4548

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/cli.js CHANGED
@@ -18,7 +18,8 @@ async function createVersionedSidebarFile({ siteDir, pluginId, sidebarPath, vers
18
18
  // Load current sidebar and create a new versioned sidebars file (if needed).
19
19
  // Note: we don't need the sidebars file to be normalized: it's ok to let
20
20
  // plugin option changes to impact older, versioned sidebars
21
- const sidebars = await (0, sidebars_1.loadSidebarsFile)(sidebarPath);
21
+ // We don't validate here, assuming the user has already built the version
22
+ const sidebars = await (0, sidebars_1.loadSidebarsFileUnsafe)(sidebarPath);
22
23
  // Do not create a useless versioned sidebars file if sidebars file is empty
23
24
  // or sidebars are disabled/false)
24
25
  const shouldCreateVersionedSidebarFile = Object.keys(sidebars).length > 0;
@@ -23,7 +23,7 @@ function getLocalDocId(docId) {
23
23
  exports.CategoryMetadataFilenameBase = '_category_';
24
24
  exports.CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
25
25
  // Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
26
- const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, isCategoryIndex, docs: allDocs, options, item: { dirName: autogenDir }, categoriesMetadata, }) => {
26
+ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, isCategoryIndex, docs: allDocs, item: { dirName: autogenDir }, categoriesMetadata, }) => {
27
27
  const docsById = (0, docs_1.createDocsByIdIndex)(allDocs);
28
28
  const findDoc = (docId) => docsById[docId];
29
29
  const getDoc = (docId) => {
@@ -101,7 +101,7 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, isCategoryInde
101
101
  };
102
102
  }
103
103
  async function createCategoryItem(dir, fullPath, folderName) {
104
- var _a, _b, _c, _d;
104
+ var _a, _b;
105
105
  const categoryMetadata = categoriesMetadata[(0, utils_1.posixPath)(path_1.default.join(autogenDir, fullPath))];
106
106
  const className = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.className;
107
107
  const { filename, numberPrefix } = numberPrefixParser(folderName);
@@ -140,16 +140,15 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, isCategoryInde
140
140
  type: 'doc',
141
141
  id: categoryLinkedDocId, // We "remap" a potentially "local id" to a "qualified id"
142
142
  }
143
- : // TODO typing issue
144
- categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.link;
143
+ : categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.link;
145
144
  // If a doc is linked, remove it from the category subItems
146
145
  const items = allItems.filter((item) => !(item.type === 'doc' && item.id === categoryLinkedDocId));
147
146
  return {
148
147
  type: 'category',
149
148
  label: (_a = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.label) !== null && _a !== void 0 ? _a : filename,
150
- collapsible: (_b = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.collapsible) !== null && _b !== void 0 ? _b : options.sidebarCollapsible,
151
- collapsed: (_c = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.collapsed) !== null && _c !== void 0 ? _c : options.sidebarCollapsed,
152
- position: (_d = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.position) !== null && _d !== void 0 ? _d : numberPrefix,
149
+ collapsible: categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.collapsible,
150
+ collapsed: categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.collapsed,
151
+ position: (_b = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.position) !== null && _b !== void 0 ? _b : numberPrefix,
153
152
  ...(className !== undefined && { className }),
154
153
  items,
155
154
  ...(link && { link }),
@@ -4,13 +4,10 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import type { SidebarsConfig, Sidebars, NormalizedSidebars } from './types';
8
- import type { NormalizeSidebarsParams } from '../types';
9
- import { type SidebarProcessorParams } from './processor';
7
+ import type { SidebarsConfig, Sidebars, SidebarProcessorParams } from './types';
10
8
  import type { PluginOptions } from '@docusaurus/plugin-content-docs';
11
9
  export declare const DefaultSidebars: SidebarsConfig;
12
10
  export declare const DisabledSidebars: SidebarsConfig;
13
11
  export declare function resolveSidebarPathOption(siteDir: string, sidebarPathOption: PluginOptions['sidebarPath']): PluginOptions['sidebarPath'];
14
- export declare function loadSidebarsFile(sidebarFilePath: string | false | undefined): Promise<SidebarsConfig>;
15
- export declare function loadNormalizedSidebars(sidebarFilePath: string | false | undefined, params: NormalizeSidebarsParams): Promise<NormalizedSidebars>;
16
- export declare function loadSidebars(sidebarFilePath: string | false | undefined, options: Omit<SidebarProcessorParams, 'categoriesMetadata'>): Promise<Sidebars>;
12
+ export declare function loadSidebarsFileUnsafe(sidebarFilePath: string | false | undefined): Promise<SidebarsConfig>;
13
+ export declare function loadSidebars(sidebarFilePath: string | false | undefined, options: SidebarProcessorParams): Promise<Sidebars>;
@@ -6,13 +6,14 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.loadSidebars = exports.loadNormalizedSidebars = exports.loadSidebarsFile = exports.resolveSidebarPathOption = exports.DisabledSidebars = exports.DefaultSidebars = void 0;
9
+ exports.loadSidebars = exports.loadSidebarsFileUnsafe = exports.resolveSidebarPathOption = exports.DisabledSidebars = exports.DefaultSidebars = void 0;
10
10
  const tslib_1 = require("tslib");
11
11
  const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
12
12
  const import_fresh_1 = (0, tslib_1.__importDefault)(require("import-fresh"));
13
13
  const validation_1 = require("./validation");
14
14
  const normalization_1 = require("./normalization");
15
15
  const processor_1 = require("./processor");
16
+ const postProcessor_1 = require("./postProcessor");
16
17
  const path_1 = (0, tslib_1.__importDefault)(require("path"));
17
18
  const utils_1 = require("@docusaurus/utils");
18
19
  const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
@@ -74,25 +75,14 @@ async function loadSidebarsFileUnsafe(sidebarFilePath) {
74
75
  // We don't want sidebars to be cached because of hot reloading.
75
76
  return (0, import_fresh_1.default)(sidebarFilePath);
76
77
  }
77
- async function loadSidebarsFile(sidebarFilePath) {
78
- const sidebarsConfig = await loadSidebarsFileUnsafe(sidebarFilePath);
79
- (0, validation_1.validateSidebars)(sidebarsConfig);
80
- return sidebarsConfig;
81
- }
82
- exports.loadSidebarsFile = loadSidebarsFile;
83
- async function loadNormalizedSidebars(sidebarFilePath, params) {
84
- return (0, normalization_1.normalizeSidebars)(await loadSidebarsFile(sidebarFilePath), params);
85
- }
86
- exports.loadNormalizedSidebars = loadNormalizedSidebars;
78
+ exports.loadSidebarsFileUnsafe = loadSidebarsFileUnsafe;
87
79
  // Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
88
80
  async function loadSidebars(sidebarFilePath, options) {
89
- const normalizeSidebarsParams = {
90
- ...options.sidebarOptions,
91
- version: options.version,
92
- categoryLabelSlugger: (0, utils_1.createSlugger)(),
93
- };
94
- const normalizedSidebars = await loadNormalizedSidebars(sidebarFilePath, normalizeSidebarsParams);
81
+ const sidebarsConfig = await loadSidebarsFileUnsafe(sidebarFilePath);
82
+ const normalizedSidebars = (0, normalization_1.normalizeSidebars)(sidebarsConfig);
83
+ (0, validation_1.validateSidebars)(normalizedSidebars);
95
84
  const categoriesMetadata = await readCategoriesMetadata(options.version.contentPath);
96
- return (0, processor_1.processSidebars)(normalizedSidebars, { ...options, categoriesMetadata });
85
+ const processedSidebars = await (0, processor_1.processSidebars)(normalizedSidebars, categoriesMetadata, options);
86
+ return (0, postProcessor_1.postProcessSidebars)(processedSidebars, options);
97
87
  }
98
88
  exports.loadSidebars = loadSidebars;
@@ -4,11 +4,10 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import type { NormalizeSidebarsParams } from '../types';
8
7
  import type { NormalizedSidebarItem, NormalizedSidebars, SidebarItemConfig, SidebarsConfig } from './types';
9
8
  /**
10
9
  * Normalizes recursively item and all its children. Ensures that at the end
11
10
  * each item will be an object with the corresponding type.
12
11
  */
13
- export declare function normalizeItem(item: SidebarItemConfig, options: NormalizeSidebarsParams): NormalizedSidebarItem[];
14
- export declare function normalizeSidebars(sidebars: SidebarsConfig, params: NormalizeSidebarsParams): NormalizedSidebars;
12
+ export declare function normalizeItem(item: SidebarItemConfig): NormalizedSidebarItem[];
13
+ export declare function normalizeSidebars(sidebars: SidebarsConfig): NormalizedSidebars;
@@ -9,27 +9,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.normalizeSidebars = exports.normalizeItem = void 0;
10
10
  const utils_1 = require("./utils");
11
11
  const lodash_1 = require("lodash");
12
- const utils_2 = require("@docusaurus/utils");
13
- function normalizeCategoryLink(category, params) {
14
- var _a, _b;
15
- if (((_a = category.link) === null || _a === void 0 ? void 0 : _a.type) === 'generated-index') {
16
- // default slug logic can be improved
17
- const getDefaultSlug = () => `/category/${params.categoryLabelSlugger.slug(category.label)}`;
18
- const slug = (_b = category.link.slug) !== null && _b !== void 0 ? _b : getDefaultSlug();
19
- const permalink = (0, utils_2.normalizeUrl)([params.version.versionPath, slug]);
20
- return {
21
- ...category.link,
22
- slug,
23
- permalink,
24
- };
25
- }
26
- return category.link;
27
- }
28
- function normalizeCategoriesShorthand(sidebar, options) {
12
+ function normalizeCategoriesShorthand(sidebar) {
29
13
  return Object.entries(sidebar).map(([label, items]) => ({
30
14
  type: 'category',
31
- collapsed: options.sidebarCollapsed,
32
- collapsible: options.sidebarCollapsible,
33
15
  label,
34
16
  items,
35
17
  }));
@@ -38,8 +20,7 @@ function normalizeCategoriesShorthand(sidebar, options) {
38
20
  * Normalizes recursively item and all its children. Ensures that at the end
39
21
  * each item will be an object with the corresponding type.
40
22
  */
41
- function normalizeItem(item, options) {
42
- var _a, _b, _c;
23
+ function normalizeItem(item) {
43
24
  if (typeof item === 'string') {
44
25
  return [
45
26
  {
@@ -49,29 +30,28 @@ function normalizeItem(item, options) {
49
30
  ];
50
31
  }
51
32
  if ((0, utils_1.isCategoriesShorthand)(item)) {
52
- return normalizeCategoriesShorthand(item, options).flatMap((subItem) => normalizeItem(subItem, options));
33
+ return normalizeCategoriesShorthand(item).flatMap((subItem) => normalizeItem(subItem));
53
34
  }
54
35
  if (item.type === 'category') {
55
- const link = normalizeCategoryLink(item, options);
36
+ if (typeof item.items !== 'undefined' && !Array.isArray(item.items)) {
37
+ throw new Error(`Invalid category ${JSON.stringify(item)}: items must be an array`);
38
+ }
56
39
  const normalizedCategory = {
57
40
  ...item,
58
- link,
59
- items: ((_a = item.items) !== null && _a !== void 0 ? _a : []).flatMap((subItem) => normalizeItem(subItem, options)),
60
- collapsible: (_b = item.collapsible) !== null && _b !== void 0 ? _b : options.sidebarCollapsible,
61
- collapsed: (_c = item.collapsed) !== null && _c !== void 0 ? _c : options.sidebarCollapsed,
41
+ items: item.items.flatMap((subItem) => normalizeItem(subItem)),
62
42
  };
63
43
  return [normalizedCategory];
64
44
  }
65
45
  return [item];
66
46
  }
67
47
  exports.normalizeItem = normalizeItem;
68
- function normalizeSidebar(sidebar, options) {
48
+ function normalizeSidebar(sidebar) {
69
49
  const normalizedSidebar = Array.isArray(sidebar)
70
50
  ? sidebar
71
- : normalizeCategoriesShorthand(sidebar, options);
72
- return normalizedSidebar.flatMap((subItem) => normalizeItem(subItem, options));
51
+ : normalizeCategoriesShorthand(sidebar);
52
+ return normalizedSidebar.flatMap((subItem) => normalizeItem(subItem));
73
53
  }
74
- function normalizeSidebars(sidebars, params) {
75
- return (0, lodash_1.mapValues)(sidebars, (items) => normalizeSidebar(items, params));
54
+ function normalizeSidebars(sidebars) {
55
+ return (0, lodash_1.mapValues)(sidebars, normalizeSidebar);
76
56
  }
77
57
  exports.normalizeSidebars = normalizeSidebars;
@@ -0,0 +1,8 @@
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
+ import type { Sidebars, SidebarProcessorParams, ProcessedSidebars } from './types';
8
+ export declare function postProcessSidebars(sidebars: ProcessedSidebars, params: SidebarProcessorParams): Sidebars;
@@ -0,0 +1,71 @@
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.postProcessSidebars = void 0;
10
+ const utils_1 = require("@docusaurus/utils");
11
+ const lodash_1 = require("lodash");
12
+ function normalizeCategoryLink(category, params) {
13
+ var _a, _b;
14
+ if (((_a = category.link) === null || _a === void 0 ? void 0 : _a.type) === 'generated-index') {
15
+ // default slug logic can be improved
16
+ const getDefaultSlug = () => `/category/${params.categoryLabelSlugger.slug(category.label)}`;
17
+ const slug = (_b = category.link.slug) !== null && _b !== void 0 ? _b : getDefaultSlug();
18
+ const permalink = (0, utils_1.normalizeUrl)([params.version.versionPath, slug]);
19
+ return {
20
+ ...category.link,
21
+ slug,
22
+ permalink,
23
+ };
24
+ }
25
+ return category.link;
26
+ }
27
+ function postProcessSidebarItem(item, params) {
28
+ var _a, _b;
29
+ if (item.type === 'category') {
30
+ const category = {
31
+ ...item,
32
+ collapsed: (_a = item.collapsed) !== null && _a !== void 0 ? _a : params.sidebarOptions.sidebarCollapsed,
33
+ collapsible: (_b = item.collapsible) !== null && _b !== void 0 ? _b : params.sidebarOptions.sidebarCollapsible,
34
+ link: normalizeCategoryLink(item, params),
35
+ items: item.items.map((subItem) => postProcessSidebarItem(subItem, params)),
36
+ };
37
+ // If the current category doesn't have subitems, we render a normal link
38
+ // instead.
39
+ if (category.items.length === 0) {
40
+ if (!category.link) {
41
+ throw new Error(`Sidebar category ${item.label} has neither any subitem nor a link. This makes this item not able to link to anything.`);
42
+ }
43
+ switch (category.link.type) {
44
+ case 'doc':
45
+ return {
46
+ type: 'doc',
47
+ label: category.label,
48
+ id: category.link.id,
49
+ };
50
+ case 'generated-index':
51
+ return {
52
+ type: 'link',
53
+ label: category.label,
54
+ href: category.link.permalink,
55
+ };
56
+ default:
57
+ throw new Error('Unexpected sidebar category link type');
58
+ }
59
+ }
60
+ // A non-collapsible category can't be collapsed!
61
+ if (category.collapsible === false) {
62
+ category.collapsed = false;
63
+ }
64
+ return category;
65
+ }
66
+ return item;
67
+ }
68
+ function postProcessSidebars(sidebars, params) {
69
+ return (0, lodash_1.mapValues)(sidebars, (sidebar) => sidebar.map((item) => postProcessSidebarItem(item, params)));
70
+ }
71
+ exports.postProcessSidebars = postProcessSidebars;
@@ -4,17 +4,5 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import type { DocMetadataBase, VersionMetadata } from '../types';
8
- import type { Sidebars, NormalizedSidebars, SidebarItemsGeneratorOption, CategoryMetadataFile } from './types';
9
- import type { Slugger } from '@docusaurus/utils';
10
- import type { NumberPrefixParser, SidebarOptions } from '@docusaurus/plugin-content-docs';
11
- export declare type SidebarProcessorParams = {
12
- sidebarItemsGenerator: SidebarItemsGeneratorOption;
13
- numberPrefixParser: NumberPrefixParser;
14
- docs: DocMetadataBase[];
15
- version: VersionMetadata;
16
- categoryLabelSlugger: Slugger;
17
- sidebarOptions: SidebarOptions;
18
- categoriesMetadata: Record<string, CategoryMetadataFile>;
19
- };
20
- export declare function processSidebars(unprocessedSidebars: NormalizedSidebars, params: SidebarProcessorParams): Promise<Sidebars>;
7
+ import type { NormalizedSidebars, ProcessedSidebars, SidebarProcessorParams, CategoryMetadataFile } from './types';
8
+ export declare function processSidebars(unprocessedSidebars: NormalizedSidebars, categoriesMetadata: Record<string, CategoryMetadataFile>, params: SidebarProcessorParams): Promise<ProcessedSidebars>;
@@ -8,11 +8,10 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.processSidebars = void 0;
10
10
  const tslib_1 = require("tslib");
11
- const utils_1 = require("./utils");
12
11
  const generator_1 = require("./generator");
12
+ const validation_1 = require("./validation");
13
13
  const lodash_1 = require("lodash");
14
14
  const combine_promises_1 = (0, tslib_1.__importDefault)(require("combine-promises"));
15
- const normalization_1 = require("./normalization");
16
15
  const docs_1 = require("../docs");
17
16
  function toSidebarItemsGeneratorDoc(doc) {
18
17
  return (0, lodash_1.pick)(doc, [
@@ -29,22 +28,14 @@ function toSidebarItemsGeneratorVersion(version) {
29
28
  }
30
29
  // Handle the generation of autogenerated sidebar items and other
31
30
  // post-processing checks
32
- async function processSidebar(unprocessedSidebar, params) {
33
- const { sidebarItemsGenerator, numberPrefixParser, docs, version, sidebarOptions, categoriesMetadata, } = params;
31
+ async function processSidebar(unprocessedSidebar, categoriesMetadata, params) {
32
+ const { sidebarItemsGenerator, numberPrefixParser, docs, version, sidebarOptions, } = params;
34
33
  // Just a minor lazy transformation optimization
35
34
  const getSidebarItemsGeneratorDocsAndVersion = (0, lodash_1.memoize)(() => ({
36
35
  docs: docs.map(toSidebarItemsGeneratorDoc),
37
36
  version: toSidebarItemsGeneratorVersion(version),
38
37
  }));
39
- async function processCategoryItem(item) {
40
- return {
41
- ...item,
42
- items: (await Promise.all(item.items.map(processItem))).flat(),
43
- };
44
- }
45
38
  async function processAutoGeneratedItem(item) {
46
- // TODO the returned type can't be trusted in practice (generator can be
47
- // user-provided)
48
39
  const generatedItems = await sidebarItemsGenerator({
49
40
  item,
50
41
  numberPrefixParser,
@@ -54,41 +45,20 @@ async function processSidebar(unprocessedSidebar, params) {
54
45
  options: sidebarOptions,
55
46
  categoriesMetadata,
56
47
  });
57
- // TODO validate generated items: user can generate bad items
58
- const generatedItemsNormalized = generatedItems.flatMap((generatedItem) => (0, normalization_1.normalizeItem)(generatedItem, { ...params, ...sidebarOptions }));
59
48
  // Process again... weird but sidebar item generated might generate some
60
49
  // auto-generated items?
61
- return processItems(generatedItemsNormalized);
50
+ // TODO repeatedly process & unwrap autogenerated items until there are no
51
+ // more autogenerated items, or when loop count (e.g. 10) is reached
52
+ return processItems(generatedItems);
62
53
  }
63
54
  async function processItem(item) {
64
55
  if (item.type === 'category') {
65
- // If the current category doesn't have subitems, we render a normal doc link instead.
66
- if (item.items.length === 0) {
67
- if (!item.link) {
68
- throw new Error(`Sidebar category ${item.label} has neither any subitem nor a link. This makes this item not able to link to anything.`);
69
- }
70
- switch (item.link.type) {
71
- case 'doc':
72
- return [
73
- {
74
- type: 'doc',
75
- label: item.label,
76
- id: item.link.id,
77
- },
78
- ];
79
- case 'generated-index':
80
- return [
81
- {
82
- type: 'link',
83
- label: item.label,
84
- href: item.link.permalink,
85
- },
86
- ];
87
- default:
88
- throw new Error('Unexpected sidebar category link type');
89
- }
90
- }
91
- return [await processCategoryItem(item)];
56
+ return [
57
+ {
58
+ ...item,
59
+ items: (await Promise.all(item.items.map(processItem))).flat(),
60
+ },
61
+ ];
92
62
  }
93
63
  if (item.type === 'autogenerated') {
94
64
  return processAutoGeneratedItem(item);
@@ -99,19 +69,11 @@ async function processSidebar(unprocessedSidebar, params) {
99
69
  return (await Promise.all(items.map(processItem))).flat();
100
70
  }
101
71
  const processedSidebar = await processItems(unprocessedSidebar);
102
- const fixSidebarItemInconsistencies = (item) => {
103
- // A non-collapsible category can't be collapsed!
104
- if (item.type === 'category' && !item.collapsible && item.collapsed) {
105
- return {
106
- ...item,
107
- collapsed: false,
108
- };
109
- }
110
- return item;
111
- };
112
- return (0, utils_1.transformSidebarItems)(processedSidebar, fixSidebarItemInconsistencies);
72
+ return processedSidebar;
113
73
  }
114
- async function processSidebars(unprocessedSidebars, params) {
115
- return (0, combine_promises_1.default)((0, lodash_1.mapValues)(unprocessedSidebars, (unprocessedSidebar) => processSidebar(unprocessedSidebar, params)));
74
+ async function processSidebars(unprocessedSidebars, categoriesMetadata, params) {
75
+ const processedSidebars = await (0, combine_promises_1.default)((0, lodash_1.mapValues)(unprocessedSidebars, (unprocessedSidebar) => processSidebar(unprocessedSidebar, categoriesMetadata, params)));
76
+ (0, validation_1.validateSidebars)(processedSidebars);
77
+ return processedSidebars;
116
78
  }
117
79
  exports.processSidebars = processSidebars;
@@ -7,6 +7,7 @@
7
7
  import type { Optional, Required } from 'utility-types';
8
8
  import type { DocMetadataBase, VersionMetadata } from '../types';
9
9
  import type { NumberPrefixParser, SidebarOptions, CategoryIndexMatcher } from '@docusaurus/plugin-content-docs';
10
+ import type { Slugger } from '@docusaurus/utils';
10
11
  declare type Expand<T extends Record<string, unknown>> = {
11
12
  [P in keyof T]: T[P];
12
13
  };
@@ -74,15 +75,24 @@ export declare type SidebarConfig = SidebarCategoriesShorthand | SidebarItemConf
74
75
  export declare type SidebarsConfig = {
75
76
  [sidebarId: string]: SidebarConfig;
76
77
  };
77
- export declare type NormalizedSidebarItemCategory = Expand<SidebarItemCategoryBase & {
78
+ export declare type NormalizedSidebarItemCategory = Expand<Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
78
79
  items: NormalizedSidebarItem[];
79
- link?: SidebarItemCategoryLink;
80
+ link?: SidebarItemCategoryLinkConfig;
80
81
  }>;
81
82
  export declare type NormalizedSidebarItem = SidebarItemDoc | SidebarItemHtml | SidebarItemLink | NormalizedSidebarItemCategory | SidebarItemAutogenerated;
82
83
  export declare type NormalizedSidebar = NormalizedSidebarItem[];
83
84
  export declare type NormalizedSidebars = {
84
85
  [sidebarId: string]: NormalizedSidebar;
85
86
  };
87
+ export declare type ProcessedSidebarItemCategory = Expand<Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
88
+ items: ProcessedSidebarItem[];
89
+ link?: SidebarItemCategoryLinkConfig;
90
+ }>;
91
+ export declare type ProcessedSidebarItem = SidebarItemDoc | SidebarItemHtml | SidebarItemLink | ProcessedSidebarItemCategory;
92
+ export declare type ProcessedSidebar = ProcessedSidebarItem[];
93
+ export declare type ProcessedSidebars = {
94
+ [sidebarId: string]: ProcessedSidebar;
95
+ };
86
96
  export declare type SidebarItemCategory = Expand<SidebarItemCategoryBase & {
87
97
  items: SidebarItem[];
88
98
  link?: SidebarItemCategoryLink;
@@ -139,9 +149,17 @@ export declare type SidebarItemsGeneratorArgs = {
139
149
  categoriesMetadata: Record<string, CategoryMetadataFile>;
140
150
  options: SidebarOptions;
141
151
  };
142
- export declare type SidebarItemsGenerator = (generatorArgs: SidebarItemsGeneratorArgs) => Promise<SidebarItem[]>;
152
+ export declare type SidebarItemsGenerator = (generatorArgs: SidebarItemsGeneratorArgs) => Promise<NormalizedSidebar>;
143
153
  export declare type SidebarItemsGeneratorOptionArgs = {
144
154
  defaultSidebarItemsGenerator: SidebarItemsGenerator;
145
155
  } & SidebarItemsGeneratorArgs;
146
- export declare type SidebarItemsGeneratorOption = (generatorArgs: SidebarItemsGeneratorOptionArgs) => Promise<SidebarItem[]>;
156
+ export declare type SidebarItemsGeneratorOption = (generatorArgs: SidebarItemsGeneratorOptionArgs) => Promise<NormalizedSidebarItem[]>;
157
+ export declare type SidebarProcessorParams = {
158
+ sidebarItemsGenerator: SidebarItemsGeneratorOption;
159
+ numberPrefixParser: NumberPrefixParser;
160
+ docs: DocMetadataBase[];
161
+ version: VersionMetadata;
162
+ categoryLabelSlugger: Slugger;
163
+ sidebarOptions: SidebarOptions;
164
+ };
147
165
  export {};
@@ -4,6 +4,6 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import type { SidebarsConfig, CategoryMetadataFile } from './types';
8
- export declare function validateSidebars(sidebars: unknown): asserts sidebars is SidebarsConfig;
7
+ import type { NormalizedSidebars, CategoryMetadataFile } from './types';
8
+ export declare function validateSidebars(sidebars: Record<string, unknown>): asserts sidebars is NormalizedSidebars;
9
9
  export declare function validateCategoryMetadataFile(unsafeContent: unknown): CategoryMetadataFile;
@@ -8,7 +8,6 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.validateCategoryMetadataFile = exports.validateSidebars = void 0;
10
10
  const utils_validation_1 = require("@docusaurus/utils-validation");
11
- const utils_1 = require("./utils");
12
11
  // NOTE: we don't add any default values during validation on purpose!
13
12
  // Config types are exposed to users for typechecking and we use the same type
14
13
  // in normalization
@@ -31,7 +30,7 @@ const sidebarItemDocSchema = sidebarItemBaseSchema.append({
31
30
  const sidebarItemHtmlSchema = sidebarItemBaseSchema.append({
32
31
  type: 'html',
33
32
  value: utils_validation_1.Joi.string().required(),
34
- defaultStyle: utils_validation_1.Joi.boolean().default(false),
33
+ defaultStyle: utils_validation_1.Joi.boolean(),
35
34
  });
36
35
  const sidebarItemLinkSchema = sidebarItemBaseSchema.append({
37
36
  type: 'link',
@@ -65,23 +64,23 @@ const sidebarItemCategoryLinkSchema = utils_validation_1.Joi.object()
65
64
  }),
66
65
  },
67
66
  {
68
- is: utils_validation_1.Joi.string().required(),
67
+ is: utils_validation_1.Joi.required(),
69
68
  then: utils_validation_1.Joi.forbidden().messages({
70
69
  'any.unknown': 'Unknown sidebar category link type "{.type}".',
71
70
  }),
72
71
  },
73
72
  ],
74
- })
75
- .id('sidebarCategoryLinkSchema');
73
+ });
76
74
  const sidebarItemCategorySchema = sidebarItemBaseSchema.append({
77
75
  type: 'category',
78
76
  label: utils_validation_1.Joi.string()
79
77
  .required()
80
78
  .messages({ 'any.unknown': '"label" must be a string' }),
81
- // TODO: Joi doesn't allow mutual recursion. See https://github.com/sideway/joi/issues/2611
82
79
  items: utils_validation_1.Joi.array()
83
80
  .required()
84
81
  .messages({ 'any.unknown': '"items" must be an array' }),
82
+ // TODO: Joi doesn't allow mutual recursion. See https://github.com/sideway/joi/issues/2611
83
+ // .items(Joi.link('#sidebarItemSchema')),
85
84
  link: sidebarItemCategoryLinkSchema,
86
85
  collapsed: utils_validation_1.Joi.boolean().messages({
87
86
  'any.unknown': '"collapsed" must be a boolean',
@@ -90,8 +89,7 @@ const sidebarItemCategorySchema = sidebarItemBaseSchema.append({
90
89
  'any.unknown': '"collapsible" must be a boolean',
91
90
  }),
92
91
  });
93
- const sidebarItemSchema = utils_validation_1.Joi.object()
94
- .when('.type', {
92
+ const sidebarItemSchema = utils_validation_1.Joi.object().when('.type', {
95
93
  switch: [
96
94
  { is: 'link', then: sidebarItemLinkSchema },
97
95
  {
@@ -108,33 +106,20 @@ const sidebarItemSchema = utils_validation_1.Joi.object()
108
106
  }),
109
107
  },
110
108
  ],
111
- })
112
- .id('sidebarItemSchema');
109
+ });
110
+ // .id('sidebarItemSchema');
113
111
  function validateSidebarItem(item) {
114
- if (typeof item === 'string') {
115
- return;
116
- }
117
112
  // TODO: remove once with proper Joi support
118
113
  // Because we can't use Joi to validate nested items (see above), we do it
119
114
  // manually
120
- if ((0, utils_1.isCategoriesShorthand)(item)) {
121
- Object.values(item).forEach((category) => category.forEach(validateSidebarItem));
122
- }
123
- else {
124
- utils_validation_1.Joi.assert(item, sidebarItemSchema);
125
- if (item.type === 'category') {
126
- item.items.forEach(validateSidebarItem);
127
- }
115
+ utils_validation_1.Joi.assert(item, sidebarItemSchema);
116
+ if (item.type === 'category') {
117
+ item.items.forEach(validateSidebarItem);
128
118
  }
129
119
  }
130
120
  function validateSidebars(sidebars) {
131
121
  Object.values(sidebars).forEach((sidebar) => {
132
- if (Array.isArray(sidebar)) {
133
- sidebar.forEach(validateSidebarItem);
134
- }
135
- else {
136
- validateSidebarItem(sidebar);
137
- }
122
+ sidebar.forEach(validateSidebarItem);
138
123
  });
139
124
  }
140
125
  exports.validateSidebars = validateSidebars;
package/lib/types.d.ts CHANGED
@@ -5,9 +5,9 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import type { Sidebars } from './sidebars/types';
8
- import type { Tag, FrontMatterTag, Slugger } from '@docusaurus/utils';
8
+ import type { Tag, FrontMatterTag } from '@docusaurus/utils';
9
9
  import type { BrokenMarkdownLink as IBrokenMarkdownLink, ContentPaths } from '@docusaurus/utils/lib/markdownLinks';
10
- import type { VersionBanner, SidebarOptions } from '@docusaurus/plugin-content-docs';
10
+ import type { VersionBanner } from '@docusaurus/plugin-content-docs';
11
11
  export declare type DocFile = {
12
12
  contentPath: string;
13
13
  filePath: string;
@@ -29,10 +29,6 @@ export declare type VersionMetadata = ContentPaths & {
29
29
  sidebarFilePath: string | false | undefined;
30
30
  routePriority: number | undefined;
31
31
  };
32
- export declare type NormalizeSidebarsParams = SidebarOptions & {
33
- version: VersionMetadata;
34
- categoryLabelSlugger: Slugger;
35
- };
36
32
  export declare type LastUpdateData = {
37
33
  lastUpdatedAt?: number;
38
34
  formattedLastUpdatedAt?: string;