@docusaurus/plugin-content-docs 2.0.0-beta.8bda3b2db → 2.0.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/.tsbuildinfo +1 -1
- package/lib/cli.d.ts +2 -2
- package/lib/cli.js +20 -24
- package/lib/client/docsClientUtils.d.ts +1 -4
- package/lib/client/docsClientUtils.js +12 -16
- package/lib/docFrontMatter.js +7 -3
- package/lib/docs.d.ts +4 -2
- package/lib/docs.js +77 -23
- package/lib/index.js +88 -94
- package/lib/lastUpdate.js +8 -8
- package/lib/markdown/index.d.ts +3 -6
- package/lib/markdown/index.js +3 -3
- package/lib/markdown/linkify.js +2 -2
- package/lib/options.d.ts +1 -1
- package/lib/options.js +39 -11
- package/lib/props.d.ts +7 -2
- package/lib/props.js +27 -4
- package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +3 -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/slug.js +4 -4
- package/lib/tags.d.ts +8 -0
- package/lib/tags.js +22 -0
- package/lib/theme/hooks/useDocs.js +24 -21
- package/lib/translations.d.ts +1 -1
- package/lib/translations.js +13 -13
- package/lib/types.d.ts +35 -58
- package/lib/versions.d.ts +1 -1
- package/lib/versions.js +75 -22
- package/package.json +15 -14
- package/src/__tests__/__fixtures__/simple-site/docs/_partials/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/simple-site/docs/_partials/subfolder/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/simple-site/docs/_somePartial.md +3 -0
- package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +5 -0
- package/src/__tests__/__fixtures__/simple-site/docs/hello.md +1 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +2 -0
- package/src/__tests__/__fixtures__/simple-site/sidebars.json +15 -1
- package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +6 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_partials/subfolder/somePartial.md +3 -0
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/_somePartial.md +3 -0
- package/src/__tests__/__snapshots__/cli.test.ts.snap +33 -0
- package/src/__tests__/__snapshots__/docs.test.ts.snap +140 -0
- package/src/__tests__/__snapshots__/index.test.ts.snap +478 -60
- package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -3
- package/src/__tests__/cli.test.ts +14 -10
- package/src/__tests__/docFrontMatter.test.ts +163 -48
- package/src/__tests__/docs.test.ts +167 -21
- package/src/__tests__/index.test.ts +74 -30
- package/src/__tests__/lastUpdate.test.ts +3 -2
- package/src/__tests__/options.test.ts +46 -3
- package/src/__tests__/props.test.ts +62 -0
- package/src/__tests__/translations.test.ts +0 -1
- package/src/__tests__/versions.test.ts +88 -60
- package/src/cli.ts +27 -30
- package/src/client/__tests__/docsClientUtils.test.ts +4 -5
- package/src/client/docsClientUtils.ts +6 -27
- package/src/docFrontMatter.ts +8 -3
- package/src/docs.ts +92 -9
- package/src/index.ts +114 -121
- package/src/lastUpdate.ts +10 -6
- package/src/markdown/index.ts +8 -12
- package/src/numberPrefix.ts +4 -2
- package/src/options.ts +47 -17
- package/src/plugin-content-docs.d.ts +121 -34
- package/src/props.ts +42 -6
- 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} +21 -6
- package/src/{__tests__/sidebarItemsGenerator.test.ts → sidebars/__tests__/generator.test.ts} +29 -7
- 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/tags.ts +21 -0
- package/src/theme/hooks/useDocs.ts +5 -1
- package/src/translations.ts +26 -36
- package/src/types.ts +48 -99
- package/src/versions.ts +109 -17
- package/lib/sidebarItemsGenerator.js +0 -211
- package/lib/sidebars.d.ts +0 -43
- package/lib/sidebars.js +0 -319
- package/src/__tests__/sidebars.test.ts +0 -639
- package/src/sidebarItemsGenerator.ts +0 -307
- package/src/sidebars.ts +0 -506
|
@@ -1,211 +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 = tslib_1.__importDefault(require("chalk"));
|
|
15
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
16
|
-
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
17
|
-
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
|
|
18
|
-
const sidebars_1 = require("./sidebars");
|
|
19
|
-
const BreadcrumbSeparator = '/';
|
|
20
|
-
exports.CategoryMetadataFilenameBase = '_category_';
|
|
21
|
-
exports.CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
|
|
22
|
-
const CategoryMetadatasFileSchema = utils_validation_1.Joi.object({
|
|
23
|
-
label: utils_validation_1.Joi.string(),
|
|
24
|
-
position: utils_validation_1.Joi.number(),
|
|
25
|
-
collapsed: utils_validation_1.Joi.boolean(),
|
|
26
|
-
});
|
|
27
|
-
// TODO I now believe we should read all the category metadata files ahead of time: we may need this metadata to customize docs metadata
|
|
28
|
-
// 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...
|
|
29
|
-
// TODO later if there is `CategoryFolder/index.md`, we may want to read the metadata as yaml on it
|
|
30
|
-
// see https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
|
31
|
-
async function readCategoryMetadatasFile(categoryDirPath) {
|
|
32
|
-
var _a, _b;
|
|
33
|
-
function validateCategoryMetadataFile(content) {
|
|
34
|
-
return utils_validation_1.Joi.attempt(content, CategoryMetadatasFileSchema);
|
|
35
|
-
}
|
|
36
|
-
async function tryReadFile(fileNameWithExtension, parse) {
|
|
37
|
-
// Simpler to use only posix paths for mocking file metadatas in tests
|
|
38
|
-
const filePath = utils_1.posixPath(path_1.default.join(categoryDirPath, fileNameWithExtension));
|
|
39
|
-
if (await fs_extra_1.default.pathExists(filePath)) {
|
|
40
|
-
const contentString = await fs_extra_1.default.readFile(filePath, { encoding: 'utf8' });
|
|
41
|
-
const unsafeContent = parse(contentString);
|
|
42
|
-
try {
|
|
43
|
-
return validateCategoryMetadataFile(unsafeContent);
|
|
44
|
-
}
|
|
45
|
-
catch (e) {
|
|
46
|
-
console.error(chalk_1.default.red(`The docs sidebar category metadata file looks invalid!\nPath: ${filePath}`));
|
|
47
|
-
throw e;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
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 :
|
|
53
|
-
// eslint-disable-next-line no-return-await
|
|
54
|
-
(await tryReadFile(`${exports.CategoryMetadataFilenameBase}.yaml`, js_yaml_1.default.load)));
|
|
55
|
-
}
|
|
56
|
-
// [...parents, tail]
|
|
57
|
-
function parseBreadcrumb(breadcrumb) {
|
|
58
|
-
return {
|
|
59
|
-
parents: lodash_1.take(breadcrumb, breadcrumb.length - 1),
|
|
60
|
-
tail: lodash_1.last(breadcrumb),
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
// Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
|
64
|
-
const DefaultSidebarItemsGenerator = async function defaultSidebarItemsGenerator({ item, docs: allDocs, version, numberPrefixParser, }) {
|
|
65
|
-
// Doc at the root of the autogenerated sidebar dir
|
|
66
|
-
function isRootDoc(doc) {
|
|
67
|
-
return doc.sourceDirName === item.dirName;
|
|
68
|
-
}
|
|
69
|
-
// Doc inside a subfolder of the autogenerated sidebar dir
|
|
70
|
-
function isCategoryDoc(doc) {
|
|
71
|
-
if (isRootDoc(doc)) {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
return (
|
|
75
|
-
// autogen dir is . and doc is in subfolder
|
|
76
|
-
item.dirName === '.' ||
|
|
77
|
-
// autogen dir is not . and doc is in subfolder
|
|
78
|
-
// "api/myDoc" startsWith "api/" (note "api2/myDoc" is not included)
|
|
79
|
-
doc.sourceDirName.startsWith(utils_1.addTrailingSlash(item.dirName)));
|
|
80
|
-
}
|
|
81
|
-
function isInAutogeneratedDir(doc) {
|
|
82
|
-
return isRootDoc(doc) || isCategoryDoc(doc);
|
|
83
|
-
}
|
|
84
|
-
// autogenDir=a/b and docDir=a/b/c/d => returns c/d
|
|
85
|
-
// autogenDir=a/b and docDir=a/b => returns .
|
|
86
|
-
function getDocDirRelativeToAutogenDir(doc) {
|
|
87
|
-
if (!isInAutogeneratedDir(doc)) {
|
|
88
|
-
throw new Error('getDocDirRelativeToAutogenDir() can only be called for subdocs of the sidebar autogen dir.');
|
|
89
|
-
}
|
|
90
|
-
// Is there a node API to compare 2 relative paths more easily?
|
|
91
|
-
// path.relative() does not give good results
|
|
92
|
-
if (item.dirName === '.') {
|
|
93
|
-
return doc.sourceDirName;
|
|
94
|
-
}
|
|
95
|
-
else if (item.dirName === doc.sourceDirName) {
|
|
96
|
-
return '.';
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
return doc.sourceDirName.replace(utils_1.addTrailingSlash(item.dirName), '');
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
// Get only docs in the autogen dir
|
|
103
|
-
// Sort by folder+filename at once
|
|
104
|
-
const docs = lodash_1.sortBy(allDocs.filter(isInAutogeneratedDir), (d) => d.source);
|
|
105
|
-
if (docs.length === 0) {
|
|
106
|
-
console.warn(chalk_1.default.yellow(`No docs found in dir ${item.dirName}: can't auto-generate a sidebar.`));
|
|
107
|
-
}
|
|
108
|
-
function createDocSidebarItem(doc) {
|
|
109
|
-
return {
|
|
110
|
-
type: 'doc',
|
|
111
|
-
id: doc.id,
|
|
112
|
-
...(doc.frontMatter.sidebar_label && {
|
|
113
|
-
label: doc.frontMatter.sidebar_label,
|
|
114
|
-
}),
|
|
115
|
-
...(typeof doc.sidebarPosition !== 'undefined' && {
|
|
116
|
-
position: doc.sidebarPosition,
|
|
117
|
-
}),
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
async function createCategorySidebarItem({ breadcrumb, }) {
|
|
121
|
-
var _a, _b, _c;
|
|
122
|
-
const categoryDirPath = path_1.default.join(version.contentPath, item.dirName, // fix https://github.com/facebook/docusaurus/issues/4638
|
|
123
|
-
breadcrumb.join(BreadcrumbSeparator));
|
|
124
|
-
const categoryMetadatas = await readCategoryMetadatasFile(categoryDirPath);
|
|
125
|
-
const { tail } = parseBreadcrumb(breadcrumb);
|
|
126
|
-
const { filename, numberPrefix } = numberPrefixParser(tail);
|
|
127
|
-
const position = (_a = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.position) !== null && _a !== void 0 ? _a : numberPrefix;
|
|
128
|
-
return {
|
|
129
|
-
type: 'category',
|
|
130
|
-
label: (_b = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.label) !== null && _b !== void 0 ? _b : filename,
|
|
131
|
-
items: [],
|
|
132
|
-
collapsed: (_c = categoryMetadatas === null || categoryMetadatas === void 0 ? void 0 : categoryMetadatas.collapsed) !== null && _c !== void 0 ? _c : sidebars_1.DefaultCategoryCollapsedValue,
|
|
133
|
-
...(typeof position !== 'undefined' && { position }),
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
// Not sure how to simplify this algorithm :/
|
|
137
|
-
async function autogenerateSidebarItems() {
|
|
138
|
-
const sidebarItems = []; // mutable result
|
|
139
|
-
const categoriesByBreadcrumb = {}; // mutable cache of categories already created
|
|
140
|
-
async function getOrCreateCategoriesForBreadcrumb(breadcrumb) {
|
|
141
|
-
if (breadcrumb.length === 0) {
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
const { parents } = parseBreadcrumb(breadcrumb);
|
|
145
|
-
const parentCategory = await getOrCreateCategoriesForBreadcrumb(parents);
|
|
146
|
-
const existingCategory = categoriesByBreadcrumb[breadcrumb.join(BreadcrumbSeparator)];
|
|
147
|
-
if (existingCategory) {
|
|
148
|
-
return existingCategory;
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
const newCategory = await createCategorySidebarItem({
|
|
152
|
-
breadcrumb,
|
|
153
|
-
});
|
|
154
|
-
if (parentCategory) {
|
|
155
|
-
parentCategory.items.push(newCategory);
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
sidebarItems.push(newCategory);
|
|
159
|
-
}
|
|
160
|
-
categoriesByBreadcrumb[breadcrumb.join(BreadcrumbSeparator)] = newCategory;
|
|
161
|
-
return newCategory;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
// Get the category breadcrumb of a doc (relative to the dir of the autogenerated sidebar item)
|
|
165
|
-
function getRelativeBreadcrumb(doc) {
|
|
166
|
-
const relativeDirPath = getDocDirRelativeToAutogenDir(doc);
|
|
167
|
-
if (relativeDirPath === '.') {
|
|
168
|
-
return [];
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
return relativeDirPath.split(BreadcrumbSeparator);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
async function handleDocItem(doc) {
|
|
175
|
-
const breadcrumb = getRelativeBreadcrumb(doc);
|
|
176
|
-
const category = await getOrCreateCategoriesForBreadcrumb(breadcrumb);
|
|
177
|
-
const docSidebarItem = createDocSidebarItem(doc);
|
|
178
|
-
if (category) {
|
|
179
|
-
category.items.push(docSidebarItem);
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
sidebarItems.push(docSidebarItem);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
// async process made sequential on purpose! order matters
|
|
186
|
-
for (const doc of docs) {
|
|
187
|
-
// eslint-disable-next-line no-await-in-loop
|
|
188
|
-
await handleDocItem(doc);
|
|
189
|
-
}
|
|
190
|
-
return sidebarItems;
|
|
191
|
-
}
|
|
192
|
-
const sidebarItems = await autogenerateSidebarItems();
|
|
193
|
-
return sortSidebarItems(sidebarItems);
|
|
194
|
-
};
|
|
195
|
-
exports.DefaultSidebarItemsGenerator = DefaultSidebarItemsGenerator;
|
|
196
|
-
// Recursively sort the categories/docs + remove the "position" attribute from final output
|
|
197
|
-
// Note: the "position" is only used to sort "inside" a sidebar slice
|
|
198
|
-
// It is not used to sort across multiple consecutive sidebar slices (ie a whole Category composed of multiple autogenerated items)
|
|
199
|
-
function sortSidebarItems(sidebarItems) {
|
|
200
|
-
const processedSidebarItems = sidebarItems.map((item) => {
|
|
201
|
-
if (item.type === 'category') {
|
|
202
|
-
return {
|
|
203
|
-
...item,
|
|
204
|
-
items: sortSidebarItems(item.items),
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
return item;
|
|
208
|
-
});
|
|
209
|
-
const sortedSidebarItems = lodash_1.orderBy(processedSidebarItems, (item) => item.position, ['asc']);
|
|
210
|
-
return sortedSidebarItems.map(({ position: _removed, ...item }) => item);
|
|
211
|
-
}
|
package/lib/sidebars.d.ts
DELETED
|
@@ -1,43 +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, PluginOptions } from './types';
|
|
8
|
-
export declare const DefaultCategoryCollapsedValue = true;
|
|
9
|
-
export declare const DefaultSidebars: UnprocessedSidebars;
|
|
10
|
-
export declare const DisabledSidebars: UnprocessedSidebars;
|
|
11
|
-
export declare function resolveSidebarPathOption(siteDir: string, sidebarPathOption: PluginOptions['sidebarPath']): PluginOptions['sidebarPath'];
|
|
12
|
-
export declare function loadSidebars(sidebarFilePath: string | false | undefined): UnprocessedSidebars;
|
|
13
|
-
export declare function toSidebarItemsGeneratorDoc(doc: DocMetadataBase): SidebarItemsGeneratorDoc;
|
|
14
|
-
export declare function toSidebarItemsGeneratorVersion(version: VersionMetadata): SidebarItemsGeneratorVersion;
|
|
15
|
-
export declare function processSidebar({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebar, docs, version, }: {
|
|
16
|
-
sidebarItemsGenerator: SidebarItemsGeneratorOption;
|
|
17
|
-
numberPrefixParser: NumberPrefixParser;
|
|
18
|
-
unprocessedSidebar: UnprocessedSidebar;
|
|
19
|
-
docs: DocMetadataBase[];
|
|
20
|
-
version: VersionMetadata;
|
|
21
|
-
}): Promise<Sidebar>;
|
|
22
|
-
export declare function processSidebars({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebars, docs, version, }: {
|
|
23
|
-
sidebarItemsGenerator: SidebarItemsGeneratorOption;
|
|
24
|
-
numberPrefixParser: NumberPrefixParser;
|
|
25
|
-
unprocessedSidebars: UnprocessedSidebars;
|
|
26
|
-
docs: DocMetadataBase[];
|
|
27
|
-
version: VersionMetadata;
|
|
28
|
-
}): Promise<Sidebars>;
|
|
29
|
-
export declare function collectSidebarDocItems(sidebar: Sidebar): SidebarItemDoc[];
|
|
30
|
-
export declare function collectSidebarCategories(sidebar: Sidebar): SidebarItemCategory[];
|
|
31
|
-
export declare function collectSidebarLinks(sidebar: Sidebar): SidebarItemLink[];
|
|
32
|
-
export declare function transformSidebarItems(sidebar: Sidebar, updateFn: (item: SidebarItem) => SidebarItem): Sidebar;
|
|
33
|
-
export declare function collectSidebarsDocIds(sidebars: Sidebars): Record<string, string[]>;
|
|
34
|
-
export declare function createSidebarsUtils(sidebars: Sidebars): {
|
|
35
|
-
getFirstDocIdOfFirstSidebar: () => string | undefined;
|
|
36
|
-
getSidebarNameByDocId: (docId: string) => string | undefined;
|
|
37
|
-
getDocNavigation: (docId: string) => {
|
|
38
|
-
sidebarName: string | undefined;
|
|
39
|
-
previousId: string | undefined;
|
|
40
|
-
nextId: string | undefined;
|
|
41
|
-
};
|
|
42
|
-
checkSidebarsDocIds: (validDocIds: string[], sidebarFilePath: string) => void;
|
|
43
|
-
};
|
package/lib/sidebars.js
DELETED
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.createSidebarsUtils = exports.collectSidebarsDocIds = exports.transformSidebarItems = exports.collectSidebarLinks = exports.collectSidebarCategories = exports.collectSidebarDocItems = exports.processSidebars = exports.processSidebar = exports.toSidebarItemsGeneratorVersion = exports.toSidebarItemsGeneratorDoc = exports.loadSidebars = exports.resolveSidebarPathOption = exports.DisabledSidebars = exports.DefaultSidebars = exports.DefaultCategoryCollapsedValue = void 0;
|
|
10
|
-
const tslib_1 = require("tslib");
|
|
11
|
-
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
12
|
-
const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
|
|
13
|
-
const lodash_1 = require("lodash");
|
|
14
|
-
const utils_1 = require("@docusaurus/utils");
|
|
15
|
-
const combine_promises_1 = tslib_1.__importDefault(require("combine-promises"));
|
|
16
|
-
const sidebarItemsGenerator_1 = require("./sidebarItemsGenerator");
|
|
17
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
18
|
-
function isCategoryShorthand(item) {
|
|
19
|
-
return typeof item !== 'string' && !item.type;
|
|
20
|
-
}
|
|
21
|
-
// categories are collapsed by default, unless user set collapsed = false
|
|
22
|
-
exports.DefaultCategoryCollapsedValue = true;
|
|
23
|
-
/**
|
|
24
|
-
* Convert {category1: [item1,item2]} shorthand syntax to long-form syntax
|
|
25
|
-
*/
|
|
26
|
-
function normalizeCategoryShorthand(sidebar) {
|
|
27
|
-
return Object.entries(sidebar).map(([label, items]) => ({
|
|
28
|
-
type: 'category',
|
|
29
|
-
collapsed: exports.DefaultCategoryCollapsedValue,
|
|
30
|
-
label,
|
|
31
|
-
items,
|
|
32
|
-
}));
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Check that item contains only allowed keys.
|
|
36
|
-
*/
|
|
37
|
-
function assertItem(item, keys) {
|
|
38
|
-
const unknownKeys = Object.keys(item).filter(
|
|
39
|
-
// @ts-expect-error: key is always string
|
|
40
|
-
(key) => !keys.includes(key) && key !== 'type');
|
|
41
|
-
if (unknownKeys.length) {
|
|
42
|
-
throw new Error(`Unknown sidebar item keys: ${unknownKeys}. Item: ${JSON.stringify(item)}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
function assertIsCategory(item) {
|
|
46
|
-
assertItem(item, ['items', 'label', 'collapsed', 'customProps']);
|
|
47
|
-
if (typeof item.label !== 'string') {
|
|
48
|
-
throw new Error(`Error loading ${JSON.stringify(item)}: "label" must be a string.`);
|
|
49
|
-
}
|
|
50
|
-
if (!Array.isArray(item.items)) {
|
|
51
|
-
throw new Error(`Error loading ${JSON.stringify(item)}: "items" must be an array.`);
|
|
52
|
-
}
|
|
53
|
-
// "collapsed" is an optional property
|
|
54
|
-
if (item.hasOwnProperty('collapsed') && typeof item.collapsed !== 'boolean') {
|
|
55
|
-
throw new Error(`Error loading ${JSON.stringify(item)}: "collapsed" must be a boolean.`);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
function assertIsAutogenerated(item) {
|
|
59
|
-
assertItem(item, ['dirName', 'customProps']);
|
|
60
|
-
if (typeof item.dirName !== 'string') {
|
|
61
|
-
throw new Error(`Error loading ${JSON.stringify(item)}: "dirName" must be a string.`);
|
|
62
|
-
}
|
|
63
|
-
if (item.dirName.startsWith('/') || item.dirName.endsWith('/')) {
|
|
64
|
-
throw new Error(`Error loading ${JSON.stringify(item)}: "dirName" must be a dir path relative to the docs folder root, and should not start or end with slash`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function assertIsDoc(item) {
|
|
68
|
-
assertItem(item, ['id', 'label', 'customProps']);
|
|
69
|
-
if (typeof item.id !== 'string') {
|
|
70
|
-
throw new Error(`Error loading ${JSON.stringify(item)}: "id" must be a string.`);
|
|
71
|
-
}
|
|
72
|
-
if (item.label && typeof item.label !== 'string') {
|
|
73
|
-
throw new Error(`Error loading ${JSON.stringify(item)}: "label" must be a string.`);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
function assertIsLink(item) {
|
|
77
|
-
assertItem(item, ['href', 'label', 'customProps']);
|
|
78
|
-
if (typeof item.href !== 'string') {
|
|
79
|
-
throw new Error(`Error loading ${JSON.stringify(item)}: "href" must be a string.`);
|
|
80
|
-
}
|
|
81
|
-
if (typeof item.label !== 'string') {
|
|
82
|
-
throw new Error(`Error loading ${JSON.stringify(item)}: "label" must be a string.`);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Normalizes recursively item and all its children. Ensures that at the end
|
|
87
|
-
* each item will be an object with the corresponding type.
|
|
88
|
-
*/
|
|
89
|
-
function normalizeItem(item) {
|
|
90
|
-
if (typeof item === 'string') {
|
|
91
|
-
return [
|
|
92
|
-
{
|
|
93
|
-
type: 'doc',
|
|
94
|
-
id: item,
|
|
95
|
-
},
|
|
96
|
-
];
|
|
97
|
-
}
|
|
98
|
-
if (isCategoryShorthand(item)) {
|
|
99
|
-
return lodash_1.flatMap(normalizeCategoryShorthand(item), normalizeItem);
|
|
100
|
-
}
|
|
101
|
-
switch (item.type) {
|
|
102
|
-
case 'category':
|
|
103
|
-
assertIsCategory(item);
|
|
104
|
-
return [
|
|
105
|
-
{
|
|
106
|
-
collapsed: exports.DefaultCategoryCollapsedValue,
|
|
107
|
-
...item,
|
|
108
|
-
items: lodash_1.flatMap(item.items, normalizeItem),
|
|
109
|
-
},
|
|
110
|
-
];
|
|
111
|
-
case 'autogenerated':
|
|
112
|
-
assertIsAutogenerated(item);
|
|
113
|
-
return [item];
|
|
114
|
-
case 'link':
|
|
115
|
-
assertIsLink(item);
|
|
116
|
-
return [item];
|
|
117
|
-
case 'ref':
|
|
118
|
-
case 'doc':
|
|
119
|
-
assertIsDoc(item);
|
|
120
|
-
return [item];
|
|
121
|
-
default: {
|
|
122
|
-
const extraMigrationError = item.type === 'subcategory'
|
|
123
|
-
? 'Docusaurus v2: "subcategory" has been renamed as "category".'
|
|
124
|
-
: '';
|
|
125
|
-
throw new Error(`Unknown sidebar item type "${item.type}". Sidebar item is ${JSON.stringify(item)}.\n${extraMigrationError}`);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
function normalizeSidebar(sidebar) {
|
|
130
|
-
const normalizedSidebar = Array.isArray(sidebar)
|
|
131
|
-
? sidebar
|
|
132
|
-
: normalizeCategoryShorthand(sidebar);
|
|
133
|
-
return lodash_1.flatMap(normalizedSidebar, normalizeItem);
|
|
134
|
-
}
|
|
135
|
-
function normalizeSidebars(sidebars) {
|
|
136
|
-
return lodash_1.mapValues(sidebars, normalizeSidebar);
|
|
137
|
-
}
|
|
138
|
-
exports.DefaultSidebars = {
|
|
139
|
-
defaultSidebar: [
|
|
140
|
-
{
|
|
141
|
-
type: 'autogenerated',
|
|
142
|
-
dirName: '.',
|
|
143
|
-
},
|
|
144
|
-
],
|
|
145
|
-
};
|
|
146
|
-
exports.DisabledSidebars = {};
|
|
147
|
-
// If a path is provided, make it absolute
|
|
148
|
-
// use this before loadSidebars()
|
|
149
|
-
function resolveSidebarPathOption(siteDir, sidebarPathOption) {
|
|
150
|
-
return sidebarPathOption
|
|
151
|
-
? path_1.default.resolve(siteDir, sidebarPathOption)
|
|
152
|
-
: sidebarPathOption;
|
|
153
|
-
}
|
|
154
|
-
exports.resolveSidebarPathOption = resolveSidebarPathOption;
|
|
155
|
-
// TODO refactor: make async
|
|
156
|
-
// Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
|
|
157
|
-
function loadSidebars(sidebarFilePath) {
|
|
158
|
-
// false => no sidebars
|
|
159
|
-
if (sidebarFilePath === false) {
|
|
160
|
-
return exports.DisabledSidebars;
|
|
161
|
-
}
|
|
162
|
-
// undefined => defaults to autogenerated sidebars
|
|
163
|
-
if (typeof sidebarFilePath === 'undefined') {
|
|
164
|
-
return exports.DefaultSidebars;
|
|
165
|
-
}
|
|
166
|
-
// unexisting sidebars file: no sidebars
|
|
167
|
-
// Note: this edge case can happen on versioned docs, not current version
|
|
168
|
-
// We avoid creating empty versioned sidebars file with the CLI
|
|
169
|
-
if (!fs_extra_1.default.existsSync(sidebarFilePath)) {
|
|
170
|
-
return exports.DisabledSidebars;
|
|
171
|
-
}
|
|
172
|
-
// We don't want sidebars to be cached because of hot reloading.
|
|
173
|
-
const sidebarJson = import_fresh_1.default(sidebarFilePath);
|
|
174
|
-
return normalizeSidebars(sidebarJson);
|
|
175
|
-
}
|
|
176
|
-
exports.loadSidebars = loadSidebars;
|
|
177
|
-
function toSidebarItemsGeneratorDoc(doc) {
|
|
178
|
-
return lodash_1.pick(doc, [
|
|
179
|
-
'id',
|
|
180
|
-
'frontMatter',
|
|
181
|
-
'source',
|
|
182
|
-
'sourceDirName',
|
|
183
|
-
'sidebarPosition',
|
|
184
|
-
]);
|
|
185
|
-
}
|
|
186
|
-
exports.toSidebarItemsGeneratorDoc = toSidebarItemsGeneratorDoc;
|
|
187
|
-
function toSidebarItemsGeneratorVersion(version) {
|
|
188
|
-
return lodash_1.pick(version, ['versionName', 'contentPath']);
|
|
189
|
-
}
|
|
190
|
-
exports.toSidebarItemsGeneratorVersion = toSidebarItemsGeneratorVersion;
|
|
191
|
-
// Handle the generation of autogenerated sidebar items
|
|
192
|
-
async function processSidebar({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebar, docs, version, }) {
|
|
193
|
-
// Just a minor lazy transformation optimization
|
|
194
|
-
const getSidebarItemsGeneratorDocsAndVersion = lodash_1.memoize(() => ({
|
|
195
|
-
docs: docs.map(toSidebarItemsGeneratorDoc),
|
|
196
|
-
version: toSidebarItemsGeneratorVersion(version),
|
|
197
|
-
}));
|
|
198
|
-
async function processRecursive(item) {
|
|
199
|
-
if (item.type === 'category') {
|
|
200
|
-
return [
|
|
201
|
-
{
|
|
202
|
-
...item,
|
|
203
|
-
items: (await Promise.all(item.items.map(processRecursive))).flat(),
|
|
204
|
-
},
|
|
205
|
-
];
|
|
206
|
-
}
|
|
207
|
-
if (item.type === 'autogenerated') {
|
|
208
|
-
return sidebarItemsGenerator({
|
|
209
|
-
item,
|
|
210
|
-
numberPrefixParser,
|
|
211
|
-
defaultSidebarItemsGenerator: sidebarItemsGenerator_1.DefaultSidebarItemsGenerator,
|
|
212
|
-
...getSidebarItemsGeneratorDocsAndVersion(),
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
return [item];
|
|
216
|
-
}
|
|
217
|
-
return (await Promise.all(unprocessedSidebar.map(processRecursive))).flat();
|
|
218
|
-
}
|
|
219
|
-
exports.processSidebar = processSidebar;
|
|
220
|
-
async function processSidebars({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebars, docs, version, }) {
|
|
221
|
-
return combine_promises_1.default(lodash_1.mapValues(unprocessedSidebars, (unprocessedSidebar) => processSidebar({
|
|
222
|
-
sidebarItemsGenerator,
|
|
223
|
-
numberPrefixParser,
|
|
224
|
-
unprocessedSidebar,
|
|
225
|
-
docs,
|
|
226
|
-
version,
|
|
227
|
-
})));
|
|
228
|
-
}
|
|
229
|
-
exports.processSidebars = processSidebars;
|
|
230
|
-
function collectSidebarItemsOfType(type, sidebar) {
|
|
231
|
-
function collectRecursive(item) {
|
|
232
|
-
const currentItemsCollected = item.type === type ? [item] : [];
|
|
233
|
-
const childItemsCollected = item.type === 'category' ? lodash_1.flatten(item.items.map(collectRecursive)) : [];
|
|
234
|
-
return [...currentItemsCollected, ...childItemsCollected];
|
|
235
|
-
}
|
|
236
|
-
return lodash_1.flatten(sidebar.map(collectRecursive));
|
|
237
|
-
}
|
|
238
|
-
function collectSidebarDocItems(sidebar) {
|
|
239
|
-
return collectSidebarItemsOfType('doc', sidebar);
|
|
240
|
-
}
|
|
241
|
-
exports.collectSidebarDocItems = collectSidebarDocItems;
|
|
242
|
-
function collectSidebarCategories(sidebar) {
|
|
243
|
-
return collectSidebarItemsOfType('category', sidebar);
|
|
244
|
-
}
|
|
245
|
-
exports.collectSidebarCategories = collectSidebarCategories;
|
|
246
|
-
function collectSidebarLinks(sidebar) {
|
|
247
|
-
return collectSidebarItemsOfType('link', sidebar);
|
|
248
|
-
}
|
|
249
|
-
exports.collectSidebarLinks = collectSidebarLinks;
|
|
250
|
-
function transformSidebarItems(sidebar, updateFn) {
|
|
251
|
-
function transformRecursive(item) {
|
|
252
|
-
if (item.type === 'category') {
|
|
253
|
-
return updateFn({
|
|
254
|
-
...item,
|
|
255
|
-
items: item.items.map(transformRecursive),
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
return updateFn(item);
|
|
259
|
-
}
|
|
260
|
-
return sidebar.map(transformRecursive);
|
|
261
|
-
}
|
|
262
|
-
exports.transformSidebarItems = transformSidebarItems;
|
|
263
|
-
function collectSidebarsDocIds(sidebars) {
|
|
264
|
-
return lodash_1.mapValues(sidebars, (sidebar) => {
|
|
265
|
-
return collectSidebarDocItems(sidebar).map((docItem) => docItem.id);
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
exports.collectSidebarsDocIds = collectSidebarsDocIds;
|
|
269
|
-
function createSidebarsUtils(sidebars) {
|
|
270
|
-
const sidebarNameToDocIds = collectSidebarsDocIds(sidebars);
|
|
271
|
-
function getFirstDocIdOfFirstSidebar() {
|
|
272
|
-
var _a;
|
|
273
|
-
return (_a = Object.values(sidebarNameToDocIds)[0]) === null || _a === void 0 ? void 0 : _a[0];
|
|
274
|
-
}
|
|
275
|
-
function getSidebarNameByDocId(docId) {
|
|
276
|
-
// TODO lookup speed can be optimized
|
|
277
|
-
const entry = Object.entries(sidebarNameToDocIds).find(([_sidebarName, docIds]) => docIds.includes(docId));
|
|
278
|
-
return entry === null || entry === void 0 ? void 0 : entry[0];
|
|
279
|
-
}
|
|
280
|
-
function getDocNavigation(docId) {
|
|
281
|
-
const sidebarName = getSidebarNameByDocId(docId);
|
|
282
|
-
if (sidebarName) {
|
|
283
|
-
const docIds = sidebarNameToDocIds[sidebarName];
|
|
284
|
-
const currentIndex = docIds.indexOf(docId);
|
|
285
|
-
const { previous, next } = utils_1.getElementsAround(docIds, currentIndex);
|
|
286
|
-
return {
|
|
287
|
-
sidebarName,
|
|
288
|
-
previousId: previous,
|
|
289
|
-
nextId: next,
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
return {
|
|
294
|
-
sidebarName: undefined,
|
|
295
|
-
previousId: undefined,
|
|
296
|
-
nextId: undefined,
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
function checkSidebarsDocIds(validDocIds, sidebarFilePath) {
|
|
301
|
-
const allSidebarDocIds = lodash_1.flatten(Object.values(sidebarNameToDocIds));
|
|
302
|
-
const invalidSidebarDocIds = lodash_1.difference(allSidebarDocIds, validDocIds);
|
|
303
|
-
if (invalidSidebarDocIds.length > 0) {
|
|
304
|
-
throw new Error(`Invalid sidebar file at "${utils_1.toMessageRelativeFilePath(sidebarFilePath)}".
|
|
305
|
-
These sidebar document ids do not exist:
|
|
306
|
-
- ${invalidSidebarDocIds.sort().join('\n- ')}
|
|
307
|
-
|
|
308
|
-
Available document ids are:
|
|
309
|
-
- ${validDocIds.sort().join('\n- ')}`);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
return {
|
|
313
|
-
getFirstDocIdOfFirstSidebar,
|
|
314
|
-
getSidebarNameByDocId,
|
|
315
|
-
getDocNavigation,
|
|
316
|
-
checkSidebarsDocIds,
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
exports.createSidebarsUtils = createSidebarsUtils;
|