@docusaurus/theme-common 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/components/Collapsible/index.d.ts +36 -0
- package/lib/components/Collapsible/index.d.ts.map +1 -0
- package/lib/components/Collapsible/index.js +143 -0
- package/lib/components/Collapsible/index.js.map +1 -0
- package/lib/components/Details/index.d.ts +12 -0
- package/lib/components/Details/index.d.ts.map +1 -0
- package/lib/components/Details/index.js +66 -0
- package/lib/components/Details/index.js.map +1 -0
- package/lib/components/Details/styles.module.css +58 -0
- package/{src/utils/docsUtils.ts → lib/hooks/styles.css} +3 -4
- package/lib/hooks/useHideableNavbar.d.ts +13 -0
- package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
- package/lib/hooks/useHideableNavbar.js +59 -0
- package/lib/hooks/useHideableNavbar.js.map +1 -0
- package/lib/hooks/useKeyboardNavigation.d.ts +10 -0
- package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -0
- package/lib/hooks/useKeyboardNavigation.js +31 -0
- package/lib/hooks/useKeyboardNavigation.js.map +1 -0
- package/lib/hooks/useLockBodyScroll.d.ts +8 -0
- package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
- package/lib/hooks/useLockBodyScroll.js +16 -0
- package/lib/hooks/useLockBodyScroll.js.map +1 -0
- package/lib/hooks/usePrismTheme.d.ts +9 -0
- package/lib/hooks/usePrismTheme.d.ts.map +1 -0
- package/lib/hooks/usePrismTheme.js +18 -0
- package/lib/hooks/usePrismTheme.js.map +1 -0
- package/lib/hooks/useSearchPage.d.ts +14 -0
- package/lib/hooks/useSearchPage.d.ts.map +1 -0
- package/lib/hooks/useSearchPage.js +42 -0
- package/lib/hooks/useSearchPage.js.map +1 -0
- package/lib/hooks/useWindowSize.d.ts +15 -0
- package/lib/hooks/useWindowSize.d.ts.map +1 -0
- package/lib/hooks/useWindowSize.js +56 -0
- package/lib/hooks/useWindowSize.js.map +1 -0
- package/lib/index.d.ts +32 -3
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +26 -2
- package/lib/index.js.map +1 -0
- package/lib/utils/ThemeClassNames.d.ts +40 -12
- package/lib/utils/ThemeClassNames.d.ts.map +1 -0
- package/lib/utils/ThemeClassNames.js +41 -3
- package/lib/utils/ThemeClassNames.js.map +1 -0
- package/lib/utils/announcementBarUtils.d.ts +6 -5
- package/lib/utils/announcementBarUtils.d.ts.map +1 -0
- package/lib/utils/announcementBarUtils.js +20 -21
- package/lib/utils/announcementBarUtils.js.map +1 -0
- package/lib/utils/codeBlockUtils.d.ts +10 -0
- package/lib/utils/codeBlockUtils.d.ts.map +1 -0
- package/lib/utils/codeBlockUtils.js +125 -3
- package/lib/utils/codeBlockUtils.js.map +1 -0
- package/lib/utils/colorModeUtils.d.ts +18 -0
- package/lib/utils/colorModeUtils.d.ts.map +1 -0
- package/lib/utils/colorModeUtils.js +107 -0
- package/lib/utils/colorModeUtils.js.map +1 -0
- package/lib/utils/docSidebarItemsExpandedState.d.ts +17 -0
- package/lib/utils/docSidebarItemsExpandedState.d.ts.map +1 -0
- package/lib/utils/docSidebarItemsExpandedState.js +23 -0
- package/lib/utils/docSidebarItemsExpandedState.js.map +1 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +3 -2
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +1 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +9 -11
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +1 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +2 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +1 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +2 -3
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +1 -0
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +12 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +1 -0
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +3 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +1 -0
- package/lib/utils/docsUtils.d.ts +26 -0
- package/lib/utils/docsUtils.d.ts.map +1 -0
- package/lib/utils/docsUtils.js +136 -1
- package/lib/utils/docsUtils.js.map +1 -0
- package/lib/utils/generalUtils.d.ts +7 -0
- package/lib/utils/generalUtils.d.ts.map +1 -0
- package/lib/utils/generalUtils.js +3 -2
- package/lib/utils/generalUtils.js.map +1 -0
- package/lib/utils/historyUtils.d.ts +23 -0
- package/lib/utils/historyUtils.d.ts.map +1 -0
- package/lib/utils/historyUtils.js +41 -0
- package/lib/utils/historyUtils.js.map +1 -0
- package/lib/utils/jsUtils.d.ts +23 -0
- package/lib/utils/jsUtils.d.ts.map +1 -0
- package/lib/utils/jsUtils.js +29 -0
- package/lib/utils/jsUtils.js.map +1 -0
- package/lib/utils/mobileSecondaryMenu.d.ts +21 -0
- package/lib/utils/mobileSecondaryMenu.d.ts.map +1 -0
- package/lib/utils/mobileSecondaryMenu.js +51 -0
- package/lib/utils/mobileSecondaryMenu.js.map +1 -0
- package/lib/utils/pathUtils.d.ts +1 -0
- package/lib/utils/pathUtils.d.ts.map +1 -0
- package/lib/utils/pathUtils.js +5 -4
- package/lib/utils/pathUtils.js.map +1 -0
- package/lib/utils/reactUtils.d.ts +31 -0
- package/lib/utils/reactUtils.d.ts.map +1 -0
- package/lib/utils/reactUtils.js +43 -0
- package/lib/utils/reactUtils.js.map +1 -0
- package/lib/utils/regexpUtils.d.ts +11 -0
- package/lib/utils/regexpUtils.d.ts.map +1 -0
- package/lib/utils/regexpUtils.js +17 -0
- package/lib/utils/regexpUtils.js.map +1 -0
- package/lib/utils/routesUtils.d.ts +14 -0
- package/lib/utils/routesUtils.d.ts.map +1 -0
- package/lib/utils/routesUtils.js +41 -0
- package/lib/utils/routesUtils.js.map +1 -0
- package/lib/utils/scrollUtils.d.ts +53 -0
- package/lib/utils/scrollUtils.d.ts.map +1 -0
- package/lib/utils/scrollUtils.js +135 -0
- package/lib/utils/scrollUtils.js.map +1 -0
- package/lib/utils/searchUtils.d.ts +1 -0
- package/lib/utils/searchUtils.d.ts.map +1 -0
- package/lib/utils/searchUtils.js +1 -0
- package/lib/utils/searchUtils.js.map +1 -0
- package/lib/utils/storageUtils.d.ts +5 -0
- package/lib/utils/storageUtils.d.ts.map +1 -0
- package/lib/utils/storageUtils.js +39 -14
- package/lib/utils/storageUtils.js.map +1 -0
- package/lib/utils/tabGroupChoiceUtils.d.ts +19 -0
- package/lib/utils/tabGroupChoiceUtils.d.ts.map +1 -0
- package/lib/utils/tabGroupChoiceUtils.js +55 -0
- package/lib/utils/tabGroupChoiceUtils.js.map +1 -0
- package/lib/utils/tagsUtils.d.ts +19 -0
- package/lib/utils/tagsUtils.d.ts.map +1 -0
- package/lib/utils/tagsUtils.js +33 -0
- package/lib/utils/tagsUtils.js.map +1 -0
- package/lib/utils/tocUtils.d.ts +20 -0
- package/lib/utils/tocUtils.d.ts.map +1 -0
- package/lib/utils/tocUtils.js +73 -0
- package/lib/utils/tocUtils.js.map +1 -0
- package/lib/utils/useAlternatePageUtils.d.ts +1 -0
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
- package/lib/utils/useAlternatePageUtils.js +3 -1
- package/lib/utils/useAlternatePageUtils.js.map +1 -0
- package/lib/utils/useContextualSearchFilters.d.ts +12 -0
- package/lib/utils/useContextualSearchFilters.d.ts.map +1 -0
- package/lib/utils/useContextualSearchFilters.js +36 -0
- package/lib/utils/useContextualSearchFilters.js.map +1 -0
- package/lib/utils/useLocalPathname.d.ts +8 -0
- package/lib/utils/useLocalPathname.d.ts.map +1 -0
- package/lib/utils/useLocalPathname.js +17 -0
- package/lib/utils/useLocalPathname.js.map +1 -0
- package/lib/utils/useLocationChange.d.ts +2 -1
- package/lib/utils/useLocationChange.d.ts.map +1 -0
- package/lib/utils/useLocationChange.js +12 -10
- package/lib/utils/useLocationChange.js.map +1 -0
- package/lib/utils/usePluralForm.d.ts +1 -0
- package/lib/utils/usePluralForm.d.ts.map +1 -0
- package/lib/utils/usePluralForm.js +28 -24
- package/lib/utils/usePluralForm.js.map +1 -0
- package/lib/utils/usePrevious.d.ts +1 -0
- package/lib/utils/usePrevious.d.ts.map +1 -0
- package/lib/utils/usePrevious.js +4 -2
- package/lib/utils/usePrevious.js.map +1 -0
- package/lib/utils/useTOCHighlight.d.ts +14 -0
- package/lib/utils/useTOCHighlight.d.ts.map +1 -0
- package/lib/utils/useTOCHighlight.js +126 -0
- package/lib/utils/useTOCHighlight.js.map +1 -0
- package/lib/utils/useThemeConfig.d.ts +35 -17
- package/lib/utils/useThemeConfig.d.ts.map +1 -0
- package/lib/utils/useThemeConfig.js +1 -0
- package/lib/utils/useThemeConfig.js.map +1 -0
- package/package.json +19 -13
- package/src/components/Collapsible/index.tsx +247 -0
- package/src/components/Details/index.tsx +102 -0
- package/src/components/Details/styles.module.css +58 -0
- package/src/hooks/styles.css +10 -0
- package/src/hooks/useHideableNavbar.ts +77 -0
- package/src/hooks/useKeyboardNavigation.ts +37 -0
- package/src/hooks/useLockBodyScroll.ts +18 -0
- package/src/hooks/usePrismTheme.ts +20 -0
- package/src/hooks/useSearchPage.ts +66 -0
- package/src/hooks/useWindowSize.ts +70 -0
- package/src/index.ts +95 -3
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +46 -4
- package/src/utils/announcementBarUtils.tsx +27 -22
- package/src/utils/codeBlockUtils.ts +153 -2
- package/src/utils/colorModeUtils.tsx +158 -0
- package/src/utils/docSidebarItemsExpandedState.tsx +40 -0
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +17 -16
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +3 -4
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +18 -14
- package/src/utils/docsUtils.tsx +231 -0
- package/src/utils/generalUtils.ts +3 -2
- package/src/utils/historyUtils.ts +51 -0
- package/src/utils/jsUtils.ts +36 -0
- package/src/utils/mobileSecondaryMenu.tsx +114 -0
- package/src/utils/pathUtils.ts +6 -4
- package/src/utils/reactUtils.tsx +53 -0
- package/src/utils/regexpUtils.ts +23 -0
- package/src/utils/routesUtils.ts +56 -0
- package/src/utils/scrollUtils.tsx +235 -0
- package/src/utils/storageUtils.ts +37 -12
- package/src/utils/tabGroupChoiceUtils.tsx +89 -0
- package/src/utils/tagsUtils.ts +48 -0
- package/src/utils/tocUtils.ts +108 -0
- package/src/utils/useAlternatePageUtils.ts +4 -3
- package/src/utils/useContextualSearchFilters.ts +53 -0
- package/src/utils/useLocalPathname.ts +20 -0
- package/src/utils/useLocationChange.ts +13 -11
- package/src/utils/usePluralForm.ts +31 -26
- package/src/utils/usePrevious.ts +3 -2
- package/src/utils/useTOCHighlight.ts +183 -0
- package/src/utils/useThemeConfig.ts +37 -17
- package/lib/.tsbuildinfo +0 -1
- package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
- package/src/utils/__tests__/pathUtils.test.ts +0 -32
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,231 @@
|
|
|
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, {createContext, type ReactNode, useContext} from 'react';
|
|
9
|
+
import {
|
|
10
|
+
useActivePlugin,
|
|
11
|
+
useAllDocsData,
|
|
12
|
+
} from '@docusaurus/plugin-content-docs/client';
|
|
13
|
+
import type {
|
|
14
|
+
PropSidebar,
|
|
15
|
+
PropSidebarItem,
|
|
16
|
+
PropSidebarItemCategory,
|
|
17
|
+
PropVersionDoc,
|
|
18
|
+
PropVersionMetadata,
|
|
19
|
+
PropSidebarBreadcrumbsItem,
|
|
20
|
+
} from '@docusaurus/plugin-content-docs';
|
|
21
|
+
import {isSamePath} from './pathUtils';
|
|
22
|
+
import {ReactContextError} from './reactUtils';
|
|
23
|
+
import {useLocation} from '@docusaurus/router';
|
|
24
|
+
|
|
25
|
+
// TODO not ideal, see also "useDocs"
|
|
26
|
+
export const isDocsPluginEnabled: boolean = !!useAllDocsData;
|
|
27
|
+
|
|
28
|
+
// Using a Symbol because null is a valid context value (a doc with no sidebar)
|
|
29
|
+
// Inspired by https://github.com/jamiebuilds/unstated-next/blob/master/src/unstated-next.tsx
|
|
30
|
+
const EmptyContextValue: unique symbol = Symbol('EmptyContext');
|
|
31
|
+
|
|
32
|
+
const DocsVersionContext = createContext<
|
|
33
|
+
PropVersionMetadata | typeof EmptyContextValue
|
|
34
|
+
>(EmptyContextValue);
|
|
35
|
+
|
|
36
|
+
export function DocsVersionProvider({
|
|
37
|
+
children,
|
|
38
|
+
version,
|
|
39
|
+
}: {
|
|
40
|
+
children: ReactNode;
|
|
41
|
+
version: PropVersionMetadata | typeof EmptyContextValue;
|
|
42
|
+
}): JSX.Element {
|
|
43
|
+
return (
|
|
44
|
+
<DocsVersionContext.Provider value={version}>
|
|
45
|
+
{children}
|
|
46
|
+
</DocsVersionContext.Provider>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function useDocsVersion(): PropVersionMetadata {
|
|
51
|
+
const version = useContext(DocsVersionContext);
|
|
52
|
+
if (version === EmptyContextValue) {
|
|
53
|
+
throw new ReactContextError('DocsVersionProvider');
|
|
54
|
+
}
|
|
55
|
+
return version;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function useDocById(id: string): PropVersionDoc;
|
|
59
|
+
export function useDocById(id: string | undefined): PropVersionDoc | undefined;
|
|
60
|
+
export function useDocById(id: string | undefined): PropVersionDoc | undefined {
|
|
61
|
+
const version = useDocsVersion();
|
|
62
|
+
if (!id) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const doc = version.docs[id];
|
|
66
|
+
if (!doc) {
|
|
67
|
+
throw new Error(`no version doc found by id=${id}`);
|
|
68
|
+
}
|
|
69
|
+
return doc;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const DocsSidebarContext = createContext<
|
|
73
|
+
PropSidebar | null | typeof EmptyContextValue
|
|
74
|
+
>(EmptyContextValue);
|
|
75
|
+
|
|
76
|
+
export function DocsSidebarProvider({
|
|
77
|
+
children,
|
|
78
|
+
sidebar,
|
|
79
|
+
}: {
|
|
80
|
+
children: ReactNode;
|
|
81
|
+
sidebar: PropSidebar | null;
|
|
82
|
+
}): JSX.Element {
|
|
83
|
+
return (
|
|
84
|
+
<DocsSidebarContext.Provider value={sidebar}>
|
|
85
|
+
{children}
|
|
86
|
+
</DocsSidebarContext.Provider>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function useDocsSidebar(): PropSidebar | null {
|
|
91
|
+
const sidebar = useContext(DocsSidebarContext);
|
|
92
|
+
if (sidebar === EmptyContextValue) {
|
|
93
|
+
throw new ReactContextError('DocsSidebarProvider');
|
|
94
|
+
}
|
|
95
|
+
return sidebar;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Use the components props and the sidebar in context
|
|
99
|
+
// to get back the related sidebar category that we want to render
|
|
100
|
+
export function findSidebarCategory(
|
|
101
|
+
sidebar: PropSidebar,
|
|
102
|
+
predicate: (category: PropSidebarItemCategory) => boolean,
|
|
103
|
+
): PropSidebarItemCategory | undefined {
|
|
104
|
+
for (const item of sidebar) {
|
|
105
|
+
if (item.type === 'category') {
|
|
106
|
+
if (predicate(item)) {
|
|
107
|
+
return item;
|
|
108
|
+
}
|
|
109
|
+
const subItem = findSidebarCategory(item.items, predicate);
|
|
110
|
+
if (subItem) {
|
|
111
|
+
return subItem;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// If a category card has no link => link to the first subItem having a link
|
|
119
|
+
export function findFirstCategoryLink(
|
|
120
|
+
item: PropSidebarItemCategory,
|
|
121
|
+
): string | undefined {
|
|
122
|
+
if (item.href) {
|
|
123
|
+
return item.href;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
for (const subItem of item.items) {
|
|
127
|
+
if (subItem.type === 'link') {
|
|
128
|
+
return subItem.href;
|
|
129
|
+
} else if (subItem.type === 'category') {
|
|
130
|
+
const categoryLink = findFirstCategoryLink(subItem);
|
|
131
|
+
if (categoryLink) {
|
|
132
|
+
return categoryLink;
|
|
133
|
+
}
|
|
134
|
+
} else if (subItem.type === 'html') {
|
|
135
|
+
// skip
|
|
136
|
+
} else {
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Unexpected category item type for ${JSON.stringify(subItem)}`,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function useCurrentSidebarCategory(): PropSidebarItemCategory {
|
|
146
|
+
const {pathname} = useLocation();
|
|
147
|
+
const sidebar = useDocsSidebar();
|
|
148
|
+
if (!sidebar) {
|
|
149
|
+
throw new Error('Unexpected: cant find current sidebar in context');
|
|
150
|
+
}
|
|
151
|
+
const category = findSidebarCategory(sidebar, (item) =>
|
|
152
|
+
isSamePath(item.href, pathname),
|
|
153
|
+
);
|
|
154
|
+
if (!category) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`Unexpected: sidebar category could not be found for pathname='${pathname}'.
|
|
157
|
+
Hook useCurrentSidebarCategory() should only be used on Category pages`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return category;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function containsActiveSidebarItem(
|
|
164
|
+
items: PropSidebarItem[],
|
|
165
|
+
activePath: string,
|
|
166
|
+
): boolean {
|
|
167
|
+
return items.some((subItem) => isActiveSidebarItem(subItem, activePath));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function isActiveSidebarItem(
|
|
171
|
+
item: PropSidebarItem,
|
|
172
|
+
activePath: string,
|
|
173
|
+
): boolean {
|
|
174
|
+
const isActive = (testedPath: string | undefined) =>
|
|
175
|
+
typeof testedPath !== 'undefined' && isSamePath(testedPath, activePath);
|
|
176
|
+
|
|
177
|
+
if (item.type === 'link') {
|
|
178
|
+
return isActive(item.href);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (item.type === 'category') {
|
|
182
|
+
return (
|
|
183
|
+
isActive(item.href) || containsActiveSidebarItem(item.items, activePath)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function getBreadcrumbs({
|
|
191
|
+
sidebar,
|
|
192
|
+
pathname,
|
|
193
|
+
}: {
|
|
194
|
+
sidebar: PropSidebar;
|
|
195
|
+
pathname: string;
|
|
196
|
+
}): PropSidebarBreadcrumbsItem[] {
|
|
197
|
+
const breadcrumbs: PropSidebarBreadcrumbsItem[] = [];
|
|
198
|
+
|
|
199
|
+
function extract(items: PropSidebar) {
|
|
200
|
+
for (const item of items) {
|
|
201
|
+
if (
|
|
202
|
+
item.type === 'category' &&
|
|
203
|
+
(isSamePath(item.href, pathname) || extract(item.items))
|
|
204
|
+
) {
|
|
205
|
+
breadcrumbs.push(item);
|
|
206
|
+
return true;
|
|
207
|
+
} else if (item.type === 'link' && isSamePath(item.href, pathname)) {
|
|
208
|
+
breadcrumbs.push(item);
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
extract(sidebar);
|
|
217
|
+
|
|
218
|
+
return breadcrumbs.reverse();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function useSidebarBreadcrumbs(): PropSidebarBreadcrumbsItem[] | null {
|
|
222
|
+
const sidebar = useDocsSidebar();
|
|
223
|
+
const {pathname} = useLocation();
|
|
224
|
+
const breadcrumbsOption = useActivePlugin()?.pluginData.breadcrumbs;
|
|
225
|
+
|
|
226
|
+
if (breadcrumbsOption === false || !sidebar) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return getBreadcrumbs({sidebar, pathname});
|
|
231
|
+
}
|
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
|
|
7
8
|
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
8
9
|
|
|
9
10
|
export const useTitleFormatter = (title?: string | undefined): string => {
|
|
10
|
-
const {siteConfig
|
|
11
|
-
const {title: siteTitle, titleDelimiter
|
|
11
|
+
const {siteConfig} = useDocusaurusContext();
|
|
12
|
+
const {title: siteTitle, titleDelimiter} = siteConfig;
|
|
12
13
|
return title && title.trim().length
|
|
13
14
|
? `${title.trim()} ${titleDelimiter} ${siteTitle}`
|
|
14
15
|
: siteTitle;
|
|
@@ -0,0 +1,51 @@
|
|
|
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 {useEffect, useRef} from 'react';
|
|
9
|
+
import {useHistory} from '@docusaurus/router';
|
|
10
|
+
import type {Location, Action} from 'history';
|
|
11
|
+
|
|
12
|
+
type HistoryBlockHandler = (location: Location, action: Action) => void | false;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Permits to register a handler that will be called on history actions (pop,
|
|
16
|
+
* push, replace) If the handler returns false, the navigation transition will
|
|
17
|
+
* be blocked/cancelled
|
|
18
|
+
*/
|
|
19
|
+
export function useHistoryActionHandler(handler: HistoryBlockHandler): void {
|
|
20
|
+
const {block} = useHistory();
|
|
21
|
+
|
|
22
|
+
// Avoid stale closure issues without triggering useless re-renders
|
|
23
|
+
const lastHandlerRef = useRef(handler);
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
lastHandlerRef.current = handler;
|
|
26
|
+
}, [handler]);
|
|
27
|
+
|
|
28
|
+
useEffect(
|
|
29
|
+
() =>
|
|
30
|
+
// See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
|
|
31
|
+
block((location, action) => lastHandlerRef.current(location, action)),
|
|
32
|
+
[block, lastHandlerRef],
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Permits to register a handler that will be called on history pop navigation
|
|
38
|
+
* (backward/forward) If the handler returns false, the backward/forward
|
|
39
|
+
* transition will be blocked. Unfortunately there's no good way to detect the
|
|
40
|
+
* "direction" (backward/forward) of the POP event.
|
|
41
|
+
*/
|
|
42
|
+
export function useHistoryPopHandler(handler: HistoryBlockHandler): void {
|
|
43
|
+
useHistoryActionHandler((location, action) => {
|
|
44
|
+
if (action === 'POP') {
|
|
45
|
+
// Eventually block navigation if handler returns false
|
|
46
|
+
return handler(location, action);
|
|
47
|
+
}
|
|
48
|
+
// Don't block other navigation actions
|
|
49
|
+
return undefined;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -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
|
+
// A replacement of lodash in client code
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Gets the duplicate values in an array.
|
|
12
|
+
* @param arr The array.
|
|
13
|
+
* @param comparator Compares two values and returns `true` if they are equal
|
|
14
|
+
* (duplicated).
|
|
15
|
+
* @returns Value of the elements `v` that have a preceding element `u` where
|
|
16
|
+
* `comparator(u, v) === true`. Values within the returned array are not
|
|
17
|
+
* guaranteed to be unique.
|
|
18
|
+
*/
|
|
19
|
+
export function duplicates<T>(
|
|
20
|
+
arr: readonly T[],
|
|
21
|
+
comparator: (a: T, b: T) => boolean = (a, b) => a === b,
|
|
22
|
+
): T[] {
|
|
23
|
+
return arr.filter(
|
|
24
|
+
(v, vIndex) => arr.findIndex((u) => comparator(u, v)) !== vIndex,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Remove duplicate array items (similar to _.uniq)
|
|
30
|
+
* @param arr The array.
|
|
31
|
+
* @returns An array with duplicate elements removed by reference comparison.
|
|
32
|
+
*/
|
|
33
|
+
export function uniq<T>(arr: T[]): T[] {
|
|
34
|
+
// Note: had problems with [...new Set()]: https://github.com/facebook/docusaurus/issues/4972#issuecomment-863895061
|
|
35
|
+
return Array.from(new Set(arr));
|
|
36
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
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, {
|
|
9
|
+
useState,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
useContext,
|
|
12
|
+
createContext,
|
|
13
|
+
useEffect,
|
|
14
|
+
type ComponentType,
|
|
15
|
+
useMemo,
|
|
16
|
+
} from 'react';
|
|
17
|
+
import {ReactContextError} from './reactUtils';
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
The idea behind all this is that a specific component must be able to fill a
|
|
21
|
+
placeholder in the generic layout. The doc page should be able to fill the
|
|
22
|
+
secondary menu of the main mobile navbar. This permits to reduce coupling
|
|
23
|
+
between the main layout and the specific page.
|
|
24
|
+
|
|
25
|
+
This kind of feature is often called portal/teleport/gateway... various
|
|
26
|
+
unmaintained React libs exist. Most up-to-date one: https://github.com/gregberge/react-teleporter
|
|
27
|
+
Not sure any of those is safe regarding concurrent mode.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
type ExtraProps = {
|
|
31
|
+
toggleSidebar: () => void;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type MobileSecondaryMenuComponent<Props> = ComponentType<
|
|
35
|
+
Props & ExtraProps
|
|
36
|
+
>;
|
|
37
|
+
|
|
38
|
+
type State = {
|
|
39
|
+
component: MobileSecondaryMenuComponent<unknown>;
|
|
40
|
+
props: unknown;
|
|
41
|
+
} | null;
|
|
42
|
+
|
|
43
|
+
function useContextValue() {
|
|
44
|
+
return useState<State>(null);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type ContextValue = ReturnType<typeof useContextValue>;
|
|
48
|
+
|
|
49
|
+
const Context = createContext<ContextValue | null>(null);
|
|
50
|
+
|
|
51
|
+
export function MobileSecondaryMenuProvider({
|
|
52
|
+
children,
|
|
53
|
+
}: {
|
|
54
|
+
children: ReactNode;
|
|
55
|
+
}): JSX.Element {
|
|
56
|
+
return (
|
|
57
|
+
<Context.Provider value={useContextValue()}>{children}</Context.Provider>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function useMobileSecondaryMenuContext(): ContextValue {
|
|
62
|
+
const value = useContext(Context);
|
|
63
|
+
if (value === null) {
|
|
64
|
+
throw new ReactContextError('MobileSecondaryMenuProvider');
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function useMobileSecondaryMenuRenderer(): (
|
|
70
|
+
extraProps: ExtraProps,
|
|
71
|
+
) => ReactNode | undefined {
|
|
72
|
+
const [state] = useMobileSecondaryMenuContext();
|
|
73
|
+
if (state) {
|
|
74
|
+
const Comp = state.component;
|
|
75
|
+
return function render(extraProps) {
|
|
76
|
+
return <Comp {...state.props} {...extraProps} />;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return () => undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function useShallowMemoizedObject<O extends Record<string, unknown>>(obj: O) {
|
|
83
|
+
return useMemo(
|
|
84
|
+
() => obj,
|
|
85
|
+
// Is this safe?
|
|
86
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
87
|
+
[...Object.keys(obj), ...Object.values(obj)],
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Fill the secondary menu placeholder with some real content
|
|
92
|
+
export function MobileSecondaryMenuFiller<
|
|
93
|
+
Props extends Record<string, unknown>,
|
|
94
|
+
>({
|
|
95
|
+
component,
|
|
96
|
+
props,
|
|
97
|
+
}: {
|
|
98
|
+
component: MobileSecondaryMenuComponent<Props & ExtraProps>;
|
|
99
|
+
props: Props;
|
|
100
|
+
}): JSX.Element | null {
|
|
101
|
+
const [, setState] = useMobileSecondaryMenuContext();
|
|
102
|
+
|
|
103
|
+
// To avoid useless context re-renders, props are memoized shallowly
|
|
104
|
+
const memoizedProps = useShallowMemoizedObject(props);
|
|
105
|
+
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
// @ts-expect-error: context is not 100% type-safe but it's ok
|
|
108
|
+
setState({component, props: memoizedProps});
|
|
109
|
+
}, [setState, component, memoizedProps]);
|
|
110
|
+
|
|
111
|
+
useEffect(() => () => setState(null), [setState]);
|
|
112
|
+
|
|
113
|
+
return null;
|
|
114
|
+
}
|
package/src/utils/pathUtils.ts
CHANGED
|
@@ -5,13 +5,15 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
// Compare the 2 paths, ignoring trailing
|
|
8
|
+
// Compare the 2 paths, case insensitive and ignoring trailing slash
|
|
9
9
|
export const isSamePath = (
|
|
10
10
|
path1: string | undefined,
|
|
11
11
|
path2: string | undefined,
|
|
12
12
|
): boolean => {
|
|
13
|
-
const normalize = (pathname: string | undefined) =>
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
const normalize = (pathname: string | undefined) =>
|
|
14
|
+
(!pathname || pathname?.endsWith('/')
|
|
15
|
+
? pathname
|
|
16
|
+
: `${pathname}/`
|
|
17
|
+
)?.toLowerCase();
|
|
16
18
|
return normalize(path1) === normalize(path2);
|
|
17
19
|
};
|
|
@@ -0,0 +1,53 @@
|
|
|
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 {useCallback, useEffect, useLayoutEffect, useRef} from 'react';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* This hook is like useLayoutEffect, but without the SSR warning
|
|
12
|
+
* It seems hacky but it's used in many React libs (Redux, Formik...)
|
|
13
|
+
* Also mentioned here: https://github.com/facebook/react/issues/16956
|
|
14
|
+
* It is useful when you need to update a ref as soon as possible after a React
|
|
15
|
+
* render (before `useEffect`)
|
|
16
|
+
*/
|
|
17
|
+
export const useIsomorphicLayoutEffect =
|
|
18
|
+
typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Permits to transform an unstable callback (like an arrow function provided as
|
|
22
|
+
* props) to a "stable" callback that is safe to use in a useEffect dependency
|
|
23
|
+
* array. Useful to avoid React stale closure problems + avoid useless effect
|
|
24
|
+
* re-executions
|
|
25
|
+
*
|
|
26
|
+
* Workaround until the React team recommends a good solution, see
|
|
27
|
+
* https://github.com/facebook/react/issues/16956
|
|
28
|
+
* This generally works but has some potential drawbacks, such as
|
|
29
|
+
* https://github.com/facebook/react/issues/16956#issuecomment-536636418
|
|
30
|
+
*/
|
|
31
|
+
export function useDynamicCallback<T extends (...args: never[]) => unknown>(
|
|
32
|
+
callback: T,
|
|
33
|
+
): T {
|
|
34
|
+
const ref = useRef<T>(callback);
|
|
35
|
+
|
|
36
|
+
useIsomorphicLayoutEffect(() => {
|
|
37
|
+
ref.current = callback;
|
|
38
|
+
}, [callback]);
|
|
39
|
+
|
|
40
|
+
// @ts-expect-error: TS is right that this callback may be a supertype of T,
|
|
41
|
+
// but good enough for our use
|
|
42
|
+
return useCallback<T>((...args) => ref.current(...args), []);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class ReactContextError extends Error {
|
|
46
|
+
constructor(providerName: string, additionalInfo?: string) {
|
|
47
|
+
super();
|
|
48
|
+
this.name = 'ReactContextError';
|
|
49
|
+
this.message = `Hook ${
|
|
50
|
+
this.stack?.split('\n')[1]?.match(/at (?<name>\w+)/)?.groups!.name
|
|
51
|
+
} is called outside the <${providerName}>. ${additionalInfo || ''}`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
/**
|
|
9
|
+
* Converts an optional string into a Regex case insensitive and global
|
|
10
|
+
*/
|
|
11
|
+
export function isRegexpStringMatch(
|
|
12
|
+
regexAsString?: string,
|
|
13
|
+
valueToTest?: string,
|
|
14
|
+
): boolean {
|
|
15
|
+
if (
|
|
16
|
+
typeof regexAsString === 'undefined' ||
|
|
17
|
+
typeof valueToTest === 'undefined'
|
|
18
|
+
) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return new RegExp(regexAsString, 'gi').test(valueToTest);
|
|
23
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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 GeneratedRoutes, {type Route} from '@generated/routes';
|
|
9
|
+
import {useMemo} from 'react';
|
|
10
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
11
|
+
|
|
12
|
+
// Note that all sites don't always have a homepage in practice
|
|
13
|
+
// See https://github.com/facebook/docusaurus/pull/6517#issuecomment-1048709116
|
|
14
|
+
export function findHomePageRoute({
|
|
15
|
+
baseUrl,
|
|
16
|
+
routes: initialRoutes,
|
|
17
|
+
}: {
|
|
18
|
+
routes: Route[];
|
|
19
|
+
baseUrl: string;
|
|
20
|
+
}): Route | undefined {
|
|
21
|
+
function isHomePageRoute(route: Route): boolean {
|
|
22
|
+
return route.path === baseUrl && route.exact === true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isHomeParentRoute(route: Route): boolean {
|
|
26
|
+
return route.path === baseUrl && !route.exact;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function doFindHomePageRoute(routes: Route[]): Route | undefined {
|
|
30
|
+
if (routes.length === 0) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const homePage = routes.find(isHomePageRoute);
|
|
34
|
+
if (homePage) {
|
|
35
|
+
return homePage;
|
|
36
|
+
}
|
|
37
|
+
const indexSubRoutes = routes
|
|
38
|
+
.filter(isHomeParentRoute)
|
|
39
|
+
.flatMap((route) => route.routes ?? []);
|
|
40
|
+
return doFindHomePageRoute(indexSubRoutes);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return doFindHomePageRoute(initialRoutes);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function useHomePageRoute(): Route | undefined {
|
|
47
|
+
const {baseUrl} = useDocusaurusContext().siteConfig;
|
|
48
|
+
return useMemo(
|
|
49
|
+
() =>
|
|
50
|
+
findHomePageRoute({
|
|
51
|
+
routes: GeneratedRoutes,
|
|
52
|
+
baseUrl,
|
|
53
|
+
}),
|
|
54
|
+
[baseUrl],
|
|
55
|
+
);
|
|
56
|
+
}
|