@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.
- package/lib/.tsbuildinfo +1 -1
- package/lib/cli.d.ts +1 -1
- package/lib/cli.js +12 -16
- package/lib/index.js +12 -16
- package/lib/options.js +2 -2
- package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +1 -1
- package/lib/sidebars/generator.js +174 -0
- package/lib/sidebars/index.d.ts +14 -0
- package/lib/sidebars/index.js +64 -0
- package/lib/sidebars/normalization.d.ts +9 -0
- package/lib/sidebars/normalization.js +58 -0
- package/lib/sidebars/processor.d.ts +16 -0
- package/lib/sidebars/processor.js +70 -0
- package/lib/sidebars/types.d.ts +87 -0
- package/lib/sidebars/types.js +13 -0
- package/lib/sidebars/utils.d.ts +22 -0
- package/lib/sidebars/utils.js +101 -0
- package/lib/sidebars/validation.d.ts +8 -0
- package/lib/sidebars/validation.js +102 -0
- package/lib/translations.d.ts +1 -1
- package/lib/translations.js +9 -9
- package/lib/types.d.ts +1 -58
- package/package.json +10 -9
- package/src/__tests__/index.test.ts +4 -5
- package/src/__tests__/options.test.ts +1 -1
- package/src/cli.ts +23 -29
- package/src/index.ts +19 -27
- package/src/options.ts +1 -1
- package/src/plugin-content-docs.d.ts +6 -24
- package/src/props.ts +3 -5
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-category.js +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-collapsed.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-link.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-unknown-type.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars-wrong-field.json +0 -0
- package/src/{__tests__ → sidebars/__tests__}/__fixtures__/sidebars/sidebars.json +0 -0
- package/src/{__tests__/__snapshots__/sidebars.test.ts.snap → sidebars/__tests__/__snapshots__/index.test.ts.snap} +6 -6
- package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +2 -2
- package/src/sidebars/__tests__/index.test.ts +202 -0
- package/src/sidebars/__tests__/processor.test.ts +148 -0
- package/src/sidebars/__tests__/utils.test.ts +395 -0
- package/src/sidebars/generator.ts +253 -0
- package/src/sidebars/index.ts +84 -0
- package/src/sidebars/normalization.ts +88 -0
- package/src/sidebars/processor.ts +124 -0
- package/src/sidebars/types.ts +156 -0
- package/src/sidebars/utils.ts +146 -0
- package/src/sidebars/validation.ts +124 -0
- package/src/translations.ts +7 -12
- package/src/types.ts +1 -94
- package/lib/sidebarItemsGenerator.js +0 -222
- package/lib/sidebars.d.ts +0 -45
- package/lib/sidebars.js +0 -365
- package/src/__tests__/sidebars.test.ts +0 -741
- package/src/sidebarItemsGenerator.ts +0 -322
- package/src/sidebars.ts +0 -609
|
@@ -0,0 +1,124 @@
|
|
|
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 {Joi, URISchema} from '@docusaurus/utils-validation';
|
|
9
|
+
import {
|
|
10
|
+
SidebarItemConfig,
|
|
11
|
+
SidebarCategoriesShorthand,
|
|
12
|
+
SidebarItemBase,
|
|
13
|
+
SidebarItemAutogenerated,
|
|
14
|
+
SidebarItemDoc,
|
|
15
|
+
SidebarItemLink,
|
|
16
|
+
SidebarItemCategoryConfig,
|
|
17
|
+
SidebarsConfig,
|
|
18
|
+
isCategoriesShorthand,
|
|
19
|
+
} from './types';
|
|
20
|
+
|
|
21
|
+
const sidebarItemBaseSchema = Joi.object<SidebarItemBase>({
|
|
22
|
+
className: Joi.string(),
|
|
23
|
+
customProps: Joi.object().unknown(),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const sidebarItemAutogeneratedSchema =
|
|
27
|
+
sidebarItemBaseSchema.append<SidebarItemAutogenerated>({
|
|
28
|
+
type: 'autogenerated',
|
|
29
|
+
dirName: Joi.string()
|
|
30
|
+
.required()
|
|
31
|
+
.pattern(/^[^/](.*[^/])?$/)
|
|
32
|
+
.message(
|
|
33
|
+
'"dirName" must be a dir path relative to the docs folder root, and should not start or end with slash',
|
|
34
|
+
),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const sidebarItemDocSchema = sidebarItemBaseSchema.append<SidebarItemDoc>({
|
|
38
|
+
type: Joi.string().valid('doc', 'ref').required(),
|
|
39
|
+
id: Joi.string().required(),
|
|
40
|
+
label: Joi.string(),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const sidebarItemLinkSchema = sidebarItemBaseSchema.append<SidebarItemLink>({
|
|
44
|
+
type: 'link',
|
|
45
|
+
href: URISchema.required(),
|
|
46
|
+
label: Joi.string()
|
|
47
|
+
.required()
|
|
48
|
+
.messages({'any.unknown': '"label" must be a string'}),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const sidebarItemCategorySchema =
|
|
52
|
+
sidebarItemBaseSchema.append<SidebarItemCategoryConfig>({
|
|
53
|
+
type: 'category',
|
|
54
|
+
label: Joi.string()
|
|
55
|
+
.required()
|
|
56
|
+
.messages({'any.unknown': '"label" must be a string'}),
|
|
57
|
+
// TODO: Joi doesn't allow mutual recursion. See https://github.com/sideway/joi/issues/2611
|
|
58
|
+
items: Joi.array()
|
|
59
|
+
.required()
|
|
60
|
+
.messages({'any.unknown': '"items" must be an array'}), // .items(Joi.link('#sidebarItemSchema')),
|
|
61
|
+
collapsed: Joi.boolean().messages({
|
|
62
|
+
'any.unknown': '"collapsed" must be a boolean',
|
|
63
|
+
}),
|
|
64
|
+
collapsible: Joi.boolean().messages({
|
|
65
|
+
'any.unknown': '"collapsible" must be a boolean',
|
|
66
|
+
}),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const sidebarItemSchema: Joi.Schema<SidebarItemConfig> = Joi.object()
|
|
70
|
+
.when('.type', {
|
|
71
|
+
switch: [
|
|
72
|
+
{is: 'link', then: sidebarItemLinkSchema},
|
|
73
|
+
{
|
|
74
|
+
is: Joi.string().valid('doc', 'ref').required(),
|
|
75
|
+
then: sidebarItemDocSchema,
|
|
76
|
+
},
|
|
77
|
+
{is: 'autogenerated', then: sidebarItemAutogeneratedSchema},
|
|
78
|
+
{is: 'category', then: sidebarItemCategorySchema},
|
|
79
|
+
{
|
|
80
|
+
is: 'subcategory',
|
|
81
|
+
then: Joi.forbidden().messages({
|
|
82
|
+
'any.unknown':
|
|
83
|
+
'Docusaurus v2: "subcategory" has been renamed as "category".',
|
|
84
|
+
}),
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
is: Joi.string().required(),
|
|
88
|
+
then: Joi.forbidden().messages({
|
|
89
|
+
'any.unknown': 'Unknown sidebar item type "{.type}".',
|
|
90
|
+
}),
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
})
|
|
94
|
+
.id('sidebarItemSchema');
|
|
95
|
+
|
|
96
|
+
function validateSidebarItem(item: unknown): asserts item is SidebarItemConfig {
|
|
97
|
+
if (typeof item === 'string') {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
// TODO: remove once with proper Joi support
|
|
101
|
+
// Because we can't use Joi to validate nested items (see above), we do it manually
|
|
102
|
+
if (isCategoriesShorthand(item as SidebarItemConfig)) {
|
|
103
|
+
Object.values(item as SidebarCategoriesShorthand).forEach((category) =>
|
|
104
|
+
category.forEach(validateSidebarItem),
|
|
105
|
+
);
|
|
106
|
+
} else {
|
|
107
|
+
Joi.assert(item, sidebarItemSchema);
|
|
108
|
+
if ((item as SidebarItemCategoryConfig).type === 'category') {
|
|
109
|
+
(item as SidebarItemCategoryConfig).items.forEach(validateSidebarItem);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function validateSidebars(
|
|
115
|
+
sidebars: unknown,
|
|
116
|
+
): asserts sidebars is SidebarsConfig {
|
|
117
|
+
Object.values(sidebars as SidebarsConfig).forEach((sidebar) => {
|
|
118
|
+
if (Array.isArray(sidebar)) {
|
|
119
|
+
sidebar.forEach(validateSidebarItem);
|
|
120
|
+
} else {
|
|
121
|
+
validateSidebarItem(sidebar);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
package/src/translations.ts
CHANGED
|
@@ -5,20 +5,15 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
Sidebars,
|
|
13
|
-
SidebarItem,
|
|
14
|
-
} from './types';
|
|
15
|
-
|
|
16
|
-
import {chain, mapValues, flatten, keyBy} from 'lodash';
|
|
8
|
+
import type {LoadedVersion, LoadedContent} from './types';
|
|
9
|
+
import type {Sidebar, Sidebars} from './sidebars/types';
|
|
10
|
+
|
|
11
|
+
import {chain, mapValues, keyBy} from 'lodash';
|
|
17
12
|
import {
|
|
18
13
|
collectSidebarCategories,
|
|
19
14
|
transformSidebarItems,
|
|
20
15
|
collectSidebarLinks,
|
|
21
|
-
} from './sidebars';
|
|
16
|
+
} from './sidebars/utils';
|
|
22
17
|
import {
|
|
23
18
|
TranslationFileContent,
|
|
24
19
|
TranslationFile,
|
|
@@ -131,7 +126,7 @@ function translateSidebar({
|
|
|
131
126
|
sidebarName: string;
|
|
132
127
|
sidebarsTranslations: TranslationFileContent;
|
|
133
128
|
}): Sidebar {
|
|
134
|
-
return transformSidebarItems(sidebar, (item
|
|
129
|
+
return transformSidebarItems(sidebar, (item) => {
|
|
135
130
|
if (item.type === 'category') {
|
|
136
131
|
return {
|
|
137
132
|
...item,
|
|
@@ -222,7 +217,7 @@ function translateVersion(
|
|
|
222
217
|
function getVersionsTranslationFiles(
|
|
223
218
|
versions: LoadedVersion[],
|
|
224
219
|
): TranslationFiles {
|
|
225
|
-
return
|
|
220
|
+
return versions.flatMap(getVersionTranslationFiles);
|
|
226
221
|
}
|
|
227
222
|
function translateVersions(
|
|
228
223
|
versions: LoadedVersion[],
|
package/src/types.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
BrokenMarkdownLink as IBrokenMarkdownLink,
|
|
14
14
|
ContentPaths,
|
|
15
15
|
} from '@docusaurus/utils/lib/markdownLinks';
|
|
16
|
+
import type {SidebarItemsGeneratorOption, Sidebars} from './sidebars/types';
|
|
16
17
|
|
|
17
18
|
export type DocFile = {
|
|
18
19
|
contentPath: string; // /!\ may be localized
|
|
@@ -104,100 +105,6 @@ export type PluginOptions = MetadataOptions &
|
|
|
104
105
|
tagsBasePath: string;
|
|
105
106
|
};
|
|
106
107
|
|
|
107
|
-
export type SidebarItemBase = {
|
|
108
|
-
className?: string;
|
|
109
|
-
customProps?: Record<string, unknown>;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
export type SidebarItemDoc = SidebarItemBase & {
|
|
113
|
-
type: 'doc' | 'ref';
|
|
114
|
-
label?: string;
|
|
115
|
-
id: string;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
export type SidebarItemLink = SidebarItemBase & {
|
|
119
|
-
type: 'link';
|
|
120
|
-
href: string;
|
|
121
|
-
label: string;
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
export type SidebarItemCategory = SidebarItemBase & {
|
|
125
|
-
type: 'category';
|
|
126
|
-
label: string;
|
|
127
|
-
items: SidebarItem[];
|
|
128
|
-
collapsed: boolean;
|
|
129
|
-
collapsible: boolean;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
export type UnprocessedSidebarItemAutogenerated = {
|
|
133
|
-
type: 'autogenerated';
|
|
134
|
-
dirName: string;
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
export type UnprocessedSidebarItemCategory = SidebarItemBase & {
|
|
138
|
-
type: 'category';
|
|
139
|
-
label: string;
|
|
140
|
-
items: UnprocessedSidebarItem[];
|
|
141
|
-
collapsed: boolean;
|
|
142
|
-
collapsible: boolean;
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
export type UnprocessedSidebarItem =
|
|
146
|
-
| SidebarItemDoc
|
|
147
|
-
| SidebarItemLink
|
|
148
|
-
| UnprocessedSidebarItemCategory
|
|
149
|
-
| UnprocessedSidebarItemAutogenerated;
|
|
150
|
-
|
|
151
|
-
export type UnprocessedSidebar = UnprocessedSidebarItem[];
|
|
152
|
-
export type UnprocessedSidebars = Record<string, UnprocessedSidebar>;
|
|
153
|
-
|
|
154
|
-
export type SidebarItem =
|
|
155
|
-
| SidebarItemDoc
|
|
156
|
-
| SidebarItemLink
|
|
157
|
-
| SidebarItemCategory;
|
|
158
|
-
|
|
159
|
-
export type Sidebar = SidebarItem[];
|
|
160
|
-
export type SidebarItemType = SidebarItem['type'];
|
|
161
|
-
export type Sidebars = Record<string, Sidebar>;
|
|
162
|
-
|
|
163
|
-
// Reduce API surface for options.sidebarItemsGenerator
|
|
164
|
-
// The user-provided generator fn should receive only a subset of metadatas
|
|
165
|
-
// A change to any of these metadatas can be considered as a breaking change
|
|
166
|
-
export type SidebarItemsGeneratorDoc = Pick<
|
|
167
|
-
DocMetadataBase,
|
|
168
|
-
'id' | 'frontMatter' | 'source' | 'sourceDirName' | 'sidebarPosition'
|
|
169
|
-
>;
|
|
170
|
-
export type SidebarItemsGeneratorVersion = Pick<
|
|
171
|
-
VersionMetadata,
|
|
172
|
-
'versionName' | 'contentPath'
|
|
173
|
-
>;
|
|
174
|
-
|
|
175
|
-
export type SidebarItemsGeneratorArgs = {
|
|
176
|
-
item: UnprocessedSidebarItemAutogenerated;
|
|
177
|
-
version: SidebarItemsGeneratorVersion;
|
|
178
|
-
docs: SidebarItemsGeneratorDoc[];
|
|
179
|
-
numberPrefixParser: NumberPrefixParser;
|
|
180
|
-
options: SidebarOptions;
|
|
181
|
-
};
|
|
182
|
-
export type SidebarItemsGenerator = (
|
|
183
|
-
generatorArgs: SidebarItemsGeneratorArgs,
|
|
184
|
-
) => Promise<SidebarItem[]>;
|
|
185
|
-
|
|
186
|
-
// Also inject the default generator to conveniently wrap/enhance/sort the default sidebar gen logic
|
|
187
|
-
// see https://github.com/facebook/docusaurus/issues/4640#issuecomment-822292320
|
|
188
|
-
export type SidebarItemsGeneratorOptionArgs = {
|
|
189
|
-
defaultSidebarItemsGenerator: SidebarItemsGenerator;
|
|
190
|
-
} & SidebarItemsGeneratorArgs;
|
|
191
|
-
export type SidebarItemsGeneratorOption = (
|
|
192
|
-
generatorArgs: SidebarItemsGeneratorOptionArgs,
|
|
193
|
-
) => Promise<SidebarItem[]>;
|
|
194
|
-
|
|
195
|
-
export type OrderMetadata = {
|
|
196
|
-
previous?: string;
|
|
197
|
-
next?: string;
|
|
198
|
-
sidebar?: string;
|
|
199
|
-
};
|
|
200
|
-
|
|
201
108
|
export type LastUpdateData = {
|
|
202
109
|
lastUpdatedAt?: number;
|
|
203
110
|
formattedLastUpdatedAt?: string;
|
|
@@ -1,222 +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.DefaultSidebarItemsGenerator = exports.CategoryMetadataFilenamePattern = exports.CategoryMetadataFilenameBase = void 0;
|
|
10
|
-
const tslib_1 = require("tslib");
|
|
11
|
-
const lodash_1 = require("lodash");
|
|
12
|
-
const utils_1 = require("@docusaurus/utils");
|
|
13
|
-
const utils_validation_1 = require("@docusaurus/utils-validation");
|
|
14
|
-
const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
|
|
15
|
-
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
16
|
-
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
17
|
-
const js_yaml_1 = (0, tslib_1.__importDefault)(require("js-yaml"));
|
|
18
|
-
const BreadcrumbSeparator = '/';
|
|
19
|
-
exports.CategoryMetadataFilenameBase = '_category_';
|
|
20
|
-
exports.CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
|
|
21
|
-
const CategoryMetadatasFileSchema = utils_validation_1.Joi.object({
|
|
22
|
-
label: utils_validation_1.Joi.string(),
|
|
23
|
-
position: utils_validation_1.Joi.number(),
|
|
24
|
-
collapsed: utils_validation_1.Joi.boolean(),
|
|
25
|
-
collapsible: utils_validation_1.Joi.boolean(),
|
|
26
|
-
className: utils_validation_1.Joi.string(),
|
|
27
|
-
});
|
|
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
|
|
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...
|
|
30
|
-
// TODO later if there is `CategoryFolder/index.md`, we may want to read the metadata as yaml on it
|
|
31
|
-
// see https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
|
32
|
-
async function readCategoryMetadatasFile(categoryDirPath) {
|
|
33
|
-
var _a, _b;
|
|
34
|
-
function validateCategoryMetadataFile(content) {
|
|
35
|
-
return utils_validation_1.Joi.attempt(content, CategoryMetadatasFileSchema);
|
|
36
|
-
}
|
|
37
|
-
async function tryReadFile(fileNameWithExtension, parse) {
|
|
38
|
-
// Simpler to use only posix paths for mocking file metadatas in tests
|
|
39
|
-
const filePath = (0, utils_1.posixPath)(path_1.default.join(categoryDirPath, fileNameWithExtension));
|
|
40
|
-
if (await fs_extra_1.default.pathExists(filePath)) {
|
|
41
|
-
const contentString = await fs_extra_1.default.readFile(filePath, { encoding: 'utf8' });
|
|
42
|
-
const unsafeContent = parse(contentString);
|
|
43
|
-
try {
|
|
44
|
-
return validateCategoryMetadataFile(unsafeContent);
|
|
45
|
-
}
|
|
46
|
-
catch (e) {
|
|
47
|
-
console.error(chalk_1.default.red(`The docs sidebar category metadata file looks invalid!\nPath: ${filePath}`));
|
|
48
|
-
throw e;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
return ((_b = (_a = (await tryReadFile(`${exports.CategoryMetadataFilenameBase}.json`, JSON.parse))) !== null && _a !== void 0 ? _a : (await tryReadFile(`${exports.CategoryMetadataFilenameBase}.yml`, js_yaml_1.default.load))) !== null && _b !== void 0 ? _b :
|
|
54
|
-
// eslint-disable-next-line no-return-await
|
|
55
|
-
(await tryReadFile(`${exports.CategoryMetadataFilenameBase}.yaml`, js_yaml_1.default.load)));
|
|
56
|
-
}
|
|
57
|
-
// [...parents, tail]
|
|
58
|
-
function parseBreadcrumb(breadcrumb) {
|
|
59
|
-
return {
|
|
60
|
-
parents: (0, lodash_1.take)(breadcrumb, breadcrumb.length - 1),
|
|
61
|
-
tail: (0, lodash_1.last)(breadcrumb),
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
// Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
|
65
|
-
const DefaultSidebarItemsGenerator = async ({ item, docs: allDocs, version, numberPrefixParser, options, }) => {
|
|
66
|
-
// Doc at the root of the autogenerated sidebar dir
|
|
67
|
-
function isRootDoc(doc) {
|
|
68
|
-
return doc.sourceDirName === item.dirName;
|
|
69
|
-
}
|
|
70
|
-
// Doc inside a subfolder of the autogenerated sidebar dir
|
|
71
|
-
function isCategoryDoc(doc) {
|
|
72
|
-
if (isRootDoc(doc)) {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
return (
|
|
76
|
-
// autogen dir is . and doc is in subfolder
|
|
77
|
-
item.dirName === '.' ||
|
|
78
|
-
// autogen dir is not . and doc is in subfolder
|
|
79
|
-
// "api/myDoc" startsWith "api/" (note "api2/myDoc" is not included)
|
|
80
|
-
doc.sourceDirName.startsWith((0, utils_1.addTrailingSlash)(item.dirName)));
|
|
81
|
-
}
|
|
82
|
-
function isInAutogeneratedDir(doc) {
|
|
83
|
-
return isRootDoc(doc) || isCategoryDoc(doc);
|
|
84
|
-
}
|
|
85
|
-
// autogenDir=a/b and docDir=a/b/c/d => returns c/d
|
|
86
|
-
// autogenDir=a/b and docDir=a/b => returns .
|
|
87
|
-
function getDocDirRelativeToAutogenDir(doc) {
|
|
88
|
-
if (!isInAutogeneratedDir(doc)) {
|
|
89
|
-
throw new Error('getDocDirRelativeToAutogenDir() can only be called for subdocs of the sidebar autogen dir.');
|
|
90
|
-
}
|
|
91
|
-
// Is there a node API to compare 2 relative paths more easily?
|
|
92
|
-
// path.relative() does not give good results
|
|
93
|
-
if (item.dirName === '.') {
|
|
94
|
-
return doc.sourceDirName;
|
|
95
|
-
}
|
|
96
|
-
else if (item.dirName === doc.sourceDirName) {
|
|
97
|
-
return '.';
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
return doc.sourceDirName.replace((0, utils_1.addTrailingSlash)(item.dirName), '');
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
// Get only docs in the autogen dir
|
|
104
|
-
// Sort by folder+filename at once
|
|
105
|
-
const docs = (0, lodash_1.sortBy)(allDocs.filter(isInAutogeneratedDir), (d) => d.source);
|
|
106
|
-
if (docs.length === 0) {
|
|
107
|
-
console.warn(chalk_1.default.yellow(`No docs found in dir ${item.dirName}: can't auto-generate a sidebar.`));
|
|
108
|
-
}
|
|
109
|
-
function createDocSidebarItem(doc) {
|
|
110
|
-
return {
|
|
111
|
-
type: 'doc',
|
|
112
|
-
id: doc.id,
|
|
113
|
-
...(doc.frontMatter.sidebar_label && {
|
|
114
|
-
label: doc.frontMatter.sidebar_label,
|
|
115
|
-
}),
|
|
116
|
-
...(doc.frontMatter.sidebar_class_name && {
|
|
117
|
-
className: doc.frontMatter.sidebar_class_name,
|
|
118
|
-
}),
|
|
119
|
-
...(typeof doc.sidebarPosition !== 'undefined' && {
|
|
120
|
-
position: doc.sidebarPosition,
|
|
121
|
-
}),
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
async function createCategorySidebarItem({ breadcrumb, }) {
|
|
125
|
-
var _a, _b, _c, _d;
|
|
126
|
-
const categoryDirPath = path_1.default.join(version.contentPath, item.dirName, // fix https://github.com/facebook/docusaurus/issues/4638
|
|
127
|
-
breadcrumb.join(BreadcrumbSeparator));
|
|
128
|
-
const categoryMetadatas = await readCategoryMetadatasFile(categoryDirPath);
|
|
129
|
-
const { tail } = parseBreadcrumb(breadcrumb);
|
|
130
|
-
const { filename, numberPrefix } = numberPrefixParser(tail);
|
|
131
|
-
const position = (_a = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.position) !== null && _a !== void 0 ? _a : numberPrefix;
|
|
132
|
-
const collapsible = (_b = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.collapsible) !== null && _b !== void 0 ? _b : options.sidebarCollapsible;
|
|
133
|
-
const collapsed = (_c = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.collapsed) !== null && _c !== void 0 ? _c : options.sidebarCollapsed;
|
|
134
|
-
const className = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.className;
|
|
135
|
-
return {
|
|
136
|
-
type: 'category',
|
|
137
|
-
label: (_d = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.label) !== null && _d !== void 0 ? _d : filename,
|
|
138
|
-
items: [],
|
|
139
|
-
collapsed,
|
|
140
|
-
collapsible,
|
|
141
|
-
...(typeof position !== 'undefined' && { position }),
|
|
142
|
-
...(typeof className !== 'undefined' && { className }),
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
// Not sure how to simplify this algorithm :/
|
|
146
|
-
async function autogenerateSidebarItems() {
|
|
147
|
-
const sidebarItems = []; // mutable result
|
|
148
|
-
const categoriesByBreadcrumb = {}; // mutable cache of categories already created
|
|
149
|
-
async function getOrCreateCategoriesForBreadcrumb(breadcrumb) {
|
|
150
|
-
if (breadcrumb.length === 0) {
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
const { parents } = parseBreadcrumb(breadcrumb);
|
|
154
|
-
const parentCategory = await getOrCreateCategoriesForBreadcrumb(parents);
|
|
155
|
-
const existingCategory = categoriesByBreadcrumb[breadcrumb.join(BreadcrumbSeparator)];
|
|
156
|
-
if (existingCategory) {
|
|
157
|
-
return existingCategory;
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
const newCategory = await createCategorySidebarItem({
|
|
161
|
-
breadcrumb,
|
|
162
|
-
});
|
|
163
|
-
if (parentCategory) {
|
|
164
|
-
parentCategory.items.push(newCategory);
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
sidebarItems.push(newCategory);
|
|
168
|
-
}
|
|
169
|
-
categoriesByBreadcrumb[breadcrumb.join(BreadcrumbSeparator)] =
|
|
170
|
-
newCategory;
|
|
171
|
-
return newCategory;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
// Get the category breadcrumb of a doc (relative to the dir of the autogenerated sidebar item)
|
|
175
|
-
function getRelativeBreadcrumb(doc) {
|
|
176
|
-
const relativeDirPath = getDocDirRelativeToAutogenDir(doc);
|
|
177
|
-
if (relativeDirPath === '.') {
|
|
178
|
-
return [];
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
return relativeDirPath.split(BreadcrumbSeparator);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
async function handleDocItem(doc) {
|
|
185
|
-
const breadcrumb = getRelativeBreadcrumb(doc);
|
|
186
|
-
const category = await getOrCreateCategoriesForBreadcrumb(breadcrumb);
|
|
187
|
-
const docSidebarItem = createDocSidebarItem(doc);
|
|
188
|
-
if (category) {
|
|
189
|
-
category.items.push(docSidebarItem);
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
sidebarItems.push(docSidebarItem);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
// async process made sequential on purpose! order matters
|
|
196
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
197
|
-
for (const doc of docs) {
|
|
198
|
-
// eslint-disable-next-line no-await-in-loop
|
|
199
|
-
await handleDocItem(doc);
|
|
200
|
-
}
|
|
201
|
-
return sidebarItems;
|
|
202
|
-
}
|
|
203
|
-
const sidebarItems = await autogenerateSidebarItems();
|
|
204
|
-
return sortSidebarItems(sidebarItems);
|
|
205
|
-
};
|
|
206
|
-
exports.DefaultSidebarItemsGenerator = DefaultSidebarItemsGenerator;
|
|
207
|
-
// Recursively sort the categories/docs + remove the "position" attribute from final output
|
|
208
|
-
// Note: the "position" is only used to sort "inside" a sidebar slice
|
|
209
|
-
// It is not used to sort across multiple consecutive sidebar slices (ie a whole Category composed of multiple autogenerated items)
|
|
210
|
-
function sortSidebarItems(sidebarItems) {
|
|
211
|
-
const processedSidebarItems = sidebarItems.map((item) => {
|
|
212
|
-
if (item.type === 'category') {
|
|
213
|
-
return {
|
|
214
|
-
...item,
|
|
215
|
-
items: sortSidebarItems(item.items),
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
return item;
|
|
219
|
-
});
|
|
220
|
-
const sortedSidebarItems = (0, lodash_1.orderBy)(processedSidebarItems, (item) => item.position, ['asc']);
|
|
221
|
-
return sortedSidebarItems.map(({ position, ...item }) => item);
|
|
222
|
-
}
|
package/lib/sidebars.d.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
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 { Sidebars, SidebarItem, SidebarItemLink, SidebarItemDoc, Sidebar, SidebarItemCategory, UnprocessedSidebars, UnprocessedSidebar, DocMetadataBase, VersionMetadata, SidebarItemsGeneratorDoc, SidebarItemsGeneratorVersion, NumberPrefixParser, SidebarItemsGeneratorOption, SidebarOptions, PluginOptions } from './types';
|
|
8
|
-
export declare const DefaultSidebars: UnprocessedSidebars;
|
|
9
|
-
export declare const DisabledSidebars: UnprocessedSidebars;
|
|
10
|
-
export declare function resolveSidebarPathOption(siteDir: string, sidebarPathOption: PluginOptions['sidebarPath']): PluginOptions['sidebarPath'];
|
|
11
|
-
export declare function loadSidebars(sidebarFilePath: string | false | undefined, options: SidebarOptions): UnprocessedSidebars;
|
|
12
|
-
export declare function toSidebarItemsGeneratorDoc(doc: DocMetadataBase): SidebarItemsGeneratorDoc;
|
|
13
|
-
export declare function toSidebarItemsGeneratorVersion(version: VersionMetadata): SidebarItemsGeneratorVersion;
|
|
14
|
-
export declare function fixSidebarItemInconsistencies(item: SidebarItem): SidebarItem;
|
|
15
|
-
export declare function processSidebar({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebar, docs, version, options, }: {
|
|
16
|
-
sidebarItemsGenerator: SidebarItemsGeneratorOption;
|
|
17
|
-
numberPrefixParser: NumberPrefixParser;
|
|
18
|
-
unprocessedSidebar: UnprocessedSidebar;
|
|
19
|
-
docs: DocMetadataBase[];
|
|
20
|
-
version: VersionMetadata;
|
|
21
|
-
options: SidebarOptions;
|
|
22
|
-
}): Promise<Sidebar>;
|
|
23
|
-
export declare function processSidebars({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebars, docs, version, options, }: {
|
|
24
|
-
sidebarItemsGenerator: SidebarItemsGeneratorOption;
|
|
25
|
-
numberPrefixParser: NumberPrefixParser;
|
|
26
|
-
unprocessedSidebars: UnprocessedSidebars;
|
|
27
|
-
docs: DocMetadataBase[];
|
|
28
|
-
version: VersionMetadata;
|
|
29
|
-
options: SidebarOptions;
|
|
30
|
-
}): Promise<Sidebars>;
|
|
31
|
-
export declare function collectSidebarDocItems(sidebar: Sidebar): SidebarItemDoc[];
|
|
32
|
-
export declare function collectSidebarCategories(sidebar: Sidebar): SidebarItemCategory[];
|
|
33
|
-
export declare function collectSidebarLinks(sidebar: Sidebar): SidebarItemLink[];
|
|
34
|
-
export declare function transformSidebarItems(sidebar: Sidebar, updateFn: (item: SidebarItem) => SidebarItem): Sidebar;
|
|
35
|
-
export declare function collectSidebarsDocIds(sidebars: Sidebars): Record<string, string[]>;
|
|
36
|
-
export declare function createSidebarsUtils(sidebars: Sidebars): {
|
|
37
|
-
getFirstDocIdOfFirstSidebar: () => string | undefined;
|
|
38
|
-
getSidebarNameByDocId: (docId: string) => string | undefined;
|
|
39
|
-
getDocNavigation: (docId: string) => {
|
|
40
|
-
sidebarName: string | undefined;
|
|
41
|
-
previousId: string | undefined;
|
|
42
|
-
nextId: string | undefined;
|
|
43
|
-
};
|
|
44
|
-
checkSidebarsDocIds: (validDocIds: string[], sidebarFilePath: string) => void;
|
|
45
|
-
};
|