@azerion/bunosaurus-theme-common 0.0.0-stage → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/README.md +2 -2
  2. package/lib/components/Collapsible/index.d.ts +59 -0
  3. package/lib/components/Collapsible/index.d.ts.map +1 -0
  4. package/lib/components/Collapsible/index.js +145 -0
  5. package/lib/components/Collapsible/index.js.map +1 -0
  6. package/lib/components/Details/index.d.ts +20 -0
  7. package/lib/components/Details/index.d.ts.map +1 -0
  8. package/lib/components/Details/index.js +86 -0
  9. package/lib/components/Details/index.js.map +1 -0
  10. package/lib/components/Details/styles.module.css +66 -0
  11. package/lib/components/ThemedComponent/index.d.ts +31 -0
  12. package/lib/components/ThemedComponent/index.d.ts.map +1 -0
  13. package/lib/components/ThemedComponent/index.js +47 -0
  14. package/lib/components/ThemedComponent/index.js.map +1 -0
  15. package/lib/components/ThemedComponent/styles.module.css +26 -0
  16. package/lib/contexts/announcementBar.d.ts +21 -0
  17. package/lib/contexts/announcementBar.d.ts.map +1 -0
  18. package/lib/contexts/announcementBar.js +72 -0
  19. package/lib/contexts/announcementBar.js.map +1 -0
  20. package/lib/contexts/colorMode.d.ts +23 -0
  21. package/lib/contexts/colorMode.d.ts.map +1 -0
  22. package/lib/contexts/colorMode.js +163 -0
  23. package/lib/contexts/colorMode.js.map +1 -0
  24. package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
  25. package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
  26. package/lib/contexts/navbarMobileSidebar.js +65 -0
  27. package/lib/contexts/navbarMobileSidebar.js.map +1 -0
  28. package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
  29. package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
  30. package/lib/contexts/navbarSecondaryMenu/content.js +50 -0
  31. package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
  32. package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
  33. package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
  34. package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
  35. package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
  36. package/lib/hooks/useBackToTopButton.d.ts +27 -0
  37. package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
  38. package/lib/hooks/useBackToTopButton.js +50 -0
  39. package/lib/hooks/useBackToTopButton.js.map +1 -0
  40. package/lib/hooks/useCodeWordWrap.d.ts +15 -0
  41. package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
  42. package/lib/hooks/useCodeWordWrap.js +67 -0
  43. package/lib/hooks/useCodeWordWrap.js.map +1 -0
  44. package/lib/hooks/useHideableNavbar.d.ts +17 -0
  45. package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
  46. package/lib/hooks/useHideableNavbar.js +65 -0
  47. package/lib/hooks/useHideableNavbar.js.map +1 -0
  48. package/lib/hooks/useLockBodyScroll.d.ts +12 -0
  49. package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
  50. package/lib/hooks/useLockBodyScroll.js +20 -0
  51. package/lib/hooks/useLockBodyScroll.js.map +1 -0
  52. package/lib/hooks/useMutationObserver.d.ts +4 -0
  53. package/lib/hooks/useMutationObserver.d.ts.map +1 -0
  54. package/lib/hooks/useMutationObserver.js +29 -0
  55. package/lib/hooks/useMutationObserver.js.map +1 -0
  56. package/lib/hooks/usePrismTheme.d.ts +13 -0
  57. package/lib/hooks/usePrismTheme.d.ts.map +1 -0
  58. package/lib/hooks/usePrismTheme.js +21 -0
  59. package/lib/hooks/usePrismTheme.js.map +1 -0
  60. package/lib/hooks/useTOCHighlight.d.ts +25 -0
  61. package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
  62. package/lib/hooks/useTOCHighlight.js +130 -0
  63. package/lib/hooks/useTOCHighlight.js.map +1 -0
  64. package/lib/hooks/useWindowSize.d.ts +27 -0
  65. package/lib/hooks/useWindowSize.d.ts.map +1 -0
  66. package/lib/hooks/useWindowSize.js +49 -0
  67. package/lib/hooks/useWindowSize.js.map +1 -0
  68. package/lib/index.d.ts +30 -0
  69. package/lib/index.d.ts.map +1 -0
  70. package/lib/index.js +37 -0
  71. package/lib/index.js.map +1 -0
  72. package/lib/internal.d.ts +35 -0
  73. package/lib/internal.d.ts.map +1 -0
  74. package/lib/internal.js +45 -0
  75. package/lib/internal.js.map +1 -0
  76. package/lib/translations/blogTranslations.d.ts +20 -0
  77. package/lib/translations/blogTranslations.d.ts.map +1 -0
  78. package/lib/translations/blogTranslations.js +52 -0
  79. package/lib/translations/blogTranslations.js.map +1 -0
  80. package/lib/translations/contentVisibilityTranslations.d.ts +12 -0
  81. package/lib/translations/contentVisibilityTranslations.d.ts.map +1 -0
  82. package/lib/translations/contentVisibilityTranslations.js +30 -0
  83. package/lib/translations/contentVisibilityTranslations.js.map +1 -0
  84. package/lib/translations/docsTranslations.d.ts +8 -0
  85. package/lib/translations/docsTranslations.d.ts.map +1 -0
  86. package/lib/translations/docsTranslations.js +17 -0
  87. package/lib/translations/docsTranslations.js.map +1 -0
  88. package/lib/utils/IntlUtils.d.ts +2 -0
  89. package/lib/utils/IntlUtils.d.ts.map +1 -0
  90. package/lib/utils/IntlUtils.js +20 -0
  91. package/lib/utils/IntlUtils.js.map +1 -0
  92. package/lib/utils/ThemeClassNames.d.ts +99 -0
  93. package/lib/utils/ThemeClassNames.d.ts.map +1 -0
  94. package/lib/utils/ThemeClassNames.js +107 -0
  95. package/lib/utils/ThemeClassNames.js.map +1 -0
  96. package/lib/utils/accessibilityUtils.d.ts +8 -0
  97. package/lib/utils/accessibilityUtils.d.ts.map +1 -0
  98. package/lib/utils/accessibilityUtils.js +10 -0
  99. package/lib/utils/accessibilityUtils.js.map +1 -0
  100. package/lib/utils/admonitionUtils.d.ts +12 -0
  101. package/lib/utils/admonitionUtils.d.ts.map +1 -0
  102. package/lib/utils/admonitionUtils.js +32 -0
  103. package/lib/utils/admonitionUtils.js.map +1 -0
  104. package/lib/utils/anchorUtils.d.ts +16 -0
  105. package/lib/utils/anchorUtils.d.ts.map +1 -0
  106. package/lib/utils/anchorUtils.js +26 -0
  107. package/lib/utils/anchorUtils.js.map +1 -0
  108. package/lib/utils/anchorUtils.module.css +14 -0
  109. package/lib/utils/codeBlockUtils.d.ts +101 -0
  110. package/lib/utils/codeBlockUtils.d.ts.map +1 -0
  111. package/lib/utils/codeBlockUtils.js +310 -0
  112. package/lib/utils/codeBlockUtils.js.map +1 -0
  113. package/lib/utils/emojiUtils.d.ts +20 -0
  114. package/lib/utils/emojiUtils.d.ts.map +1 -0
  115. package/lib/utils/emojiUtils.js +32 -0
  116. package/lib/utils/emojiUtils.js.map +1 -0
  117. package/lib/utils/errorBoundaryUtils.d.ts +31 -0
  118. package/lib/utils/errorBoundaryUtils.d.ts.map +1 -0
  119. package/lib/utils/errorBoundaryUtils.js +48 -0
  120. package/lib/utils/errorBoundaryUtils.js.map +1 -0
  121. package/lib/utils/errorBoundaryUtils.module.css +16 -0
  122. package/lib/utils/footerUtils.d.ts +13 -0
  123. package/lib/utils/footerUtils.d.ts.map +1 -0
  124. package/lib/utils/footerUtils.js +14 -0
  125. package/lib/utils/footerUtils.js.map +1 -0
  126. package/lib/utils/historyUtils.d.ts +39 -0
  127. package/lib/utils/historyUtils.d.ts.map +1 -0
  128. package/lib/utils/historyUtils.js +148 -0
  129. package/lib/utils/historyUtils.js.map +1 -0
  130. package/lib/utils/jsUtils.d.ts +24 -0
  131. package/lib/utils/jsUtils.d.ts.map +1 -0
  132. package/lib/utils/jsUtils.js +43 -0
  133. package/lib/utils/jsUtils.js.map +1 -0
  134. package/lib/utils/metadataUtils.d.ts +38 -0
  135. package/lib/utils/metadataUtils.d.ts.map +1 -0
  136. package/lib/utils/metadataUtils.js +87 -0
  137. package/lib/utils/metadataUtils.js.map +1 -0
  138. package/lib/utils/navbarUtils.d.ts +21 -0
  139. package/lib/utils/navbarUtils.d.ts.map +1 -0
  140. package/lib/utils/navbarUtils.js +35 -0
  141. package/lib/utils/navbarUtils.js.map +1 -0
  142. package/lib/utils/reactUtils.d.ts +60 -0
  143. package/lib/utils/reactUtils.d.ts.map +1 -0
  144. package/lib/utils/reactUtils.js +91 -0
  145. package/lib/utils/reactUtils.js.map +1 -0
  146. package/lib/utils/regexpUtils.d.ts +12 -0
  147. package/lib/utils/regexpUtils.d.ts.map +1 -0
  148. package/lib/utils/regexpUtils.js +18 -0
  149. package/lib/utils/regexpUtils.js.map +1 -0
  150. package/lib/utils/routesUtils.d.ts +26 -0
  151. package/lib/utils/routesUtils.d.ts.map +1 -0
  152. package/lib/utils/routesUtils.js +54 -0
  153. package/lib/utils/routesUtils.js.map +1 -0
  154. package/lib/utils/scrollUtils.d.ts +83 -0
  155. package/lib/utils/scrollUtils.d.ts.map +1 -0
  156. package/lib/utils/scrollUtils.js +201 -0
  157. package/lib/utils/scrollUtils.js.map +1 -0
  158. package/lib/utils/searchUtils.d.ts +8 -0
  159. package/lib/utils/searchUtils.d.ts.map +1 -0
  160. package/lib/utils/searchUtils.js +8 -0
  161. package/lib/utils/searchUtils.js.map +1 -0
  162. package/lib/utils/skipToContentUtils.d.ts +20 -0
  163. package/lib/utils/skipToContentUtils.d.ts.map +1 -0
  164. package/lib/utils/skipToContentUtils.js +75 -0
  165. package/lib/utils/skipToContentUtils.js.map +1 -0
  166. package/lib/utils/storageUtils.d.ts +36 -0
  167. package/lib/utils/storageUtils.d.ts.map +1 -0
  168. package/lib/utils/storageUtils.js +193 -0
  169. package/lib/utils/storageUtils.js.map +1 -0
  170. package/lib/utils/tabsUtils.d.ts +55 -0
  171. package/lib/utils/tabsUtils.d.ts.map +1 -0
  172. package/lib/utils/tabsUtils.js +179 -0
  173. package/lib/utils/tabsUtils.js.map +1 -0
  174. package/lib/utils/tagsUtils.d.ts +20 -0
  175. package/lib/utils/tagsUtils.d.ts.map +1 -0
  176. package/lib/utils/tagsUtils.js +41 -0
  177. package/lib/utils/tagsUtils.js.map +1 -0
  178. package/lib/utils/titleFormatterUtils.d.ts +59 -0
  179. package/lib/utils/titleFormatterUtils.d.ts.map +1 -0
  180. package/lib/utils/titleFormatterUtils.js +49 -0
  181. package/lib/utils/titleFormatterUtils.js.map +1 -0
  182. package/lib/utils/tocUtils.d.ts +36 -0
  183. package/lib/utils/tocUtils.d.ts.map +1 -0
  184. package/lib/utils/tocUtils.js +84 -0
  185. package/lib/utils/tocUtils.js.map +1 -0
  186. package/lib/utils/useAlternatePageUtils.d.ts +32 -0
  187. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
  188. package/lib/utils/useAlternatePageUtils.js +42 -0
  189. package/lib/utils/useAlternatePageUtils.js.map +1 -0
  190. package/lib/utils/useLocalPathname.d.ts +13 -0
  191. package/lib/utils/useLocalPathname.d.ts.map +1 -0
  192. package/lib/utils/useLocalPathname.js +19 -0
  193. package/lib/utils/useLocalPathname.js.map +1 -0
  194. package/lib/utils/useLocationChange.d.ts +17 -0
  195. package/lib/utils/useLocationChange.d.ts.map +1 -0
  196. package/lib/utils/useLocationChange.js +31 -0
  197. package/lib/utils/useLocationChange.js.map +1 -0
  198. package/lib/utils/usePluralForm.d.ts +21 -0
  199. package/lib/utils/usePluralForm.d.ts.map +1 -0
  200. package/lib/utils/usePluralForm.js +93 -0
  201. package/lib/utils/usePluralForm.js.map +1 -0
  202. package/lib/utils/useThemeConfig.d.ts +120 -0
  203. package/lib/utils/useThemeConfig.d.ts.map +1 -0
  204. package/lib/utils/useThemeConfig.js +14 -0
  205. package/lib/utils/useThemeConfig.js.map +1 -0
  206. package/package.json +58 -4
  207. package/src/components/Collapsible/index.tsx +250 -0
  208. package/src/components/Details/index.tsx +130 -0
  209. package/src/components/Details/styles.module.css +66 -0
  210. package/src/components/ThemedComponent/index.tsx +75 -0
  211. package/src/components/ThemedComponent/styles.module.css +26 -0
  212. package/src/contexts/announcementBar.tsx +117 -0
  213. package/src/contexts/colorMode.tsx +257 -0
  214. package/src/contexts/navbarMobileSidebar.tsx +119 -0
  215. package/src/contexts/navbarSecondaryMenu/content.tsx +100 -0
  216. package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
  217. package/src/hooks/useBackToTopButton.ts +73 -0
  218. package/src/hooks/useCodeWordWrap.ts +107 -0
  219. package/src/hooks/useHideableNavbar.ts +81 -0
  220. package/src/hooks/useLockBodyScroll.ts +21 -0
  221. package/src/hooks/useMutationObserver.ts +38 -0
  222. package/src/hooks/usePrismTheme.ts +24 -0
  223. package/src/hooks/useTOCHighlight.ts +189 -0
  224. package/src/hooks/useWindowSize.ts +67 -0
  225. package/src/index.ts +112 -0
  226. package/src/internal.ts +107 -0
  227. package/src/translations/blogTranslations.tsx +89 -0
  228. package/src/translations/contentVisibilityTranslations.tsx +51 -0
  229. package/src/translations/docsTranslations.tsx +28 -0
  230. package/src/types.d.ts +8 -0
  231. package/src/utils/IntlUtils.ts +27 -0
  232. package/src/utils/ThemeClassNames.ts +119 -0
  233. package/src/utils/accessibilityUtils.ts +10 -0
  234. package/src/utils/admonitionUtils.tsx +44 -0
  235. package/src/utils/anchorUtils.module.css +14 -0
  236. package/src/utils/anchorUtils.ts +31 -0
  237. package/src/utils/codeBlockUtils.tsx +494 -0
  238. package/src/utils/emojiUtils.ts +40 -0
  239. package/src/utils/errorBoundaryUtils.module.css +16 -0
  240. package/src/utils/errorBoundaryUtils.tsx +71 -0
  241. package/src/utils/footerUtils.ts +18 -0
  242. package/src/utils/historyUtils.ts +199 -0
  243. package/src/utils/jsUtils.ts +54 -0
  244. package/src/utils/metadataUtils.tsx +145 -0
  245. package/src/utils/navbarUtils.tsx +45 -0
  246. package/src/utils/reactUtils.tsx +120 -0
  247. package/src/utils/regexpUtils.ts +24 -0
  248. package/src/utils/routesUtils.ts +75 -0
  249. package/src/utils/scrollUtils.tsx +303 -0
  250. package/src/utils/searchUtils.ts +8 -0
  251. package/src/utils/skipToContentUtils.tsx +108 -0
  252. package/src/utils/storageUtils.ts +260 -0
  253. package/src/utils/tabsUtils.tsx +307 -0
  254. package/src/utils/tagsUtils.tsx +61 -0
  255. package/src/utils/titleFormatterUtils.tsx +120 -0
  256. package/src/utils/tocUtils.ts +119 -0
  257. package/src/utils/useAlternatePageUtils.ts +80 -0
  258. package/src/utils/useLocalPathname.ts +22 -0
  259. package/src/utils/useLocationChange.ts +41 -0
  260. package/src/utils/usePluralForm.ts +131 -0
  261. package/src/utils/useThemeConfig.ts +147 -0
@@ -0,0 +1,199 @@
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, useMemo, useSyncExternalStore} from 'react';
9
+ import {useHistory} from '@docusaurus/router';
10
+ import {useEvent} from './reactUtils';
11
+
12
+ import type {History, Location, Action} from 'history';
13
+
14
+ type HistoryBlockHandler = (location: Location, action: Action) => void | false;
15
+
16
+ /**
17
+ * Permits to register a handler that will be called on history actions (pop,
18
+ * push, replace). If the handler returns `false`, the navigation transition
19
+ * will be blocked/cancelled.
20
+ */
21
+ function useHistoryActionHandler(handler: HistoryBlockHandler): void {
22
+ const history = useHistory();
23
+ const stableHandler = useEvent(handler);
24
+ useEffect(
25
+ // See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
26
+ () => history.block((location, action) => stableHandler(location, action)),
27
+ [history, stableHandler],
28
+ );
29
+ }
30
+
31
+ /**
32
+ * Permits to register a handler that will be called on history pop navigation
33
+ * (backward/forward). If the handler returns `false`, the backward/forward
34
+ * transition will be blocked. Unfortunately there's no good way to detect the
35
+ * "direction" (backward/forward) of the POP event.
36
+ */
37
+ export function useHistoryPopHandler(handler: HistoryBlockHandler): void {
38
+ useHistoryActionHandler((location, action) => {
39
+ if (action === 'POP') {
40
+ // Maybe block navigation if handler returns false
41
+ return handler(location, action);
42
+ }
43
+ // Don't block other navigation actions
44
+ return undefined;
45
+ });
46
+ }
47
+
48
+ /**
49
+ * Permits to efficiently subscribe to a slice of the history
50
+ * See https://thisweekinreact.com/articles/useSyncExternalStore-the-underrated-react-api
51
+ * @param selector
52
+ */
53
+ export function useHistorySelector<Value>(
54
+ selector: (history: History<unknown>) => Value,
55
+ ): Value {
56
+ const history = useHistory();
57
+ return useSyncExternalStore(
58
+ history.listen,
59
+ () => selector(history),
60
+ () =>
61
+ selector({
62
+ ...history,
63
+ location: {
64
+ ...history.location,
65
+ // On the server/hydration, these attributes should always be empty
66
+ // Forcing empty state makes this hook safe from hydration errors
67
+ search: '',
68
+ hash: '',
69
+ state: undefined,
70
+ },
71
+ }),
72
+ );
73
+ }
74
+
75
+ /**
76
+ * Permits to efficiently subscribe to a specific querystring value
77
+ * @param key
78
+ */
79
+ export function useQueryStringValue(key: string | null): string | null {
80
+ return useHistorySelector((history) => {
81
+ if (key === null) {
82
+ return null;
83
+ }
84
+ return new URLSearchParams(history.location.search).get(key);
85
+ });
86
+ }
87
+
88
+ function useQueryStringUpdater(
89
+ key: string,
90
+ ): (newValue: string | null, options?: {push: boolean}) => void {
91
+ const history = useHistory();
92
+ return useCallback(
93
+ (newValue, options) => {
94
+ const searchParams = new URLSearchParams(history.location.search);
95
+ if (newValue) {
96
+ searchParams.set(key, newValue);
97
+ } else {
98
+ searchParams.delete(key);
99
+ }
100
+ const updateHistory = options?.push ? history.push : history.replace;
101
+ updateHistory({
102
+ search: searchParams.toString(),
103
+ });
104
+ },
105
+ [key, history],
106
+ );
107
+ }
108
+
109
+ export function useQueryString(
110
+ key: string,
111
+ ): [string, (newValue: string | null, options?: {push: boolean}) => void] {
112
+ const value = useQueryStringValue(key) ?? '';
113
+ const update = useQueryStringUpdater(key);
114
+ return [value, update];
115
+ }
116
+
117
+ function useQueryStringListValues(key: string): string[] {
118
+ // Unfortunately we can't just use searchParams.getAll(key) in the selector
119
+ // It would create a new array every time and lead to an infinite loop...
120
+ // The selector has to return a primitive/string value to avoid that...
121
+ const arrayJsonString = useHistorySelector((history) => {
122
+ const values = new URLSearchParams(history.location.search).getAll(key);
123
+ return JSON.stringify(values);
124
+ });
125
+ return useMemo(() => JSON.parse(arrayJsonString), [arrayJsonString]);
126
+ }
127
+
128
+ type ListUpdate = string[] | ((oldValues: string[]) => string[]);
129
+ type ListUpdateFunction = (
130
+ update: ListUpdate,
131
+ options?: {push: boolean},
132
+ ) => void;
133
+
134
+ function useQueryStringListUpdater(key: string): ListUpdateFunction {
135
+ const history = useHistory();
136
+ const setValues: ListUpdateFunction = useCallback(
137
+ (update, options) => {
138
+ const searchParams = new URLSearchParams(history.location.search);
139
+ const newValues = Array.isArray(update)
140
+ ? update
141
+ : update(searchParams.getAll(key));
142
+ searchParams.delete(key);
143
+ newValues.forEach((v) => searchParams.append(key, v));
144
+
145
+ const updateHistory = options?.push ? history.push : history.replace;
146
+ updateHistory({
147
+ search: searchParams.toString(),
148
+ });
149
+ },
150
+ [history, key],
151
+ );
152
+ return setValues;
153
+ }
154
+
155
+ export function useQueryStringList(
156
+ key: string,
157
+ ): [string[], ListUpdateFunction] {
158
+ const values = useQueryStringListValues(key);
159
+ const setValues = useQueryStringListUpdater(key);
160
+ return [values, setValues];
161
+ }
162
+
163
+ export function useClearQueryString(): () => void {
164
+ const history = useHistory();
165
+ return useCallback(() => {
166
+ history.replace({
167
+ search: undefined,
168
+ });
169
+ }, [history]);
170
+ }
171
+
172
+ export function mergeSearchParams(
173
+ params: URLSearchParams[],
174
+ strategy: 'append' | 'set',
175
+ ): URLSearchParams {
176
+ const result = new URLSearchParams();
177
+ for (const item of params) {
178
+ for (const [key, value] of item.entries()) {
179
+ if (strategy === 'append') {
180
+ result.append(key, value);
181
+ } else {
182
+ result.set(key, value);
183
+ }
184
+ }
185
+ }
186
+ return result;
187
+ }
188
+
189
+ export function mergeSearchStrings(
190
+ searchStrings: (string | undefined)[],
191
+ strategy: 'append' | 'set',
192
+ ): string {
193
+ const params = mergeSearchParams(
194
+ searchStrings.map((s) => new URLSearchParams(s ?? '')),
195
+ strategy,
196
+ );
197
+ const str = params.toString();
198
+ return str ? `?${str}` : str;
199
+ }
@@ -0,0 +1,54 @@
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
+ }
37
+
38
+ // TODO 2025: replace by std Object.groupBy ?
39
+ // This is a local polyfill with exact same TS signature
40
+ // see https://github.com/microsoft/TypeScript/blob/main/src/lib/esnext.object.d.ts
41
+ export function groupBy<K extends PropertyKey, T>(
42
+ items: Iterable<T>,
43
+ keySelector: (item: T, index: number) => K,
44
+ ): Partial<Record<K, T[]>> {
45
+ const result: Partial<Record<K, T[]>> = {};
46
+ let index = 0;
47
+ for (const item of items) {
48
+ const key = keySelector(item, index);
49
+ result[key] ??= [];
50
+ result[key]!.push(item);
51
+ index += 1;
52
+ }
53
+ return result;
54
+ }
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode} from 'react';
9
+ import clsx from 'clsx';
10
+ import Head from '@docusaurus/Head';
11
+ import useRouteContext from '@docusaurus/useRouteContext';
12
+ import {useBaseUrlUtils} from '@docusaurus/useBaseUrl';
13
+ import {useTitleFormatter} from './titleFormatterUtils';
14
+
15
+ type PageMetadataProps = {
16
+ readonly title?: string;
17
+ readonly description?: string;
18
+ readonly keywords?: readonly string[] | string;
19
+ readonly image?: string;
20
+ readonly children?: ReactNode;
21
+ };
22
+
23
+ function TitleMetadata({title}: {title: string}) {
24
+ const titleFormatter = useTitleFormatter();
25
+ const formattedTitle = titleFormatter.format(title);
26
+ return (
27
+ <Head>
28
+ <title>{formattedTitle}</title>
29
+ <meta property="og:title" content={formattedTitle} />
30
+ </Head>
31
+ );
32
+ }
33
+
34
+ function DescriptionMetadata({description}: {description: string}) {
35
+ return (
36
+ <Head>
37
+ <meta name="description" content={description} />
38
+ <meta property="og:description" content={description} />
39
+ </Head>
40
+ );
41
+ }
42
+
43
+ function ImageMetadata({image}: {image: string}) {
44
+ const {withBaseUrl} = useBaseUrlUtils();
45
+ const pageImage = withBaseUrl(image, {absolute: true});
46
+ return (
47
+ <Head>
48
+ <meta property="og:image" content={pageImage} />
49
+ <meta name="twitter:image" content={pageImage} />
50
+ </Head>
51
+ );
52
+ }
53
+
54
+ function KeywordsMetadata({
55
+ keywords,
56
+ }: {
57
+ keywords: PageMetadataProps['keywords'];
58
+ }) {
59
+ return (
60
+ <Head>
61
+ <meta
62
+ name="keywords"
63
+ content={
64
+ // https://github.com/microsoft/TypeScript/issues/17002
65
+ (Array.isArray(keywords) ? keywords.join(',') : keywords) as string
66
+ }
67
+ />
68
+ </Head>
69
+ );
70
+ }
71
+
72
+ /**
73
+ * Helper component to manipulate page metadata and override site defaults.
74
+ * Works in the same way as Helmet.
75
+ */
76
+ export function PageMetadata({
77
+ title,
78
+ description,
79
+ keywords,
80
+ image,
81
+ children,
82
+ }: PageMetadataProps): ReactNode {
83
+ return (
84
+ <>
85
+ {title && <TitleMetadata title={title} />}
86
+ {description && <DescriptionMetadata description={description} />}
87
+ {keywords && <KeywordsMetadata keywords={keywords} />}
88
+ {image && <ImageMetadata image={image} />}
89
+ {children && <Head>{children}</Head>}
90
+ </>
91
+ );
92
+ }
93
+
94
+ const HtmlClassNameContext = React.createContext<string | undefined>(undefined);
95
+
96
+ /**
97
+ * Every layer of this provider will append a class name to the HTML element.
98
+ * There's no consumer for this hook: it's side-effect-only. This wrapper is
99
+ * necessary because Helmet does not "merge" classes.
100
+ * @see https://github.com/staylor/react-helmet-async/issues/161
101
+ */
102
+ export function HtmlClassNameProvider({
103
+ className: classNameProp,
104
+ children,
105
+ }: {
106
+ className: string;
107
+ children: ReactNode;
108
+ }): ReactNode {
109
+ const classNameContext = React.useContext(HtmlClassNameContext);
110
+ const className = clsx(classNameContext, classNameProp);
111
+ return (
112
+ <HtmlClassNameContext value={className}>
113
+ <Head>
114
+ <html className={className} />
115
+ </Head>
116
+ {children}
117
+ </HtmlClassNameContext>
118
+ );
119
+ }
120
+
121
+ function pluginNameToClassName(pluginName: string) {
122
+ return `plugin-${pluginName.replace(
123
+ /docusaurus-(?:plugin|theme)-(?:content-)?/gi,
124
+ '',
125
+ )}`;
126
+ }
127
+
128
+ /**
129
+ * A very thin wrapper around `HtmlClassNameProvider` that adds the plugin ID +
130
+ * name to the HTML class name.
131
+ */
132
+ export function PluginHtmlClassNameProvider({
133
+ children,
134
+ }: {
135
+ children: ReactNode;
136
+ }): ReactNode {
137
+ const routeContext = useRouteContext();
138
+ const nameClass = pluginNameToClassName(routeContext.plugin.name);
139
+ const idClass = `plugin-id-${routeContext.plugin.id}`;
140
+ return (
141
+ <HtmlClassNameProvider className={clsx(nameClass, idClass)}>
142
+ {children}
143
+ </HtmlClassNameProvider>
144
+ );
145
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import type {ReactNode} from 'react';
9
+ import {NavbarMobileSidebarProvider} from '../contexts/navbarMobileSidebar';
10
+ import {NavbarSecondaryMenuContentProvider} from '../contexts/navbarSecondaryMenu/content';
11
+ import {NavbarSecondaryMenuDisplayProvider} from '../contexts/navbarSecondaryMenu/display';
12
+
13
+ const DefaultNavItemPosition = 'right';
14
+
15
+ /**
16
+ * Split links by left/right. If position is unspecified, fallback to right.
17
+ */
18
+ export function splitNavbarItems<T extends {position?: 'left' | 'right'}>(
19
+ items: T[],
20
+ ): [leftItems: T[], rightItems: T[]] {
21
+ function isLeft(item: T): boolean {
22
+ return (item.position ?? DefaultNavItemPosition) === 'left';
23
+ }
24
+
25
+ const leftItems = items.filter(isLeft);
26
+ const rightItems = items.filter((item) => !isLeft(item));
27
+
28
+ return [leftItems, rightItems];
29
+ }
30
+
31
+ /**
32
+ * Composes multiple navbar state providers that are mutually dependent and
33
+ * hence can't be re-ordered.
34
+ */
35
+ export function NavbarProvider({children}: {children: ReactNode}): ReactNode {
36
+ return (
37
+ <NavbarSecondaryMenuContentProvider>
38
+ <NavbarMobileSidebarProvider>
39
+ <NavbarSecondaryMenuDisplayProvider>
40
+ {children}
41
+ </NavbarSecondaryMenuDisplayProvider>
42
+ </NavbarMobileSidebarProvider>
43
+ </NavbarSecondaryMenuContentProvider>
44
+ );
45
+ }
@@ -0,0 +1,120 @@
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 {
9
+ useCallback,
10
+ useLayoutEffect,
11
+ useMemo,
12
+ useRef,
13
+ type ComponentType,
14
+ type ReactNode,
15
+ } from 'react';
16
+
17
+ /**
18
+ * Temporary userland implementation until an official hook is implemented
19
+ * See RFC: https://github.com/reactjs/rfcs/pull/220
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
+ * This generally works but has some potential drawbacks, such as
27
+ * https://github.com/facebook/react/issues/16956#issuecomment-536636418
28
+ */
29
+ export function useEvent<T extends (...args: never[]) => unknown>(
30
+ callback: T,
31
+ ): T {
32
+ const ref = useRef<T>(callback);
33
+
34
+ useLayoutEffect(() => {
35
+ ref.current = callback;
36
+ }, [callback]);
37
+
38
+ // @ts-expect-error: TS is right that this callback may be a supertype of T,
39
+ // but good enough for our use
40
+ return useCallback<T>((...args) => ref.current(...args), []);
41
+ }
42
+
43
+ /**
44
+ * Gets `value` from the last render.
45
+ */
46
+ export function usePrevious<T>(value: T): T | undefined {
47
+ const ref = useRef<T>(undefined);
48
+
49
+ useLayoutEffect(() => {
50
+ ref.current = value;
51
+ });
52
+
53
+ // TODO need to fix this React Compiler lint error
54
+ // probably requires changing the API though
55
+ // eslint-disable-next-line react-hooks/refs
56
+ return ref.current;
57
+ }
58
+
59
+ /**
60
+ * This error is thrown when a context is consumed outside its provider. Allows
61
+ * reusing a generic error message format and reduces bundle size. The hook's
62
+ * name will be extracted from its stack, so only the provider's name is needed.
63
+ */
64
+ export class ReactContextError extends Error {
65
+ constructor(providerName: string, additionalInfo?: string) {
66
+ super();
67
+ this.name = 'ReactContextError';
68
+ this.message = `Hook ${
69
+ this.stack?.split('\n')[1]?.match(/at (?:\w+\.)?(?<name>\w+)/)?.groups!
70
+ .name ?? ''
71
+ } is called outside the <${providerName}>. ${additionalInfo ?? ''}`;
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Shallow-memoize an object. This means the returned object will be the same as
77
+ * the previous render if the property keys and values did not change. This
78
+ * works for simple cases: when property values are primitives or stable
79
+ * objects.
80
+ *
81
+ * @param obj
82
+ */
83
+ export function useShallowMemoObject<O extends object>(obj: O): O {
84
+ const deps = Object.entries(obj);
85
+ // Sort by keys to make it order-insensitive
86
+ deps.sort((a, b) => a[0].localeCompare(b[0]));
87
+ // eslint-disable-next-line react-hooks/exhaustive-deps,react-hooks/use-memo
88
+ return useMemo(() => obj, deps.flat());
89
+ }
90
+
91
+ type SimpleProvider = ComponentType<{children: ReactNode}>;
92
+
93
+ /**
94
+ * Creates a single React provider from an array of existing providers
95
+ * assuming providers only take "children" as props.
96
+ *
97
+ * Prevents the annoying React element nesting
98
+ * Example here: https://getfrontend.tips/compose-multiple-react-providers/
99
+ *
100
+ * The order matters:
101
+ * - The first provider is at the top of the tree.
102
+ * - The last provider is the most nested one
103
+ *
104
+ * @param providers array of providers to compose
105
+ */
106
+ export function composeProviders(providers: SimpleProvider[]): SimpleProvider {
107
+ // Creates a single React component: it's cheaper to compose JSX elements
108
+ return function ComposedProvider({children}: {children: ReactNode}) {
109
+ return (
110
+ <>
111
+ {providers.reduceRight(
112
+ (element, CurrentProvider) => (
113
+ <CurrentProvider>{element}</CurrentProvider>
114
+ ),
115
+ children,
116
+ )}
117
+ </>
118
+ );
119
+ };
120
+ }
@@ -0,0 +1,24 @@
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
+ * Matches a string regex (as provided from the config) against a target in a
10
+ * null-safe fashion, case insensitive and global.
11
+ */
12
+ export function isRegexpStringMatch(
13
+ regexAsString?: string,
14
+ valueToTest?: string,
15
+ ): boolean {
16
+ if (
17
+ typeof regexAsString === 'undefined' ||
18
+ typeof valueToTest === 'undefined'
19
+ ) {
20
+ return false;
21
+ }
22
+
23
+ return new RegExp(regexAsString, 'gi').test(valueToTest);
24
+ }
@@ -0,0 +1,75 @@
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 generatedRoutes from '@generated/routes';
10
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
11
+ import type {RouteConfig} from 'react-router-config';
12
+
13
+ /**
14
+ * Compare the 2 paths, case insensitive and ignoring trailing slash
15
+ */
16
+ export function isSamePath(
17
+ path1: string | undefined,
18
+ path2: string | undefined,
19
+ ): boolean {
20
+ const normalize = (pathname: string | undefined) =>
21
+ (!pathname || pathname.endsWith('/')
22
+ ? pathname
23
+ : `${pathname}/`
24
+ )?.toLowerCase();
25
+ return normalize(path1) === normalize(path2);
26
+ }
27
+
28
+ /**
29
+ * Note that sites don't always have a homepage in practice, so we can't assume
30
+ * that linking to '/' is always safe.
31
+ * @see https://github.com/facebook/docusaurus/pull/6517#issuecomment-1048709116
32
+ */
33
+ export function findHomePageRoute({
34
+ baseUrl,
35
+ routes: initialRoutes,
36
+ }: {
37
+ routes: RouteConfig[];
38
+ baseUrl: string;
39
+ }): RouteConfig | undefined {
40
+ function isHomePageRoute(route: RouteConfig): boolean {
41
+ return route.path === baseUrl && route.exact === true;
42
+ }
43
+
44
+ function isHomeParentRoute(route: RouteConfig): boolean {
45
+ return route.path === baseUrl && !route.exact;
46
+ }
47
+
48
+ function doFindHomePageRoute(routes: RouteConfig[]): RouteConfig | undefined {
49
+ if (routes.length === 0) {
50
+ return undefined;
51
+ }
52
+ const homePage = routes.find(isHomePageRoute);
53
+ if (homePage) {
54
+ return homePage;
55
+ }
56
+ const indexSubRoutes = routes
57
+ .filter(isHomeParentRoute)
58
+ .flatMap((route) => route.routes ?? []);
59
+ return doFindHomePageRoute(indexSubRoutes);
60
+ }
61
+
62
+ return doFindHomePageRoute(initialRoutes);
63
+ }
64
+
65
+ /**
66
+ * Fetches the route that points to "/". Use this instead of the naive "/",
67
+ * because the homepage may not exist.
68
+ */
69
+ export function useHomePageRoute(): RouteConfig | undefined {
70
+ const {baseUrl} = useDocusaurusContext().siteConfig;
71
+ return useMemo(
72
+ () => findHomePageRoute({routes: generatedRoutes, baseUrl}),
73
+ [baseUrl],
74
+ );
75
+ }