@azerion/bunosaurus-plugin-content-docs 0.0.0-stage → 4.0.0

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 (119) hide show
  1. package/README.md +6 -2
  2. package/lib/categoryGeneratedIndex.d.ts +12 -0
  3. package/lib/categoryGeneratedIndex.js +36 -0
  4. package/lib/cli.d.ts +13 -0
  5. package/lib/cli.js +124 -0
  6. package/lib/client/doc.d.ts +29 -0
  7. package/lib/client/doc.js +47 -0
  8. package/lib/client/docSidebarItemsExpandedState.d.ts +30 -0
  9. package/lib/client/docSidebarItemsExpandedState.js +27 -0
  10. package/lib/client/docsClientUtils.d.ts +15 -0
  11. package/lib/client/docsClientUtils.js +86 -0
  12. package/lib/client/docsPreferredVersion.d.ts +29 -0
  13. package/lib/client/docsPreferredVersion.js +124 -0
  14. package/lib/client/docsSearch.d.ts +19 -0
  15. package/lib/client/docsSearch.js +39 -0
  16. package/lib/client/docsSidebar.d.ts +25 -0
  17. package/lib/client/docsSidebar.js +29 -0
  18. package/lib/client/docsUtils.d.ts +112 -0
  19. package/lib/client/docsUtils.js +299 -0
  20. package/lib/client/docsVersion.d.ts +19 -0
  21. package/lib/client/docsVersion.js +25 -0
  22. package/lib/client/index.d.ts +91 -0
  23. package/lib/client/index.js +83 -0
  24. package/lib/client/structuredDataUtils.d.ts +11 -0
  25. package/lib/client/structuredDataUtils.js +35 -0
  26. package/lib/constants.d.ts +14 -0
  27. package/lib/constants.js +17 -0
  28. package/lib/contentHelpers.d.ts +12 -0
  29. package/lib/contentHelpers.js +31 -0
  30. package/lib/docs.d.ts +48 -0
  31. package/lib/docs.js +252 -0
  32. package/lib/frontMatter.d.ts +10 -0
  33. package/lib/frontMatter.js +46 -0
  34. package/lib/globalData.d.ts +9 -0
  35. package/lib/globalData.js +59 -0
  36. package/lib/index.d.ts +10 -0
  37. package/lib/index.js +198 -0
  38. package/lib/numberPrefix.d.ts +11 -0
  39. package/lib/numberPrefix.js +50 -0
  40. package/lib/options.d.ts +10 -0
  41. package/lib/options.js +138 -0
  42. package/lib/props.d.ts +21 -0
  43. package/lib/props.js +156 -0
  44. package/lib/routes.d.ts +22 -0
  45. package/lib/routes.js +184 -0
  46. package/lib/server-export.d.ts +9 -0
  47. package/lib/server-export.js +25 -0
  48. package/lib/sidebars/generator.d.ts +10 -0
  49. package/lib/sidebars/generator.js +217 -0
  50. package/lib/sidebars/index.d.ts +13 -0
  51. package/lib/sidebars/index.js +99 -0
  52. package/lib/sidebars/normalization.d.ts +13 -0
  53. package/lib/sidebars/normalization.js +58 -0
  54. package/lib/sidebars/postProcessor.d.ts +11 -0
  55. package/lib/sidebars/postProcessor.js +82 -0
  56. package/lib/sidebars/processor.d.ts +10 -0
  57. package/lib/sidebars/processor.js +77 -0
  58. package/lib/sidebars/types.d.ts +192 -0
  59. package/lib/sidebars/types.js +8 -0
  60. package/lib/sidebars/utils.d.ts +69 -0
  61. package/lib/sidebars/utils.js +327 -0
  62. package/lib/sidebars/validation.d.ts +11 -0
  63. package/lib/sidebars/validation.js +152 -0
  64. package/lib/slug.d.ts +15 -0
  65. package/lib/slug.js +51 -0
  66. package/lib/translations.d.ts +10 -0
  67. package/lib/translations.js +243 -0
  68. package/lib/types.d.ts +27 -0
  69. package/lib/types.js +8 -0
  70. package/lib/versions/files.d.ts +54 -0
  71. package/lib/versions/files.js +152 -0
  72. package/lib/versions/loadVersion.d.ts +17 -0
  73. package/lib/versions/loadVersion.js +126 -0
  74. package/lib/versions/validation.d.ts +17 -0
  75. package/lib/versions/validation.js +70 -0
  76. package/lib/versions/version.d.ts +40 -0
  77. package/lib/versions/version.js +189 -0
  78. package/package.json +70 -4
  79. package/src/categoryGeneratedIndex.ts +60 -0
  80. package/src/cli.ts +180 -0
  81. package/src/client/doc.tsx +71 -0
  82. package/src/client/docSidebarItemsExpandedState.tsx +55 -0
  83. package/src/client/docsClientUtils.ts +140 -0
  84. package/src/client/docsPreferredVersion.tsx +245 -0
  85. package/src/client/docsSearch.test.ts +15 -0
  86. package/src/client/docsSearch.ts +57 -0
  87. package/src/client/docsSidebar.tsx +50 -0
  88. package/src/client/docsUtils.tsx +442 -0
  89. package/src/client/docsVersion.tsx +36 -0
  90. package/src/client/index.ts +211 -0
  91. package/src/client/structuredDataUtils.ts +54 -0
  92. package/src/constants.ts +15 -0
  93. package/src/contentHelpers.ts +34 -0
  94. package/src/docs.ts +402 -0
  95. package/src/frontMatter.ts +57 -0
  96. package/src/globalData.ts +79 -0
  97. package/src/index.ts +289 -0
  98. package/src/numberPrefix.ts +62 -0
  99. package/src/options.ts +185 -0
  100. package/src/plugin-content-docs.d.ts +667 -0
  101. package/src/props.ts +223 -0
  102. package/src/routes.ts +263 -0
  103. package/src/server-export.ts +26 -0
  104. package/src/sidebars/README.md +10 -0
  105. package/src/sidebars/generator.ts +307 -0
  106. package/src/sidebars/index.ts +130 -0
  107. package/src/sidebars/normalization.ts +91 -0
  108. package/src/sidebars/postProcessor.ts +115 -0
  109. package/src/sidebars/processor.ts +122 -0
  110. package/src/sidebars/types.ts +293 -0
  111. package/src/sidebars/utils.ts +529 -0
  112. package/src/sidebars/validation.ts +189 -0
  113. package/src/slug.ts +79 -0
  114. package/src/translations.ts +365 -0
  115. package/src/types.ts +34 -0
  116. package/src/versions/files.ts +240 -0
  117. package/src/versions/loadVersion.ts +185 -0
  118. package/src/versions/validation.ts +115 -0
  119. package/src/versions/version.ts +300 -0
@@ -0,0 +1,442 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {type ReactNode, useMemo} from 'react';
9
+ import {matchPath, useLocation} from '@docusaurus/router';
10
+ import renderRoutes from '@docusaurus/renderRoutes';
11
+ import {
12
+ useActivePlugin,
13
+ useActiveDocContext,
14
+ useLatestVersion,
15
+ type GlobalVersion,
16
+ type GlobalSidebar,
17
+ type GlobalDoc,
18
+ } from '@docusaurus/plugin-content-docs/client';
19
+ import {isSamePath} from '@docusaurus/theme-common/internal';
20
+ import {uniq} from '@docusaurus/theme-common';
21
+ import type {Props as DocRootProps} from '@theme/DocRoot';
22
+ import {useDocsPreferredVersion} from './docsPreferredVersion';
23
+ import {useDocsVersion} from './docsVersion';
24
+ import {useDocsSidebar} from './docsSidebar';
25
+
26
+ import type {
27
+ PropSidebar,
28
+ PropSidebarItem,
29
+ PropSidebarItemCategory,
30
+ PropVersionDoc,
31
+ PropSidebarBreadcrumbsItem,
32
+ } from '@docusaurus/plugin-content-docs';
33
+
34
+ /**
35
+ * A null-safe way to access a doc's data by ID in the active version.
36
+ */
37
+ export function useDocById(id: string): PropVersionDoc;
38
+ /**
39
+ * A null-safe way to access a doc's data by ID in the active version.
40
+ */
41
+ export function useDocById(id: string | undefined): PropVersionDoc | undefined;
42
+ export function useDocById(id: string | undefined): PropVersionDoc | undefined {
43
+ const version = useDocsVersion();
44
+ if (!id) {
45
+ return undefined;
46
+ }
47
+ const doc = version.docs[id];
48
+ if (!doc) {
49
+ throw new Error(`no version doc found by id=${id}`);
50
+ }
51
+ return doc;
52
+ }
53
+
54
+ /**
55
+ * Pure function, similar to `Array#find`, but works on the sidebar tree.
56
+ */
57
+ export function findSidebarCategory(
58
+ sidebar: PropSidebar,
59
+ predicate: (category: PropSidebarItemCategory) => boolean,
60
+ ): PropSidebarItemCategory | undefined {
61
+ for (const item of sidebar) {
62
+ if (item.type === 'category') {
63
+ if (predicate(item)) {
64
+ return item;
65
+ }
66
+ const subItem = findSidebarCategory(item.items, predicate);
67
+ if (subItem) {
68
+ return subItem;
69
+ }
70
+ }
71
+ }
72
+ return undefined;
73
+ }
74
+
75
+ /**
76
+ * Best effort to assign a link to a sidebar category. If the category doesn't
77
+ * have a link itself, we link to the first sub item with a link.
78
+ */
79
+ export function findFirstSidebarItemCategoryLink(
80
+ item: PropSidebarItemCategory,
81
+ ): string | undefined {
82
+ if (item.href && !item.linkUnlisted) {
83
+ return item.href;
84
+ }
85
+
86
+ for (const subItem of item.items) {
87
+ const link = findFirstSidebarItemLink(subItem);
88
+ if (link) {
89
+ return link;
90
+ }
91
+ }
92
+ return undefined;
93
+ }
94
+
95
+ /**
96
+ * Best effort to assign a link to a sidebar item.
97
+ */
98
+ export function findFirstSidebarItemLink(
99
+ item: PropSidebarItem,
100
+ ): string | undefined {
101
+ if (item.type === 'link' && !item.unlisted) {
102
+ return item.href;
103
+ }
104
+ if (item.type === 'category') {
105
+ return findFirstSidebarItemCategoryLink(item);
106
+ }
107
+ // Other items types, like "html"
108
+ return undefined;
109
+ }
110
+
111
+ /**
112
+ * Gets the category associated with the current location. Should only be used
113
+ * on category index pages.
114
+ */
115
+ export function useCurrentSidebarCategory(): PropSidebarItemCategory {
116
+ const {pathname} = useLocation();
117
+ const sidebar = useDocsSidebar();
118
+ if (!sidebar) {
119
+ throw new Error('Unexpected: cant find current sidebar in context');
120
+ }
121
+ const categoryBreadcrumbs = getSidebarBreadcrumbs({
122
+ sidebarItems: sidebar.items,
123
+ pathname,
124
+ onlyCategories: true,
125
+ });
126
+ const deepestCategory = categoryBreadcrumbs.slice(-1)[0];
127
+ if (!deepestCategory) {
128
+ throw new Error(
129
+ `${pathname} is not associated with a category. useCurrentSidebarCategory() should only be used on category index pages.`,
130
+ );
131
+ }
132
+ return deepestCategory;
133
+ }
134
+
135
+ /**
136
+ * Gets the category associated with the current location. Should only be used
137
+ * on category index pages.
138
+ */
139
+ export function useCurrentSidebarSiblings(): PropSidebarItem[] {
140
+ const {pathname} = useLocation();
141
+ const sidebar = useDocsSidebar();
142
+ if (!sidebar) {
143
+ throw new Error('Unexpected: cant find current sidebar in context');
144
+ }
145
+ const categoryBreadcrumbs = getSidebarBreadcrumbs({
146
+ sidebarItems: sidebar.items,
147
+ pathname,
148
+ onlyCategories: true,
149
+ });
150
+ const deepestCategory = categoryBreadcrumbs.slice(-1)[0];
151
+ return deepestCategory?.items ?? sidebar.items;
152
+ }
153
+
154
+ const isActive = (testedPath: string | undefined, activePath: string) =>
155
+ typeof testedPath !== 'undefined' && isSamePath(testedPath, activePath);
156
+ const containsActiveSidebarItem = (
157
+ items: PropSidebarItem[],
158
+ activePath: string,
159
+ ) => items.some((subItem) => isActiveSidebarItem(subItem, activePath));
160
+
161
+ /**
162
+ * Checks if a sidebar item should be active, based on the active path.
163
+ */
164
+ export function isActiveSidebarItem(
165
+ item: PropSidebarItem,
166
+ activePath: string,
167
+ ): boolean {
168
+ if (item.type === 'link') {
169
+ return isActive(item.href, activePath);
170
+ }
171
+
172
+ if (item.type === 'category') {
173
+ return (
174
+ isActive(item.href, activePath) ||
175
+ containsActiveSidebarItem(item.items, activePath)
176
+ );
177
+ }
178
+
179
+ return false;
180
+ }
181
+
182
+ export function isVisibleSidebarItem(
183
+ item: PropSidebarItem,
184
+ activePath: string,
185
+ ): boolean {
186
+ switch (item.type) {
187
+ case 'category':
188
+ return (
189
+ isActiveSidebarItem(item, activePath) ||
190
+ (typeof item.href !== 'undefined' && !item.linkUnlisted) ||
191
+ item.items.some((subItem) => isVisibleSidebarItem(subItem, activePath))
192
+ );
193
+ case 'link':
194
+ // An unlisted item remains visible if it is active
195
+ return !item.unlisted || isActiveSidebarItem(item, activePath);
196
+ default:
197
+ return true;
198
+ }
199
+ }
200
+
201
+ export function useVisibleSidebarItems(
202
+ items: readonly PropSidebarItem[],
203
+ activePath: string,
204
+ ): PropSidebarItem[] {
205
+ return useMemo(
206
+ () => items.filter((item) => isVisibleSidebarItem(item, activePath)),
207
+ [items, activePath],
208
+ );
209
+ }
210
+
211
+ function getSidebarBreadcrumbs(param: {
212
+ sidebarItems: PropSidebar;
213
+ pathname: string;
214
+ onlyCategories: true;
215
+ }): PropSidebarItemCategory[];
216
+
217
+ function getSidebarBreadcrumbs(param: {
218
+ sidebarItems: PropSidebar;
219
+ pathname: string;
220
+ }): PropSidebarBreadcrumbsItem[];
221
+
222
+ /**
223
+ * Get the sidebar the breadcrumbs for a given pathname
224
+ * Ordered from top to bottom
225
+ */
226
+ function getSidebarBreadcrumbs({
227
+ sidebarItems,
228
+ pathname,
229
+ onlyCategories = false,
230
+ }: {
231
+ sidebarItems: PropSidebar;
232
+ pathname: string;
233
+ onlyCategories?: boolean;
234
+ }): PropSidebarBreadcrumbsItem[] {
235
+ const breadcrumbs: PropSidebarBreadcrumbsItem[] = [];
236
+
237
+ function extract(items: PropSidebarItem[]): boolean {
238
+ for (const item of items) {
239
+ // Extract category item
240
+ if (item.type === 'category') {
241
+ if (isSamePath(item.href, pathname) || extract(item.items)) {
242
+ breadcrumbs.unshift(item);
243
+ return true;
244
+ }
245
+ }
246
+ // Extract doc item
247
+ else if (
248
+ item.type === 'link' &&
249
+ item.docId &&
250
+ isSamePath(item.href, pathname)
251
+ ) {
252
+ if (!onlyCategories) {
253
+ breadcrumbs.unshift(item);
254
+ }
255
+ return true;
256
+ }
257
+ }
258
+
259
+ return false;
260
+ }
261
+
262
+ extract(sidebarItems);
263
+
264
+ return breadcrumbs;
265
+ }
266
+
267
+ /**
268
+ * Gets the breadcrumbs of the current doc page, based on its sidebar location.
269
+ * Returns `null` if there's no sidebar or breadcrumbs are disabled.
270
+ */
271
+ export function useSidebarBreadcrumbs(): PropSidebarBreadcrumbsItem[] | null {
272
+ const sidebar = useDocsSidebar();
273
+ const {pathname} = useLocation();
274
+ const breadcrumbsOption = useActivePlugin()?.pluginData.breadcrumbs;
275
+ if (breadcrumbsOption === false || !sidebar) {
276
+ return null;
277
+ }
278
+ return getSidebarBreadcrumbs({sidebarItems: sidebar.items, pathname});
279
+ }
280
+
281
+ /**
282
+ * "Version candidates" are mostly useful for the layout components, which must
283
+ * be able to work on all pages. For example, if a user has `{ type: "doc",
284
+ * docId: "intro" }` as a navbar item, which version does that refer to? We
285
+ * believe that it could refer to at most three version candidates:
286
+ *
287
+ * 1. The **active version**, the one that the user is currently browsing. See
288
+ * {@link useActiveDocContext}.
289
+ * 2. The **preferred version**, the one that the user last visited. See
290
+ * {@link useDocsPreferredVersion}.
291
+ * 3. The **latest version**, the "default". See {@link useLatestVersion}.
292
+ *
293
+ * @param docsPluginId The plugin ID to get versions from.
294
+ * @returns An array of 1~3 versions with priorities defined above, guaranteed
295
+ * to be unique and non-sparse. Will be memoized, hence stable for deps array.
296
+ */
297
+ export function useDocsVersionCandidates(
298
+ docsPluginId?: string,
299
+ ): [GlobalVersion, ...GlobalVersion[]] {
300
+ const {activeVersion} = useActiveDocContext(docsPluginId);
301
+ const {preferredVersion} = useDocsPreferredVersion(docsPluginId);
302
+ const latestVersion = useLatestVersion(docsPluginId);
303
+ return useMemo(
304
+ () =>
305
+ uniq(
306
+ [activeVersion, preferredVersion, latestVersion].filter(Boolean),
307
+ ) as [GlobalVersion, ...GlobalVersion[]],
308
+ [activeVersion, preferredVersion, latestVersion],
309
+ );
310
+ }
311
+
312
+ /**
313
+ * The layout components, like navbar items, must be able to work on all pages,
314
+ * even on non-doc ones where there's no version context, so a sidebar ID could
315
+ * be ambiguous. This hook would always return a sidebar to be linked to. See
316
+ * also {@link useDocsVersionCandidates} for how this selection is done.
317
+ *
318
+ * @throws This hook throws if a sidebar with said ID is not found.
319
+ */
320
+ export function useLayoutDocsSidebar(
321
+ sidebarId: string,
322
+ docsPluginId?: string,
323
+ ): GlobalSidebar {
324
+ const versions = useDocsVersionCandidates(docsPluginId);
325
+ return useMemo(() => {
326
+ const allSidebars = versions.flatMap((version) =>
327
+ version.sidebars ? Object.entries(version.sidebars) : [],
328
+ );
329
+ const sidebarEntry = allSidebars.find(
330
+ (sidebar) => sidebar[0] === sidebarId,
331
+ );
332
+ if (!sidebarEntry) {
333
+ throw new Error(
334
+ `Can't find any sidebar with id "${sidebarId}" in version${
335
+ versions.length > 1 ? 's' : ''
336
+ } ${versions.map((version) => version.name).join(', ')}".
337
+ Available sidebar ids are:
338
+ - ${allSidebars.map((entry) => entry[0]).join('\n- ')}`,
339
+ );
340
+ }
341
+ return sidebarEntry[1];
342
+ }, [sidebarId, versions]);
343
+ }
344
+
345
+ /**
346
+ * The layout components, like navbar items, must be able to work on all pages,
347
+ * even on non-doc ones where there's no version context, so a doc ID could be
348
+ * ambiguous. This hook would always return a doc to be linked to. See also
349
+ * {@link useDocsVersionCandidates} for how this selection is done.
350
+ *
351
+ * @throws This hook throws if a doc with said ID is not found.
352
+ */
353
+ export function useLayoutDoc(
354
+ docId: string,
355
+ docsPluginId?: string,
356
+ ): GlobalDoc | null {
357
+ const versions = useDocsVersionCandidates(docsPluginId);
358
+ return useMemo(() => {
359
+ const allDocs = versions.flatMap((version) => version.docs);
360
+ const doc = allDocs.find((versionDoc) => versionDoc.id === docId);
361
+ if (!doc) {
362
+ const isDraft = versions
363
+ .flatMap((version) => version.draftIds)
364
+ .includes(docId);
365
+ // Drafts should be silently filtered instead of throwing
366
+ if (isDraft) {
367
+ return null;
368
+ }
369
+ throw new Error(
370
+ `Couldn't find any doc with id "${docId}" in version${
371
+ versions.length > 1 ? 's' : ''
372
+ } "${versions.map((version) => version.name).join(', ')}".
373
+ Available doc ids are:
374
+ - ${uniq(allDocs.map((versionDoc) => versionDoc.id)).join('\n- ')}`,
375
+ );
376
+ }
377
+ return doc;
378
+ }, [docId, versions]);
379
+ }
380
+
381
+ // TODO later read version/route directly from context
382
+ /**
383
+ * The docs plugin creates nested routes, with the top-level route providing the
384
+ * version metadata, and the subroutes creating individual doc pages. This hook
385
+ * will match the current location against all known sub-routes.
386
+ *
387
+ * @param props The props received by `@theme/DocRoot`
388
+ * @returns The data of the relevant document at the current location, or `null`
389
+ * if no document associated with the current location can be found.
390
+ */
391
+ export function useDocRootMetadata({route}: DocRootProps): null | {
392
+ /** The element that should be rendered at the current location. */
393
+ docElement: ReactNode;
394
+ /**
395
+ * The name of the sidebar associated with the current doc. `sidebarName` and
396
+ * `sidebarItems` correspond to the value of {@link useDocsSidebar}.
397
+ */
398
+ sidebarName: string | undefined;
399
+ /** The items of the sidebar associated with the current doc. */
400
+ sidebarItems: PropSidebar | undefined;
401
+ } {
402
+ const location = useLocation();
403
+ const versionMetadata = useDocsVersion();
404
+ const docRoutes = route.routes!;
405
+ const currentDocRoute = docRoutes.find((docRoute) =>
406
+ matchPath(location.pathname, docRoute),
407
+ );
408
+ if (!currentDocRoute) {
409
+ return null;
410
+ }
411
+
412
+ // For now, the sidebarName is added as route config: not ideal!
413
+ const sidebarName = currentDocRoute.sidebar as string;
414
+
415
+ const sidebarItems = sidebarName
416
+ ? versionMetadata.docsSidebars[sidebarName]
417
+ : undefined;
418
+
419
+ const docElement = renderRoutes(docRoutes);
420
+
421
+ return {
422
+ docElement,
423
+ sidebarName,
424
+ sidebarItems,
425
+ };
426
+ }
427
+
428
+ /**
429
+ * Filter items we don't want to display on the doc card list view
430
+ * @param items
431
+ */
432
+ export function filterDocCardListItems(
433
+ items: PropSidebarItem[],
434
+ ): PropSidebarItem[] {
435
+ return items.filter((item) => {
436
+ const canHaveLink = item.type === 'category' || item.type === 'link';
437
+ if (canHaveLink) {
438
+ return !!findFirstSidebarItemLink(item);
439
+ }
440
+ return true;
441
+ });
442
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode, useContext} from 'react';
9
+ import {ReactContextError} from '@docusaurus/theme-common';
10
+ import type {PropVersionMetadata} from '@docusaurus/plugin-content-docs';
11
+
12
+ const Context = React.createContext<PropVersionMetadata | null>(null);
13
+
14
+ /**
15
+ * Provide the current version's metadata to your children.
16
+ */
17
+ export function DocsVersionProvider({
18
+ children,
19
+ version,
20
+ }: {
21
+ children: ReactNode;
22
+ version: PropVersionMetadata | null;
23
+ }): ReactNode {
24
+ return <Context value={version}>{children}</Context>;
25
+ }
26
+
27
+ /**
28
+ * Gets the version metadata of the current doc page.
29
+ */
30
+ export function useDocsVersion(): PropVersionMetadata {
31
+ const version = useContext(Context);
32
+ if (version === null) {
33
+ throw new ReactContextError('DocsVersionProvider');
34
+ }
35
+ return version;
36
+ }
@@ -0,0 +1,211 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {useLocation} from '@docusaurus/router';
9
+ import {
10
+ useAllPluginInstancesData,
11
+ usePluginData,
12
+ } from '@docusaurus/useGlobalData';
13
+
14
+ import {
15
+ getActivePlugin,
16
+ getLatestVersion,
17
+ getActiveVersion,
18
+ getActiveDocContext,
19
+ getDocVersionSuggestions,
20
+ } from './docsClientUtils';
21
+ import type {UseDataOptions} from '@docusaurus/types';
22
+
23
+ export {
24
+ useDocById,
25
+ findSidebarCategory,
26
+ findFirstSidebarItemLink,
27
+ isActiveSidebarItem,
28
+ isVisibleSidebarItem,
29
+ useVisibleSidebarItems,
30
+ useSidebarBreadcrumbs,
31
+ useDocsVersionCandidates,
32
+ useLayoutDoc,
33
+ useLayoutDocsSidebar,
34
+ useDocRootMetadata,
35
+ useCurrentSidebarCategory,
36
+ useCurrentSidebarSiblings,
37
+ filterDocCardListItems,
38
+ } from './docsUtils';
39
+
40
+ export {useDocsPreferredVersion} from './docsPreferredVersion';
41
+
42
+ export {
43
+ DocSidebarItemsExpandedStateProvider,
44
+ useDocSidebarItemsExpandedState,
45
+ } from './docSidebarItemsExpandedState';
46
+
47
+ export {DocsVersionProvider, useDocsVersion} from './docsVersion';
48
+
49
+ export {DocsSidebarProvider, useDocsSidebar} from './docsSidebar';
50
+
51
+ export {DocProvider, useDoc, type DocContextValue} from './doc';
52
+
53
+ export {
54
+ useDocsPreferredVersionByPluginId,
55
+ DocsPreferredVersionContextProvider,
56
+ } from './docsPreferredVersion';
57
+
58
+ export {
59
+ useDocsContextualSearchTags,
60
+ getDocsVersionSearchTag,
61
+ } from './docsSearch';
62
+
63
+ export {useBreadcrumbsStructuredData} from './structuredDataUtils';
64
+
65
+ export type ActivePlugin = {
66
+ pluginId: string;
67
+ pluginData: GlobalPluginData;
68
+ };
69
+ export type ActiveDocContext = {
70
+ activeVersion?: GlobalVersion;
71
+ activeDoc?: GlobalDoc;
72
+ alternateDocVersions: {[versionName: string]: GlobalDoc};
73
+ };
74
+ export type GlobalDoc = {
75
+ /**
76
+ * For generated index pages, this is the `slug`, **not** `permalink`
77
+ * (without base URL). Because slugs have leading slashes but IDs don't,
78
+ * there won't be clashes.
79
+ */
80
+ id: string;
81
+ path: string;
82
+ sidebar?: string;
83
+ unlisted?: boolean;
84
+ };
85
+
86
+ export type GlobalVersion = {
87
+ name: string;
88
+ label: string;
89
+ isLast: boolean;
90
+ path: string;
91
+ /** The doc with `slug: /`, or first doc in first sidebar */
92
+ mainDocId: string;
93
+ docs: GlobalDoc[];
94
+ /** Unversioned IDs. In development, this list is empty. */
95
+ draftIds: string[];
96
+ sidebars?: {[sidebarId: string]: GlobalSidebar};
97
+ };
98
+
99
+ export type GlobalSidebar = {
100
+ link?: {
101
+ label: string;
102
+ path: string;
103
+ };
104
+ // ... we may add other things here later
105
+ };
106
+ export type GlobalPluginData = {
107
+ path: string;
108
+ versions: GlobalVersion[];
109
+ breadcrumbs: boolean;
110
+ };
111
+ export type DocVersionSuggestions = {
112
+ /** Suggest the latest version */
113
+ latestVersionSuggestion: GlobalVersion;
114
+ /** Suggest the same doc, in latest version (if one exists) */
115
+ latestDocSuggestion?: GlobalDoc;
116
+ };
117
+
118
+ // Important to use a constant object to avoid React useEffect executions etc.
119
+ // see https://github.com/facebook/docusaurus/issues/5089
120
+ const StableEmptyObject = {};
121
+
122
+ // In blog-only mode, docs hooks are still used by the theme. We need a fail-
123
+ // safe fallback when the docs plugin is not in use
124
+ export const useAllDocsData = (): {[pluginId: string]: GlobalPluginData} =>
125
+ (useAllPluginInstancesData('docusaurus-plugin-content-docs') as
126
+ | {
127
+ [pluginId: string]: GlobalPluginData;
128
+ }
129
+ | undefined) ?? StableEmptyObject;
130
+
131
+ export const useDocsData = (pluginId: string | undefined): GlobalPluginData => {
132
+ try {
133
+ return usePluginData('docusaurus-plugin-content-docs', pluginId, {
134
+ failfast: true,
135
+ }) as GlobalPluginData;
136
+ } catch (error) {
137
+ throw new Error(
138
+ `You are using a feature of the Docusaurus docs plugin, but this plugin does not seem to be enabled${
139
+ pluginId === 'Default' ? '' : ` (pluginId=${pluginId}`
140
+ }`,
141
+ {cause: error},
142
+ );
143
+ }
144
+ };
145
+
146
+ // TODO this feature should be provided by docusaurus core
147
+ export function useActivePlugin(
148
+ options: UseDataOptions = {},
149
+ ): ActivePlugin | undefined {
150
+ const data = useAllDocsData();
151
+ const {pathname} = useLocation();
152
+ return getActivePlugin(data, pathname, options);
153
+ }
154
+
155
+ export function useActivePluginAndVersion(
156
+ options: UseDataOptions = {},
157
+ ):
158
+ | {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined}
159
+ | undefined {
160
+ const activePlugin = useActivePlugin(options);
161
+ const {pathname} = useLocation();
162
+ if (!activePlugin) {
163
+ return undefined;
164
+ }
165
+ const activeVersion = getActiveVersion(activePlugin.pluginData, pathname);
166
+ return {
167
+ activePlugin,
168
+ activeVersion,
169
+ };
170
+ }
171
+
172
+ /** Versions are returned ordered (most recent first). */
173
+ export function useVersions(pluginId: string | undefined): GlobalVersion[] {
174
+ const data = useDocsData(pluginId);
175
+ return data.versions;
176
+ }
177
+
178
+ export function useLatestVersion(pluginId: string | undefined): GlobalVersion {
179
+ const data = useDocsData(pluginId);
180
+ return getLatestVersion(data);
181
+ }
182
+
183
+ /**
184
+ * Returns `undefined` on doc-unrelated pages, because there's no version
185
+ * currently considered as active.
186
+ */
187
+ export function useActiveVersion(
188
+ pluginId: string | undefined,
189
+ ): GlobalVersion | undefined {
190
+ const data = useDocsData(pluginId);
191
+ const {pathname} = useLocation();
192
+ return getActiveVersion(data, pathname);
193
+ }
194
+
195
+ export function useActiveDocContext(
196
+ pluginId: string | undefined,
197
+ ): ActiveDocContext {
198
+ const data = useDocsData(pluginId);
199
+ const {pathname} = useLocation();
200
+ return getActiveDocContext(data, pathname);
201
+ }
202
+ /**
203
+ * Useful to say "hey, you are not on the latest docs version, please switch"
204
+ */
205
+ export function useDocVersionSuggestions(
206
+ pluginId: string | undefined,
207
+ ): DocVersionSuggestions {
208
+ const data = useDocsData(pluginId);
209
+ const {pathname} = useLocation();
210
+ return getDocVersionSuggestions(data, pathname);
211
+ }