@docusaurus/plugin-content-docs 2.0.0-beta.15d451942 → 2.0.0-beta.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/lib/categoryGeneratedIndex.d.ts +12 -0
  2. package/lib/categoryGeneratedIndex.js +35 -0
  3. package/lib/cli.d.ts +2 -2
  4. package/lib/cli.js +37 -51
  5. package/lib/client/docsClientUtils.d.ts +6 -26
  6. package/lib/client/docsClientUtils.js +28 -34
  7. package/lib/{theme/hooks/useDocs.d.ts → client/index.d.ts} +4 -3
  8. package/lib/{theme/hooks/useDocs.js → client/index.js} +28 -25
  9. package/lib/docs.d.ts +31 -4
  10. package/lib/docs.js +160 -54
  11. package/{src/__tests__/__fixtures__/simple-site/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
  12. package/lib/{docFrontMatter.js → frontMatter.js} +13 -6
  13. package/lib/globalData.d.ts +2 -2
  14. package/lib/globalData.js +32 -3
  15. package/lib/index.d.ts +4 -3
  16. package/lib/index.js +123 -136
  17. package/lib/lastUpdate.d.ts +4 -6
  18. package/lib/lastUpdate.js +22 -26
  19. package/lib/markdown/index.d.ts +3 -6
  20. package/lib/markdown/index.js +3 -3
  21. package/lib/markdown/linkify.d.ts +1 -1
  22. package/lib/markdown/linkify.js +7 -3
  23. package/lib/numberPrefix.d.ts +1 -1
  24. package/lib/numberPrefix.js +16 -21
  25. package/lib/options.d.ts +3 -5
  26. package/lib/options.js +55 -19
  27. package/lib/props.d.ts +7 -2
  28. package/lib/props.js +70 -14
  29. package/lib/routes.d.ts +28 -0
  30. package/lib/routes.js +110 -0
  31. package/lib/server-export.d.ts +8 -0
  32. package/lib/server-export.js +23 -0
  33. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +1 -6
  34. package/lib/sidebars/generator.js +209 -0
  35. package/lib/sidebars/index.d.ts +13 -0
  36. package/lib/sidebars/index.js +94 -0
  37. package/lib/sidebars/normalization.d.ts +13 -0
  38. package/lib/sidebars/normalization.js +55 -0
  39. package/lib/sidebars/postProcessor.d.ts +8 -0
  40. package/lib/sidebars/postProcessor.js +65 -0
  41. package/lib/sidebars/processor.d.ts +10 -0
  42. package/lib/sidebars/processor.js +79 -0
  43. package/lib/sidebars/types.d.ts +174 -0
  44. package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js → lib/sidebars/types.js} +2 -10
  45. package/lib/sidebars/utils.d.ts +54 -0
  46. package/lib/sidebars/utils.js +255 -0
  47. package/lib/sidebars/validation.d.ts +11 -0
  48. package/lib/sidebars/validation.js +138 -0
  49. package/lib/slug.d.ts +6 -4
  50. package/lib/slug.js +29 -19
  51. package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js → lib/tags.d.ts} +2 -8
  52. package/lib/tags.js +21 -0
  53. package/lib/translations.d.ts +2 -2
  54. package/lib/translations.js +89 -49
  55. package/lib/types.d.ts +60 -130
  56. package/lib/versions.d.ts +29 -4
  57. package/lib/versions.js +134 -97
  58. package/package.json +30 -26
  59. package/src/categoryGeneratedIndex.ts +55 -0
  60. package/src/cli.ts +47 -63
  61. package/src/client/docsClientUtils.ts +38 -73
  62. package/src/{theme/hooks/useDocs.ts → client/index.ts} +16 -11
  63. package/{types.d.ts → src/deps.d.ts} +1 -1
  64. package/src/docs.ts +212 -46
  65. package/src/{docFrontMatter.ts → frontMatter.ts} +21 -26
  66. package/src/globalData.ts +53 -3
  67. package/src/index.ts +168 -178
  68. package/src/lastUpdate.ts +26 -33
  69. package/src/markdown/index.ts +10 -16
  70. package/src/markdown/linkify.ts +6 -2
  71. package/src/numberPrefix.ts +19 -26
  72. package/src/options.ts +60 -32
  73. package/src/plugin-content-docs.d.ts +263 -40
  74. package/src/props.ts +105 -21
  75. package/src/routes.ts +185 -0
  76. package/src/server-export.ts +24 -0
  77. package/src/sidebars/README.md +9 -0
  78. package/src/sidebars/generator.ts +292 -0
  79. package/src/sidebars/index.ts +120 -0
  80. package/src/sidebars/normalization.ts +85 -0
  81. package/src/sidebars/postProcessor.ts +89 -0
  82. package/src/sidebars/processor.ts +120 -0
  83. package/src/sidebars/types.ts +274 -0
  84. package/src/sidebars/utils.ts +388 -0
  85. package/src/sidebars/validation.ts +174 -0
  86. package/src/slug.ts +40 -23
  87. package/src/tags.ts +19 -0
  88. package/src/translations.ts +124 -66
  89. package/src/types.ts +67 -187
  90. package/src/versions.ts +205 -110
  91. package/lib/.tsbuildinfo +0 -4717
  92. package/lib/docFrontMatter.d.ts +0 -21
  93. package/lib/sidebarItemsGenerator.js +0 -211
  94. package/lib/sidebars.d.ts +0 -42
  95. package/lib/sidebars.js +0 -309
  96. package/src/__tests__/__fixtures__/bad-id-site/docs/invalid-id.md +0 -5
  97. package/src/__tests__/__fixtures__/bad-slug-on-doc-home-site/docs/docWithSlug.md +0 -5
  98. package/src/__tests__/__fixtures__/empty-site/docusaurus.config.js +0 -16
  99. package/src/__tests__/__fixtures__/empty-site/sidebars.json +0 -1
  100. package/src/__tests__/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -34
  101. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -11
  102. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -11
  103. package/src/__tests__/__fixtures__/sidebars/sidebars-category.js +0 -44
  104. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -20
  105. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed.json +0 -21
  106. package/src/__tests__/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -10
  107. package/src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js +0 -20
  108. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -11
  109. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -11
  110. package/src/__tests__/__fixtures__/sidebars/sidebars-link.json +0 -11
  111. package/src/__tests__/__fixtures__/sidebars/sidebars-unknown-type.json +0 -14
  112. package/src/__tests__/__fixtures__/sidebars/sidebars-wrong-field.json +0 -20
  113. package/src/__tests__/__fixtures__/sidebars/sidebars.json +0 -20
  114. package/src/__tests__/__fixtures__/simple-site/docs/foo/bar.md +0 -69
  115. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +0 -67
  116. package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
  117. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -52
  118. package/src/__tests__/__fixtures__/simple-site/docs/ipsum.md +0 -5
  119. package/src/__tests__/__fixtures__/simple-site/docs/lorem.md +0 -6
  120. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +0 -5
  121. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +0 -5
  122. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +0 -5
  123. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +0 -5
  124. package/src/__tests__/__fixtures__/simple-site/docs/slugs/absoluteSlug.md +0 -5
  125. package/src/__tests__/__fixtures__/simple-site/docs/slugs/relativeSlug.md +0 -5
  126. package/src/__tests__/__fixtures__/simple-site/docs/slugs/resolvedSlug.md +0 -5
  127. package/src/__tests__/__fixtures__/simple-site/docs/slugs/tryToEscapeSlug.md +0 -5
  128. package/src/__tests__/__fixtures__/simple-site/sidebars.json +0 -23
  129. package/src/__tests__/__fixtures__/simple-site/wrong-sidebars.json +0 -7
  130. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/0-getting-started.md +0 -3
  131. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/1-installation.md +0 -3
  132. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/00_api-overview.md +0 -3
  133. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/0 --- Client API.md +0 -1
  134. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/1 --- Server API.md +0 -1
  135. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/0. Plugin API.md +0 -1
  136. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/1. Theme API.md +0 -1
  137. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/_category_.yml +0 -1
  138. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/03_api-end.md +0 -3
  139. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/_category_.json +0 -3
  140. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/0-guide2.5.md +0 -8
  141. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/02-guide2.md +0 -7
  142. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/_category_.json +0 -3
  143. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/a-guide4.md +0 -7
  144. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/b-guide5.md +0 -7
  145. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/guide3.md +0 -8
  146. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/z-guide1.md +0 -8
  147. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars.js +0 -23
  148. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +0 -7
  149. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-2.md +0 -8
  150. package/src/__tests__/__fixtures__/site-with-doc-label/docusaurus.config.js +0 -14
  151. package/src/__tests__/__fixtures__/site-with-doc-label/sidebars.json +0 -14
  152. package/src/__tests__/__fixtures__/versioned-site/community/team.md +0 -1
  153. package/src/__tests__/__fixtures__/versioned-site/community_sidebars.json +0 -3
  154. package/src/__tests__/__fixtures__/versioned-site/community_versioned_docs/version-1.0.0/team.md +0 -1
  155. package/src/__tests__/__fixtures__/versioned-site/community_versioned_sidebars/version-1.0.0-sidebars.json +0 -3
  156. package/src/__tests__/__fixtures__/versioned-site/community_versions.json +0 -1
  157. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +0 -4
  158. package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +0 -1
  159. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/absoluteSlug.md +0 -5
  160. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/relativeSlug.md +0 -5
  161. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/resolvedSlug.md +0 -5
  162. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/tryToEscapeSlug.md +0 -5
  163. package/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js +0 -18
  164. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  165. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md +0 -5
  166. package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  167. package/src/__tests__/__fixtures__/versioned-site/sidebars.json +0 -10
  168. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/bar.md +0 -4
  169. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/baz.md +0 -1
  170. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +0 -1
  171. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/foo/bar.md +0 -1
  172. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +0 -1
  173. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootAbsoluteSlug.md +0 -5
  174. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootRelativeSlug.md +0 -5
  175. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootResolvedSlug.md +0 -5
  176. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootTryToEscapeSlug.md +0 -5
  177. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/absoluteSlug.md +0 -5
  178. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/relativeSlug.md +0 -5
  179. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/resolvedSlug.md +0 -5
  180. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/tryToEscapeSlug.md +0 -5
  181. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.0-sidebars.json +0 -11
  182. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +0 -10
  183. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-withSlugs-sidebars.json +0 -5
  184. package/src/__tests__/__fixtures__/versioned-site/versions.json +0 -5
  185. package/src/__tests__/__snapshots__/cli.test.ts.snap +0 -90
  186. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -1907
  187. package/src/__tests__/__snapshots__/sidebars.test.ts.snap +0 -218
  188. package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -487
  189. package/src/__tests__/cli.test.ts +0 -333
  190. package/src/__tests__/docFrontMatter.test.ts +0 -204
  191. package/src/__tests__/docs.test.ts +0 -875
  192. package/src/__tests__/index.test.ts +0 -1831
  193. package/src/__tests__/lastUpdate.test.ts +0 -68
  194. package/src/__tests__/numberPrefix.test.ts +0 -199
  195. package/src/__tests__/options.test.ts +0 -232
  196. package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
  197. package/src/__tests__/sidebars.test.ts +0 -638
  198. package/src/__tests__/slug.test.ts +0 -109
  199. package/src/__tests__/translations.test.ts +0 -159
  200. package/src/__tests__/versions.test.ts +0 -718
  201. package/src/client/__tests__/docsClientUtils.test.ts +0 -372
  202. package/src/markdown/__tests__/__fixtures__/docs/doc-localized.md +0 -1
  203. package/src/markdown/__tests__/__fixtures__/docs/doc1.md +0 -13
  204. package/src/markdown/__tests__/__fixtures__/docs/doc2.md +0 -12
  205. package/src/markdown/__tests__/__fixtures__/docs/doc4.md +0 -19
  206. package/src/markdown/__tests__/__fixtures__/docs/doc5.md +0 -6
  207. package/src/markdown/__tests__/__fixtures__/docs/subdir/doc3.md +0 -3
  208. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/doc2.md +0 -7
  209. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/subdir/doc1.md +0 -3
  210. package/src/markdown/__tests__/__snapshots__/linkify.test.ts.snap +0 -82
  211. package/src/markdown/__tests__/linkify.test.ts +0 -190
  212. package/src/sidebarItemsGenerator.ts +0 -307
  213. package/src/sidebars.ts +0 -489
  214. package/tsconfig.json +0 -9
@@ -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 lodash_1 = tslib_1.__importDefault(require("lodash"));
12
+ const utils_1 = require("@docusaurus/utils");
13
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
14
+ const path_1 = tslib_1.__importDefault(require("path"));
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 = async ({ 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[(0, utils_1.posixPath)(path_1.default.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 { SidebarsConfig, Sidebars, SidebarProcessorParams } from './types';
8
+ import type { PluginOptions } from '@docusaurus/plugin-content-docs';
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,94 @@
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 import_fresh_1 = tslib_1.__importDefault(require("import-fresh"));
13
+ const validation_1 = require("./validation");
14
+ const normalization_1 = require("./normalization");
15
+ const processor_1 = require("./processor");
16
+ const postProcessor_1 = require("./postProcessor");
17
+ const path_1 = tslib_1.__importDefault(require("path"));
18
+ const utils_1 = require("@docusaurus/utils");
19
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
20
+ const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
21
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
22
+ const combine_promises_1 = tslib_1.__importDefault(require("combine-promises"));
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
+ // use this before loadSidebars()
34
+ function resolveSidebarPathOption(siteDir, sidebarPathOption) {
35
+ return sidebarPathOption
36
+ ? path_1.default.resolve(siteDir, sidebarPathOption)
37
+ : sidebarPathOption;
38
+ }
39
+ exports.resolveSidebarPathOption = resolveSidebarPathOption;
40
+ async function readCategoriesMetadata(contentPath) {
41
+ const categoryFiles = await (0, utils_1.Globby)('**/_category_.{json,yml,yaml}', {
42
+ cwd: contentPath,
43
+ });
44
+ const categoryToFile = lodash_1.default.groupBy(categoryFiles, path_1.default.dirname);
45
+ return (0, combine_promises_1.default)(lodash_1.default.mapValues(categoryToFile, async (files, folder) => {
46
+ const filePath = files[0];
47
+ if (files.length > 1) {
48
+ logger_1.default.warn `There are more than one category metadata files for path=${folder}: ${files.join(', ')}. The behavior is undetermined.`;
49
+ }
50
+ const content = await fs_extra_1.default.readFile(path_1.default.join(contentPath, filePath), 'utf-8');
51
+ try {
52
+ return (0, validation_1.validateCategoryMetadataFile)(js_yaml_1.default.load(content));
53
+ }
54
+ catch (err) {
55
+ logger_1.default.error `The docs sidebar category metadata file path=${filePath} looks invalid!`;
56
+ throw err;
57
+ }
58
+ }));
59
+ }
60
+ async function loadSidebarsFileUnsafe(sidebarFilePath) {
61
+ // false => no sidebars
62
+ if (sidebarFilePath === false) {
63
+ return exports.DisabledSidebars;
64
+ }
65
+ // undefined => defaults to autogenerated sidebars
66
+ if (typeof sidebarFilePath === 'undefined') {
67
+ return exports.DefaultSidebars;
68
+ }
69
+ // Non-existent sidebars file: no sidebars
70
+ // Note: this edge case can happen on versioned docs, not current version
71
+ // We avoid creating empty versioned sidebars file with the CLI
72
+ if (!(await fs_extra_1.default.pathExists(sidebarFilePath))) {
73
+ return exports.DisabledSidebars;
74
+ }
75
+ // We don't want sidebars to be cached because of hot reloading.
76
+ return (0, import_fresh_1.default)(sidebarFilePath);
77
+ }
78
+ exports.loadSidebarsFileUnsafe = loadSidebarsFileUnsafe;
79
+ // Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
80
+ async function loadSidebars(sidebarFilePath, options) {
81
+ try {
82
+ const sidebarsConfig = await loadSidebarsFileUnsafe(sidebarFilePath);
83
+ const normalizedSidebars = (0, normalization_1.normalizeSidebars)(sidebarsConfig);
84
+ (0, validation_1.validateSidebars)(normalizedSidebars);
85
+ const categoriesMetadata = await readCategoriesMetadata(options.version.contentPath);
86
+ const processedSidebars = await (0, processor_1.processSidebars)(normalizedSidebars, categoriesMetadata, options);
87
+ return (0, postProcessor_1.postProcessSidebars)(processedSidebars, options);
88
+ }
89
+ catch (err) {
90
+ logger_1.default.error `Sidebars file at path=${sidebarFilePath} failed to be loaded.`;
91
+ throw err;
92
+ }
93
+ }
94
+ 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,55 @@
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 utils_1 = require("./utils");
12
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
13
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
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 === 'category') {
34
+ const normalizedCategory = {
35
+ ...item,
36
+ items: normalizeSidebar(item.items, logger_1.default.interpolate `code=${'items'} of the category name=${item.label}`),
37
+ };
38
+ return [normalizedCategory];
39
+ }
40
+ return [item];
41
+ }
42
+ exports.normalizeItem = normalizeItem;
43
+ function normalizeSidebar(sidebar, place) {
44
+ if (!Array.isArray(sidebar) && !(0, utils_1.isCategoriesShorthand)(sidebar)) {
45
+ 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 path=${'https://docusaurus.io/docs/sidebar/items'} for all valid syntaxes.`);
46
+ }
47
+ const normalizedSidebar = Array.isArray(sidebar)
48
+ ? sidebar
49
+ : normalizeCategoriesShorthand(sidebar);
50
+ return normalizedSidebar.flatMap((subItem) => normalizeItem(subItem));
51
+ }
52
+ function normalizeSidebars(sidebars) {
53
+ return lodash_1.default.mapValues(sidebars, (sidebar, id) => normalizeSidebar(sidebar, logger_1.default.interpolate `sidebar name=${id}`));
54
+ }
55
+ exports.normalizeSidebars = normalizeSidebars;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { Sidebars, SidebarProcessorParams, ProcessedSidebars } from './types';
8
+ export declare function postProcessSidebars(sidebars: ProcessedSidebars, params: SidebarProcessorParams): Sidebars;
@@ -0,0 +1,65 @@
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 utils_1 = require("@docusaurus/utils");
12
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
13
+ function normalizeCategoryLink(category, params) {
14
+ if (category.link?.type === 'generated-index') {
15
+ // default slug logic can be improved
16
+ const getDefaultSlug = () => `/category/${params.categoryLabelSlugger.slug(category.label)}`;
17
+ const slug = category.link.slug ?? getDefaultSlug();
18
+ const permalink = (0, utils_1.normalizeUrl)([params.version.versionPath, slug]);
19
+ return {
20
+ ...category.link,
21
+ slug,
22
+ permalink,
23
+ };
24
+ }
25
+ return category.link;
26
+ }
27
+ function postProcessSidebarItem(item, params) {
28
+ if (item.type === 'category') {
29
+ const category = {
30
+ ...item,
31
+ collapsed: item.collapsed ?? params.sidebarOptions.sidebarCollapsed,
32
+ collapsible: item.collapsible ?? params.sidebarOptions.sidebarCollapsible,
33
+ link: normalizeCategoryLink(item, params),
34
+ items: item.items.map((subItem) => postProcessSidebarItem(subItem, params)),
35
+ };
36
+ // If the current category doesn't have subitems, we render a normal link
37
+ // instead.
38
+ if (category.items.length === 0) {
39
+ if (!category.link) {
40
+ throw new Error(`Sidebar category ${item.label} has neither any subitem nor a link. This makes this item not able to link to anything.`);
41
+ }
42
+ return category.link.type === 'doc'
43
+ ? {
44
+ type: 'doc',
45
+ label: category.label,
46
+ id: category.link.id,
47
+ }
48
+ : {
49
+ type: 'link',
50
+ label: category.label,
51
+ href: category.link.permalink,
52
+ };
53
+ }
54
+ // A non-collapsible category can't be collapsed!
55
+ if (category.collapsible === false) {
56
+ category.collapsed = false;
57
+ }
58
+ return category;
59
+ }
60
+ return item;
61
+ }
62
+ function postProcessSidebars(sidebars, params) {
63
+ return lodash_1.default.mapValues(sidebars, (sidebar) => sidebar.map((item) => postProcessSidebarItem(item, params)));
64
+ }
65
+ 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 generator_1 = require("./generator");
12
+ const validation_1 = require("./validation");
13
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
14
+ const combine_promises_1 = tslib_1.__importDefault(require("combine-promises"));
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;