@docusaurus/theme-common 2.0.0-beta.1ec2c95e3 → 2.0.0-beta.21
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/Details.d.ts +14 -0
- package/lib/components/Collapsible/index.d.ts +64 -0
- package/lib/components/Collapsible/index.d.ts.map +1 -0
- package/lib/components/Collapsible/index.js +150 -0
- package/lib/components/Collapsible/index.js.map +1 -0
- package/lib/components/Details/index.d.ts +17 -0
- package/lib/components/Details/index.d.ts.map +1 -0
- package/lib/components/Details/index.js +76 -0
- package/lib/components/Details/index.js.map +1 -0
- package/lib/components/Details/styles.module.css +62 -0
- package/lib/contexts/announcementBar.d.ts +22 -0
- package/lib/contexts/announcementBar.d.ts.map +1 -0
- package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +30 -31
- package/lib/contexts/announcementBar.js.map +1 -0
- package/lib/contexts/colorMode.d.ts +27 -0
- package/lib/contexts/colorMode.d.ts.map +1 -0
- package/lib/contexts/colorMode.js +132 -0
- package/lib/contexts/colorMode.js.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.js +28 -0
- package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
- package/lib/contexts/docsPreferredVersion.d.ts +31 -0
- package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
- package/lib/contexts/docsPreferredVersion.js +130 -0
- package/lib/contexts/docsPreferredVersion.js.map +1 -0
- package/lib/contexts/docsSidebar.d.ts +26 -0
- package/lib/contexts/docsSidebar.d.ts.map +1 -0
- package/lib/contexts/docsSidebar.js +30 -0
- package/lib/contexts/docsSidebar.js.map +1 -0
- package/lib/contexts/docsVersion.d.ts +20 -0
- package/lib/contexts/docsVersion.d.ts.map +1 -0
- package/lib/contexts/docsVersion.js +26 -0
- package/lib/contexts/docsVersion.js.map +1 -0
- package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
- package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
- package/lib/contexts/navbarMobileSidebar.js +56 -0
- package/lib/contexts/navbarMobileSidebar.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.js +56 -0
- package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
- package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
- package/lib/contexts/tabGroupChoice.d.ts +21 -0
- package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
- package/lib/contexts/tabGroupChoice.js +49 -0
- package/lib/contexts/tabGroupChoice.js.map +1 -0
- package/lib/{utils/usePrevious.d.ts → hooks/styles.css} +4 -1
- package/lib/hooks/useBackToTopButton.d.ts +27 -0
- package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
- package/lib/hooks/useBackToTopButton.js +50 -0
- package/lib/hooks/useBackToTopButton.js.map +1 -0
- package/lib/hooks/useCodeWordWrap.d.ts +14 -0
- package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
- package/lib/hooks/useCodeWordWrap.js +41 -0
- package/lib/hooks/useCodeWordWrap.js.map +1 -0
- package/lib/hooks/useHideableNavbar.d.ts +17 -0
- package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
- package/lib/hooks/useHideableNavbar.js +60 -0
- package/lib/hooks/useHideableNavbar.js.map +1 -0
- package/lib/hooks/useKeyboardNavigation.d.ts +20 -0
- package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -0
- package/lib/hooks/useKeyboardNavigation.js +39 -0
- package/lib/hooks/useKeyboardNavigation.js.map +1 -0
- package/lib/hooks/useLockBodyScroll.d.ts +12 -0
- package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
- package/lib/hooks/useLockBodyScroll.js +20 -0
- package/lib/hooks/useLockBodyScroll.js.map +1 -0
- package/lib/hooks/usePrismTheme.d.ts +13 -0
- package/lib/hooks/usePrismTheme.d.ts.map +1 -0
- package/lib/hooks/usePrismTheme.js +21 -0
- package/lib/hooks/usePrismTheme.js.map +1 -0
- package/lib/hooks/useSearchPage.d.ts +25 -0
- package/lib/hooks/useSearchPage.d.ts.map +1 -0
- package/lib/hooks/useSearchPage.js +43 -0
- package/lib/hooks/useSearchPage.js.map +1 -0
- package/lib/hooks/useSkipToContent.d.ts +25 -0
- package/lib/hooks/useSkipToContent.d.ts.map +1 -0
- package/lib/hooks/useSkipToContent.js +35 -0
- package/lib/hooks/useSkipToContent.js.map +1 -0
- package/lib/hooks/useTOCHighlight.d.ts +25 -0
- package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
- package/lib/hooks/useTOCHighlight.js +130 -0
- package/lib/hooks/useTOCHighlight.js.map +1 -0
- package/lib/hooks/useWindowSize.d.ts +28 -0
- package/lib/hooks/useWindowSize.d.ts.map +1 -0
- package/lib/hooks/useWindowSize.js +59 -0
- package/lib/hooks/useWindowSize.js.map +1 -0
- package/lib/index.d.ts +38 -10
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +38 -9
- package/lib/index.js.map +1 -0
- package/lib/utils/ThemeClassNames.d.ts +47 -12
- package/lib/utils/ThemeClassNames.d.ts.map +1 -0
- package/lib/utils/ThemeClassNames.js +45 -4
- package/lib/utils/ThemeClassNames.js.map +1 -0
- package/lib/utils/codeBlockUtils.d.ts +63 -0
- package/lib/utils/codeBlockUtils.d.ts.map +1 -0
- package/lib/utils/codeBlockUtils.js +159 -3
- package/lib/utils/codeBlockUtils.js.map +1 -0
- package/lib/utils/docsUtils.d.ts +91 -0
- package/lib/utils/docsUtils.d.ts.map +1 -0
- package/lib/utils/docsUtils.js +217 -1
- package/lib/utils/docsUtils.js.map +1 -0
- package/lib/utils/footerUtils.d.ts +13 -0
- package/lib/utils/footerUtils.d.ts.map +1 -0
- package/lib/utils/footerUtils.js +14 -0
- package/lib/utils/footerUtils.js.map +1 -0
- package/lib/utils/generalUtils.d.ts +11 -1
- package/lib/utils/generalUtils.d.ts.map +1 -0
- package/lib/utils/generalUtils.js +9 -5
- package/lib/utils/generalUtils.js.map +1 -0
- package/lib/utils/historyUtils.d.ts +17 -0
- package/lib/utils/historyUtils.d.ts.map +1 -0
- package/lib/utils/historyUtils.js +38 -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/metadataUtils.d.ts +38 -0
- package/lib/utils/metadataUtils.d.ts.map +1 -0
- package/lib/utils/metadataUtils.js +70 -0
- package/lib/utils/metadataUtils.js.map +1 -0
- package/lib/utils/navbarUtils.d.ts +21 -0
- package/lib/utils/navbarUtils.d.ts.map +1 -0
- package/lib/utils/navbarUtils.js +36 -0
- package/lib/utils/navbarUtils.js.map +1 -0
- package/lib/utils/reactUtils.d.ts +42 -0
- package/lib/utils/reactUtils.d.ts.map +1 -0
- package/lib/utils/reactUtils.js +64 -0
- package/lib/utils/reactUtils.js.map +1 -0
- package/lib/utils/regexpUtils.d.ts +12 -0
- package/lib/utils/regexpUtils.d.ts.map +1 -0
- package/lib/utils/regexpUtils.js +18 -0
- package/lib/utils/regexpUtils.js.map +1 -0
- package/lib/utils/routesUtils.d.ts +26 -0
- package/lib/utils/routesUtils.d.ts.map +1 -0
- package/lib/utils/routesUtils.js +54 -0
- package/lib/utils/routesUtils.js.map +1 -0
- package/lib/utils/scrollUtils.d.ts +83 -0
- package/lib/utils/scrollUtils.d.ts.map +1 -0
- package/lib/utils/scrollUtils.js +200 -0
- package/lib/utils/scrollUtils.js.map +1 -0
- package/lib/utils/searchUtils.d.ts +13 -0
- package/lib/utils/searchUtils.d.ts.map +1 -0
- package/lib/utils/searchUtils.js +37 -0
- package/lib/utils/searchUtils.js.map +1 -0
- package/lib/utils/storageUtils.d.ts +15 -7
- package/lib/utils/storageUtils.d.ts.map +1 -0
- package/lib/utils/storageUtils.js +55 -22
- package/lib/utils/storageUtils.js.map +1 -0
- package/lib/utils/tagsUtils.d.ts +18 -0
- package/lib/utils/tagsUtils.d.ts.map +1 -0
- package/lib/utils/tagsUtils.js +36 -0
- package/lib/utils/tagsUtils.js.map +1 -0
- package/lib/utils/tocUtils.d.ts +36 -0
- package/lib/utils/tocUtils.d.ts.map +1 -0
- package/lib/utils/tocUtils.js +84 -0
- package/lib/utils/tocUtils.js.map +1 -0
- package/lib/utils/useAlternatePageUtils.d.ts +21 -1
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
- package/lib/utils/useAlternatePageUtils.js +9 -4
- package/lib/utils/useAlternatePageUtils.js.map +1 -0
- package/lib/utils/useLocalPathname.d.ts +13 -0
- package/lib/utils/useLocalPathname.d.ts.map +1 -0
- package/lib/utils/useLocalPathname.js +19 -0
- package/lib/utils/useLocalPathname.js.map +1 -0
- package/lib/utils/useLocationChange.d.ts +9 -6
- package/lib/utils/useLocationChange.d.ts.map +1 -0
- package/lib/utils/useLocationChange.js +17 -11
- package/lib/utils/useLocationChange.js.map +1 -0
- package/lib/utils/usePluralForm.d.ts +12 -0
- package/lib/utils/usePluralForm.d.ts.map +1 -0
- package/lib/utils/usePluralForm.js +36 -37
- package/lib/utils/usePluralForm.js.map +1 -0
- package/lib/utils/useThemeConfig.d.ts +56 -25
- package/lib/utils/useThemeConfig.d.ts.map +1 -0
- package/lib/utils/useThemeConfig.js +4 -0
- package/lib/utils/useThemeConfig.js.map +1 -0
- package/package.json +28 -13
- package/src/components/Collapsible/index.tsx +263 -0
- package/src/components/Details/index.tsx +109 -0
- package/src/components/Details/styles.module.css +62 -0
- package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +43 -39
- package/src/contexts/colorMode.tsx +198 -0
- package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
- package/src/contexts/docsPreferredVersion.tsx +251 -0
- package/src/contexts/docsSidebar.tsx +50 -0
- package/src/contexts/docsVersion.tsx +36 -0
- package/src/contexts/navbarMobileSidebar.tsx +99 -0
- package/src/contexts/navbarSecondaryMenu/content.tsx +110 -0
- package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
- package/src/contexts/tabGroupChoice.tsx +85 -0
- package/{lib/utils/pathUtils.d.ts → src/hooks/styles.css} +4 -1
- package/src/hooks/useBackToTopButton.ts +73 -0
- package/src/hooks/useCodeWordWrap.ts +56 -0
- package/src/hooks/useHideableNavbar.ts +75 -0
- package/src/hooks/useKeyboardNavigation.ts +45 -0
- package/src/hooks/useLockBodyScroll.ts +21 -0
- package/src/hooks/usePrismTheme.ts +24 -0
- package/src/hooks/useSearchPage.ts +79 -0
- package/src/hooks/useSkipToContent.ts +58 -0
- package/src/hooks/useTOCHighlight.ts +189 -0
- package/src/hooks/useWindowSize.ts +72 -0
- package/src/index.ts +130 -19
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +51 -5
- package/src/utils/codeBlockUtils.ts +241 -2
- package/src/utils/docsUtils.tsx +334 -0
- package/src/utils/footerUtils.ts +18 -0
- package/src/utils/generalUtils.ts +9 -5
- package/src/utils/historyUtils.ts +45 -0
- package/src/utils/jsUtils.ts +36 -0
- package/src/utils/metadataUtils.tsx +115 -0
- package/src/utils/navbarUtils.tsx +45 -0
- package/src/utils/reactUtils.tsx +76 -0
- package/src/utils/regexpUtils.ts +24 -0
- package/src/utils/routesUtils.ts +75 -0
- package/src/utils/scrollUtils.tsx +304 -0
- package/src/utils/searchUtils.ts +51 -0
- package/src/utils/storageUtils.ts +56 -23
- package/src/utils/tagsUtils.ts +50 -0
- package/src/utils/tocUtils.ts +119 -0
- package/src/utils/useAlternatePageUtils.ts +19 -6
- package/src/utils/useLocalPathname.ts +22 -0
- package/src/utils/useLocationChange.ts +24 -20
- package/src/utils/usePluralForm.ts +50 -38
- package/src/utils/useThemeConfig.ts +55 -27
- package/lib/.tsbuildinfo +0 -1
- package/lib/utils/announcementBarUtils.d.ts +0 -17
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -21
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -94
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -13
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -20
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -5
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
- package/lib/utils/pathUtils.js +0 -13
- package/lib/utils/usePrevious.js +0 -14
- package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
- package/src/utils/__tests__/pathUtils.test.ts +0 -32
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -165
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -34
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -66
- package/src/utils/docsUtils.ts +0 -11
- package/src/utils/pathUtils.ts +0 -17
- package/src/utils/usePrevious.ts +0 -18
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,304 @@
|
|
|
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
|
+
useCallback,
|
|
10
|
+
useContext,
|
|
11
|
+
useEffect,
|
|
12
|
+
useLayoutEffect,
|
|
13
|
+
useMemo,
|
|
14
|
+
useRef,
|
|
15
|
+
type ReactNode,
|
|
16
|
+
} from 'react';
|
|
17
|
+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
18
|
+
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
19
|
+
import {useDynamicCallback, ReactContextError} from './reactUtils';
|
|
20
|
+
|
|
21
|
+
type ScrollController = {
|
|
22
|
+
/** A boolean ref tracking whether scroll events are enabled. */
|
|
23
|
+
scrollEventsEnabledRef: React.MutableRefObject<boolean>;
|
|
24
|
+
/** Enable scroll events in `useScrollPosition`. */
|
|
25
|
+
enableScrollEvents: () => void;
|
|
26
|
+
/** Disable scroll events in `useScrollPosition`. */
|
|
27
|
+
disableScrollEvents: () => void;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function useScrollControllerContextValue(): ScrollController {
|
|
31
|
+
const scrollEventsEnabledRef = useRef(true);
|
|
32
|
+
|
|
33
|
+
return useMemo(
|
|
34
|
+
() => ({
|
|
35
|
+
scrollEventsEnabledRef,
|
|
36
|
+
enableScrollEvents: () => {
|
|
37
|
+
scrollEventsEnabledRef.current = true;
|
|
38
|
+
},
|
|
39
|
+
disableScrollEvents: () => {
|
|
40
|
+
scrollEventsEnabledRef.current = false;
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
43
|
+
[],
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const ScrollMonitorContext = React.createContext<ScrollController | undefined>(
|
|
48
|
+
undefined,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
export function ScrollControllerProvider({
|
|
52
|
+
children,
|
|
53
|
+
}: {
|
|
54
|
+
children: ReactNode;
|
|
55
|
+
}): JSX.Element {
|
|
56
|
+
const value = useScrollControllerContextValue();
|
|
57
|
+
return (
|
|
58
|
+
<ScrollMonitorContext.Provider value={value}>
|
|
59
|
+
{children}
|
|
60
|
+
</ScrollMonitorContext.Provider>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* We need a way to update the scroll position while ignoring scroll events
|
|
66
|
+
* so as not to toggle Navbar/BackToTop visibility.
|
|
67
|
+
*
|
|
68
|
+
* This API permits to temporarily disable/ignore scroll events. Motivated by
|
|
69
|
+
* https://github.com/facebook/docusaurus/pull/5618
|
|
70
|
+
*/
|
|
71
|
+
export function useScrollController(): ScrollController {
|
|
72
|
+
const context = useContext(ScrollMonitorContext);
|
|
73
|
+
if (context == null) {
|
|
74
|
+
throw new ReactContextError('ScrollControllerProvider');
|
|
75
|
+
}
|
|
76
|
+
return context;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
type ScrollPosition = {scrollX: number; scrollY: number};
|
|
80
|
+
|
|
81
|
+
const getScrollPosition = (): ScrollPosition | null =>
|
|
82
|
+
ExecutionEnvironment.canUseDOM
|
|
83
|
+
? {
|
|
84
|
+
scrollX: window.pageXOffset,
|
|
85
|
+
scrollY: window.pageYOffset,
|
|
86
|
+
}
|
|
87
|
+
: null;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* This hook fires an effect when the scroll position changes. The effect will
|
|
91
|
+
* be provided with the before/after scroll positions. Note that the effect may
|
|
92
|
+
* not be always run: if scrolling is disabled through `useScrollController`, it
|
|
93
|
+
* will be a no-op.
|
|
94
|
+
*
|
|
95
|
+
* @see {@link useScrollController}
|
|
96
|
+
*/
|
|
97
|
+
export function useScrollPosition(
|
|
98
|
+
effect: (
|
|
99
|
+
position: ScrollPosition,
|
|
100
|
+
lastPosition: ScrollPosition | null,
|
|
101
|
+
) => void,
|
|
102
|
+
deps: unknown[] = [],
|
|
103
|
+
): void {
|
|
104
|
+
const {scrollEventsEnabledRef} = useScrollController();
|
|
105
|
+
const lastPositionRef = useRef<ScrollPosition | null>(getScrollPosition());
|
|
106
|
+
|
|
107
|
+
const dynamicEffect = useDynamicCallback(effect);
|
|
108
|
+
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
const handleScroll = () => {
|
|
111
|
+
if (!scrollEventsEnabledRef.current) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const currentPosition = getScrollPosition()!;
|
|
115
|
+
dynamicEffect(currentPosition, lastPositionRef.current);
|
|
116
|
+
lastPositionRef.current = currentPosition;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const opts: AddEventListenerOptions & EventListenerOptions = {
|
|
120
|
+
passive: true,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
handleScroll();
|
|
124
|
+
window.addEventListener('scroll', handleScroll, opts);
|
|
125
|
+
|
|
126
|
+
return () => window.removeEventListener('scroll', handleScroll, opts);
|
|
127
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
128
|
+
}, [dynamicEffect, scrollEventsEnabledRef, ...deps]);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
type UseScrollPositionSaver = {
|
|
132
|
+
/** Measure the top of an element, and store the details. */
|
|
133
|
+
save: (elem: HTMLElement) => void;
|
|
134
|
+
/**
|
|
135
|
+
* Restore the page position to keep the stored element's position from
|
|
136
|
+
* the top of the viewport, and remove the stored details.
|
|
137
|
+
*/
|
|
138
|
+
restore: () => {restored: boolean};
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
function useScrollPositionSaver(): UseScrollPositionSaver {
|
|
142
|
+
const lastElementRef = useRef<{elem: HTMLElement | null; top: number}>({
|
|
143
|
+
elem: null,
|
|
144
|
+
top: 0,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const save = useCallback((elem: HTMLElement) => {
|
|
148
|
+
lastElementRef.current = {
|
|
149
|
+
elem,
|
|
150
|
+
top: elem.getBoundingClientRect().top,
|
|
151
|
+
};
|
|
152
|
+
}, []);
|
|
153
|
+
|
|
154
|
+
const restore = useCallback(() => {
|
|
155
|
+
const {
|
|
156
|
+
current: {elem, top},
|
|
157
|
+
} = lastElementRef;
|
|
158
|
+
if (!elem) {
|
|
159
|
+
return {restored: false};
|
|
160
|
+
}
|
|
161
|
+
const newTop = elem.getBoundingClientRect().top;
|
|
162
|
+
const heightDiff = newTop - top;
|
|
163
|
+
if (heightDiff) {
|
|
164
|
+
window.scrollBy({left: 0, top: heightDiff});
|
|
165
|
+
}
|
|
166
|
+
lastElementRef.current = {elem: null, top: 0};
|
|
167
|
+
|
|
168
|
+
return {restored: heightDiff !== 0};
|
|
169
|
+
}, []);
|
|
170
|
+
|
|
171
|
+
return useMemo(() => ({save, restore}), [restore, save]);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* This hook permits to "block" the scroll position of a DOM element.
|
|
176
|
+
* The idea is that we should be able to update DOM content above this element
|
|
177
|
+
* but the screen position of this element should not change.
|
|
178
|
+
*
|
|
179
|
+
* Feature motivated by the Tabs groups: clicking on a tab may affect tabs of
|
|
180
|
+
* the same group upper in the tree, yet to avoid a bad UX, the clicked tab must
|
|
181
|
+
* remain under the user mouse.
|
|
182
|
+
*
|
|
183
|
+
* @see https://github.com/facebook/docusaurus/pull/5618
|
|
184
|
+
*/
|
|
185
|
+
export function useScrollPositionBlocker(): {
|
|
186
|
+
/**
|
|
187
|
+
* Takes an element, and keeps its screen position no matter what's getting
|
|
188
|
+
* rendered above it, until the next render.
|
|
189
|
+
*/
|
|
190
|
+
blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
|
|
191
|
+
} {
|
|
192
|
+
const scrollController = useScrollController();
|
|
193
|
+
const scrollPositionSaver = useScrollPositionSaver();
|
|
194
|
+
|
|
195
|
+
const nextLayoutEffectCallbackRef = useRef<(() => void) | undefined>(
|
|
196
|
+
undefined,
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const blockElementScrollPositionUntilNextRender = useCallback(
|
|
200
|
+
(el: HTMLElement) => {
|
|
201
|
+
scrollPositionSaver.save(el);
|
|
202
|
+
scrollController.disableScrollEvents();
|
|
203
|
+
nextLayoutEffectCallbackRef.current = () => {
|
|
204
|
+
const {restored} = scrollPositionSaver.restore();
|
|
205
|
+
nextLayoutEffectCallbackRef.current = undefined;
|
|
206
|
+
|
|
207
|
+
// Restoring the former scroll position will trigger a scroll event. We
|
|
208
|
+
// need to wait for next scroll event to happen before enabling the
|
|
209
|
+
// scrollController events again.
|
|
210
|
+
if (restored) {
|
|
211
|
+
const handleScrollRestoreEvent = () => {
|
|
212
|
+
scrollController.enableScrollEvents();
|
|
213
|
+
window.removeEventListener('scroll', handleScrollRestoreEvent);
|
|
214
|
+
};
|
|
215
|
+
window.addEventListener('scroll', handleScrollRestoreEvent);
|
|
216
|
+
} else {
|
|
217
|
+
scrollController.enableScrollEvents();
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
[scrollController, scrollPositionSaver],
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
useLayoutEffect(() => {
|
|
225
|
+
nextLayoutEffectCallbackRef.current?.();
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
blockElementScrollPositionUntilNextRender,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
type CancelScrollTop = () => void;
|
|
234
|
+
|
|
235
|
+
function smoothScrollNative(top: number): CancelScrollTop {
|
|
236
|
+
window.scrollTo({top, behavior: 'smooth'});
|
|
237
|
+
return () => {
|
|
238
|
+
// Nothing to cancel, it's natively cancelled if user tries to scroll down
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function smoothScrollPolyfill(top: number): CancelScrollTop {
|
|
243
|
+
let raf: number | null = null;
|
|
244
|
+
const isUpScroll = document.documentElement.scrollTop > top;
|
|
245
|
+
function rafRecursion() {
|
|
246
|
+
const currentScroll = document.documentElement.scrollTop;
|
|
247
|
+
if (
|
|
248
|
+
(isUpScroll && currentScroll > top) ||
|
|
249
|
+
(!isUpScroll && currentScroll < top)
|
|
250
|
+
) {
|
|
251
|
+
raf = requestAnimationFrame(rafRecursion);
|
|
252
|
+
window.scrollTo(0, Math.floor((currentScroll - top) * 0.85) + top);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
rafRecursion();
|
|
256
|
+
|
|
257
|
+
// Break the recursion. Prevents the user from "fighting" against that
|
|
258
|
+
// recursion producing a weird UX
|
|
259
|
+
return () => raf && cancelAnimationFrame(raf);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* A "smart polyfill" of `window.scrollTo({ top, behavior: "smooth" })`.
|
|
264
|
+
* This currently always uses a polyfilled implementation unless
|
|
265
|
+
* `scroll-behavior: smooth` has been set in CSS, because native support
|
|
266
|
+
* detection for scroll behavior seems unreliable.
|
|
267
|
+
*
|
|
268
|
+
* This hook does not do anything by itself: it returns a start and a stop
|
|
269
|
+
* handle. You can execute either handle at any time.
|
|
270
|
+
*/
|
|
271
|
+
export function useSmoothScrollTo(): {
|
|
272
|
+
/**
|
|
273
|
+
* Start the scroll.
|
|
274
|
+
*
|
|
275
|
+
* @param top The final scroll top position.
|
|
276
|
+
*/
|
|
277
|
+
startScroll: (top: number) => void;
|
|
278
|
+
/**
|
|
279
|
+
* A cancel function, because the non-native smooth scroll-top
|
|
280
|
+
* implementation must be interrupted if user scrolls down. If there's no
|
|
281
|
+
* existing animation or the scroll is using native behavior, this is a no-op.
|
|
282
|
+
*/
|
|
283
|
+
cancelScroll: CancelScrollTop;
|
|
284
|
+
} {
|
|
285
|
+
const cancelRef = useRef<CancelScrollTop | null>(null);
|
|
286
|
+
const isBrowser = useIsBrowser();
|
|
287
|
+
// Not all have support for smooth scrolling (particularly Safari mobile iOS)
|
|
288
|
+
// TODO proper detection is currently unreliable!
|
|
289
|
+
// see https://github.com/wessberg/scroll-behavior-polyfill/issues/16
|
|
290
|
+
// For now, we only use native scroll behavior if smooth is already set,
|
|
291
|
+
// because otherwise the polyfill produces a weird UX when both CSS and JS try
|
|
292
|
+
// to scroll a page, and they cancel each other.
|
|
293
|
+
const supportsNativeSmoothScrolling =
|
|
294
|
+
isBrowser &&
|
|
295
|
+
getComputedStyle(document.documentElement).scrollBehavior === 'smooth';
|
|
296
|
+
return {
|
|
297
|
+
startScroll: (top: number) => {
|
|
298
|
+
cancelRef.current = supportsNativeSmoothScrolling
|
|
299
|
+
? smoothScrollNative(top)
|
|
300
|
+
: smoothScrollPolyfill(top);
|
|
301
|
+
},
|
|
302
|
+
cancelScroll: () => cancelRef.current?.(),
|
|
303
|
+
};
|
|
304
|
+
}
|
package/src/utils/searchUtils.ts
CHANGED
|
@@ -5,11 +5,62 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import {
|
|
9
|
+
useAllDocsData,
|
|
10
|
+
useActivePluginAndVersion,
|
|
11
|
+
} from '@docusaurus/plugin-content-docs/client';
|
|
12
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
13
|
+
import {useDocsPreferredVersionByPluginId} from '../contexts/docsPreferredVersion';
|
|
14
|
+
|
|
8
15
|
export const DEFAULT_SEARCH_TAG = 'default';
|
|
9
16
|
|
|
17
|
+
/** The search tag to append as each doc's metadata. */
|
|
10
18
|
export function docVersionSearchTag(
|
|
11
19
|
pluginId: string,
|
|
12
20
|
versionName: string,
|
|
13
21
|
): string {
|
|
14
22
|
return `docs-${pluginId}-${versionName}`;
|
|
15
23
|
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Gets the relevant context information for contextual search.
|
|
27
|
+
*
|
|
28
|
+
* The value is generic and not coupled to Algolia/DocSearch, since we may want
|
|
29
|
+
* to support multiple search engines, or allowing users to use their own search
|
|
30
|
+
* engine solution.
|
|
31
|
+
*/
|
|
32
|
+
export function useContextualSearchFilters(): {locale: string; tags: string[]} {
|
|
33
|
+
const {i18n} = useDocusaurusContext();
|
|
34
|
+
const allDocsData = useAllDocsData();
|
|
35
|
+
const activePluginAndVersion = useActivePluginAndVersion();
|
|
36
|
+
const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
|
|
37
|
+
|
|
38
|
+
// This can't use more specialized hooks because we are mapping over all
|
|
39
|
+
// plugin instances.
|
|
40
|
+
function getDocPluginTags(pluginId: string) {
|
|
41
|
+
const activeVersion =
|
|
42
|
+
activePluginAndVersion?.activePlugin.pluginId === pluginId
|
|
43
|
+
? activePluginAndVersion.activeVersion
|
|
44
|
+
: undefined;
|
|
45
|
+
|
|
46
|
+
const preferredVersion = docsPreferredVersionByPluginId[pluginId];
|
|
47
|
+
|
|
48
|
+
const latestVersion = allDocsData[pluginId]!.versions.find(
|
|
49
|
+
(v) => v.isLast,
|
|
50
|
+
)!;
|
|
51
|
+
|
|
52
|
+
const version = activeVersion ?? preferredVersion ?? latestVersion;
|
|
53
|
+
|
|
54
|
+
return docVersionSearchTag(pluginId, version.name);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const tags = [
|
|
58
|
+
DEFAULT_SEARCH_TAG,
|
|
59
|
+
...Object.keys(allDocsData).map(getDocPluginTags),
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
locale: i18n.currentLocale,
|
|
64
|
+
tags,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -11,8 +11,12 @@ export type StorageType = typeof StorageTypes[number];
|
|
|
11
11
|
|
|
12
12
|
const DefaultStorageType: StorageType = 'localStorage';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Will return `null` if browser storage is unavailable (like running Docusaurus
|
|
16
|
+
* in an iframe). This should NOT be called in SSR.
|
|
17
|
+
*
|
|
18
|
+
* @see https://github.com/facebook/docusaurus/pull/4501
|
|
19
|
+
*/
|
|
16
20
|
function getBrowserStorage(
|
|
17
21
|
storageType: StorageType = DefaultStorageType,
|
|
18
22
|
): Storage | null {
|
|
@@ -23,21 +27,21 @@ function getBrowserStorage(
|
|
|
23
27
|
}
|
|
24
28
|
if (storageType === 'none') {
|
|
25
29
|
return null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return window[storageType];
|
|
33
|
+
} catch (err) {
|
|
34
|
+
logOnceBrowserStorageNotAvailableWarning(err as Error);
|
|
35
|
+
return null;
|
|
33
36
|
}
|
|
34
37
|
}
|
|
35
38
|
|
|
39
|
+
let hasLoggedBrowserStorageNotAvailableWarning = false;
|
|
36
40
|
/**
|
|
37
|
-
* Poor man's memoization to avoid logging multiple times the same warning
|
|
38
|
-
* Sometimes, localStorage
|
|
41
|
+
* Poor man's memoization to avoid logging multiple times the same warning.
|
|
42
|
+
* Sometimes, `localStorage`/`sessionStorage` is unavailable due to browser
|
|
43
|
+
* policies.
|
|
39
44
|
*/
|
|
40
|
-
let hasLoggedBrowserStorageNotAvailableWarning = false;
|
|
41
45
|
function logOnceBrowserStorageNotAvailableWarning(error: Error) {
|
|
42
46
|
if (!hasLoggedBrowserStorageNotAvailableWarning) {
|
|
43
47
|
console.warn(
|
|
@@ -50,11 +54,11 @@ Possible reasons: running Docusaurus in an iframe, in an incognito browser sessi
|
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
// Convenient storage interface for a single storage key
|
|
53
|
-
export
|
|
57
|
+
export type StorageSlot = {
|
|
54
58
|
get: () => string | null;
|
|
55
59
|
set: (value: string) => void;
|
|
56
60
|
del: () => void;
|
|
57
|
-
}
|
|
61
|
+
};
|
|
58
62
|
|
|
59
63
|
const NoopStorageSlot: StorageSlot = {
|
|
60
64
|
get: () => null,
|
|
@@ -62,7 +66,7 @@ const NoopStorageSlot: StorageSlot = {
|
|
|
62
66
|
del: () => {},
|
|
63
67
|
};
|
|
64
68
|
|
|
65
|
-
//
|
|
69
|
+
// Fail-fast, as storage APIs should not be used during the SSR process
|
|
66
70
|
function createServerStorageSlot(key: string): StorageSlot {
|
|
67
71
|
function throwError(): never {
|
|
68
72
|
throw new Error(`Illegal storage API usage for storage key "${key}".
|
|
@@ -78,12 +82,19 @@ Please only call storage APIs in effects and event handlers.`);
|
|
|
78
82
|
}
|
|
79
83
|
|
|
80
84
|
/**
|
|
81
|
-
* Creates an
|
|
85
|
+
* Creates an interface to work on a particular key in the storage model.
|
|
86
|
+
* Note that this function only initializes the interface, but doesn't allocate
|
|
87
|
+
* anything by itself (i.e. no side-effects).
|
|
88
|
+
*
|
|
89
|
+
* The API is fail-safe, since usage of browser storage should be considered
|
|
90
|
+
* unreliable. Local storage might simply be unavailable (iframe + browser
|
|
91
|
+
* security) or operations might fail individually. Please assume that using
|
|
92
|
+
* this API can be a no-op. See also https://github.com/facebook/docusaurus/issues/6036
|
|
82
93
|
*/
|
|
83
|
-
export
|
|
94
|
+
export function createStorageSlot(
|
|
84
95
|
key: string,
|
|
85
96
|
options?: {persistence?: StorageType},
|
|
86
|
-
): StorageSlot
|
|
97
|
+
): StorageSlot {
|
|
87
98
|
if (typeof window === 'undefined') {
|
|
88
99
|
return createServerStorageSlot(key);
|
|
89
100
|
}
|
|
@@ -92,14 +103,36 @@ export const createStorageSlot = (
|
|
|
92
103
|
return NoopStorageSlot;
|
|
93
104
|
}
|
|
94
105
|
return {
|
|
95
|
-
get: () =>
|
|
96
|
-
|
|
97
|
-
|
|
106
|
+
get: () => {
|
|
107
|
+
try {
|
|
108
|
+
return browserStorage.getItem(key);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
console.error(`Docusaurus storage error, can't get key=${key}`, err);
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
set: (value) => {
|
|
115
|
+
try {
|
|
116
|
+
browserStorage.setItem(key, value);
|
|
117
|
+
} catch (err) {
|
|
118
|
+
console.error(
|
|
119
|
+
`Docusaurus storage error, can't set ${key}=${value}`,
|
|
120
|
+
err,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
del: () => {
|
|
125
|
+
try {
|
|
126
|
+
browserStorage.removeItem(key);
|
|
127
|
+
} catch (err) {
|
|
128
|
+
console.error(`Docusaurus storage error, can't delete key=${key}`, err);
|
|
129
|
+
}
|
|
130
|
+
},
|
|
98
131
|
};
|
|
99
|
-
}
|
|
132
|
+
}
|
|
100
133
|
|
|
101
134
|
/**
|
|
102
|
-
* Returns a list of all the keys currently stored in browser storage
|
|
135
|
+
* Returns a list of all the keys currently stored in browser storage,
|
|
103
136
|
* or an empty list if browser storage can't be accessed.
|
|
104
137
|
*/
|
|
105
138
|
export function listStorageKeys(
|
|
@@ -0,0 +1,50 @@
|
|
|
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 {translate} from '@docusaurus/Translate';
|
|
9
|
+
import type {TagsListItem} from '@docusaurus/utils';
|
|
10
|
+
|
|
11
|
+
export const translateTagsPageTitle = (): string =>
|
|
12
|
+
translate({
|
|
13
|
+
id: 'theme.tags.tagsPageTitle',
|
|
14
|
+
message: 'Tags',
|
|
15
|
+
description: 'The title of the tag list page',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export type TagLetterEntry = {letter: string; tags: TagsListItem[]};
|
|
19
|
+
|
|
20
|
+
function getTagLetter(tag: string): string {
|
|
21
|
+
return tag[0]!.toUpperCase();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Takes a list of tags (as provided by the content plugins), and groups them by
|
|
26
|
+
* their initials.
|
|
27
|
+
*/
|
|
28
|
+
export function listTagsByLetters(
|
|
29
|
+
tags: readonly TagsListItem[],
|
|
30
|
+
): TagLetterEntry[] {
|
|
31
|
+
const groups: {[initial: string]: TagsListItem[]} = {};
|
|
32
|
+
Object.values(tags).forEach((tag) => {
|
|
33
|
+
const initial = getTagLetter(tag.label);
|
|
34
|
+
groups[initial] ??= [];
|
|
35
|
+
groups[initial]!.push(tag);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
Object.entries(groups)
|
|
40
|
+
// Sort letters
|
|
41
|
+
.sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
|
|
42
|
+
.map(([letter, letterTags]) => {
|
|
43
|
+
// Sort tags inside a letter
|
|
44
|
+
const sortedTags = letterTags.sort((tag1, tag2) =>
|
|
45
|
+
tag1.label.localeCompare(tag2.label),
|
|
46
|
+
);
|
|
47
|
+
return {letter, tags: sortedTags};
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
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 {useMemo} from 'react';
|
|
9
|
+
import type {TOCItem} from '@docusaurus/mdx-loader';
|
|
10
|
+
|
|
11
|
+
export type TOCTreeNode = {
|
|
12
|
+
readonly value: string;
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly level: number;
|
|
15
|
+
readonly children: readonly TOCTreeNode[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function treeifyTOC(flatTOC: readonly TOCItem[]): TOCTreeNode[] {
|
|
19
|
+
const headings = flatTOC.map((heading) => ({
|
|
20
|
+
...heading,
|
|
21
|
+
parentIndex: -1,
|
|
22
|
+
children: [] as TOCTreeNode[],
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
// Keep track of which previous index would be the current heading's direct
|
|
26
|
+
// parent. Each entry <i> is the last index of the `headings` array at heading
|
|
27
|
+
// level <i>. We will modify these indices as we iterate through all headings.
|
|
28
|
+
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
29
|
+
// indices 0 and 1 will remain unused.
|
|
30
|
+
const prevIndexForLevel = Array<number>(7).fill(-1);
|
|
31
|
+
|
|
32
|
+
headings.forEach((curr, currIndex) => {
|
|
33
|
+
// Take the last seen index for each ancestor level. the highest index will
|
|
34
|
+
// be the direct ancestor of the current heading.
|
|
35
|
+
const ancestorLevelIndexes = prevIndexForLevel.slice(2, curr.level);
|
|
36
|
+
curr.parentIndex = Math.max(...ancestorLevelIndexes);
|
|
37
|
+
// Mark that curr.level was last seen at the current index.
|
|
38
|
+
prevIndexForLevel[curr.level] = currIndex;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const rootNodes: TOCTreeNode[] = [];
|
|
42
|
+
|
|
43
|
+
// For a given parentIndex, add each Node into that parent's `children` array
|
|
44
|
+
headings.forEach((heading) => {
|
|
45
|
+
const {parentIndex, ...rest} = heading;
|
|
46
|
+
if (parentIndex >= 0) {
|
|
47
|
+
headings[parentIndex]!.children.push(rest);
|
|
48
|
+
} else {
|
|
49
|
+
rootNodes.push(rest);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return rootNodes;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Takes a flat TOC list (from the MDX loader) and treeifies it into what the
|
|
57
|
+
* TOC components expect. Memoized for performance.
|
|
58
|
+
*/
|
|
59
|
+
export function useTreeifiedTOC(toc: TOCItem[]): readonly TOCTreeNode[] {
|
|
60
|
+
return useMemo(() => treeifyTOC(toc), [toc]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function filterTOC({
|
|
64
|
+
toc,
|
|
65
|
+
minHeadingLevel,
|
|
66
|
+
maxHeadingLevel,
|
|
67
|
+
}: {
|
|
68
|
+
toc: readonly TOCTreeNode[];
|
|
69
|
+
minHeadingLevel: number;
|
|
70
|
+
maxHeadingLevel: number;
|
|
71
|
+
}): TOCTreeNode[] {
|
|
72
|
+
function isValid(item: TOCTreeNode) {
|
|
73
|
+
return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return toc.flatMap((item) => {
|
|
77
|
+
const filteredChildren = filterTOC({
|
|
78
|
+
toc: item.children,
|
|
79
|
+
minHeadingLevel,
|
|
80
|
+
maxHeadingLevel,
|
|
81
|
+
});
|
|
82
|
+
if (isValid(item)) {
|
|
83
|
+
return [
|
|
84
|
+
{
|
|
85
|
+
...item,
|
|
86
|
+
children: filteredChildren,
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
return filteredChildren;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Takes a flat TOC list (from the MDX loader) and treeifies it into what the
|
|
96
|
+
* TOC components expect, applying the `minHeadingLevel` and `maxHeadingLevel`.
|
|
97
|
+
* Memoized for performance.
|
|
98
|
+
*
|
|
99
|
+
* **Important**: this is not the same as `useTreeifiedTOC(toc.filter(...))`,
|
|
100
|
+
* because we have to filter the TOC after it has been treeified. This is mostly
|
|
101
|
+
* to ensure that weird TOC structures preserve their semantics. For example, an
|
|
102
|
+
* h3-h2-h4 sequence should not be treeified as an "h3 > h4" hierarchy with
|
|
103
|
+
* min=3, max=4, but should rather be "[h3, h4]" (since the h2 heading has split
|
|
104
|
+
* the two headings and they are not parent-children)
|
|
105
|
+
*/
|
|
106
|
+
export function useFilteredAndTreeifiedTOC({
|
|
107
|
+
toc,
|
|
108
|
+
minHeadingLevel,
|
|
109
|
+
maxHeadingLevel,
|
|
110
|
+
}: {
|
|
111
|
+
toc: readonly TOCItem[];
|
|
112
|
+
minHeadingLevel: number;
|
|
113
|
+
maxHeadingLevel: number;
|
|
114
|
+
}): readonly TOCTreeNode[] {
|
|
115
|
+
return useMemo(
|
|
116
|
+
() => filterTOC({toc: treeifyTOC(toc), minHeadingLevel, maxHeadingLevel}),
|
|
117
|
+
[toc, minHeadingLevel, maxHeadingLevel],
|
|
118
|
+
);
|
|
119
|
+
}
|