@docusaurus/theme-common 2.0.0-beta.12faed89d → 2.0.0-beta.14

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 (156) hide show
  1. package/lib/{utils/useCollapsible.d.ts → components/Collapsible/index.d.ts} +10 -3
  2. package/lib/components/Collapsible/index.d.ts.map +1 -0
  3. package/lib/{utils/useCollapsible.js → components/Collapsible/index.js} +37 -14
  4. package/lib/components/Collapsible/index.js.map +1 -0
  5. package/lib/components/Details/index.d.ts +13 -0
  6. package/lib/components/Details/index.d.ts.map +1 -0
  7. package/lib/components/Details/index.js +65 -0
  8. package/lib/components/Details/index.js.map +1 -0
  9. package/lib/components/Details/styles.module.css +58 -0
  10. package/lib/index.d.ts +20 -5
  11. package/lib/index.d.ts.map +1 -0
  12. package/lib/index.js +15 -3
  13. package/lib/index.js.map +1 -0
  14. package/lib/utils/ThemeClassNames.d.ts +38 -12
  15. package/lib/utils/ThemeClassNames.d.ts.map +1 -0
  16. package/lib/utils/ThemeClassNames.js +38 -3
  17. package/lib/utils/ThemeClassNames.js.map +1 -0
  18. package/lib/utils/announcementBarUtils.d.ts +4 -3
  19. package/lib/utils/announcementBarUtils.d.ts.map +1 -0
  20. package/lib/utils/announcementBarUtils.js +15 -18
  21. package/lib/utils/announcementBarUtils.js.map +1 -0
  22. package/lib/utils/codeBlockUtils.d.ts +11 -0
  23. package/lib/utils/codeBlockUtils.d.ts.map +1 -0
  24. package/lib/utils/codeBlockUtils.js +120 -0
  25. package/lib/utils/codeBlockUtils.js.map +1 -0
  26. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +3 -2
  27. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +1 -0
  28. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +3 -2
  29. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +1 -0
  30. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +1 -0
  31. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +1 -0
  32. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +2 -3
  33. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +1 -0
  34. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +12 -3
  35. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +1 -0
  36. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +2 -2
  37. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +1 -0
  38. package/lib/utils/docsUtils.d.ts +21 -0
  39. package/lib/utils/docsUtils.d.ts.map +1 -0
  40. package/lib/utils/docsUtils.js +107 -0
  41. package/lib/utils/docsUtils.js.map +1 -0
  42. package/lib/utils/generalUtils.d.ts +7 -0
  43. package/lib/utils/generalUtils.d.ts.map +1 -0
  44. package/lib/utils/generalUtils.js +3 -2
  45. package/lib/utils/generalUtils.js.map +1 -0
  46. package/lib/utils/historyUtils.d.ts +12 -0
  47. package/lib/utils/historyUtils.d.ts.map +1 -0
  48. package/lib/utils/historyUtils.js +40 -0
  49. package/lib/utils/historyUtils.js.map +1 -0
  50. package/lib/utils/jsUtils.d.ts +20 -0
  51. package/lib/utils/jsUtils.d.ts.map +1 -0
  52. package/lib/utils/jsUtils.js +26 -0
  53. package/lib/utils/jsUtils.js.map +1 -0
  54. package/lib/utils/mobileSecondaryMenu.d.ts +3 -2
  55. package/lib/utils/mobileSecondaryMenu.d.ts.map +1 -0
  56. package/lib/utils/mobileSecondaryMenu.js +4 -4
  57. package/lib/utils/mobileSecondaryMenu.js.map +1 -0
  58. package/lib/utils/pathUtils.d.ts +1 -0
  59. package/lib/utils/pathUtils.d.ts.map +1 -0
  60. package/lib/utils/pathUtils.js +2 -3
  61. package/lib/utils/pathUtils.js.map +1 -0
  62. package/lib/utils/reactUtils.d.ts +10 -0
  63. package/lib/utils/reactUtils.d.ts.map +1 -0
  64. package/lib/utils/reactUtils.js +27 -0
  65. package/lib/utils/reactUtils.js.map +1 -0
  66. package/lib/utils/regexpUtils.d.ts +11 -0
  67. package/lib/utils/regexpUtils.d.ts.map +1 -0
  68. package/lib/utils/regexpUtils.js +17 -0
  69. package/lib/utils/regexpUtils.js.map +1 -0
  70. package/lib/utils/scrollUtils.d.ts +53 -0
  71. package/lib/utils/scrollUtils.d.ts.map +1 -0
  72. package/lib/utils/scrollUtils.js +136 -0
  73. package/lib/utils/scrollUtils.js.map +1 -0
  74. package/lib/utils/searchUtils.d.ts +1 -0
  75. package/lib/utils/searchUtils.d.ts.map +1 -0
  76. package/lib/utils/searchUtils.js +1 -0
  77. package/lib/utils/searchUtils.js.map +1 -0
  78. package/lib/utils/storageUtils.d.ts +5 -0
  79. package/lib/utils/storageUtils.d.ts.map +1 -0
  80. package/lib/utils/storageUtils.js +30 -3
  81. package/lib/utils/storageUtils.js.map +1 -0
  82. package/lib/utils/tagsUtils.d.ts +19 -0
  83. package/lib/utils/tagsUtils.d.ts.map +1 -0
  84. package/lib/utils/tagsUtils.js +34 -0
  85. package/lib/utils/tagsUtils.js.map +1 -0
  86. package/lib/utils/tocUtils.d.ts +16 -0
  87. package/lib/utils/tocUtils.d.ts.map +1 -0
  88. package/lib/utils/tocUtils.js +35 -0
  89. package/lib/utils/tocUtils.js.map +1 -0
  90. package/lib/utils/useAlternatePageUtils.d.ts +1 -0
  91. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
  92. package/lib/utils/useAlternatePageUtils.js +1 -0
  93. package/lib/utils/useAlternatePageUtils.js.map +1 -0
  94. package/lib/utils/useContextualSearchFilters.d.ts +12 -0
  95. package/lib/utils/useContextualSearchFilters.d.ts.map +1 -0
  96. package/lib/utils/useContextualSearchFilters.js +37 -0
  97. package/lib/utils/useContextualSearchFilters.js.map +1 -0
  98. package/{src/utils/docsUtils.ts → lib/utils/useLocalPathname.d.ts} +2 -5
  99. package/lib/utils/useLocalPathname.d.ts.map +1 -0
  100. package/lib/utils/useLocalPathname.js +17 -0
  101. package/lib/utils/useLocalPathname.js.map +1 -0
  102. package/lib/utils/useLocationChange.d.ts +1 -0
  103. package/lib/utils/useLocationChange.d.ts.map +1 -0
  104. package/lib/utils/useLocationChange.js +10 -11
  105. package/lib/utils/useLocationChange.js.map +1 -0
  106. package/lib/utils/usePluralForm.d.ts +1 -0
  107. package/lib/utils/usePluralForm.d.ts.map +1 -0
  108. package/lib/utils/usePluralForm.js +2 -3
  109. package/lib/utils/usePluralForm.js.map +1 -0
  110. package/lib/utils/usePrevious.d.ts +1 -0
  111. package/lib/utils/usePrevious.d.ts.map +1 -0
  112. package/lib/utils/usePrevious.js +4 -2
  113. package/lib/utils/usePrevious.js.map +1 -0
  114. package/lib/utils/useTOCHighlight.d.ts +15 -0
  115. package/lib/utils/useTOCHighlight.d.ts.map +1 -0
  116. package/lib/utils/useTOCHighlight.js +125 -0
  117. package/lib/utils/useTOCHighlight.js.map +1 -0
  118. package/lib/utils/useThemeConfig.d.ts +32 -9
  119. package/lib/utils/useThemeConfig.d.ts.map +1 -0
  120. package/lib/utils/useThemeConfig.js +1 -0
  121. package/lib/utils/useThemeConfig.js.map +1 -0
  122. package/package.json +18 -12
  123. package/src/{utils/useCollapsible.tsx → components/Collapsible/index.tsx} +70 -25
  124. package/src/components/Details/index.tsx +94 -0
  125. package/src/components/Details/styles.module.css +58 -0
  126. package/src/index.ts +56 -5
  127. package/src/types.d.ts +0 -2
  128. package/src/utils/ThemeClassNames.ts +43 -4
  129. package/src/utils/announcementBarUtils.tsx +20 -15
  130. package/src/utils/codeBlockUtils.ts +151 -0
  131. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +7 -6
  132. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +2 -3
  133. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +14 -14
  134. package/src/utils/docsUtils.tsx +185 -0
  135. package/src/utils/generalUtils.ts +3 -2
  136. package/src/utils/historyUtils.ts +50 -0
  137. package/src/utils/jsUtils.ts +33 -0
  138. package/src/utils/mobileSecondaryMenu.tsx +9 -6
  139. package/src/utils/pathUtils.ts +2 -3
  140. package/src/utils/reactUtils.tsx +34 -0
  141. package/src/utils/regexpUtils.ts +23 -0
  142. package/src/utils/scrollUtils.tsx +237 -0
  143. package/src/utils/storageUtils.ts +27 -4
  144. package/src/utils/tagsUtils.ts +48 -0
  145. package/src/utils/tocUtils.ts +55 -0
  146. package/src/utils/useContextualSearchFilters.ts +50 -0
  147. package/src/utils/useLocalPathname.ts +20 -0
  148. package/src/utils/useLocationChange.ts +10 -12
  149. package/src/utils/usePluralForm.ts +2 -3
  150. package/src/utils/usePrevious.ts +3 -2
  151. package/src/utils/useTOCHighlight.ts +179 -0
  152. package/src/utils/useThemeConfig.ts +34 -9
  153. package/lib/.tsbuildinfo +0 -1
  154. package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
  155. package/src/utils/__tests__/pathUtils.test.ts +0 -32
  156. package/tsconfig.json +0 -10
@@ -0,0 +1,237 @@
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
+ createContext,
10
+ ReactNode,
11
+ useCallback,
12
+ useContext,
13
+ useEffect,
14
+ useLayoutEffect,
15
+ useMemo,
16
+ useRef,
17
+ } from 'react';
18
+ import {useDynamicCallback} from './reactUtils';
19
+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
20
+
21
+ /**
22
+ * We need a way to update the scroll position while ignoring scroll events
23
+ * without affecting Navbar/BackToTop visibility
24
+ *
25
+ * This API permits to temporarily disable/ignore scroll events
26
+ * Motivated by https://github.com/facebook/docusaurus/pull/5618
27
+ */
28
+ type ScrollController = {
29
+ /**
30
+ * A boolean ref tracking whether scroll events are enabled
31
+ */
32
+ scrollEventsEnabledRef: React.MutableRefObject<boolean>;
33
+ /**
34
+ * Enables scroll events in `useScrollPosition`
35
+ */
36
+ enableScrollEvents: () => void;
37
+ /**
38
+ * Disables scroll events in `useScrollPosition`
39
+ */
40
+ disableScrollEvents: () => void;
41
+ };
42
+
43
+ function useScrollControllerContextValue(): ScrollController {
44
+ const scrollEventsEnabledRef = useRef(true);
45
+
46
+ return useMemo(
47
+ () => ({
48
+ scrollEventsEnabledRef,
49
+ enableScrollEvents: () => {
50
+ scrollEventsEnabledRef.current = true;
51
+ },
52
+ disableScrollEvents: () => {
53
+ scrollEventsEnabledRef.current = false;
54
+ },
55
+ }),
56
+ [],
57
+ );
58
+ }
59
+
60
+ const ScrollMonitorContext = createContext<ScrollController | undefined>(
61
+ undefined,
62
+ );
63
+
64
+ export function ScrollControllerProvider({
65
+ children,
66
+ }: {
67
+ children: ReactNode;
68
+ }): JSX.Element {
69
+ return (
70
+ <ScrollMonitorContext.Provider value={useScrollControllerContextValue()}>
71
+ {children}
72
+ </ScrollMonitorContext.Provider>
73
+ );
74
+ }
75
+
76
+ export function useScrollController(): ScrollController {
77
+ const context = useContext(ScrollMonitorContext);
78
+ if (context == null) {
79
+ throw new Error(
80
+ '"useScrollController" is used but no context provider was found in the React tree.',
81
+ );
82
+ }
83
+ return context;
84
+ }
85
+
86
+ const getScrollPosition = (): ScrollPosition | null =>
87
+ ExecutionEnvironment.canUseDOM
88
+ ? {
89
+ scrollX: window.pageXOffset,
90
+ scrollY: window.pageYOffset,
91
+ }
92
+ : null;
93
+
94
+ type ScrollPosition = {scrollX: number; scrollY: number};
95
+
96
+ export function useScrollPosition(
97
+ effect: (
98
+ position: ScrollPosition,
99
+ lastPosition: ScrollPosition | null,
100
+ ) => void,
101
+ deps: unknown[] = [],
102
+ ): void {
103
+ const {scrollEventsEnabledRef} = useScrollController();
104
+ const lastPositionRef = useRef<ScrollPosition | null>(getScrollPosition());
105
+
106
+ const dynamicEffect = useDynamicCallback(effect);
107
+
108
+ useEffect(() => {
109
+ const handleScroll = () => {
110
+ if (!scrollEventsEnabledRef.current) {
111
+ return;
112
+ }
113
+ const currentPosition = getScrollPosition()!;
114
+
115
+ if (dynamicEffect) {
116
+ dynamicEffect(currentPosition, lastPositionRef.current);
117
+ }
118
+
119
+ lastPositionRef.current = currentPosition;
120
+ };
121
+
122
+ const opts: AddEventListenerOptions & EventListenerOptions = {
123
+ passive: true,
124
+ };
125
+
126
+ handleScroll();
127
+ window.addEventListener('scroll', handleScroll, opts);
128
+
129
+ return () => window.removeEventListener('scroll', handleScroll, opts);
130
+ }, [
131
+ dynamicEffect,
132
+ scrollEventsEnabledRef,
133
+ // eslint-disable-next-line react-hooks/exhaustive-deps
134
+ ...deps,
135
+ ]);
136
+ }
137
+
138
+ type UseScrollPositionSaver = {
139
+ /**
140
+ * Measure the top of an element, and store the details
141
+ */
142
+ save: (elem: HTMLElement) => void;
143
+ /**
144
+ * Restore the page position to keep the stored element's position from
145
+ * the top of the viewport, and remove the stored details
146
+ */
147
+ restore: () => {restored: boolean};
148
+ };
149
+
150
+ function useScrollPositionSaver(): UseScrollPositionSaver {
151
+ const lastElementRef = useRef<{elem: HTMLElement | null; top: number}>({
152
+ elem: null,
153
+ top: 0,
154
+ });
155
+
156
+ const save = useCallback((elem: HTMLElement) => {
157
+ lastElementRef.current = {
158
+ elem,
159
+ top: elem.getBoundingClientRect().top,
160
+ };
161
+ }, []);
162
+
163
+ const restore = useCallback(() => {
164
+ const {
165
+ current: {elem, top},
166
+ } = lastElementRef;
167
+ if (!elem) {
168
+ return {restored: false};
169
+ }
170
+ const newTop = elem.getBoundingClientRect().top;
171
+ const heightDiff = newTop - top;
172
+ if (heightDiff) {
173
+ window.scrollBy({left: 0, top: heightDiff});
174
+ }
175
+ lastElementRef.current = {elem: null, top: 0};
176
+
177
+ return {restored: heightDiff !== 0};
178
+ }, []);
179
+
180
+ return useMemo(() => ({save, restore}), [restore, save]);
181
+ }
182
+
183
+ type UseScrollPositionBlockerReturn = {
184
+ blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
185
+ };
186
+
187
+ /**
188
+ * This hook permits to "block" the scroll position of a dom element
189
+ * The idea is that we should be able to update DOM content above this element
190
+ * but the screen position of this element should not change
191
+ *
192
+ * Feature motivated by the Tabs groups:
193
+ * clicking on a tab may affect tabs of the same group upper in the tree
194
+ * Yet to avoid a bad UX, the clicked tab must remain under the user mouse!
195
+ * See GIF here: https://github.com/facebook/docusaurus/pull/5618
196
+ */
197
+ export function useScrollPositionBlocker(): UseScrollPositionBlockerReturn {
198
+ const scrollController = useScrollController();
199
+ const scrollPositionSaver = useScrollPositionSaver();
200
+
201
+ const nextLayoutEffectCallbackRef = useRef<(() => void) | undefined>(
202
+ undefined,
203
+ );
204
+
205
+ const blockElementScrollPositionUntilNextRender = useCallback(
206
+ (el: HTMLElement) => {
207
+ scrollPositionSaver.save(el);
208
+ scrollController.disableScrollEvents();
209
+ nextLayoutEffectCallbackRef.current = () => {
210
+ const {restored} = scrollPositionSaver.restore();
211
+ nextLayoutEffectCallbackRef.current = undefined;
212
+
213
+ // Restoring the former scroll position will trigger a scroll event
214
+ // We need to wait for next scroll event to happen
215
+ // before enabling again the scrollController events
216
+ if (restored) {
217
+ const handleScrollRestoreEvent = () => {
218
+ scrollController.enableScrollEvents();
219
+ window.removeEventListener('scroll', handleScrollRestoreEvent);
220
+ };
221
+ window.addEventListener('scroll', handleScrollRestoreEvent);
222
+ } else {
223
+ scrollController.enableScrollEvents();
224
+ }
225
+ };
226
+ },
227
+ [scrollController, scrollPositionSaver],
228
+ );
229
+
230
+ useLayoutEffect(() => {
231
+ nextLayoutEffectCallbackRef.current?.();
232
+ });
233
+
234
+ return {
235
+ blockElementScrollPositionUntilNextRender,
236
+ };
237
+ }
@@ -27,7 +27,7 @@ function getBrowserStorage(
27
27
  try {
28
28
  return window[storageType];
29
29
  } catch (e) {
30
- logOnceBrowserStorageNotAvailableWarning(e);
30
+ logOnceBrowserStorageNotAvailableWarning(e as Error);
31
31
  return null;
32
32
  }
33
33
  }
@@ -79,6 +79,10 @@ Please only call storage APIs in effects and event handlers.`);
79
79
 
80
80
  /**
81
81
  * Creates an object for accessing a particular key in localStorage.
82
+ * The API is fail-safe, and usage of browser storage should be considered unreliable
83
+ * Local storage might simply be unavailable (iframe + browser security) or operations might fail individually
84
+ * Please assume that using this API can be a NO-OP
85
+ * See also https://github.com/facebook/docusaurus/issues/6036
82
86
  */
83
87
  export const createStorageSlot = (
84
88
  key: string,
@@ -92,9 +96,28 @@ export const createStorageSlot = (
92
96
  return NoopStorageSlot;
93
97
  }
94
98
  return {
95
- get: () => browserStorage.getItem(key),
96
- set: (value) => browserStorage.setItem(key, value),
97
- del: () => browserStorage.removeItem(key),
99
+ get: () => {
100
+ try {
101
+ return browserStorage.getItem(key);
102
+ } catch (e) {
103
+ console.error(`Docusaurus storage error, can't get key=${key}`, e);
104
+ return null;
105
+ }
106
+ },
107
+ set: (value) => {
108
+ try {
109
+ browserStorage.setItem(key, value);
110
+ } catch (e) {
111
+ console.error(`Docusaurus storage error, can't set ${key}=${value}`, e);
112
+ }
113
+ },
114
+ del: () => {
115
+ try {
116
+ browserStorage.removeItem(key);
117
+ } catch (e) {
118
+ console.error(`Docusaurus storage error, can't delete key=${key}`, e);
119
+ }
120
+ },
98
121
  };
99
122
  };
100
123
 
@@ -0,0 +1,48 @@
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
+
10
+ export const translateTagsPageTitle = (): string =>
11
+ translate({
12
+ id: 'theme.tags.tagsPageTitle',
13
+ message: 'Tags',
14
+ description: 'The title of the tag list page',
15
+ });
16
+
17
+ type TagsListItem = Readonly<{name: string; permalink: string; count: number}>; // TODO remove duplicated type :s
18
+
19
+ export type TagLetterEntry = Readonly<{letter: string; tags: TagsListItem[]}>;
20
+
21
+ function getTagLetter(tag: string): string {
22
+ return tag[0].toUpperCase();
23
+ }
24
+
25
+ export function listTagsByLetters(
26
+ tags: readonly TagsListItem[],
27
+ ): TagLetterEntry[] {
28
+ // Group by letters
29
+ const groups: Record<string, TagsListItem[]> = {};
30
+ Object.values(tags).forEach((tag) => {
31
+ const letter = getTagLetter(tag.name);
32
+ groups[letter] = groups[letter] ?? [];
33
+ groups[letter].push(tag);
34
+ });
35
+
36
+ return (
37
+ Object.entries(groups)
38
+ // Sort letters
39
+ .sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
40
+ .map(([letter, letterTags]) => {
41
+ // Sort tags inside a letter
42
+ const sortedTags = letterTags.sort((tag1, tag2) =>
43
+ tag1.name.localeCompare(tag2.name),
44
+ );
45
+ return {letter, tags: sortedTags};
46
+ })
47
+ );
48
+ }
@@ -0,0 +1,55 @@
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 {TOCItem} from '@docusaurus/types';
10
+
11
+ type FilterTOCParam = {
12
+ toc: readonly TOCItem[];
13
+ minHeadingLevel: number;
14
+ maxHeadingLevel: number;
15
+ };
16
+
17
+ export function filterTOC({
18
+ toc,
19
+ minHeadingLevel,
20
+ maxHeadingLevel,
21
+ }: FilterTOCParam): TOCItem[] {
22
+ function isValid(item: TOCItem) {
23
+ return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
24
+ }
25
+
26
+ return toc.flatMap((item) => {
27
+ const filteredChildren = filterTOC({
28
+ toc: item.children,
29
+ minHeadingLevel,
30
+ maxHeadingLevel,
31
+ });
32
+ if (isValid(item)) {
33
+ return [
34
+ {
35
+ ...item,
36
+ children: filteredChildren,
37
+ },
38
+ ];
39
+ } else {
40
+ return filteredChildren;
41
+ }
42
+ });
43
+ }
44
+
45
+ // Memoize potentially expensive filtering logic
46
+ export function useTOCFilter({
47
+ toc,
48
+ minHeadingLevel,
49
+ maxHeadingLevel,
50
+ }: FilterTOCParam): readonly TOCItem[] {
51
+ return useMemo(
52
+ () => filterTOC({toc, minHeadingLevel, maxHeadingLevel}),
53
+ [toc, minHeadingLevel, maxHeadingLevel],
54
+ );
55
+ }
@@ -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 {useAllDocsData, useActivePluginAndVersion} from '@theme/hooks/useDocs';
9
+ import {useDocsPreferredVersionByPluginId} from './docsPreferredVersion/useDocsPreferredVersion';
10
+ import {docVersionSearchTag, DEFAULT_SEARCH_TAG} from './searchUtils';
11
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
12
+
13
+ export type useContextualSearchFiltersReturns = {
14
+ locale: string;
15
+ tags: string[];
16
+ };
17
+
18
+ // We may want to support multiple search engines, don't couple that to Algolia/DocSearch
19
+ // Maybe users will want to use its own search engine solution
20
+ export function useContextualSearchFilters(): useContextualSearchFiltersReturns {
21
+ const {i18n} = useDocusaurusContext();
22
+ const allDocsData = useAllDocsData();
23
+ const activePluginAndVersion = useActivePluginAndVersion();
24
+ const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
25
+
26
+ function getDocPluginTags(pluginId: string) {
27
+ const activeVersion =
28
+ activePluginAndVersion?.activePlugin?.pluginId === pluginId
29
+ ? activePluginAndVersion.activeVersion
30
+ : undefined;
31
+
32
+ const preferredVersion = docsPreferredVersionByPluginId[pluginId];
33
+
34
+ const latestVersion = allDocsData[pluginId].versions.find((v) => v.isLast)!;
35
+
36
+ const version = activeVersion ?? preferredVersion ?? latestVersion;
37
+
38
+ return docVersionSearchTag(pluginId, version.name);
39
+ }
40
+
41
+ const tags = [
42
+ DEFAULT_SEARCH_TAG,
43
+ ...Object.keys(allDocsData).map(getDocPluginTags),
44
+ ];
45
+
46
+ return {
47
+ locale: i18n.currentLocale,
48
+ tags,
49
+ };
50
+ }
@@ -0,0 +1,20 @@
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 useDocusaurusContext from '@docusaurus/useDocusaurusContext';
9
+ import {useLocation} from '@docusaurus/router';
10
+
11
+ // Get the pathname of current route, without the optional site baseUrl
12
+ // - /docs/myDoc => /docs/myDoc
13
+ // - /baseUrl/docs/myDoc => /docs/myDoc
14
+ export function useLocalPathname(): string {
15
+ const {
16
+ siteConfig: {baseUrl},
17
+ } = useDocusaurusContext();
18
+ const {pathname} = useLocation();
19
+ return pathname.replace(baseUrl, '/');
20
+ }
@@ -5,10 +5,11 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {useEffect, useRef} from 'react';
8
+ import {useEffect} from 'react';
9
9
  import {useLocation} from '@docusaurus/router';
10
10
  import {Location} from '@docusaurus/history';
11
11
  import {usePrevious} from './usePrevious';
12
+ import {useDynamicCallback} from './reactUtils';
12
13
 
13
14
  type LocationChangeEvent = {
14
15
  location: Location;
@@ -20,18 +21,15 @@ type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
20
21
  export function useLocationChange(onLocationChange: OnLocationChange): void {
21
22
  const location = useLocation();
22
23
  const previousLocation = usePrevious(location);
23
- const isFirst = useRef<boolean>(true);
24
+
25
+ const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
24
26
 
25
27
  useEffect(() => {
26
- // Prevent first effect to trigger the listener on mount
27
- if (isFirst.current) {
28
- isFirst.current = false;
29
- return;
28
+ if (location !== previousLocation) {
29
+ onLocationChangeDynamic({
30
+ location,
31
+ previousLocation,
32
+ });
30
33
  }
31
-
32
- onLocationChange({
33
- location,
34
- previousLocation,
35
- });
36
- }, [location]);
34
+ }, [onLocationChangeDynamic, location, previousLocation]);
37
35
  }
@@ -112,8 +112,7 @@ export function usePluralForm(): {
112
112
  } {
113
113
  const localePluralForm = useLocalePluralForms();
114
114
  return {
115
- selectMessage: (count: number, pluralMessages: string): string => {
116
- return selectPluralMessage(pluralMessages, count, localePluralForm);
117
- },
115
+ selectMessage: (count: number, pluralMessages: string): string =>
116
+ selectPluralMessage(pluralMessages, count, localePluralForm),
118
117
  };
119
118
  }
@@ -5,12 +5,13 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {useRef, useEffect} from 'react';
8
+ import {useRef} from 'react';
9
+ import {useIsomorphicLayoutEffect} from './reactUtils';
9
10
 
10
11
  export function usePrevious<T>(value: T): T | undefined {
11
12
  const ref = useRef<T>();
12
13
 
13
- useEffect(() => {
14
+ useIsomorphicLayoutEffect(() => {
14
15
  ref.current = value;
15
16
  });
16
17