@docusaurus/plugin-content-docs 2.0.0-beta.2 → 2.0.0-beta.22
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/categoryGeneratedIndex.d.ts +12 -0
- package/lib/categoryGeneratedIndex.js +37 -0
- package/lib/cli.d.ts +3 -2
- package/lib/cli.js +57 -72
- package/lib/client/docsClientUtils.d.ts +9 -28
- package/lib/client/docsClientUtils.js +34 -43
- package/lib/client/index.d.ts +81 -0
- package/lib/client/index.js +67 -0
- package/lib/constants.d.ts +4 -0
- package/lib/constants.js +4 -1
- package/lib/docs.d.ts +34 -7
- package/lib/docs.js +202 -79
- package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
- package/lib/{docFrontMatter.js → frontMatter.js} +23 -4
- package/lib/globalData.d.ts +3 -3
- package/lib/globalData.js +35 -6
- package/lib/index.d.ts +3 -3
- package/lib/index.js +124 -154
- package/lib/lastUpdate.d.ts +4 -6
- package/lib/lastUpdate.js +22 -26
- package/lib/markdown/index.d.ts +3 -6
- package/lib/markdown/index.js +3 -3
- package/lib/markdown/linkify.d.ts +1 -1
- package/lib/markdown/linkify.js +6 -3
- package/lib/numberPrefix.d.ts +1 -1
- package/lib/numberPrefix.js +16 -21
- package/lib/options.d.ts +3 -5
- package/lib/options.js +56 -23
- package/lib/props.d.ts +7 -2
- package/lib/props.js +84 -14
- package/lib/routes.d.ts +29 -0
- package/lib/routes.js +96 -0
- package/lib/server-export.d.ts +9 -0
- package/lib/server-export.js +22 -0
- package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +1 -6
- package/lib/sidebars/generator.js +209 -0
- package/lib/sidebars/index.d.ts +13 -0
- package/lib/sidebars/index.js +92 -0
- package/lib/sidebars/normalization.d.ts +13 -0
- package/lib/sidebars/normalization.js +59 -0
- package/lib/sidebars/postProcessor.d.ts +11 -0
- package/lib/sidebars/postProcessor.js +81 -0
- package/lib/sidebars/processor.d.ts +10 -0
- package/lib/sidebars/processor.js +79 -0
- package/lib/sidebars/types.d.ts +183 -0
- package/lib/{docFrontMatter.d.ts → sidebars/types.js} +2 -2
- package/lib/sidebars/utils.d.ts +55 -0
- package/lib/sidebars/utils.js +259 -0
- package/lib/sidebars/validation.d.ts +11 -0
- package/lib/sidebars/validation.js +143 -0
- package/lib/slug.d.ts +5 -4
- package/lib/slug.js +28 -18
- package/{src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js → lib/tags.d.ts} +3 -14
- package/lib/tags.js +21 -0
- package/lib/translations.d.ts +3 -3
- package/lib/translations.js +100 -93
- package/lib/types.d.ts +13 -185
- package/lib/versions/files.d.ts +50 -0
- package/lib/versions/files.js +141 -0
- package/lib/versions/index.d.ts +36 -0
- package/lib/versions/index.js +154 -0
- package/lib/versions/validation.d.ts +17 -0
- package/lib/versions/validation.js +71 -0
- package/package.json +44 -30
- package/src/categoryGeneratedIndex.ts +60 -0
- package/src/cli.ts +88 -114
- package/src/client/docsClientUtils.ts +44 -71
- package/src/client/index.ts +158 -0
- package/src/constants.ts +4 -2
- package/src/docs.ts +281 -83
- package/src/frontMatter.ts +63 -0
- package/src/globalData.ts +57 -7
- package/src/index.ts +183 -224
- package/src/lastUpdate.ts +27 -38
- package/src/markdown/index.ts +10 -16
- package/src/markdown/linkify.ts +7 -4
- package/src/numberPrefix.ts +19 -26
- package/src/options.ts +61 -29
- package/src/plugin-content-docs.d.ts +569 -93
- package/src/props.ts +121 -21
- package/src/routes.ts +159 -0
- package/src/server-export.ts +22 -0
- package/src/sidebars/README.md +10 -0
- package/src/sidebars/generator.ts +292 -0
- package/src/sidebars/index.ts +118 -0
- package/src/sidebars/normalization.ts +91 -0
- package/src/sidebars/postProcessor.ts +112 -0
- package/src/sidebars/processor.ts +123 -0
- package/src/sidebars/types.ts +280 -0
- package/src/sidebars/utils.ts +393 -0
- package/src/sidebars/validation.ts +179 -0
- package/src/slug.ts +41 -22
- package/src/tags.ts +20 -0
- package/src/translations.ts +155 -124
- package/src/types.ts +17 -250
- package/src/versions/files.ts +216 -0
- package/src/versions/index.ts +246 -0
- package/src/versions/validation.ts +115 -0
- package/lib/.tsbuildinfo +0 -1
- package/lib/sidebarItemsGenerator.js +0 -211
- package/lib/sidebars.d.ts +0 -43
- package/lib/sidebars.js +0 -320
- package/lib/theme/hooks/useDocs.d.ts +0 -20
- package/lib/theme/hooks/useDocs.js +0 -72
- package/lib/versions.d.ts +0 -16
- package/lib/versions.js +0 -319
- package/src/__tests__/__fixtures__/bad-id-site/docs/invalid-id.md +0 -5
- package/src/__tests__/__fixtures__/bad-slug-on-doc-home-site/docs/docWithSlug.md +0 -5
- package/src/__tests__/__fixtures__/empty-site/docusaurus.config.js +0 -16
- package/src/__tests__/__fixtures__/empty-site/sidebars.json +0 -1
- package/src/__tests__/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -34
- package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-category.js +0 -44
- package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -20
- package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed.json +0 -21
- package/src/__tests__/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -10
- package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-link.json +0 -11
- package/src/__tests__/__fixtures__/sidebars/sidebars-unknown-type.json +0 -14
- package/src/__tests__/__fixtures__/sidebars/sidebars-wrong-field.json +0 -20
- package/src/__tests__/__fixtures__/sidebars/sidebars.json +0 -20
- package/src/__tests__/__fixtures__/simple-site/docs/foo/bar.md +0 -69
- package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +0 -70
- package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
- package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -53
- package/src/__tests__/__fixtures__/simple-site/docs/ipsum.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/lorem.md +0 -6
- package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/absoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/relativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/resolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docs/slugs/tryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/simple-site/docusaurus.config.js +0 -14
- package/src/__tests__/__fixtures__/simple-site/sidebars.json +0 -23
- package/src/__tests__/__fixtures__/simple-site/wrong-sidebars.json +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/0-getting-started.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/1-installation.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/00_api-overview.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/0 --- Client API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/1 --- Server API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/0. Plugin API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/1. Theme API.md +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/_category_.yml +0 -1
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/03_api-end.md +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/_category_.json +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/0-guide2.5.md +0 -8
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/02-guide2.md +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/_category_.json +0 -3
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/a-guide4.md +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/b-guide5.md +0 -7
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/guide3.md +0 -8
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/z-guide1.md +0 -8
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars.js +0 -23
- package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js +0 -16
- package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +0 -7
- package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-2.md +0 -8
- package/src/__tests__/__fixtures__/site-with-doc-label/docusaurus.config.js +0 -14
- package/src/__tests__/__fixtures__/site-with-doc-label/sidebars.json +0 -14
- package/src/__tests__/__fixtures__/versioned-site/community/team.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/community_sidebars.json +0 -3
- package/src/__tests__/__fixtures__/versioned-site/community_versioned_docs/version-1.0.0/team.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/community_versioned_sidebars/version-1.0.0-sidebars.json +0 -3
- package/src/__tests__/__fixtures__/versioned-site/community_versions.json +0 -1
- package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +0 -4
- package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/absoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/relativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/resolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docs/slugs/tryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js +0 -18
- package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/sidebars.json +0 -10
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/bar.md +0 -4
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/baz.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/foo/bar.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +0 -1
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootAbsoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootRelativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootResolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootTryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/absoluteSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/relativeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/resolvedSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/tryToEscapeSlug.md +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.0-sidebars.json +0 -11
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +0 -10
- package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-withSlugs-sidebars.json +0 -5
- package/src/__tests__/__fixtures__/versioned-site/versions.json +0 -5
- package/src/__tests__/__snapshots__/cli.test.ts.snap +0 -90
- package/src/__tests__/__snapshots__/index.test.ts.snap +0 -1916
- package/src/__tests__/__snapshots__/sidebars.test.ts.snap +0 -218
- package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -487
- package/src/__tests__/cli.test.ts +0 -333
- package/src/__tests__/docFrontMatter.test.ts +0 -244
- package/src/__tests__/docs.test.ts +0 -878
- package/src/__tests__/index.test.ts +0 -1871
- package/src/__tests__/lastUpdate.test.ts +0 -69
- package/src/__tests__/numberPrefix.test.ts +0 -199
- package/src/__tests__/options.test.ts +0 -231
- package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
- package/src/__tests__/sidebars.test.ts +0 -639
- package/src/__tests__/slug.test.ts +0 -109
- package/src/__tests__/translations.test.ts +0 -159
- package/src/__tests__/versions.test.ts +0 -741
- package/src/client/__tests__/docsClientUtils.test.ts +0 -371
- package/src/docFrontMatter.ts +0 -41
- package/src/markdown/__tests__/__fixtures__/docs/doc-localized.md +0 -1
- package/src/markdown/__tests__/__fixtures__/docs/doc1.md +0 -13
- package/src/markdown/__tests__/__fixtures__/docs/doc2.md +0 -12
- package/src/markdown/__tests__/__fixtures__/docs/doc4.md +0 -19
- package/src/markdown/__tests__/__fixtures__/docs/doc5.md +0 -6
- package/src/markdown/__tests__/__fixtures__/docs/subdir/doc3.md +0 -3
- package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/doc2.md +0 -7
- package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/subdir/doc1.md +0 -3
- package/src/markdown/__tests__/__snapshots__/linkify.test.ts.snap +0 -82
- package/src/markdown/__tests__/linkify.test.ts +0 -190
- package/src/sidebarItemsGenerator.ts +0 -307
- package/src/sidebars.ts +0 -522
- package/src/theme/hooks/useDocs.ts +0 -99
- package/src/versions.ts +0 -572
- package/tsconfig.json +0 -9
- package/types.d.ts +0 -13
|
@@ -0,0 +1,209 @@
|
|
|
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 path_1 = tslib_1.__importDefault(require("path"));
|
|
12
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
13
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
14
|
+
const utils_1 = require("@docusaurus/utils");
|
|
15
|
+
const docs_1 = require("../docs");
|
|
16
|
+
const BreadcrumbSeparator = '/';
|
|
17
|
+
// Just an alias to the make code more explicit
|
|
18
|
+
function getLocalDocId(docId) {
|
|
19
|
+
return lodash_1.default.last(docId.split('/'));
|
|
20
|
+
}
|
|
21
|
+
exports.CategoryMetadataFilenameBase = '_category_';
|
|
22
|
+
exports.CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
|
|
23
|
+
// Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
|
24
|
+
const DefaultSidebarItemsGenerator = ({ numberPrefixParser, isCategoryIndex, docs: allDocs, item: { dirName: autogenDir }, categoriesMetadata, }) => {
|
|
25
|
+
const docsById = (0, docs_1.createDocsByIdIndex)(allDocs);
|
|
26
|
+
const findDoc = (docId) => docsById[docId];
|
|
27
|
+
const getDoc = (docId) => {
|
|
28
|
+
const doc = findDoc(docId);
|
|
29
|
+
if (!doc) {
|
|
30
|
+
throw new Error(`Can't find any doc with ID ${docId}.
|
|
31
|
+
Available doc IDs:
|
|
32
|
+
- ${Object.keys(docsById).join('\n- ')}`);
|
|
33
|
+
}
|
|
34
|
+
return doc;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Step 1. Extract the docs that are in the autogen dir.
|
|
38
|
+
*/
|
|
39
|
+
function getAutogenDocs() {
|
|
40
|
+
function isInAutogeneratedDir(doc) {
|
|
41
|
+
return (
|
|
42
|
+
// Doc at the root of the autogenerated sidebar dir
|
|
43
|
+
doc.sourceDirName === autogenDir ||
|
|
44
|
+
// Autogen dir is . and doc is in subfolder
|
|
45
|
+
autogenDir === '.' ||
|
|
46
|
+
// Autogen dir is not . and doc is in subfolder
|
|
47
|
+
// "api/myDoc" startsWith "api/" (note "api2/myDoc" is not included)
|
|
48
|
+
doc.sourceDirName.startsWith((0, utils_1.addTrailingSlash)(autogenDir)));
|
|
49
|
+
}
|
|
50
|
+
const docs = allDocs.filter(isInAutogeneratedDir);
|
|
51
|
+
if (docs.length === 0) {
|
|
52
|
+
logger_1.default.warn `No docs found in path=${autogenDir}: can't auto-generate a sidebar.`;
|
|
53
|
+
}
|
|
54
|
+
return docs;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Step 2. Turn the linear file list into a tree structure.
|
|
58
|
+
*/
|
|
59
|
+
function treeify(docs) {
|
|
60
|
+
// Get the category breadcrumb of a doc (relative to the dir of the
|
|
61
|
+
// autogenerated sidebar item)
|
|
62
|
+
// autogenDir=a/b and docDir=a/b/c/d => returns [c, d]
|
|
63
|
+
// autogenDir=a/b and docDir=a/b => returns []
|
|
64
|
+
// TODO: try to use path.relative()
|
|
65
|
+
function getRelativeBreadcrumb(doc) {
|
|
66
|
+
return autogenDir === doc.sourceDirName
|
|
67
|
+
? []
|
|
68
|
+
: doc.sourceDirName
|
|
69
|
+
.replace((0, utils_1.addTrailingSlash)(autogenDir), '')
|
|
70
|
+
.split(BreadcrumbSeparator);
|
|
71
|
+
}
|
|
72
|
+
const treeRoot = {};
|
|
73
|
+
docs.forEach((doc) => {
|
|
74
|
+
const breadcrumb = getRelativeBreadcrumb(doc);
|
|
75
|
+
// We walk down the file's path to generate the fs structure
|
|
76
|
+
let currentDir = treeRoot;
|
|
77
|
+
breadcrumb.forEach((dir) => {
|
|
78
|
+
if (typeof currentDir[dir] === 'undefined') {
|
|
79
|
+
currentDir[dir] = {}; // Create new folder.
|
|
80
|
+
}
|
|
81
|
+
currentDir = currentDir[dir]; // Go into the subdirectory.
|
|
82
|
+
});
|
|
83
|
+
// We've walked through the path. Register the file in this directory.
|
|
84
|
+
currentDir[path_1.default.basename(doc.source)] = doc.id;
|
|
85
|
+
});
|
|
86
|
+
return treeRoot;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Step 3. Recursively transform the tree-like structure to sidebar items.
|
|
90
|
+
* (From a record to an array of items, akin to normalizing shorthand)
|
|
91
|
+
*/
|
|
92
|
+
function generateSidebar(fsModel) {
|
|
93
|
+
function createDocItem(id, fullPath, fileName) {
|
|
94
|
+
const { sidebarPosition: position, frontMatter: { sidebar_label: label, sidebar_class_name: className }, } = getDoc(id);
|
|
95
|
+
return {
|
|
96
|
+
type: 'doc',
|
|
97
|
+
id,
|
|
98
|
+
position,
|
|
99
|
+
source: fileName,
|
|
100
|
+
// We don't want these fields to magically appear in the generated
|
|
101
|
+
// sidebar
|
|
102
|
+
...(label !== undefined && { label }),
|
|
103
|
+
...(className !== undefined && { className }),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function createCategoryItem(dir, fullPath, folderName) {
|
|
107
|
+
const categoryMetadata = categoriesMetadata[path_1.default.posix.join(autogenDir, fullPath)];
|
|
108
|
+
const allItems = Object.entries(dir).map(([key, content]) => dirToItem(content, key, `${fullPath}/${key}`));
|
|
109
|
+
// Try to match a doc inside the category folder,
|
|
110
|
+
// using the "local id" (myDoc) or "qualified id" (dirName/myDoc)
|
|
111
|
+
function findDocByLocalId(localId) {
|
|
112
|
+
return allItems.find((item) => item.type === 'doc' && getLocalDocId(item.id) === localId);
|
|
113
|
+
}
|
|
114
|
+
function findConventionalCategoryDocLink() {
|
|
115
|
+
return allItems.find((item) => {
|
|
116
|
+
if (item.type !== 'doc') {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
const doc = getDoc(item.id);
|
|
120
|
+
return isCategoryIndex((0, docs_1.toCategoryIndexMatcherParam)(doc));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
// In addition to the ID, this function also retrieves metadata of the
|
|
124
|
+
// linked doc that could be used as fallback values for category metadata
|
|
125
|
+
function getCategoryLinkedDocMetadata() {
|
|
126
|
+
const link = categoryMetadata?.link;
|
|
127
|
+
if (link !== undefined && link?.type !== 'doc') {
|
|
128
|
+
// If a link is explicitly specified, we won't apply conventions
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
const id = link
|
|
132
|
+
? findDocByLocalId(link.id)?.id ?? getDoc(link.id).id
|
|
133
|
+
: findConventionalCategoryDocLink()?.id;
|
|
134
|
+
if (!id) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
const doc = getDoc(id);
|
|
138
|
+
return {
|
|
139
|
+
id,
|
|
140
|
+
position: doc.sidebarPosition,
|
|
141
|
+
label: doc.frontMatter.sidebar_label ?? doc.title,
|
|
142
|
+
customProps: doc.frontMatter.sidebar_custom_props,
|
|
143
|
+
className: doc.frontMatter.sidebar_class_name,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
const categoryLinkedDoc = getCategoryLinkedDocMetadata();
|
|
147
|
+
const link = categoryLinkedDoc
|
|
148
|
+
? {
|
|
149
|
+
type: 'doc',
|
|
150
|
+
id: categoryLinkedDoc.id, // We "remap" a potentially "local id" to a "qualified id"
|
|
151
|
+
}
|
|
152
|
+
: categoryMetadata?.link;
|
|
153
|
+
// If a doc is linked, remove it from the category subItems
|
|
154
|
+
const items = allItems.filter((item) => !(item.type === 'doc' && item.id === categoryLinkedDoc?.id));
|
|
155
|
+
const className = categoryMetadata?.className ?? categoryLinkedDoc?.className;
|
|
156
|
+
const customProps = categoryMetadata?.customProps ?? categoryLinkedDoc?.customProps;
|
|
157
|
+
const { filename, numberPrefix } = numberPrefixParser(folderName);
|
|
158
|
+
return {
|
|
159
|
+
type: 'category',
|
|
160
|
+
label: categoryMetadata?.label ?? categoryLinkedDoc?.label ?? filename,
|
|
161
|
+
collapsible: categoryMetadata?.collapsible,
|
|
162
|
+
collapsed: categoryMetadata?.collapsed,
|
|
163
|
+
position: categoryMetadata?.position ??
|
|
164
|
+
categoryLinkedDoc?.position ??
|
|
165
|
+
numberPrefix,
|
|
166
|
+
source: folderName,
|
|
167
|
+
...(customProps !== undefined && { customProps }),
|
|
168
|
+
...(className !== undefined && { className }),
|
|
169
|
+
items,
|
|
170
|
+
...(link && { link }),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function dirToItem(dir, // The directory item to be transformed.
|
|
174
|
+
itemKey, // File/folder name; for categories, it's used to generate the next `relativePath`.
|
|
175
|
+
fullPath) {
|
|
176
|
+
return typeof dir === 'object'
|
|
177
|
+
? createCategoryItem(dir, fullPath, itemKey)
|
|
178
|
+
: createDocItem(dir, fullPath, itemKey);
|
|
179
|
+
}
|
|
180
|
+
return Object.entries(fsModel).map(([key, content]) => dirToItem(content, key, key));
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Step 4. Recursively sort the categories/docs + remove the "position"
|
|
184
|
+
* attribute from final output. Note: the "position" is only used to sort
|
|
185
|
+
* "inside" a sidebar slice. It is not used to sort across multiple
|
|
186
|
+
* consecutive sidebar slices (i.e. a whole category composed of multiple
|
|
187
|
+
* autogenerated items)
|
|
188
|
+
*/
|
|
189
|
+
function sortItems(sidebarItems) {
|
|
190
|
+
const processedSidebarItems = sidebarItems.map((item) => {
|
|
191
|
+
if (item.type === 'category') {
|
|
192
|
+
return { ...item, items: sortItems(item.items) };
|
|
193
|
+
}
|
|
194
|
+
return item;
|
|
195
|
+
});
|
|
196
|
+
const sortedSidebarItems = lodash_1.default.sortBy(processedSidebarItems, [
|
|
197
|
+
'position',
|
|
198
|
+
'source',
|
|
199
|
+
]);
|
|
200
|
+
return sortedSidebarItems.map(({ position, source, ...item }) => item);
|
|
201
|
+
}
|
|
202
|
+
// TODO: the whole code is designed for pipeline operator
|
|
203
|
+
const docs = getAutogenDocs();
|
|
204
|
+
const fsModel = treeify(docs);
|
|
205
|
+
const sidebarWithPosition = generateSidebar(fsModel);
|
|
206
|
+
const sortedSidebar = sortItems(sidebarWithPosition);
|
|
207
|
+
return sortedSidebar;
|
|
208
|
+
};
|
|
209
|
+
exports.DefaultSidebarItemsGenerator = DefaultSidebarItemsGenerator;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { PluginOptions } from '@docusaurus/plugin-content-docs';
|
|
8
|
+
import type { SidebarsConfig, Sidebars, SidebarProcessorParams } from './types';
|
|
9
|
+
export declare const DefaultSidebars: SidebarsConfig;
|
|
10
|
+
export declare const DisabledSidebars: SidebarsConfig;
|
|
11
|
+
export declare function resolveSidebarPathOption(siteDir: string, sidebarPathOption: PluginOptions['sidebarPath']): PluginOptions['sidebarPath'];
|
|
12
|
+
export declare function loadSidebarsFileUnsafe(sidebarFilePath: string | false | undefined): Promise<SidebarsConfig>;
|
|
13
|
+
export declare function loadSidebars(sidebarFilePath: string | false | undefined, options: SidebarProcessorParams): Promise<Sidebars>;
|
|
@@ -0,0 +1,92 @@
|
|
|
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.loadSidebars = exports.loadSidebarsFileUnsafe = exports.resolveSidebarPathOption = exports.DisabledSidebars = exports.DefaultSidebars = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
12
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
13
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
14
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
15
|
+
const utils_1 = require("@docusaurus/utils");
|
|
16
|
+
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
|
|
17
|
+
const combine_promises_1 = tslib_1.__importDefault(require("combine-promises"));
|
|
18
|
+
const import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
|
|
19
|
+
const validation_1 = require("./validation");
|
|
20
|
+
const normalization_1 = require("./normalization");
|
|
21
|
+
const processor_1 = require("./processor");
|
|
22
|
+
const postProcessor_1 = require("./postProcessor");
|
|
23
|
+
exports.DefaultSidebars = {
|
|
24
|
+
defaultSidebar: [
|
|
25
|
+
{
|
|
26
|
+
type: 'autogenerated',
|
|
27
|
+
dirName: '.',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
exports.DisabledSidebars = {};
|
|
32
|
+
// If a path is provided, make it absolute
|
|
33
|
+
function resolveSidebarPathOption(siteDir, sidebarPathOption) {
|
|
34
|
+
return sidebarPathOption
|
|
35
|
+
? path_1.default.resolve(siteDir, sidebarPathOption)
|
|
36
|
+
: sidebarPathOption;
|
|
37
|
+
}
|
|
38
|
+
exports.resolveSidebarPathOption = resolveSidebarPathOption;
|
|
39
|
+
async function readCategoriesMetadata(contentPath) {
|
|
40
|
+
const categoryFiles = await (0, utils_1.Globby)('**/_category_.{json,yml,yaml}', {
|
|
41
|
+
cwd: contentPath,
|
|
42
|
+
});
|
|
43
|
+
const categoryToFile = lodash_1.default.groupBy(categoryFiles, path_1.default.dirname);
|
|
44
|
+
return (0, combine_promises_1.default)(lodash_1.default.mapValues(categoryToFile, async (files, folder) => {
|
|
45
|
+
const filePath = files[0];
|
|
46
|
+
if (files.length > 1) {
|
|
47
|
+
logger_1.default.warn `There are more than one category metadata files for path=${folder}: ${files.join(', ')}. The behavior is undetermined.`;
|
|
48
|
+
}
|
|
49
|
+
const content = await fs_extra_1.default.readFile(path_1.default.join(contentPath, filePath), 'utf-8');
|
|
50
|
+
try {
|
|
51
|
+
return (0, validation_1.validateCategoryMetadataFile)(js_yaml_1.default.load(content));
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
logger_1.default.error `The docs sidebar category metadata file path=${filePath} looks invalid!`;
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
async function loadSidebarsFileUnsafe(sidebarFilePath) {
|
|
60
|
+
// false => no sidebars
|
|
61
|
+
if (sidebarFilePath === false) {
|
|
62
|
+
return exports.DisabledSidebars;
|
|
63
|
+
}
|
|
64
|
+
// undefined => defaults to autogenerated sidebars
|
|
65
|
+
if (typeof sidebarFilePath === 'undefined') {
|
|
66
|
+
return exports.DefaultSidebars;
|
|
67
|
+
}
|
|
68
|
+
// Non-existent sidebars file: no sidebars
|
|
69
|
+
// Note: this edge case can happen on versioned docs, not current version
|
|
70
|
+
// We avoid creating empty versioned sidebars file with the CLI
|
|
71
|
+
if (!(await fs_extra_1.default.pathExists(sidebarFilePath))) {
|
|
72
|
+
return exports.DisabledSidebars;
|
|
73
|
+
}
|
|
74
|
+
// We don't want sidebars to be cached because of hot reloading.
|
|
75
|
+
return (0, import_fresh_1.default)(sidebarFilePath);
|
|
76
|
+
}
|
|
77
|
+
exports.loadSidebarsFileUnsafe = loadSidebarsFileUnsafe;
|
|
78
|
+
async function loadSidebars(sidebarFilePath, options) {
|
|
79
|
+
try {
|
|
80
|
+
const sidebarsConfig = await loadSidebarsFileUnsafe(sidebarFilePath);
|
|
81
|
+
const normalizedSidebars = (0, normalization_1.normalizeSidebars)(sidebarsConfig);
|
|
82
|
+
(0, validation_1.validateSidebars)(normalizedSidebars);
|
|
83
|
+
const categoriesMetadata = await readCategoriesMetadata(options.version.contentPath);
|
|
84
|
+
const processedSidebars = await (0, processor_1.processSidebars)(normalizedSidebars, categoriesMetadata, options);
|
|
85
|
+
return (0, postProcessor_1.postProcessSidebars)(processedSidebars, options);
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
logger_1.default.error `Sidebars file at path=${sidebarFilePath} failed to be loaded.`;
|
|
89
|
+
throw err;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.loadSidebars = loadSidebars;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { NormalizedSidebarItem, NormalizedSidebars, SidebarItemConfig, SidebarsConfig } from './types';
|
|
8
|
+
/**
|
|
9
|
+
* Normalizes recursively item and all its children. Ensures that at the end
|
|
10
|
+
* each item will be an object with the corresponding type.
|
|
11
|
+
*/
|
|
12
|
+
export declare function normalizeItem(item: SidebarItemConfig): NormalizedSidebarItem[];
|
|
13
|
+
export declare function normalizeSidebars(sidebars: SidebarsConfig): NormalizedSidebars;
|
|
@@ -0,0 +1,59 @@
|
|
|
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.normalizeSidebars = exports.normalizeItem = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
12
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
function normalizeCategoriesShorthand(sidebar) {
|
|
15
|
+
return Object.entries(sidebar).map(([label, items]) => ({
|
|
16
|
+
type: 'category',
|
|
17
|
+
label,
|
|
18
|
+
items,
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Normalizes recursively item and all its children. Ensures that at the end
|
|
23
|
+
* each item will be an object with the corresponding type.
|
|
24
|
+
*/
|
|
25
|
+
function normalizeItem(item) {
|
|
26
|
+
if (typeof item === 'string') {
|
|
27
|
+
return [{ type: 'doc', id: item }];
|
|
28
|
+
}
|
|
29
|
+
if ((0, utils_1.isCategoriesShorthand)(item)) {
|
|
30
|
+
// This will never throw anyways
|
|
31
|
+
return normalizeSidebar(item, 'sidebar items slice');
|
|
32
|
+
}
|
|
33
|
+
if ((item.type === 'doc' || item.type === 'ref') &&
|
|
34
|
+
typeof item.label === 'string') {
|
|
35
|
+
return [{ ...item, translatable: true }];
|
|
36
|
+
}
|
|
37
|
+
if (item.type === 'category') {
|
|
38
|
+
const normalizedCategory = {
|
|
39
|
+
...item,
|
|
40
|
+
items: normalizeSidebar(item.items, logger_1.default.interpolate `code=${'items'} of the category name=${item.label}`),
|
|
41
|
+
};
|
|
42
|
+
return [normalizedCategory];
|
|
43
|
+
}
|
|
44
|
+
return [item];
|
|
45
|
+
}
|
|
46
|
+
exports.normalizeItem = normalizeItem;
|
|
47
|
+
function normalizeSidebar(sidebar, place) {
|
|
48
|
+
if (!Array.isArray(sidebar) && !(0, utils_1.isCategoriesShorthand)(sidebar)) {
|
|
49
|
+
throw new Error(logger_1.default.interpolate `Invalid sidebar items collection code=${JSON.stringify(sidebar)} in ${place}: it must either be an array of sidebar items or a shorthand notation (which doesn't contain a code=${'type'} property). See url=${'https://docusaurus.io/docs/sidebar/items'} for all valid syntaxes.`);
|
|
50
|
+
}
|
|
51
|
+
const normalizedSidebar = Array.isArray(sidebar)
|
|
52
|
+
? sidebar
|
|
53
|
+
: normalizeCategoriesShorthand(sidebar);
|
|
54
|
+
return normalizedSidebar.flatMap((subItem) => normalizeItem(subItem));
|
|
55
|
+
}
|
|
56
|
+
function normalizeSidebars(sidebars) {
|
|
57
|
+
return lodash_1.default.mapValues(sidebars, (sidebar, id) => normalizeSidebar(sidebar, logger_1.default.interpolate `sidebar name=${id}`));
|
|
58
|
+
}
|
|
59
|
+
exports.normalizeSidebars = normalizeSidebars;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { Sidebars, SidebarProcessorParams, ProcessedSidebars } from './types';
|
|
8
|
+
export declare type SidebarPostProcessorParams = SidebarProcessorParams & {
|
|
9
|
+
draftIds: Set<string>;
|
|
10
|
+
};
|
|
11
|
+
export declare function postProcessSidebars(sidebars: ProcessedSidebars, params: SidebarProcessorParams): Sidebars;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.postProcessSidebars = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
12
|
+
const utils_1 = require("@docusaurus/utils");
|
|
13
|
+
const docs_1 = require("../docs");
|
|
14
|
+
function normalizeCategoryLink(category, params) {
|
|
15
|
+
if (category.link?.type === 'doc' && params.draftIds.has(category.link.id)) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
if (category.link?.type === 'generated-index') {
|
|
19
|
+
// Default slug logic can be improved
|
|
20
|
+
const getDefaultSlug = () => `/category/${params.categoryLabelSlugger.slug(category.label)}`;
|
|
21
|
+
const slug = category.link.slug ?? getDefaultSlug();
|
|
22
|
+
const permalink = (0, utils_1.normalizeUrl)([params.version.path, slug]);
|
|
23
|
+
return {
|
|
24
|
+
...category.link,
|
|
25
|
+
slug,
|
|
26
|
+
permalink,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return category.link;
|
|
30
|
+
}
|
|
31
|
+
function postProcessSidebarItem(item, params) {
|
|
32
|
+
if (item.type === 'category') {
|
|
33
|
+
// Fail-fast if there's actually no subitems, no because all subitems are
|
|
34
|
+
// drafts. This is likely a configuration mistake.
|
|
35
|
+
if (item.items.length === 0 && !item.link) {
|
|
36
|
+
throw new Error(`Sidebar category ${item.label} has neither any subitem nor a link. This makes this item not able to link to anything.`);
|
|
37
|
+
}
|
|
38
|
+
const category = {
|
|
39
|
+
...item,
|
|
40
|
+
collapsed: item.collapsed ?? params.sidebarOptions.sidebarCollapsed,
|
|
41
|
+
collapsible: item.collapsible ?? params.sidebarOptions.sidebarCollapsible,
|
|
42
|
+
link: normalizeCategoryLink(item, params),
|
|
43
|
+
items: item.items
|
|
44
|
+
.map((subItem) => postProcessSidebarItem(subItem, params))
|
|
45
|
+
.filter((v) => Boolean(v)),
|
|
46
|
+
};
|
|
47
|
+
// If the current category doesn't have subitems, we render a normal link
|
|
48
|
+
// instead.
|
|
49
|
+
if (category.items.length === 0) {
|
|
50
|
+
// Doesn't make sense to render an empty generated index page, so we
|
|
51
|
+
// filter the entire category out as well.
|
|
52
|
+
if (!category.link ||
|
|
53
|
+
category.link.type === 'generated-index' ||
|
|
54
|
+
params.draftIds.has(category.link.id)) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
type: 'doc',
|
|
59
|
+
label: category.label,
|
|
60
|
+
id: category.link.id,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
// A non-collapsible category can't be collapsed!
|
|
64
|
+
if (!category.collapsible) {
|
|
65
|
+
category.collapsed = false;
|
|
66
|
+
}
|
|
67
|
+
return category;
|
|
68
|
+
}
|
|
69
|
+
if ((item.type === 'doc' || item.type === 'ref') &&
|
|
70
|
+
params.draftIds.has(item.id)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return item;
|
|
74
|
+
}
|
|
75
|
+
function postProcessSidebars(sidebars, params) {
|
|
76
|
+
const draftIds = new Set(params.drafts.flatMap(docs_1.getDocIds));
|
|
77
|
+
return lodash_1.default.mapValues(sidebars, (sidebar) => sidebar
|
|
78
|
+
.map((item) => postProcessSidebarItem(item, { ...params, draftIds }))
|
|
79
|
+
.filter((v) => Boolean(v)));
|
|
80
|
+
}
|
|
81
|
+
exports.postProcessSidebars = postProcessSidebars;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { NormalizedSidebars, ProcessedSidebars, SidebarProcessorParams, CategoryMetadataFile } from './types';
|
|
8
|
+
export declare function processSidebars(unprocessedSidebars: NormalizedSidebars, categoriesMetadata: {
|
|
9
|
+
[filePath: string]: CategoryMetadataFile;
|
|
10
|
+
}, params: SidebarProcessorParams): Promise<ProcessedSidebars>;
|
|
@@ -0,0 +1,79 @@
|
|
|
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.processSidebars = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
12
|
+
const combine_promises_1 = tslib_1.__importDefault(require("combine-promises"));
|
|
13
|
+
const generator_1 = require("./generator");
|
|
14
|
+
const validation_1 = require("./validation");
|
|
15
|
+
const docs_1 = require("../docs");
|
|
16
|
+
function toSidebarItemsGeneratorDoc(doc) {
|
|
17
|
+
return lodash_1.default.pick(doc, [
|
|
18
|
+
'id',
|
|
19
|
+
'unversionedId',
|
|
20
|
+
'title',
|
|
21
|
+
'frontMatter',
|
|
22
|
+
'source',
|
|
23
|
+
'sourceDirName',
|
|
24
|
+
'sidebarPosition',
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
function toSidebarItemsGeneratorVersion(version) {
|
|
28
|
+
return lodash_1.default.pick(version, ['versionName', 'contentPath']);
|
|
29
|
+
}
|
|
30
|
+
// Handle the generation of autogenerated sidebar items and other
|
|
31
|
+
// post-processing checks
|
|
32
|
+
async function processSidebar(unprocessedSidebar, categoriesMetadata, params) {
|
|
33
|
+
const { sidebarItemsGenerator, numberPrefixParser, docs, version } = params;
|
|
34
|
+
// Just a minor lazy transformation optimization
|
|
35
|
+
const getSidebarItemsGeneratorDocsAndVersion = lodash_1.default.memoize(() => ({
|
|
36
|
+
docs: docs.map(toSidebarItemsGeneratorDoc),
|
|
37
|
+
version: toSidebarItemsGeneratorVersion(version),
|
|
38
|
+
}));
|
|
39
|
+
async function processAutoGeneratedItem(item) {
|
|
40
|
+
const generatedItems = await sidebarItemsGenerator({
|
|
41
|
+
item,
|
|
42
|
+
numberPrefixParser,
|
|
43
|
+
defaultSidebarItemsGenerator: generator_1.DefaultSidebarItemsGenerator,
|
|
44
|
+
isCategoryIndex: docs_1.isCategoryIndex,
|
|
45
|
+
...getSidebarItemsGeneratorDocsAndVersion(),
|
|
46
|
+
categoriesMetadata,
|
|
47
|
+
});
|
|
48
|
+
// Process again... weird but sidebar item generated might generate some
|
|
49
|
+
// auto-generated items?
|
|
50
|
+
// TODO repeatedly process & unwrap autogenerated items until there are no
|
|
51
|
+
// more autogenerated items, or when loop count (e.g. 10) is reached
|
|
52
|
+
return processItems(generatedItems);
|
|
53
|
+
}
|
|
54
|
+
async function processItem(item) {
|
|
55
|
+
if (item.type === 'category') {
|
|
56
|
+
return [
|
|
57
|
+
{
|
|
58
|
+
...item,
|
|
59
|
+
items: (await Promise.all(item.items.map(processItem))).flat(),
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
if (item.type === 'autogenerated') {
|
|
64
|
+
return processAutoGeneratedItem(item);
|
|
65
|
+
}
|
|
66
|
+
return [item];
|
|
67
|
+
}
|
|
68
|
+
async function processItems(items) {
|
|
69
|
+
return (await Promise.all(items.map(processItem))).flat();
|
|
70
|
+
}
|
|
71
|
+
const processedSidebar = await processItems(unprocessedSidebar);
|
|
72
|
+
return processedSidebar;
|
|
73
|
+
}
|
|
74
|
+
async function processSidebars(unprocessedSidebars, categoriesMetadata, params) {
|
|
75
|
+
const processedSidebars = await (0, combine_promises_1.default)(lodash_1.default.mapValues(unprocessedSidebars, (unprocessedSidebar) => processSidebar(unprocessedSidebar, categoriesMetadata, params)));
|
|
76
|
+
(0, validation_1.validateSidebars)(processedSidebars);
|
|
77
|
+
return processedSidebars;
|
|
78
|
+
}
|
|
79
|
+
exports.processSidebars = processSidebars;
|