@docusaurus/plugin-content-docs 0.0.0-4240 → 0.0.0-4244

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 (62) 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.js +5 -23
  5. package/lib/docs.d.ts +22 -2
  6. package/lib/docs.js +71 -29
  7. package/lib/index.js +34 -62
  8. package/lib/options.js +2 -0
  9. package/lib/props.js +35 -6
  10. package/lib/routes.d.ts +27 -0
  11. package/lib/routes.js +105 -0
  12. package/lib/sidebars/generator.d.ts +2 -1
  13. package/lib/sidebars/generator.js +55 -13
  14. package/lib/sidebars/index.d.ts +5 -4
  15. package/lib/sidebars/index.js +18 -9
  16. package/lib/sidebars/normalization.d.ts +8 -3
  17. package/lib/sidebars/normalization.js +36 -17
  18. package/lib/sidebars/processor.d.ts +5 -3
  19. package/lib/sidebars/processor.js +33 -18
  20. package/lib/sidebars/types.d.ts +43 -2
  21. package/lib/sidebars/utils.d.ts +18 -6
  22. package/lib/sidebars/utils.js +149 -24
  23. package/lib/sidebars/validation.d.ts +2 -0
  24. package/lib/sidebars/validation.js +44 -8
  25. package/lib/slug.d.ts +4 -3
  26. package/lib/slug.js +26 -14
  27. package/lib/translations.js +51 -7
  28. package/lib/types.d.ts +18 -3
  29. package/package.json +8 -8
  30. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +2 -2
  31. package/src/__tests__/__snapshots__/cli.test.ts.snap +48 -106
  32. package/src/__tests__/__snapshots__/index.test.ts.snap +279 -28
  33. package/src/__tests__/__snapshots__/translations.test.ts.snap +45 -0
  34. package/src/__tests__/docs.test.ts +122 -7
  35. package/src/__tests__/index.test.ts +27 -1
  36. package/src/__tests__/options.test.ts +2 -0
  37. package/src/__tests__/slug.test.ts +127 -20
  38. package/src/__tests__/translations.test.ts +7 -0
  39. package/src/categoryGeneratedIndex.ts +57 -0
  40. package/src/cli.ts +5 -35
  41. package/src/docs.ts +103 -45
  42. package/src/index.ts +55 -93
  43. package/src/options.ts +4 -0
  44. package/src/plugin-content-docs.d.ts +71 -8
  45. package/src/props.ts +48 -9
  46. package/src/routes.ts +173 -0
  47. package/src/sidebars/__tests__/__snapshots__/index.test.ts.snap +21 -6
  48. package/src/sidebars/__tests__/generator.test.ts +105 -1
  49. package/src/sidebars/__tests__/index.test.ts +26 -24
  50. package/src/sidebars/__tests__/processor.test.ts +110 -19
  51. package/src/sidebars/__tests__/utils.test.ts +320 -20
  52. package/src/sidebars/__tests__/validation.test.ts +105 -0
  53. package/src/sidebars/generator.ts +82 -19
  54. package/src/sidebars/index.ts +23 -13
  55. package/src/sidebars/normalization.ts +47 -23
  56. package/src/sidebars/processor.ts +57 -27
  57. package/src/sidebars/types.ts +64 -3
  58. package/src/sidebars/utils.ts +217 -42
  59. package/src/sidebars/validation.ts +52 -8
  60. package/src/slug.ts +32 -17
  61. package/src/translations.ts +74 -8
  62. package/src/types.ts +22 -5
@@ -10,33 +10,31 @@ exports.DefaultSidebarItemsGenerator = exports.CategoryMetadataFilenamePattern =
10
10
  const tslib_1 = require("tslib");
11
11
  const lodash_1 = require("lodash");
12
12
  const utils_1 = require("@docusaurus/utils");
13
- const utils_validation_1 = require("@docusaurus/utils-validation");
14
13
  const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
15
14
  const path_1 = (0, tslib_1.__importDefault)(require("path"));
16
15
  const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
17
16
  const js_yaml_1 = (0, tslib_1.__importDefault)(require("js-yaml"));
17
+ const validation_1 = require("./validation");
18
+ const docs_1 = require("../docs");
18
19
  const BreadcrumbSeparator = '/';
19
20
  // To avoid possible name clashes with a folder of the same name as the ID
20
21
  const docIdPrefix = '$doc$/';
22
+ // Just an alias to the make code more explicit
23
+ function getLocalDocId(docId) {
24
+ return (0, lodash_1.last)(docId.split('/'));
25
+ }
21
26
  exports.CategoryMetadataFilenameBase = '_category_';
22
27
  exports.CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
23
- const CategoryMetadataFileSchema = utils_validation_1.Joi.object({
24
- label: utils_validation_1.Joi.string(),
25
- position: utils_validation_1.Joi.number(),
26
- collapsed: utils_validation_1.Joi.boolean(),
27
- collapsible: utils_validation_1.Joi.boolean(),
28
- className: utils_validation_1.Joi.string(),
29
- });
30
28
  // TODO I now believe we should read all the category metadata files ahead of time: we may need this metadata to customize docs metadata
31
29
  // Example use-case being able to disable number prefix parsing at the folder level, or customize the default route path segment for an intermediate directory...
32
- // TODO later if there is `CategoryFolder/index.md`, we may want to read the metadata as yaml on it
30
+ // TODO later if there is `CategoryFolder/with-category-name-doc.md`, we may want to read the metadata as yaml on it
33
31
  // see https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
34
32
  async function readCategoryMetadataFile(categoryDirPath) {
35
33
  async function tryReadFile(filePath) {
36
34
  const contentString = await fs_extra_1.default.readFile(filePath, { encoding: 'utf8' });
37
35
  const unsafeContent = js_yaml_1.default.load(contentString);
38
36
  try {
39
- return utils_validation_1.Joi.attempt(unsafeContent, CategoryMetadataFileSchema);
37
+ return (0, validation_1.validateCategoryMetadataFile)(unsafeContent);
40
38
  }
41
39
  catch (e) {
42
40
  console.error(chalk_1.default.red(`The docs sidebar category metadata file looks invalid!\nPath: ${filePath}`));
@@ -55,6 +53,15 @@ async function readCategoryMetadataFile(categoryDirPath) {
55
53
  }
56
54
  // Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
57
55
  const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, docs: allDocs, options, item: { dirName: autogenDir }, version, }) => {
56
+ const docsById = (0, docs_1.createDocsByIdIndex)(allDocs);
57
+ const findDoc = (docId) => docsById[docId];
58
+ const getDoc = (docId) => {
59
+ const doc = findDoc(docId);
60
+ if (!doc) {
61
+ throw new Error(`Can't find any doc with id=${docId}.\nAvailable doc ids:\n- ${Object.keys(docsById).join('\n- ')}`);
62
+ }
63
+ return doc;
64
+ };
58
65
  /**
59
66
  * Step 1. Extract the docs that are in the autogen dir.
60
67
  */
@@ -110,9 +117,8 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, docs: allDocs,
110
117
  * (From a record to an array of items, akin to normalizing shorthand)
111
118
  */
112
119
  function generateSidebar(fsModel) {
113
- const docsById = (0, lodash_1.keyBy)(allDocs, (doc) => doc.id);
114
120
  function createDocItem(id) {
115
- const { sidebarPosition: position, frontMatter: { sidebar_label: label, sidebar_class_name: className }, } = docsById[id];
121
+ const { sidebarPosition: position, frontMatter: { sidebar_label: label, sidebar_class_name: className }, } = getDoc(id);
116
122
  return {
117
123
  type: 'doc',
118
124
  id,
@@ -128,6 +134,41 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, docs: allDocs,
128
134
  const categoryMetadata = await readCategoryMetadataFile(categoryPath);
129
135
  const className = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.className;
130
136
  const { filename, numberPrefix } = numberPrefixParser(folderName);
137
+ const allItems = await Promise.all(Object.entries(dir).map(([key, content]) => dirToItem(content, key, `${fullPath}/${key}`)));
138
+ // Try to match a doc inside the category folder,
139
+ // using the "local id" (myDoc) or "qualified id" (dirName/myDoc)
140
+ function findDocByLocalId(localId) {
141
+ return allItems.find((item) => item.type === 'doc' && getLocalDocId(item.id) === localId);
142
+ }
143
+ function findConventionalCategoryDocLink() {
144
+ return allItems.find((item) => item.type === 'doc' && (0, docs_1.isConventionalDocIndex)(getDoc(item.id)));
145
+ }
146
+ function getCategoryLinkedDocId() {
147
+ var _a, _b;
148
+ const link = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.link;
149
+ if (link) {
150
+ if (link.type === 'doc') {
151
+ return ((_a = findDocByLocalId(link.id)) === null || _a === void 0 ? void 0 : _a.id) || getDoc(link.id).id;
152
+ }
153
+ else {
154
+ // We don't continue for other link types on purpose!
155
+ // IE if user decide to use type "generated-index", we should not pick a README.md file as the linked doc
156
+ return undefined;
157
+ }
158
+ }
159
+ // Apply default convention to pick index.md, README.md or <categoryName>.md as the category doc
160
+ return (_b = findConventionalCategoryDocLink()) === null || _b === void 0 ? void 0 : _b.id;
161
+ }
162
+ const categoryLinkedDocId = getCategoryLinkedDocId();
163
+ const link = categoryLinkedDocId
164
+ ? {
165
+ type: 'doc',
166
+ id: categoryLinkedDocId, // We "remap" a potentially "local id" to a "qualified id"
167
+ }
168
+ : // TODO typing issue
169
+ categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.link;
170
+ // If a doc is linked, remove it from the category subItems
171
+ const items = allItems.filter((item) => !(item.type === 'doc' && item.id === categoryLinkedDocId));
131
172
  return {
132
173
  type: 'category',
133
174
  label: (_a = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.label) !== null && _a !== void 0 ? _a : filename,
@@ -135,7 +176,8 @@ const DefaultSidebarItemsGenerator = async ({ numberPrefixParser, docs: allDocs,
135
176
  collapsed: (_c = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.collapsed) !== null && _c !== void 0 ? _c : options.sidebarCollapsed,
136
177
  position: (_d = categoryMetadata === null || categoryMetadata === void 0 ? void 0 : categoryMetadata.position) !== null && _d !== void 0 ? _d : numberPrefix,
137
178
  ...(className !== undefined && { className }),
138
- items: await Promise.all(Object.entries(dir).map(([key, content]) => dirToItem(content, key, `${fullPath}/${key}`))),
179
+ items,
180
+ ...(link && { link }),
139
181
  };
140
182
  }
141
183
  async function dirToItem(dir, // The directory item to be transformed.
@@ -5,10 +5,11 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import type { SidebarsConfig, Sidebars, NormalizedSidebars } from './types';
8
- import type { PluginOptions } from '../types';
9
- import { SidebarProcessorProps } from './processor';
8
+ import type { NormalizeSidebarsParams, PluginOptions } from '../types';
9
+ import { SidebarProcessorParams } from './processor';
10
10
  export declare const DefaultSidebars: SidebarsConfig;
11
11
  export declare const DisabledSidebars: SidebarsConfig;
12
12
  export declare function resolveSidebarPathOption(siteDir: string, sidebarPathOption: PluginOptions['sidebarPath']): PluginOptions['sidebarPath'];
13
- export declare function loadUnprocessedSidebars(sidebarFilePath: string | false | undefined, options: SidebarProcessorProps['options']): NormalizedSidebars;
14
- export declare function loadSidebars(sidebarFilePath: string | false | undefined, options: SidebarProcessorProps): Promise<Sidebars>;
13
+ export declare function loadSidebarsFile(sidebarFilePath: string | false | undefined): SidebarsConfig;
14
+ export declare function loadNormalizedSidebars(sidebarFilePath: string | false | undefined, params: NormalizeSidebarsParams): NormalizedSidebars;
15
+ export declare function loadSidebars(sidebarFilePath: string | false | undefined, options: SidebarProcessorParams): Promise<Sidebars>;
@@ -6,7 +6,7 @@
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.loadUnprocessedSidebars = exports.resolveSidebarPathOption = exports.DisabledSidebars = exports.DefaultSidebars = void 0;
9
+ exports.loadSidebars = exports.loadNormalizedSidebars = exports.loadSidebarsFile = 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"));
@@ -14,6 +14,7 @@ const validation_1 = require("./validation");
14
14
  const normalization_1 = require("./normalization");
15
15
  const processor_1 = require("./processor");
16
16
  const path_1 = (0, tslib_1.__importDefault)(require("path"));
17
+ const utils_1 = require("@docusaurus/utils");
17
18
  exports.DefaultSidebars = {
18
19
  defaultSidebar: [
19
20
  {
@@ -31,7 +32,7 @@ function resolveSidebarPathOption(siteDir, sidebarPathOption) {
31
32
  : sidebarPathOption;
32
33
  }
33
34
  exports.resolveSidebarPathOption = resolveSidebarPathOption;
34
- function loadSidebarFile(sidebarFilePath) {
35
+ function loadSidebarsFileUnsafe(sidebarFilePath) {
35
36
  // false => no sidebars
36
37
  if (sidebarFilePath === false) {
37
38
  return exports.DisabledSidebars;
@@ -49,16 +50,24 @@ function loadSidebarFile(sidebarFilePath) {
49
50
  // We don't want sidebars to be cached because of hot reloading.
50
51
  return (0, import_fresh_1.default)(sidebarFilePath);
51
52
  }
52
- function loadUnprocessedSidebars(sidebarFilePath, options) {
53
- const sidebarsConfig = loadSidebarFile(sidebarFilePath);
53
+ function loadSidebarsFile(sidebarFilePath) {
54
+ const sidebarsConfig = loadSidebarsFileUnsafe(sidebarFilePath);
54
55
  (0, validation_1.validateSidebars)(sidebarsConfig);
55
- const normalizedSidebars = (0, normalization_1.normalizeSidebars)(sidebarsConfig, options);
56
- return normalizedSidebars;
56
+ return sidebarsConfig;
57
57
  }
58
- exports.loadUnprocessedSidebars = loadUnprocessedSidebars;
58
+ exports.loadSidebarsFile = loadSidebarsFile;
59
+ function loadNormalizedSidebars(sidebarFilePath, params) {
60
+ return (0, normalization_1.normalizeSidebars)(loadSidebarsFile(sidebarFilePath), params);
61
+ }
62
+ exports.loadNormalizedSidebars = loadNormalizedSidebars;
59
63
  // Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
60
64
  async function loadSidebars(sidebarFilePath, options) {
61
- const unprocessedSidebars = loadUnprocessedSidebars(sidebarFilePath, options.options);
62
- return (0, processor_1.processSidebars)(unprocessedSidebars, options);
65
+ const normalizeSidebarsParams = {
66
+ ...options.sidebarOptions,
67
+ version: options.version,
68
+ categoryLabelSlugger: (0, utils_1.createSlugger)(),
69
+ };
70
+ const normalizedSidebars = loadNormalizedSidebars(sidebarFilePath, normalizeSidebarsParams);
71
+ return (0, processor_1.processSidebars)(normalizedSidebars, options);
63
72
  }
64
73
  exports.loadSidebars = loadSidebars;
@@ -4,6 +4,11 @@
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 { SidebarOptions } from '../types';
8
- import type { NormalizedSidebars, SidebarsConfig } from './types';
9
- export declare function normalizeSidebars(sidebars: SidebarsConfig, options: SidebarOptions): NormalizedSidebars;
7
+ import type { NormalizeSidebarsParams } from '../types';
8
+ import type { NormalizedSidebarItem, NormalizedSidebars, SidebarItemConfig, SidebarsConfig } from './types';
9
+ /**
10
+ * Normalizes recursively item and all its children. Ensures that at the end
11
+ * each item will be an object with the corresponding type.
12
+ */
13
+ export declare function normalizeItem(item: SidebarItemConfig, options: NormalizeSidebarsParams): NormalizedSidebarItem[];
14
+ export declare function normalizeSidebars(sidebars: SidebarsConfig, params: NormalizeSidebarsParams): NormalizedSidebars;
@@ -6,9 +6,25 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.normalizeSidebars = void 0;
10
- const lodash_1 = require("lodash");
9
+ exports.normalizeSidebars = exports.normalizeItem = void 0;
11
10
  const utils_1 = require("./utils");
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
+ }
12
28
  function normalizeCategoriesShorthand(sidebar, options) {
13
29
  return Object.entries(sidebar).map(([label, items]) => ({
14
30
  type: 'category',
@@ -23,7 +39,7 @@ function normalizeCategoriesShorthand(sidebar, options) {
23
39
  * each item will be an object with the corresponding type.
24
40
  */
25
41
  function normalizeItem(item, options) {
26
- var _a, _b;
42
+ var _a, _b, _c;
27
43
  if (typeof item === 'string') {
28
44
  return [
29
45
  {
@@ -33,26 +49,29 @@ function normalizeItem(item, options) {
33
49
  ];
34
50
  }
35
51
  if ((0, utils_1.isCategoriesShorthand)(item)) {
36
- return normalizeCategoriesShorthand(item, options).flatMap((subitem) => normalizeItem(subitem, options));
52
+ return normalizeCategoriesShorthand(item, options).flatMap((subItem) => normalizeItem(subItem, options));
37
53
  }
38
- return item.type === 'category'
39
- ? [
40
- {
41
- ...item,
42
- items: item.items.flatMap((subItem) => normalizeItem(subItem, options)),
43
- collapsible: (_a = item.collapsible) !== null && _a !== void 0 ? _a : options.sidebarCollapsible,
44
- collapsed: (_b = item.collapsed) !== null && _b !== void 0 ? _b : options.sidebarCollapsed,
45
- },
46
- ]
47
- : [item];
54
+ if (item.type === 'category') {
55
+ const link = normalizeCategoryLink(item, options);
56
+ const normalizedCategory = {
57
+ ...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,
62
+ };
63
+ return [normalizedCategory];
64
+ }
65
+ return [item];
48
66
  }
67
+ exports.normalizeItem = normalizeItem;
49
68
  function normalizeSidebar(sidebar, options) {
50
69
  const normalizedSidebar = Array.isArray(sidebar)
51
70
  ? sidebar
52
71
  : normalizeCategoriesShorthand(sidebar, options);
53
- return normalizedSidebar.flatMap((subitem) => normalizeItem(subitem, options));
72
+ return normalizedSidebar.flatMap((subItem) => normalizeItem(subItem, options));
54
73
  }
55
- function normalizeSidebars(sidebars, options) {
56
- return (0, lodash_1.mapValues)(sidebars, (subitem) => normalizeSidebar(subitem, options));
74
+ function normalizeSidebars(sidebars, params) {
75
+ return (0, lodash_1.mapValues)(sidebars, (items) => normalizeSidebar(items, params));
57
76
  }
58
77
  exports.normalizeSidebars = normalizeSidebars;
@@ -6,11 +6,13 @@
6
6
  */
7
7
  import type { NumberPrefixParser, DocMetadataBase, VersionMetadata, SidebarOptions } from '../types';
8
8
  import type { Sidebars, NormalizedSidebars, SidebarItemsGeneratorOption } from './types';
9
- export declare type SidebarProcessorProps = {
9
+ import { Slugger } from '@docusaurus/utils';
10
+ export declare type SidebarProcessorParams = {
10
11
  sidebarItemsGenerator: SidebarItemsGeneratorOption;
11
12
  numberPrefixParser: NumberPrefixParser;
12
13
  docs: DocMetadataBase[];
13
14
  version: VersionMetadata;
14
- options: SidebarOptions;
15
+ categoryLabelSlugger: Slugger;
16
+ sidebarOptions: SidebarOptions;
15
17
  };
16
- export declare function processSidebars(unprocessedSidebars: NormalizedSidebars, props: SidebarProcessorProps): Promise<Sidebars>;
18
+ export declare function processSidebars(unprocessedSidebars: NormalizedSidebars, params: SidebarProcessorParams): Promise<Sidebars>;
@@ -12,9 +12,11 @@ const utils_1 = require("./utils");
12
12
  const generator_1 = require("./generator");
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");
15
16
  function toSidebarItemsGeneratorDoc(doc) {
16
17
  return (0, lodash_1.pick)(doc, [
17
18
  'id',
19
+ 'unversionedId',
18
20
  'frontMatter',
19
21
  'source',
20
22
  'sourceDirName',
@@ -25,33 +27,46 @@ function toSidebarItemsGeneratorVersion(version) {
25
27
  return (0, lodash_1.pick)(version, ['versionName', 'contentPath']);
26
28
  }
27
29
  // Handle the generation of autogenerated sidebar items and other post-processing checks
28
- async function processSidebar(unprocessedSidebar, { sidebarItemsGenerator, numberPrefixParser, docs, version, options, }) {
30
+ async function processSidebar(unprocessedSidebar, params) {
31
+ const { sidebarItemsGenerator, numberPrefixParser, docs, version, sidebarOptions, } = params;
29
32
  // Just a minor lazy transformation optimization
30
33
  const getSidebarItemsGeneratorDocsAndVersion = (0, lodash_1.memoize)(() => ({
31
34
  docs: docs.map(toSidebarItemsGeneratorDoc),
32
35
  version: toSidebarItemsGeneratorVersion(version),
33
36
  }));
34
- async function handleAutoGeneratedItems(item) {
37
+ async function processCategoryItem(item) {
38
+ return {
39
+ ...item,
40
+ items: (await Promise.all(item.items.map(processItem))).flat(),
41
+ };
42
+ }
43
+ async function processAutoGeneratedItem(item) {
44
+ // TODO the returned type can't be trusted in practice (generator can be user-provided)
45
+ const generatedItems = await sidebarItemsGenerator({
46
+ item,
47
+ numberPrefixParser,
48
+ defaultSidebarItemsGenerator: generator_1.DefaultSidebarItemsGenerator,
49
+ ...getSidebarItemsGeneratorDocsAndVersion(),
50
+ options: sidebarOptions,
51
+ });
52
+ // TODO validate generated items: user can generate bad items
53
+ const generatedItemsNormalized = generatedItems.flatMap((generatedItem) => (0, normalization_1.normalizeItem)(generatedItem, { ...params, ...sidebarOptions }));
54
+ // Process again... weird but sidebar item generated might generate some auto-generated items?
55
+ return processItems(generatedItemsNormalized);
56
+ }
57
+ async function processItem(item) {
35
58
  if (item.type === 'category') {
36
- return [
37
- {
38
- ...item,
39
- items: (await Promise.all(item.items.map(handleAutoGeneratedItems))).flat(),
40
- },
41
- ];
59
+ return [await processCategoryItem(item)];
42
60
  }
43
61
  if (item.type === 'autogenerated') {
44
- return sidebarItemsGenerator({
45
- item,
46
- numberPrefixParser,
47
- defaultSidebarItemsGenerator: generator_1.DefaultSidebarItemsGenerator,
48
- ...getSidebarItemsGeneratorDocsAndVersion(),
49
- options,
50
- });
62
+ return processAutoGeneratedItem(item);
51
63
  }
52
64
  return [item];
53
65
  }
54
- const processedSidebar = (await Promise.all(unprocessedSidebar.map(handleAutoGeneratedItems))).flat();
66
+ async function processItems(items) {
67
+ return (await Promise.all(items.map(processItem))).flat();
68
+ }
69
+ const processedSidebar = await processItems(unprocessedSidebar);
55
70
  const fixSidebarItemInconsistencies = (item) => {
56
71
  // A non-collapsible category can't be collapsed!
57
72
  if (item.type === 'category' && !item.collapsible && item.collapsed) {
@@ -64,7 +79,7 @@ async function processSidebar(unprocessedSidebar, { sidebarItemsGenerator, numbe
64
79
  };
65
80
  return (0, utils_1.transformSidebarItems)(processedSidebar, fixSidebarItemInconsistencies);
66
81
  }
67
- async function processSidebars(unprocessedSidebars, props) {
68
- return (0, combine_promises_1.default)((0, lodash_1.mapValues)(unprocessedSidebars, (unprocessedSidebar) => processSidebar(unprocessedSidebar, props)));
82
+ async function processSidebars(unprocessedSidebars, params) {
83
+ return (0, combine_promises_1.default)((0, lodash_1.mapValues)(unprocessedSidebars, (unprocessedSidebar) => processSidebar(unprocessedSidebar, params)));
69
84
  }
70
85
  exports.processSidebars = processSidebars;
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import type { Optional } from 'utility-types';
8
8
  import type { DocMetadataBase, VersionMetadata, NumberPrefixParser, SidebarOptions } from '../types';
9
+ import { Required } from 'utility-types';
9
10
  declare type Expand<T extends Record<string, unknown>> = {
10
11
  [P in keyof T]: T[P];
11
12
  };
@@ -33,8 +34,28 @@ declare type SidebarItemCategoryBase = SidebarItemBase & {
33
34
  collapsed: boolean;
34
35
  collapsible: boolean;
35
36
  };
37
+ export declare type SidebarItemCategoryLinkDoc = {
38
+ type: 'doc';
39
+ id: string;
40
+ };
41
+ export declare type SidebarItemCategoryLinkGeneratedIndexConfig = {
42
+ type: 'generated-index';
43
+ slug?: string;
44
+ title?: string;
45
+ description?: string;
46
+ };
47
+ export declare type SidebarItemCategoryLinkGeneratedIndex = {
48
+ type: 'generated-index';
49
+ slug: string;
50
+ permalink: string;
51
+ title?: string;
52
+ description?: string;
53
+ };
54
+ export declare type SidebarItemCategoryLinkConfig = SidebarItemCategoryLinkDoc | SidebarItemCategoryLinkGeneratedIndexConfig;
55
+ export declare type SidebarItemCategoryLink = SidebarItemCategoryLinkDoc | SidebarItemCategoryLinkGeneratedIndex;
36
56
  export declare type SidebarItemCategoryConfig = Expand<Optional<SidebarItemCategoryBase, 'collapsed' | 'collapsible'> & {
37
57
  items: SidebarItemConfig[];
58
+ link?: SidebarItemCategoryLinkConfig;
38
59
  }>;
39
60
  export declare type SidebarCategoriesShorthand = {
40
61
  [sidebarCategory: string]: SidebarItemConfig[];
@@ -46,6 +67,7 @@ export declare type SidebarsConfig = {
46
67
  };
47
68
  export declare type NormalizedSidebarItemCategory = Expand<SidebarItemCategoryBase & {
48
69
  items: NormalizedSidebarItem[];
70
+ link?: SidebarItemCategoryLink;
49
71
  }>;
50
72
  export declare type NormalizedSidebarItem = SidebarItemDoc | SidebarItemLink | NormalizedSidebarItemCategory | SidebarItemAutogenerated;
51
73
  export declare type NormalizedSidebar = NormalizedSidebarItem[];
@@ -54,8 +76,14 @@ export declare type NormalizedSidebars = {
54
76
  };
55
77
  export declare type SidebarItemCategory = Expand<SidebarItemCategoryBase & {
56
78
  items: SidebarItem[];
79
+ link?: SidebarItemCategoryLink;
57
80
  }>;
81
+ export declare type SidebarItemCategoryWithLink = Required<SidebarItemCategory, 'link'>;
82
+ export declare type SidebarItemCategoryWithGeneratedIndex = SidebarItemCategoryWithLink & {
83
+ link: SidebarItemCategoryLinkGeneratedIndex;
84
+ };
58
85
  export declare type SidebarItem = SidebarItemDoc | SidebarItemLink | SidebarItemCategory;
86
+ export declare type SidebarNavigationItem = SidebarItemDoc | SidebarItemCategoryWithLink;
59
87
  export declare type Sidebar = SidebarItem[];
60
88
  export declare type SidebarItemType = SidebarItem['type'];
61
89
  export declare type Sidebars = {
@@ -63,13 +91,26 @@ export declare type Sidebars = {
63
91
  };
64
92
  export declare type PropSidebarItemCategory = Expand<SidebarItemCategoryBase & {
65
93
  items: PropSidebarItem[];
94
+ href?: string;
66
95
  }>;
67
- export declare type PropSidebarItem = SidebarItemLink | PropSidebarItemCategory;
96
+ export declare type PropSidebarItemLink = SidebarItemLink & {
97
+ docId?: string;
98
+ };
99
+ export declare type PropSidebarItem = PropSidebarItemLink | PropSidebarItemCategory;
68
100
  export declare type PropSidebar = PropSidebarItem[];
69
101
  export declare type PropSidebars = {
70
102
  [sidebarId: string]: PropSidebar;
71
103
  };
72
- export declare type SidebarItemsGeneratorDoc = Pick<DocMetadataBase, 'id' | 'frontMatter' | 'source' | 'sourceDirName' | 'sidebarPosition'>;
104
+ export declare type PropVersionDoc = {
105
+ id: string;
106
+ title: string;
107
+ description?: string;
108
+ sidebar?: string;
109
+ };
110
+ export declare type PropVersionDocs = {
111
+ [docId: string]: PropVersionDoc;
112
+ };
113
+ export declare type SidebarItemsGeneratorDoc = Pick<DocMetadataBase, 'id' | 'unversionedId' | 'frontMatter' | 'source' | 'sourceDirName' | 'sidebarPosition'>;
73
114
  export declare type SidebarItemsGeneratorVersion = Pick<VersionMetadata, 'versionName' | 'contentPath'>;
74
115
  export declare type SidebarItemsGeneratorArgs = {
75
116
  item: SidebarItemAutogenerated;
@@ -5,19 +5,31 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import type { Sidebars, Sidebar, SidebarItem, SidebarItemCategory, SidebarItemLink, SidebarItemDoc, SidebarCategoriesShorthand, SidebarItemConfig } from './types';
8
+ import { DocMetadataBase, DocNavLink } from '../types';
9
+ import { SidebarItemCategoryWithGeneratedIndex, SidebarNavigationItem } from './types';
8
10
  export declare function isCategoriesShorthand(item: SidebarItemConfig): item is SidebarCategoriesShorthand;
9
11
  export declare function transformSidebarItems(sidebar: Sidebar, updateFn: (item: SidebarItem) => SidebarItem): Sidebar;
10
12
  export declare function collectSidebarDocItems(sidebar: Sidebar): SidebarItemDoc[];
11
13
  export declare function collectSidebarCategories(sidebar: Sidebar): SidebarItemCategory[];
12
14
  export declare function collectSidebarLinks(sidebar: Sidebar): SidebarItemLink[];
15
+ export declare function collectSidebarDocIds(sidebar: Sidebar): string[];
16
+ export declare function collectSidebarNavigation(sidebar: Sidebar): SidebarNavigationItem[];
13
17
  export declare function collectSidebarsDocIds(sidebars: Sidebars): Record<string, string[]>;
14
- export declare function createSidebarsUtils(sidebars: Sidebars): {
18
+ export declare function collectSidebarsNavigations(sidebars: Sidebars): Record<string, SidebarNavigationItem[]>;
19
+ export declare type SidebarNavigation = {
20
+ sidebarName: string | undefined;
21
+ previous: SidebarNavigationItem | undefined;
22
+ next: SidebarNavigationItem | undefined;
23
+ };
24
+ export declare type SidebarsUtils = {
25
+ sidebars: Sidebars;
15
26
  getFirstDocIdOfFirstSidebar: () => string | undefined;
16
27
  getSidebarNameByDocId: (docId: string) => string | undefined;
17
- getDocNavigation: (docId: string) => {
18
- sidebarName: string | undefined;
19
- previousId: string | undefined;
20
- nextId: string | undefined;
21
- };
28
+ getDocNavigation: (unversionedId: string, versionedId: string) => SidebarNavigation;
29
+ getCategoryGeneratedIndexList: () => SidebarItemCategoryWithGeneratedIndex[];
30
+ getCategoryGeneratedIndexNavigation: (categoryGeneratedIndexPermalink: string) => SidebarNavigation;
22
31
  checkSidebarsDocIds: (validDocIds: string[], sidebarFilePath: string) => void;
23
32
  };
33
+ export declare function createSidebarsUtils(sidebars: Sidebars): SidebarsUtils;
34
+ export declare function toDocNavigationLink(doc: DocMetadataBase): DocNavLink;
35
+ export declare function toNavigationLink(navigationItem: SidebarNavigationItem | undefined, docsById: Record<string, DocMetadataBase>): DocNavLink | undefined;