@docusaurus/plugin-content-docs 2.0.0-beta.fbdeefcac → 2.0.0-rc.1

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 (230) hide show
  1. package/lib/categoryGeneratedIndex.d.ts +12 -0
  2. package/lib/categoryGeneratedIndex.js +37 -0
  3. package/lib/cli.d.ts +3 -2
  4. package/lib/cli.js +57 -72
  5. package/lib/client/docsClientUtils.d.ts +9 -28
  6. package/lib/client/docsClientUtils.js +34 -43
  7. package/lib/client/index.d.ts +81 -0
  8. package/lib/client/index.js +67 -0
  9. package/lib/constants.d.ts +4 -0
  10. package/lib/constants.js +4 -1
  11. package/lib/docs.d.ts +34 -7
  12. package/lib/docs.js +202 -79
  13. package/{src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docusaurus.config.js → lib/frontMatter.d.ts} +4 -8
  14. package/lib/{docFrontMatter.js → frontMatter.js} +22 -3
  15. package/lib/globalData.d.ts +3 -3
  16. package/lib/globalData.js +35 -6
  17. package/lib/index.d.ts +3 -3
  18. package/lib/index.js +124 -154
  19. package/lib/lastUpdate.d.ts +4 -6
  20. package/lib/lastUpdate.js +22 -26
  21. package/lib/markdown/index.d.ts +3 -6
  22. package/lib/markdown/index.js +3 -3
  23. package/lib/markdown/linkify.d.ts +1 -1
  24. package/lib/markdown/linkify.js +6 -3
  25. package/lib/numberPrefix.d.ts +1 -1
  26. package/lib/numberPrefix.js +16 -21
  27. package/lib/options.d.ts +3 -5
  28. package/lib/options.js +56 -23
  29. package/lib/props.d.ts +7 -2
  30. package/lib/props.js +84 -14
  31. package/lib/routes.d.ts +29 -0
  32. package/lib/routes.js +96 -0
  33. package/lib/server-export.d.ts +9 -0
  34. package/lib/server-export.js +25 -0
  35. package/lib/{sidebarItemsGenerator.d.ts → sidebars/generator.d.ts} +1 -6
  36. package/lib/sidebars/generator.js +209 -0
  37. package/lib/sidebars/index.d.ts +13 -0
  38. package/lib/sidebars/index.js +92 -0
  39. package/lib/sidebars/normalization.d.ts +13 -0
  40. package/lib/sidebars/normalization.js +59 -0
  41. package/lib/sidebars/postProcessor.d.ts +11 -0
  42. package/lib/sidebars/postProcessor.js +81 -0
  43. package/lib/sidebars/processor.d.ts +10 -0
  44. package/lib/sidebars/processor.js +79 -0
  45. package/lib/sidebars/types.d.ts +183 -0
  46. package/lib/{docFrontMatter.d.ts → sidebars/types.js} +2 -2
  47. package/lib/sidebars/utils.d.ts +55 -0
  48. package/lib/sidebars/utils.js +259 -0
  49. package/lib/sidebars/validation.d.ts +11 -0
  50. package/lib/sidebars/validation.js +143 -0
  51. package/lib/slug.d.ts +5 -4
  52. package/lib/slug.js +28 -18
  53. package/{src/__tests__/__fixtures__/sidebars/sidebars-first-level-not-category.js → lib/tags.d.ts} +3 -14
  54. package/lib/tags.js +21 -0
  55. package/lib/translations.d.ts +3 -3
  56. package/lib/translations.js +100 -93
  57. package/lib/types.d.ts +13 -185
  58. package/lib/versions/files.d.ts +50 -0
  59. package/lib/versions/files.js +141 -0
  60. package/lib/versions/index.d.ts +36 -0
  61. package/lib/versions/index.js +154 -0
  62. package/lib/versions/validation.d.ts +17 -0
  63. package/lib/versions/validation.js +71 -0
  64. package/package.json +44 -30
  65. package/src/categoryGeneratedIndex.ts +60 -0
  66. package/src/cli.ts +88 -114
  67. package/src/client/docsClientUtils.ts +44 -71
  68. package/src/client/index.ts +158 -0
  69. package/src/constants.ts +4 -2
  70. package/src/docs.ts +281 -83
  71. package/src/frontMatter.ts +63 -0
  72. package/src/globalData.ts +57 -7
  73. package/src/index.ts +183 -224
  74. package/src/lastUpdate.ts +27 -38
  75. package/src/markdown/index.ts +10 -16
  76. package/src/markdown/linkify.ts +7 -4
  77. package/src/numberPrefix.ts +19 -26
  78. package/src/options.ts +61 -29
  79. package/src/plugin-content-docs.d.ts +569 -93
  80. package/src/props.ts +121 -21
  81. package/src/routes.ts +159 -0
  82. package/src/server-export.ts +26 -0
  83. package/src/sidebars/README.md +10 -0
  84. package/src/sidebars/generator.ts +292 -0
  85. package/src/sidebars/index.ts +118 -0
  86. package/src/sidebars/normalization.ts +91 -0
  87. package/src/sidebars/postProcessor.ts +112 -0
  88. package/src/sidebars/processor.ts +123 -0
  89. package/src/sidebars/types.ts +280 -0
  90. package/src/sidebars/utils.ts +393 -0
  91. package/src/sidebars/validation.ts +179 -0
  92. package/src/slug.ts +41 -22
  93. package/src/tags.ts +20 -0
  94. package/src/translations.ts +155 -124
  95. package/src/types.ts +17 -250
  96. package/src/versions/files.ts +216 -0
  97. package/src/versions/index.ts +246 -0
  98. package/src/versions/validation.ts +115 -0
  99. package/lib/.tsbuildinfo +0 -1
  100. package/lib/sidebarItemsGenerator.js +0 -211
  101. package/lib/sidebars.d.ts +0 -43
  102. package/lib/sidebars.js +0 -320
  103. package/lib/theme/hooks/useDocs.d.ts +0 -20
  104. package/lib/theme/hooks/useDocs.js +0 -75
  105. package/lib/versions.d.ts +0 -16
  106. package/lib/versions.js +0 -319
  107. package/src/__tests__/__fixtures__/bad-id-site/docs/invalid-id.md +0 -5
  108. package/src/__tests__/__fixtures__/bad-slug-on-doc-home-site/docs/docWithSlug.md +0 -5
  109. package/src/__tests__/__fixtures__/empty-site/docusaurus.config.js +0 -16
  110. package/src/__tests__/__fixtures__/empty-site/sidebars.json +0 -1
  111. package/src/__tests__/__fixtures__/sidebars/sidebars-category-shorthand.js +0 -34
  112. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-items.json +0 -11
  113. package/src/__tests__/__fixtures__/sidebars/sidebars-category-wrong-label.json +0 -11
  114. package/src/__tests__/__fixtures__/sidebars/sidebars-category.js +0 -44
  115. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed-first-level.json +0 -20
  116. package/src/__tests__/__fixtures__/sidebars/sidebars-collapsed.json +0 -21
  117. package/src/__tests__/__fixtures__/sidebars/sidebars-doc-id-not-string.json +0 -10
  118. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-href.json +0 -11
  119. package/src/__tests__/__fixtures__/sidebars/sidebars-link-wrong-label.json +0 -11
  120. package/src/__tests__/__fixtures__/sidebars/sidebars-link.json +0 -11
  121. package/src/__tests__/__fixtures__/sidebars/sidebars-unknown-type.json +0 -14
  122. package/src/__tests__/__fixtures__/sidebars/sidebars-wrong-field.json +0 -20
  123. package/src/__tests__/__fixtures__/sidebars/sidebars.json +0 -20
  124. package/src/__tests__/__fixtures__/simple-site/docs/foo/bar.md +0 -69
  125. package/src/__tests__/__fixtures__/simple-site/docs/foo/baz.md +0 -70
  126. package/src/__tests__/__fixtures__/simple-site/docs/headingAsTitle.md +0 -1
  127. package/src/__tests__/__fixtures__/simple-site/docs/hello.md +0 -53
  128. package/src/__tests__/__fixtures__/simple-site/docs/ipsum.md +0 -5
  129. package/src/__tests__/__fixtures__/simple-site/docs/lorem.md +0 -6
  130. package/src/__tests__/__fixtures__/simple-site/docs/rootAbsoluteSlug.md +0 -5
  131. package/src/__tests__/__fixtures__/simple-site/docs/rootRelativeSlug.md +0 -5
  132. package/src/__tests__/__fixtures__/simple-site/docs/rootResolvedSlug.md +0 -5
  133. package/src/__tests__/__fixtures__/simple-site/docs/rootTryToEscapeSlug.md +0 -5
  134. package/src/__tests__/__fixtures__/simple-site/docs/slugs/absoluteSlug.md +0 -5
  135. package/src/__tests__/__fixtures__/simple-site/docs/slugs/relativeSlug.md +0 -5
  136. package/src/__tests__/__fixtures__/simple-site/docs/slugs/resolvedSlug.md +0 -5
  137. package/src/__tests__/__fixtures__/simple-site/docs/slugs/tryToEscapeSlug.md +0 -5
  138. package/src/__tests__/__fixtures__/simple-site/docusaurus.config.js +0 -14
  139. package/src/__tests__/__fixtures__/simple-site/sidebars.json +0 -23
  140. package/src/__tests__/__fixtures__/simple-site/wrong-sidebars.json +0 -7
  141. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/0-getting-started.md +0 -3
  142. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/1-installation.md +0 -3
  143. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/00_api-overview.md +0 -3
  144. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/0 --- Client API.md +0 -1
  145. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/01_Core APIs/1 --- Server API.md +0 -1
  146. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/0. Plugin API.md +0 -1
  147. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/1. Theme API.md +0 -1
  148. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/02_Extension APIs/_category_.yml +0 -1
  149. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/03_api-end.md +0 -3
  150. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/3-API/_category_.json +0 -3
  151. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/0-guide2.5.md +0 -8
  152. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/02-guide2.md +0 -7
  153. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/_category_.json +0 -3
  154. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/a-guide4.md +0 -7
  155. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/b-guide5.md +0 -7
  156. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/guide3.md +0 -8
  157. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/docs/Guides/z-guide1.md +0 -8
  158. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars.js +0 -23
  159. package/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js +0 -16
  160. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-1.md +0 -7
  161. package/src/__tests__/__fixtures__/site-with-doc-label/docs/hello-2.md +0 -8
  162. package/src/__tests__/__fixtures__/site-with-doc-label/docusaurus.config.js +0 -14
  163. package/src/__tests__/__fixtures__/site-with-doc-label/sidebars.json +0 -14
  164. package/src/__tests__/__fixtures__/versioned-site/community/team.md +0 -1
  165. package/src/__tests__/__fixtures__/versioned-site/community_sidebars.json +0 -3
  166. package/src/__tests__/__fixtures__/versioned-site/community_versioned_docs/version-1.0.0/team.md +0 -1
  167. package/src/__tests__/__fixtures__/versioned-site/community_versioned_sidebars/version-1.0.0-sidebars.json +0 -3
  168. package/src/__tests__/__fixtures__/versioned-site/community_versions.json +0 -1
  169. package/src/__tests__/__fixtures__/versioned-site/docs/foo/bar.md +0 -4
  170. package/src/__tests__/__fixtures__/versioned-site/docs/hello.md +0 -1
  171. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/absoluteSlug.md +0 -5
  172. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/relativeSlug.md +0 -5
  173. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/resolvedSlug.md +0 -5
  174. package/src/__tests__/__fixtures__/versioned-site/docs/slugs/tryToEscapeSlug.md +0 -5
  175. package/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js +0 -18
  176. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  177. package/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md +0 -5
  178. package/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md +0 -1
  179. package/src/__tests__/__fixtures__/versioned-site/sidebars.json +0 -10
  180. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/bar.md +0 -4
  181. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/foo/baz.md +0 -1
  182. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.0/hello.md +0 -1
  183. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/foo/bar.md +0 -1
  184. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-1.0.1/hello.md +0 -1
  185. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootAbsoluteSlug.md +0 -5
  186. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootRelativeSlug.md +0 -5
  187. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootResolvedSlug.md +0 -5
  188. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/rootTryToEscapeSlug.md +0 -5
  189. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/absoluteSlug.md +0 -5
  190. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/relativeSlug.md +0 -5
  191. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/resolvedSlug.md +0 -5
  192. package/src/__tests__/__fixtures__/versioned-site/versioned_docs/version-withSlugs/slugs/tryToEscapeSlug.md +0 -5
  193. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.0-sidebars.json +0 -11
  194. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-1.0.1-sidebars.json +0 -10
  195. package/src/__tests__/__fixtures__/versioned-site/versioned_sidebars/version-withSlugs-sidebars.json +0 -5
  196. package/src/__tests__/__fixtures__/versioned-site/versions.json +0 -5
  197. package/src/__tests__/__snapshots__/cli.test.ts.snap +0 -90
  198. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -1916
  199. package/src/__tests__/__snapshots__/sidebars.test.ts.snap +0 -218
  200. package/src/__tests__/__snapshots__/translations.test.ts.snap +0 -487
  201. package/src/__tests__/cli.test.ts +0 -333
  202. package/src/__tests__/docFrontMatter.test.ts +0 -244
  203. package/src/__tests__/docs.test.ts +0 -878
  204. package/src/__tests__/index.test.ts +0 -1871
  205. package/src/__tests__/lastUpdate.test.ts +0 -69
  206. package/src/__tests__/numberPrefix.test.ts +0 -199
  207. package/src/__tests__/options.test.ts +0 -231
  208. package/src/__tests__/sidebarItemsGenerator.test.ts +0 -336
  209. package/src/__tests__/sidebars.test.ts +0 -639
  210. package/src/__tests__/slug.test.ts +0 -109
  211. package/src/__tests__/translations.test.ts +0 -159
  212. package/src/__tests__/versions.test.ts +0 -741
  213. package/src/client/__tests__/docsClientUtils.test.ts +0 -371
  214. package/src/docFrontMatter.ts +0 -41
  215. package/src/markdown/__tests__/__fixtures__/docs/doc-localized.md +0 -1
  216. package/src/markdown/__tests__/__fixtures__/docs/doc1.md +0 -13
  217. package/src/markdown/__tests__/__fixtures__/docs/doc2.md +0 -12
  218. package/src/markdown/__tests__/__fixtures__/docs/doc4.md +0 -19
  219. package/src/markdown/__tests__/__fixtures__/docs/doc5.md +0 -6
  220. package/src/markdown/__tests__/__fixtures__/docs/subdir/doc3.md +0 -3
  221. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/doc2.md +0 -7
  222. package/src/markdown/__tests__/__fixtures__/versioned_docs/version-1.0.0/subdir/doc1.md +0 -3
  223. package/src/markdown/__tests__/__snapshots__/linkify.test.ts.snap +0 -82
  224. package/src/markdown/__tests__/linkify.test.ts +0 -190
  225. package/src/sidebarItemsGenerator.ts +0 -307
  226. package/src/sidebars.ts +0 -522
  227. package/src/theme/hooks/useDocs.ts +0 -103
  228. package/src/versions.ts +0 -572
  229. package/tsconfig.json +0 -9
  230. package/types.d.ts +0 -13
@@ -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,320 +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 (typeof item.collapsed !== 'undefined' &&
55
- typeof item.collapsed !== 'boolean') {
56
- throw new Error(`Error loading ${JSON.stringify(item)}: "collapsed" must be a boolean.`);
57
- }
58
- }
59
- function assertIsAutogenerated(item) {
60
- assertItem(item, ['dirName', 'customProps']);
61
- if (typeof item.dirName !== 'string') {
62
- throw new Error(`Error loading ${JSON.stringify(item)}: "dirName" must be a string.`);
63
- }
64
- if (item.dirName.startsWith('/') || item.dirName.endsWith('/')) {
65
- 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`);
66
- }
67
- }
68
- function assertIsDoc(item) {
69
- assertItem(item, ['id', 'label', 'customProps']);
70
- if (typeof item.id !== 'string') {
71
- throw new Error(`Error loading ${JSON.stringify(item)}: "id" must be a string.`);
72
- }
73
- if (item.label && typeof item.label !== 'string') {
74
- throw new Error(`Error loading ${JSON.stringify(item)}: "label" must be a string.`);
75
- }
76
- }
77
- function assertIsLink(item) {
78
- assertItem(item, ['href', 'label', 'customProps']);
79
- if (typeof item.href !== 'string') {
80
- throw new Error(`Error loading ${JSON.stringify(item)}: "href" must be a string.`);
81
- }
82
- if (typeof item.label !== 'string') {
83
- throw new Error(`Error loading ${JSON.stringify(item)}: "label" must be a string.`);
84
- }
85
- }
86
- /**
87
- * Normalizes recursively item and all its children. Ensures that at the end
88
- * each item will be an object with the corresponding type.
89
- */
90
- function normalizeItem(item) {
91
- if (typeof item === 'string') {
92
- return [
93
- {
94
- type: 'doc',
95
- id: item,
96
- },
97
- ];
98
- }
99
- if (isCategoryShorthand(item)) {
100
- return lodash_1.flatMap(normalizeCategoryShorthand(item), normalizeItem);
101
- }
102
- switch (item.type) {
103
- case 'category':
104
- assertIsCategory(item);
105
- return [
106
- {
107
- collapsed: exports.DefaultCategoryCollapsedValue,
108
- ...item,
109
- items: lodash_1.flatMap(item.items, normalizeItem),
110
- },
111
- ];
112
- case 'autogenerated':
113
- assertIsAutogenerated(item);
114
- return [item];
115
- case 'link':
116
- assertIsLink(item);
117
- return [item];
118
- case 'ref':
119
- case 'doc':
120
- assertIsDoc(item);
121
- return [item];
122
- default: {
123
- const extraMigrationError = item.type === 'subcategory'
124
- ? 'Docusaurus v2: "subcategory" has been renamed as "category".'
125
- : '';
126
- throw new Error(`Unknown sidebar item type "${item.type}". Sidebar item is ${JSON.stringify(item)}.\n${extraMigrationError}`);
127
- }
128
- }
129
- }
130
- function normalizeSidebar(sidebar) {
131
- const normalizedSidebar = Array.isArray(sidebar)
132
- ? sidebar
133
- : normalizeCategoryShorthand(sidebar);
134
- return lodash_1.flatMap(normalizedSidebar, normalizeItem);
135
- }
136
- function normalizeSidebars(sidebars) {
137
- return lodash_1.mapValues(sidebars, normalizeSidebar);
138
- }
139
- exports.DefaultSidebars = {
140
- defaultSidebar: [
141
- {
142
- type: 'autogenerated',
143
- dirName: '.',
144
- },
145
- ],
146
- };
147
- exports.DisabledSidebars = {};
148
- // If a path is provided, make it absolute
149
- // use this before loadSidebars()
150
- function resolveSidebarPathOption(siteDir, sidebarPathOption) {
151
- return sidebarPathOption
152
- ? path_1.default.resolve(siteDir, sidebarPathOption)
153
- : sidebarPathOption;
154
- }
155
- exports.resolveSidebarPathOption = resolveSidebarPathOption;
156
- // TODO refactor: make async
157
- // Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
158
- function loadSidebars(sidebarFilePath) {
159
- // false => no sidebars
160
- if (sidebarFilePath === false) {
161
- return exports.DisabledSidebars;
162
- }
163
- // undefined => defaults to autogenerated sidebars
164
- if (typeof sidebarFilePath === 'undefined') {
165
- return exports.DefaultSidebars;
166
- }
167
- // unexisting sidebars file: no sidebars
168
- // Note: this edge case can happen on versioned docs, not current version
169
- // We avoid creating empty versioned sidebars file with the CLI
170
- if (!fs_extra_1.default.existsSync(sidebarFilePath)) {
171
- return exports.DisabledSidebars;
172
- }
173
- // We don't want sidebars to be cached because of hot reloading.
174
- const sidebarJson = import_fresh_1.default(sidebarFilePath);
175
- return normalizeSidebars(sidebarJson);
176
- }
177
- exports.loadSidebars = loadSidebars;
178
- function toSidebarItemsGeneratorDoc(doc) {
179
- return lodash_1.pick(doc, [
180
- 'id',
181
- 'frontMatter',
182
- 'source',
183
- 'sourceDirName',
184
- 'sidebarPosition',
185
- ]);
186
- }
187
- exports.toSidebarItemsGeneratorDoc = toSidebarItemsGeneratorDoc;
188
- function toSidebarItemsGeneratorVersion(version) {
189
- return lodash_1.pick(version, ['versionName', 'contentPath']);
190
- }
191
- exports.toSidebarItemsGeneratorVersion = toSidebarItemsGeneratorVersion;
192
- // Handle the generation of autogenerated sidebar items
193
- async function processSidebar({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebar, docs, version, }) {
194
- // Just a minor lazy transformation optimization
195
- const getSidebarItemsGeneratorDocsAndVersion = lodash_1.memoize(() => ({
196
- docs: docs.map(toSidebarItemsGeneratorDoc),
197
- version: toSidebarItemsGeneratorVersion(version),
198
- }));
199
- async function processRecursive(item) {
200
- if (item.type === 'category') {
201
- return [
202
- {
203
- ...item,
204
- items: (await Promise.all(item.items.map(processRecursive))).flat(),
205
- },
206
- ];
207
- }
208
- if (item.type === 'autogenerated') {
209
- return sidebarItemsGenerator({
210
- item,
211
- numberPrefixParser,
212
- defaultSidebarItemsGenerator: sidebarItemsGenerator_1.DefaultSidebarItemsGenerator,
213
- ...getSidebarItemsGeneratorDocsAndVersion(),
214
- });
215
- }
216
- return [item];
217
- }
218
- return (await Promise.all(unprocessedSidebar.map(processRecursive))).flat();
219
- }
220
- exports.processSidebar = processSidebar;
221
- async function processSidebars({ sidebarItemsGenerator, numberPrefixParser, unprocessedSidebars, docs, version, }) {
222
- return combine_promises_1.default(lodash_1.mapValues(unprocessedSidebars, (unprocessedSidebar) => processSidebar({
223
- sidebarItemsGenerator,
224
- numberPrefixParser,
225
- unprocessedSidebar,
226
- docs,
227
- version,
228
- })));
229
- }
230
- exports.processSidebars = processSidebars;
231
- function collectSidebarItemsOfType(type, sidebar) {
232
- function collectRecursive(item) {
233
- const currentItemsCollected = item.type === type ? [item] : [];
234
- const childItemsCollected = item.type === 'category' ? lodash_1.flatten(item.items.map(collectRecursive)) : [];
235
- return [...currentItemsCollected, ...childItemsCollected];
236
- }
237
- return lodash_1.flatten(sidebar.map(collectRecursive));
238
- }
239
- function collectSidebarDocItems(sidebar) {
240
- return collectSidebarItemsOfType('doc', sidebar);
241
- }
242
- exports.collectSidebarDocItems = collectSidebarDocItems;
243
- function collectSidebarCategories(sidebar) {
244
- return collectSidebarItemsOfType('category', sidebar);
245
- }
246
- exports.collectSidebarCategories = collectSidebarCategories;
247
- function collectSidebarLinks(sidebar) {
248
- return collectSidebarItemsOfType('link', sidebar);
249
- }
250
- exports.collectSidebarLinks = collectSidebarLinks;
251
- function transformSidebarItems(sidebar, updateFn) {
252
- function transformRecursive(item) {
253
- if (item.type === 'category') {
254
- return updateFn({
255
- ...item,
256
- items: item.items.map(transformRecursive),
257
- });
258
- }
259
- return updateFn(item);
260
- }
261
- return sidebar.map(transformRecursive);
262
- }
263
- exports.transformSidebarItems = transformSidebarItems;
264
- function collectSidebarsDocIds(sidebars) {
265
- return lodash_1.mapValues(sidebars, (sidebar) => {
266
- return collectSidebarDocItems(sidebar).map((docItem) => docItem.id);
267
- });
268
- }
269
- exports.collectSidebarsDocIds = collectSidebarsDocIds;
270
- function createSidebarsUtils(sidebars) {
271
- const sidebarNameToDocIds = collectSidebarsDocIds(sidebars);
272
- function getFirstDocIdOfFirstSidebar() {
273
- var _a;
274
- return (_a = Object.values(sidebarNameToDocIds)[0]) === null || _a === void 0 ? void 0 : _a[0];
275
- }
276
- function getSidebarNameByDocId(docId) {
277
- // TODO lookup speed can be optimized
278
- const entry = Object.entries(sidebarNameToDocIds).find(([_sidebarName, docIds]) => docIds.includes(docId));
279
- return entry === null || entry === void 0 ? void 0 : entry[0];
280
- }
281
- function getDocNavigation(docId) {
282
- const sidebarName = getSidebarNameByDocId(docId);
283
- if (sidebarName) {
284
- const docIds = sidebarNameToDocIds[sidebarName];
285
- const currentIndex = docIds.indexOf(docId);
286
- const { previous, next } = utils_1.getElementsAround(docIds, currentIndex);
287
- return {
288
- sidebarName,
289
- previousId: previous,
290
- nextId: next,
291
- };
292
- }
293
- else {
294
- return {
295
- sidebarName: undefined,
296
- previousId: undefined,
297
- nextId: undefined,
298
- };
299
- }
300
- }
301
- function checkSidebarsDocIds(validDocIds, sidebarFilePath) {
302
- const allSidebarDocIds = lodash_1.flatten(Object.values(sidebarNameToDocIds));
303
- const invalidSidebarDocIds = lodash_1.difference(allSidebarDocIds, validDocIds);
304
- if (invalidSidebarDocIds.length > 0) {
305
- throw new Error(`Invalid sidebar file at "${utils_1.toMessageRelativeFilePath(sidebarFilePath)}".
306
- These sidebar document ids do not exist:
307
- - ${invalidSidebarDocIds.sort().join('\n- ')}
308
-
309
- Available document ids are:
310
- - ${validDocIds.sort().join('\n- ')}`);
311
- }
312
- }
313
- return {
314
- getFirstDocIdOfFirstSidebar,
315
- getSidebarNameByDocId,
316
- getDocNavigation,
317
- checkSidebarsDocIds,
318
- };
319
- }
320
- exports.createSidebarsUtils = createSidebarsUtils;
@@ -1,20 +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 { GlobalPluginData, GlobalVersion } from '../../types';
8
- import { ActivePlugin, ActiveDocContext, DocVersionSuggestions, GetActivePluginOptions } from '../../client/docsClientUtils';
9
- export declare const useAllDocsData: () => Record<string, GlobalPluginData>;
10
- export declare const useDocsData: (pluginId: string | undefined) => GlobalPluginData;
11
- export declare const useActivePlugin: (options?: GetActivePluginOptions) => ActivePlugin | undefined;
12
- export declare const useActivePluginAndVersion: (options?: GetActivePluginOptions) => {
13
- activePlugin: ActivePlugin;
14
- activeVersion: GlobalVersion | undefined;
15
- } | undefined;
16
- export declare const useVersions: (pluginId: string | undefined) => GlobalVersion[];
17
- export declare const useLatestVersion: (pluginId: string | undefined) => GlobalVersion;
18
- export declare const useActiveVersion: (pluginId: string | undefined) => GlobalVersion | undefined;
19
- export declare const useActiveDocContext: (pluginId: string | undefined) => ActiveDocContext;
20
- export declare const useDocVersionSuggestions: (pluginId: string | undefined) => DocVersionSuggestions;
@@ -1,75 +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.useDocVersionSuggestions = exports.useActiveDocContext = exports.useActiveVersion = exports.useLatestVersion = exports.useVersions = exports.useActivePluginAndVersion = exports.useActivePlugin = exports.useDocsData = exports.useAllDocsData = void 0;
10
- const tslib_1 = require("tslib");
11
- const router_1 = require("@docusaurus/router");
12
- const useGlobalData_1 = tslib_1.__importStar(require("@docusaurus/useGlobalData"));
13
- const docsClientUtils_1 = require("../../client/docsClientUtils");
14
- // Important to use a constant object to avoid React useEffect executions etc...,
15
- // see https://github.com/facebook/docusaurus/issues/5089
16
- const StableEmptyObject = {};
17
- // Not using useAllPluginInstancesData() because in blog-only mode, docs hooks are still used by the theme
18
- // We need a fail-safe fallback when the docs plugin is not in use
19
- const useAllDocsData = () => { var _a;
20
- // useAllPluginInstancesData('docusaurus-plugin-content-docs');
21
- return (_a = useGlobalData_1.default()['docusaurus-plugin-content-docs']) !== null && _a !== void 0 ? _a : StableEmptyObject; };
22
- exports.useAllDocsData = useAllDocsData;
23
- const useDocsData = (pluginId) => useGlobalData_1.usePluginData('docusaurus-plugin-content-docs', pluginId);
24
- exports.useDocsData = useDocsData;
25
- const useActivePlugin = (options = {}) => {
26
- const data = exports.useAllDocsData();
27
- const { pathname } = router_1.useLocation();
28
- return docsClientUtils_1.getActivePlugin(data, pathname, options);
29
- };
30
- exports.useActivePlugin = useActivePlugin;
31
- const useActivePluginAndVersion = (options = {}) => {
32
- const activePlugin = exports.useActivePlugin(options);
33
- const { pathname } = router_1.useLocation();
34
- if (activePlugin) {
35
- const activeVersion = docsClientUtils_1.getActiveVersion(activePlugin.pluginData, pathname);
36
- return {
37
- activePlugin,
38
- activeVersion,
39
- };
40
- }
41
- return undefined;
42
- };
43
- exports.useActivePluginAndVersion = useActivePluginAndVersion;
44
- // versions are returned ordered (most recent first)
45
- const useVersions = (pluginId) => {
46
- const data = exports.useDocsData(pluginId);
47
- return data.versions;
48
- };
49
- exports.useVersions = useVersions;
50
- const useLatestVersion = (pluginId) => {
51
- const data = exports.useDocsData(pluginId);
52
- return docsClientUtils_1.getLatestVersion(data);
53
- };
54
- exports.useLatestVersion = useLatestVersion;
55
- // Note: return undefined on doc-unrelated pages,
56
- // because there's no version currently considered as active
57
- const useActiveVersion = (pluginId) => {
58
- const data = exports.useDocsData(pluginId);
59
- const { pathname } = router_1.useLocation();
60
- return docsClientUtils_1.getActiveVersion(data, pathname);
61
- };
62
- exports.useActiveVersion = useActiveVersion;
63
- const useActiveDocContext = (pluginId) => {
64
- const data = exports.useDocsData(pluginId);
65
- const { pathname } = router_1.useLocation();
66
- return docsClientUtils_1.getActiveDocContext(data, pathname);
67
- };
68
- exports.useActiveDocContext = useActiveDocContext;
69
- // Useful to say "hey, you are not on the latest docs version, please switch"
70
- const useDocVersionSuggestions = (pluginId) => {
71
- const data = exports.useDocsData(pluginId);
72
- const { pathname } = router_1.useLocation();
73
- return docsClientUtils_1.getDocVersionSuggestions(data, pathname);
74
- };
75
- exports.useDocVersionSuggestions = useDocVersionSuggestions;