@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,25 @@
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 ReactNode } from 'react';
8
+ import type { PropSidebar } from '@docusaurus/plugin-content-docs';
9
+ type ContextValue = {
10
+ name: string;
11
+ items: PropSidebar;
12
+ };
13
+ /**
14
+ * Provide the current sidebar to your children.
15
+ */
16
+ export declare function DocsSidebarProvider({ children, name, items, }: {
17
+ children: ReactNode;
18
+ name: string | undefined;
19
+ items: PropSidebar | undefined;
20
+ }): ReactNode;
21
+ /**
22
+ * Gets the sidebar that's currently displayed, or `null` if there isn't one
23
+ */
24
+ export declare function useDocsSidebar(): ContextValue | null;
25
+ export {};
@@ -0,0 +1,29 @@
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 React, { useMemo, useContext } from 'react';
8
+ import { ReactContextError } from '@docusaurus/theme-common';
9
+ // Using a Symbol because null is a valid context value (a doc with no sidebar)
10
+ // Inspired by https://github.com/jamiebuilds/unstated-next/blob/master/src/unstated-next.tsx
11
+ const EmptyContext = Symbol('EmptyContext');
12
+ const Context = React.createContext(EmptyContext);
13
+ /**
14
+ * Provide the current sidebar to your children.
15
+ */
16
+ export function DocsSidebarProvider({ children, name, items, }) {
17
+ const stableValue = useMemo(() => (name && items ? { name, items } : null), [name, items]);
18
+ return <Context value={stableValue}>{children}</Context>;
19
+ }
20
+ /**
21
+ * Gets the sidebar that's currently displayed, or `null` if there isn't one
22
+ */
23
+ export function useDocsSidebar() {
24
+ const value = useContext(Context);
25
+ if (value === EmptyContext) {
26
+ throw new ReactContextError('DocsSidebarProvider');
27
+ }
28
+ return value;
29
+ }
@@ -0,0 +1,112 @@
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 ReactNode } from 'react';
8
+ import { type GlobalVersion, type GlobalSidebar, type GlobalDoc } from '@docusaurus/plugin-content-docs/client';
9
+ import type { Props as DocRootProps } from '@theme/DocRoot';
10
+ import type { PropSidebar, PropSidebarItem, PropSidebarItemCategory, PropVersionDoc, PropSidebarBreadcrumbsItem } from '@docusaurus/plugin-content-docs';
11
+ /**
12
+ * A null-safe way to access a doc's data by ID in the active version.
13
+ */
14
+ export declare function useDocById(id: string): PropVersionDoc;
15
+ /**
16
+ * A null-safe way to access a doc's data by ID in the active version.
17
+ */
18
+ export declare function useDocById(id: string | undefined): PropVersionDoc | undefined;
19
+ /**
20
+ * Pure function, similar to `Array#find`, but works on the sidebar tree.
21
+ */
22
+ export declare function findSidebarCategory(sidebar: PropSidebar, predicate: (category: PropSidebarItemCategory) => boolean): PropSidebarItemCategory | undefined;
23
+ /**
24
+ * Best effort to assign a link to a sidebar category. If the category doesn't
25
+ * have a link itself, we link to the first sub item with a link.
26
+ */
27
+ export declare function findFirstSidebarItemCategoryLink(item: PropSidebarItemCategory): string | undefined;
28
+ /**
29
+ * Best effort to assign a link to a sidebar item.
30
+ */
31
+ export declare function findFirstSidebarItemLink(item: PropSidebarItem): string | undefined;
32
+ /**
33
+ * Gets the category associated with the current location. Should only be used
34
+ * on category index pages.
35
+ */
36
+ export declare function useCurrentSidebarCategory(): PropSidebarItemCategory;
37
+ /**
38
+ * Gets the category associated with the current location. Should only be used
39
+ * on category index pages.
40
+ */
41
+ export declare function useCurrentSidebarSiblings(): PropSidebarItem[];
42
+ /**
43
+ * Checks if a sidebar item should be active, based on the active path.
44
+ */
45
+ export declare function isActiveSidebarItem(item: PropSidebarItem, activePath: string): boolean;
46
+ export declare function isVisibleSidebarItem(item: PropSidebarItem, activePath: string): boolean;
47
+ export declare function useVisibleSidebarItems(items: readonly PropSidebarItem[], activePath: string): PropSidebarItem[];
48
+ /**
49
+ * Gets the breadcrumbs of the current doc page, based on its sidebar location.
50
+ * Returns `null` if there's no sidebar or breadcrumbs are disabled.
51
+ */
52
+ export declare function useSidebarBreadcrumbs(): PropSidebarBreadcrumbsItem[] | null;
53
+ /**
54
+ * "Version candidates" are mostly useful for the layout components, which must
55
+ * be able to work on all pages. For example, if a user has `{ type: "doc",
56
+ * docId: "intro" }` as a navbar item, which version does that refer to? We
57
+ * believe that it could refer to at most three version candidates:
58
+ *
59
+ * 1. The **active version**, the one that the user is currently browsing. See
60
+ * {@link useActiveDocContext}.
61
+ * 2. The **preferred version**, the one that the user last visited. See
62
+ * {@link useDocsPreferredVersion}.
63
+ * 3. The **latest version**, the "default". See {@link useLatestVersion}.
64
+ *
65
+ * @param docsPluginId The plugin ID to get versions from.
66
+ * @returns An array of 1~3 versions with priorities defined above, guaranteed
67
+ * to be unique and non-sparse. Will be memoized, hence stable for deps array.
68
+ */
69
+ export declare function useDocsVersionCandidates(docsPluginId?: string): [GlobalVersion, ...GlobalVersion[]];
70
+ /**
71
+ * The layout components, like navbar items, must be able to work on all pages,
72
+ * even on non-doc ones where there's no version context, so a sidebar ID could
73
+ * be ambiguous. This hook would always return a sidebar to be linked to. See
74
+ * also {@link useDocsVersionCandidates} for how this selection is done.
75
+ *
76
+ * @throws This hook throws if a sidebar with said ID is not found.
77
+ */
78
+ export declare function useLayoutDocsSidebar(sidebarId: string, docsPluginId?: string): GlobalSidebar;
79
+ /**
80
+ * The layout components, like navbar items, must be able to work on all pages,
81
+ * even on non-doc ones where there's no version context, so a doc ID could be
82
+ * ambiguous. This hook would always return a doc to be linked to. See also
83
+ * {@link useDocsVersionCandidates} for how this selection is done.
84
+ *
85
+ * @throws This hook throws if a doc with said ID is not found.
86
+ */
87
+ export declare function useLayoutDoc(docId: string, docsPluginId?: string): GlobalDoc | null;
88
+ /**
89
+ * The docs plugin creates nested routes, with the top-level route providing the
90
+ * version metadata, and the subroutes creating individual doc pages. This hook
91
+ * will match the current location against all known sub-routes.
92
+ *
93
+ * @param props The props received by `@theme/DocRoot`
94
+ * @returns The data of the relevant document at the current location, or `null`
95
+ * if no document associated with the current location can be found.
96
+ */
97
+ export declare function useDocRootMetadata({ route }: DocRootProps): null | {
98
+ /** The element that should be rendered at the current location. */
99
+ docElement: ReactNode;
100
+ /**
101
+ * The name of the sidebar associated with the current doc. `sidebarName` and
102
+ * `sidebarItems` correspond to the value of {@link useDocsSidebar}.
103
+ */
104
+ sidebarName: string | undefined;
105
+ /** The items of the sidebar associated with the current doc. */
106
+ sidebarItems: PropSidebar | undefined;
107
+ };
108
+ /**
109
+ * Filter items we don't want to display on the doc card list view
110
+ * @param items
111
+ */
112
+ export declare function filterDocCardListItems(items: PropSidebarItem[]): PropSidebarItem[];
@@ -0,0 +1,299 @@
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 { useMemo } from 'react';
8
+ import { matchPath, useLocation } from '@docusaurus/router';
9
+ import renderRoutes from '@docusaurus/renderRoutes';
10
+ import { useActivePlugin, useActiveDocContext, useLatestVersion, } from '@docusaurus/plugin-content-docs/client';
11
+ import { isSamePath } from '@docusaurus/theme-common/internal';
12
+ import { uniq } from '@docusaurus/theme-common';
13
+ import { useDocsPreferredVersion } from './docsPreferredVersion';
14
+ import { useDocsVersion } from './docsVersion';
15
+ import { useDocsSidebar } from './docsSidebar';
16
+ export function useDocById(id) {
17
+ const version = useDocsVersion();
18
+ if (!id) {
19
+ return undefined;
20
+ }
21
+ const doc = version.docs[id];
22
+ if (!doc) {
23
+ throw new Error(`no version doc found by id=${id}`);
24
+ }
25
+ return doc;
26
+ }
27
+ /**
28
+ * Pure function, similar to `Array#find`, but works on the sidebar tree.
29
+ */
30
+ export function findSidebarCategory(sidebar, predicate) {
31
+ for (const item of sidebar) {
32
+ if (item.type === 'category') {
33
+ if (predicate(item)) {
34
+ return item;
35
+ }
36
+ const subItem = findSidebarCategory(item.items, predicate);
37
+ if (subItem) {
38
+ return subItem;
39
+ }
40
+ }
41
+ }
42
+ return undefined;
43
+ }
44
+ /**
45
+ * Best effort to assign a link to a sidebar category. If the category doesn't
46
+ * have a link itself, we link to the first sub item with a link.
47
+ */
48
+ export function findFirstSidebarItemCategoryLink(item) {
49
+ if (item.href && !item.linkUnlisted) {
50
+ return item.href;
51
+ }
52
+ for (const subItem of item.items) {
53
+ const link = findFirstSidebarItemLink(subItem);
54
+ if (link) {
55
+ return link;
56
+ }
57
+ }
58
+ return undefined;
59
+ }
60
+ /**
61
+ * Best effort to assign a link to a sidebar item.
62
+ */
63
+ export function findFirstSidebarItemLink(item) {
64
+ if (item.type === 'link' && !item.unlisted) {
65
+ return item.href;
66
+ }
67
+ if (item.type === 'category') {
68
+ return findFirstSidebarItemCategoryLink(item);
69
+ }
70
+ // Other items types, like "html"
71
+ return undefined;
72
+ }
73
+ /**
74
+ * Gets the category associated with the current location. Should only be used
75
+ * on category index pages.
76
+ */
77
+ export function useCurrentSidebarCategory() {
78
+ const { pathname } = useLocation();
79
+ const sidebar = useDocsSidebar();
80
+ if (!sidebar) {
81
+ throw new Error('Unexpected: cant find current sidebar in context');
82
+ }
83
+ const categoryBreadcrumbs = getSidebarBreadcrumbs({
84
+ sidebarItems: sidebar.items,
85
+ pathname,
86
+ onlyCategories: true,
87
+ });
88
+ const deepestCategory = categoryBreadcrumbs.slice(-1)[0];
89
+ if (!deepestCategory) {
90
+ throw new Error(`${pathname} is not associated with a category. useCurrentSidebarCategory() should only be used on category index pages.`);
91
+ }
92
+ return deepestCategory;
93
+ }
94
+ /**
95
+ * Gets the category associated with the current location. Should only be used
96
+ * on category index pages.
97
+ */
98
+ export function useCurrentSidebarSiblings() {
99
+ const { pathname } = useLocation();
100
+ const sidebar = useDocsSidebar();
101
+ if (!sidebar) {
102
+ throw new Error('Unexpected: cant find current sidebar in context');
103
+ }
104
+ const categoryBreadcrumbs = getSidebarBreadcrumbs({
105
+ sidebarItems: sidebar.items,
106
+ pathname,
107
+ onlyCategories: true,
108
+ });
109
+ const deepestCategory = categoryBreadcrumbs.slice(-1)[0];
110
+ return deepestCategory?.items ?? sidebar.items;
111
+ }
112
+ const isActive = (testedPath, activePath) => typeof testedPath !== 'undefined' && isSamePath(testedPath, activePath);
113
+ const containsActiveSidebarItem = (items, activePath) => items.some((subItem) => isActiveSidebarItem(subItem, activePath));
114
+ /**
115
+ * Checks if a sidebar item should be active, based on the active path.
116
+ */
117
+ export function isActiveSidebarItem(item, activePath) {
118
+ if (item.type === 'link') {
119
+ return isActive(item.href, activePath);
120
+ }
121
+ if (item.type === 'category') {
122
+ return (isActive(item.href, activePath) ||
123
+ containsActiveSidebarItem(item.items, activePath));
124
+ }
125
+ return false;
126
+ }
127
+ export function isVisibleSidebarItem(item, activePath) {
128
+ switch (item.type) {
129
+ case 'category':
130
+ return (isActiveSidebarItem(item, activePath) ||
131
+ (typeof item.href !== 'undefined' && !item.linkUnlisted) ||
132
+ item.items.some((subItem) => isVisibleSidebarItem(subItem, activePath)));
133
+ case 'link':
134
+ // An unlisted item remains visible if it is active
135
+ return !item.unlisted || isActiveSidebarItem(item, activePath);
136
+ default:
137
+ return true;
138
+ }
139
+ }
140
+ export function useVisibleSidebarItems(items, activePath) {
141
+ return useMemo(() => items.filter((item) => isVisibleSidebarItem(item, activePath)), [items, activePath]);
142
+ }
143
+ /**
144
+ * Get the sidebar the breadcrumbs for a given pathname
145
+ * Ordered from top to bottom
146
+ */
147
+ function getSidebarBreadcrumbs({ sidebarItems, pathname, onlyCategories = false, }) {
148
+ const breadcrumbs = [];
149
+ function extract(items) {
150
+ for (const item of items) {
151
+ // Extract category item
152
+ if (item.type === 'category') {
153
+ if (isSamePath(item.href, pathname) || extract(item.items)) {
154
+ breadcrumbs.unshift(item);
155
+ return true;
156
+ }
157
+ }
158
+ // Extract doc item
159
+ else if (item.type === 'link' &&
160
+ item.docId &&
161
+ isSamePath(item.href, pathname)) {
162
+ if (!onlyCategories) {
163
+ breadcrumbs.unshift(item);
164
+ }
165
+ return true;
166
+ }
167
+ }
168
+ return false;
169
+ }
170
+ extract(sidebarItems);
171
+ return breadcrumbs;
172
+ }
173
+ /**
174
+ * Gets the breadcrumbs of the current doc page, based on its sidebar location.
175
+ * Returns `null` if there's no sidebar or breadcrumbs are disabled.
176
+ */
177
+ export function useSidebarBreadcrumbs() {
178
+ const sidebar = useDocsSidebar();
179
+ const { pathname } = useLocation();
180
+ const breadcrumbsOption = useActivePlugin()?.pluginData.breadcrumbs;
181
+ if (breadcrumbsOption === false || !sidebar) {
182
+ return null;
183
+ }
184
+ return getSidebarBreadcrumbs({ sidebarItems: sidebar.items, pathname });
185
+ }
186
+ /**
187
+ * "Version candidates" are mostly useful for the layout components, which must
188
+ * be able to work on all pages. For example, if a user has `{ type: "doc",
189
+ * docId: "intro" }` as a navbar item, which version does that refer to? We
190
+ * believe that it could refer to at most three version candidates:
191
+ *
192
+ * 1. The **active version**, the one that the user is currently browsing. See
193
+ * {@link useActiveDocContext}.
194
+ * 2. The **preferred version**, the one that the user last visited. See
195
+ * {@link useDocsPreferredVersion}.
196
+ * 3. The **latest version**, the "default". See {@link useLatestVersion}.
197
+ *
198
+ * @param docsPluginId The plugin ID to get versions from.
199
+ * @returns An array of 1~3 versions with priorities defined above, guaranteed
200
+ * to be unique and non-sparse. Will be memoized, hence stable for deps array.
201
+ */
202
+ export function useDocsVersionCandidates(docsPluginId) {
203
+ const { activeVersion } = useActiveDocContext(docsPluginId);
204
+ const { preferredVersion } = useDocsPreferredVersion(docsPluginId);
205
+ const latestVersion = useLatestVersion(docsPluginId);
206
+ return useMemo(() => uniq([activeVersion, preferredVersion, latestVersion].filter(Boolean)), [activeVersion, preferredVersion, latestVersion]);
207
+ }
208
+ /**
209
+ * The layout components, like navbar items, must be able to work on all pages,
210
+ * even on non-doc ones where there's no version context, so a sidebar ID could
211
+ * be ambiguous. This hook would always return a sidebar to be linked to. See
212
+ * also {@link useDocsVersionCandidates} for how this selection is done.
213
+ *
214
+ * @throws This hook throws if a sidebar with said ID is not found.
215
+ */
216
+ export function useLayoutDocsSidebar(sidebarId, docsPluginId) {
217
+ const versions = useDocsVersionCandidates(docsPluginId);
218
+ return useMemo(() => {
219
+ const allSidebars = versions.flatMap((version) => version.sidebars ? Object.entries(version.sidebars) : []);
220
+ const sidebarEntry = allSidebars.find((sidebar) => sidebar[0] === sidebarId);
221
+ if (!sidebarEntry) {
222
+ throw new Error(`Can't find any sidebar with id "${sidebarId}" in version${versions.length > 1 ? 's' : ''} ${versions.map((version) => version.name).join(', ')}".
223
+ Available sidebar ids are:
224
+ - ${allSidebars.map((entry) => entry[0]).join('\n- ')}`);
225
+ }
226
+ return sidebarEntry[1];
227
+ }, [sidebarId, versions]);
228
+ }
229
+ /**
230
+ * The layout components, like navbar items, must be able to work on all pages,
231
+ * even on non-doc ones where there's no version context, so a doc ID could be
232
+ * ambiguous. This hook would always return a doc to be linked to. See also
233
+ * {@link useDocsVersionCandidates} for how this selection is done.
234
+ *
235
+ * @throws This hook throws if a doc with said ID is not found.
236
+ */
237
+ export function useLayoutDoc(docId, docsPluginId) {
238
+ const versions = useDocsVersionCandidates(docsPluginId);
239
+ return useMemo(() => {
240
+ const allDocs = versions.flatMap((version) => version.docs);
241
+ const doc = allDocs.find((versionDoc) => versionDoc.id === docId);
242
+ if (!doc) {
243
+ const isDraft = versions
244
+ .flatMap((version) => version.draftIds)
245
+ .includes(docId);
246
+ // Drafts should be silently filtered instead of throwing
247
+ if (isDraft) {
248
+ return null;
249
+ }
250
+ throw new Error(`Couldn't find any doc with id "${docId}" in version${versions.length > 1 ? 's' : ''} "${versions.map((version) => version.name).join(', ')}".
251
+ Available doc ids are:
252
+ - ${uniq(allDocs.map((versionDoc) => versionDoc.id)).join('\n- ')}`);
253
+ }
254
+ return doc;
255
+ }, [docId, versions]);
256
+ }
257
+ // TODO later read version/route directly from context
258
+ /**
259
+ * The docs plugin creates nested routes, with the top-level route providing the
260
+ * version metadata, and the subroutes creating individual doc pages. This hook
261
+ * will match the current location against all known sub-routes.
262
+ *
263
+ * @param props The props received by `@theme/DocRoot`
264
+ * @returns The data of the relevant document at the current location, or `null`
265
+ * if no document associated with the current location can be found.
266
+ */
267
+ export function useDocRootMetadata({ route }) {
268
+ const location = useLocation();
269
+ const versionMetadata = useDocsVersion();
270
+ const docRoutes = route.routes;
271
+ const currentDocRoute = docRoutes.find((docRoute) => matchPath(location.pathname, docRoute));
272
+ if (!currentDocRoute) {
273
+ return null;
274
+ }
275
+ // For now, the sidebarName is added as route config: not ideal!
276
+ const sidebarName = currentDocRoute.sidebar;
277
+ const sidebarItems = sidebarName
278
+ ? versionMetadata.docsSidebars[sidebarName]
279
+ : undefined;
280
+ const docElement = renderRoutes(docRoutes);
281
+ return {
282
+ docElement,
283
+ sidebarName,
284
+ sidebarItems,
285
+ };
286
+ }
287
+ /**
288
+ * Filter items we don't want to display on the doc card list view
289
+ * @param items
290
+ */
291
+ export function filterDocCardListItems(items) {
292
+ return items.filter((item) => {
293
+ const canHaveLink = item.type === 'category' || item.type === 'link';
294
+ if (canHaveLink) {
295
+ return !!findFirstSidebarItemLink(item);
296
+ }
297
+ return true;
298
+ });
299
+ }
@@ -0,0 +1,19 @@
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 ReactNode } from 'react';
8
+ import type { PropVersionMetadata } from '@docusaurus/plugin-content-docs';
9
+ /**
10
+ * Provide the current version's metadata to your children.
11
+ */
12
+ export declare function DocsVersionProvider({ children, version, }: {
13
+ children: ReactNode;
14
+ version: PropVersionMetadata | null;
15
+ }): ReactNode;
16
+ /**
17
+ * Gets the version metadata of the current doc page.
18
+ */
19
+ export declare function useDocsVersion(): PropVersionMetadata;
@@ -0,0 +1,25 @@
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 React, { useContext } from 'react';
8
+ import { ReactContextError } from '@docusaurus/theme-common';
9
+ const Context = React.createContext(null);
10
+ /**
11
+ * Provide the current version's metadata to your children.
12
+ */
13
+ export function DocsVersionProvider({ children, version, }) {
14
+ return <Context value={version}>{children}</Context>;
15
+ }
16
+ /**
17
+ * Gets the version metadata of the current doc page.
18
+ */
19
+ export function useDocsVersion() {
20
+ const version = useContext(Context);
21
+ if (version === null) {
22
+ throw new ReactContextError('DocsVersionProvider');
23
+ }
24
+ return version;
25
+ }
@@ -0,0 +1,91 @@
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 { UseDataOptions } from '@docusaurus/types';
8
+ export { useDocById, findSidebarCategory, findFirstSidebarItemLink, isActiveSidebarItem, isVisibleSidebarItem, useVisibleSidebarItems, useSidebarBreadcrumbs, useDocsVersionCandidates, useLayoutDoc, useLayoutDocsSidebar, useDocRootMetadata, useCurrentSidebarCategory, useCurrentSidebarSiblings, filterDocCardListItems, } from './docsUtils';
9
+ export { useDocsPreferredVersion } from './docsPreferredVersion';
10
+ export { DocSidebarItemsExpandedStateProvider, useDocSidebarItemsExpandedState, } from './docSidebarItemsExpandedState';
11
+ export { DocsVersionProvider, useDocsVersion } from './docsVersion';
12
+ export { DocsSidebarProvider, useDocsSidebar } from './docsSidebar';
13
+ export { DocProvider, useDoc, type DocContextValue } from './doc';
14
+ export { useDocsPreferredVersionByPluginId, DocsPreferredVersionContextProvider, } from './docsPreferredVersion';
15
+ export { useDocsContextualSearchTags, getDocsVersionSearchTag, } from './docsSearch';
16
+ export { useBreadcrumbsStructuredData } from './structuredDataUtils';
17
+ export type ActivePlugin = {
18
+ pluginId: string;
19
+ pluginData: GlobalPluginData;
20
+ };
21
+ export type ActiveDocContext = {
22
+ activeVersion?: GlobalVersion;
23
+ activeDoc?: GlobalDoc;
24
+ alternateDocVersions: {
25
+ [versionName: string]: GlobalDoc;
26
+ };
27
+ };
28
+ export type GlobalDoc = {
29
+ /**
30
+ * For generated index pages, this is the `slug`, **not** `permalink`
31
+ * (without base URL). Because slugs have leading slashes but IDs don't,
32
+ * there won't be clashes.
33
+ */
34
+ id: string;
35
+ path: string;
36
+ sidebar?: string;
37
+ unlisted?: boolean;
38
+ };
39
+ export type GlobalVersion = {
40
+ name: string;
41
+ label: string;
42
+ isLast: boolean;
43
+ path: string;
44
+ /** The doc with `slug: /`, or first doc in first sidebar */
45
+ mainDocId: string;
46
+ docs: GlobalDoc[];
47
+ /** Unversioned IDs. In development, this list is empty. */
48
+ draftIds: string[];
49
+ sidebars?: {
50
+ [sidebarId: string]: GlobalSidebar;
51
+ };
52
+ };
53
+ export type GlobalSidebar = {
54
+ link?: {
55
+ label: string;
56
+ path: string;
57
+ };
58
+ };
59
+ export type GlobalPluginData = {
60
+ path: string;
61
+ versions: GlobalVersion[];
62
+ breadcrumbs: boolean;
63
+ };
64
+ export type DocVersionSuggestions = {
65
+ /** Suggest the latest version */
66
+ latestVersionSuggestion: GlobalVersion;
67
+ /** Suggest the same doc, in latest version (if one exists) */
68
+ latestDocSuggestion?: GlobalDoc;
69
+ };
70
+ export declare const useAllDocsData: () => {
71
+ [pluginId: string]: GlobalPluginData;
72
+ };
73
+ export declare const useDocsData: (pluginId: string | undefined) => GlobalPluginData;
74
+ export declare function useActivePlugin(options?: UseDataOptions): ActivePlugin | undefined;
75
+ export declare function useActivePluginAndVersion(options?: UseDataOptions): {
76
+ activePlugin: ActivePlugin;
77
+ activeVersion: GlobalVersion | undefined;
78
+ } | undefined;
79
+ /** Versions are returned ordered (most recent first). */
80
+ export declare function useVersions(pluginId: string | undefined): GlobalVersion[];
81
+ export declare function useLatestVersion(pluginId: string | undefined): GlobalVersion;
82
+ /**
83
+ * Returns `undefined` on doc-unrelated pages, because there's no version
84
+ * currently considered as active.
85
+ */
86
+ export declare function useActiveVersion(pluginId: string | undefined): GlobalVersion | undefined;
87
+ export declare function useActiveDocContext(pluginId: string | undefined): ActiveDocContext;
88
+ /**
89
+ * Useful to say "hey, you are not on the latest docs version, please switch"
90
+ */
91
+ export declare function useDocVersionSuggestions(pluginId: string | undefined): DocVersionSuggestions;