@docusaurus/theme-common 2.0.0-beta.16 → 2.0.0-beta.19

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 (279) hide show
  1. package/Details.d.ts +14 -0
  2. package/lib/components/Collapsible/index.d.ts +32 -4
  3. package/lib/components/Collapsible/index.d.ts.map +1 -1
  4. package/lib/components/Collapsible/index.js +18 -10
  5. package/lib/components/Collapsible/index.js.map +1 -1
  6. package/lib/components/Details/index.d.ts +6 -1
  7. package/lib/components/Details/index.d.ts.map +1 -1
  8. package/lib/components/Details/index.js +11 -6
  9. package/lib/components/Details/index.js.map +1 -1
  10. package/lib/components/Details/styles.module.css +4 -0
  11. package/lib/{utils/announcementBarUtils.d.ts → contexts/announcementBar.d.ts} +7 -3
  12. package/lib/contexts/announcementBar.d.ts.map +1 -0
  13. package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +15 -14
  14. package/lib/contexts/announcementBar.js.map +1 -0
  15. package/lib/contexts/colorMode.d.ts +27 -0
  16. package/lib/contexts/colorMode.d.ts.map +1 -0
  17. package/lib/contexts/colorMode.js +132 -0
  18. package/lib/contexts/colorMode.js.map +1 -0
  19. package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
  20. package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
  21. package/lib/{utils → contexts}/docSidebarItemsExpandedState.js +10 -4
  22. package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
  23. package/lib/contexts/docsPreferredVersion.d.ts +31 -0
  24. package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
  25. package/lib/contexts/docsPreferredVersion.js +128 -0
  26. package/lib/contexts/docsPreferredVersion.js.map +1 -0
  27. package/lib/contexts/docsSidebar.d.ts +26 -0
  28. package/lib/contexts/docsSidebar.d.ts.map +1 -0
  29. package/lib/contexts/docsSidebar.js +30 -0
  30. package/lib/contexts/docsSidebar.js.map +1 -0
  31. package/lib/contexts/docsVersion.d.ts +20 -0
  32. package/lib/contexts/docsVersion.d.ts.map +1 -0
  33. package/lib/contexts/docsVersion.js +26 -0
  34. package/lib/contexts/docsVersion.js.map +1 -0
  35. package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
  36. package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
  37. package/lib/contexts/navbarMobileSidebar.js +56 -0
  38. package/lib/contexts/navbarMobileSidebar.js.map +1 -0
  39. package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
  40. package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
  41. package/lib/contexts/navbarSecondaryMenu/content.js +56 -0
  42. package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
  43. package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
  44. package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
  45. package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
  46. package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
  47. package/lib/{utils/tabGroupChoiceUtils.d.ts → contexts/tabGroupChoice.d.ts} +5 -3
  48. package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
  49. package/lib/{utils/tabGroupChoiceUtils.js → contexts/tabGroupChoice.js} +15 -20
  50. package/lib/contexts/tabGroupChoice.js.map +1 -0
  51. package/lib/hooks/useBackToTopButton.d.ts +27 -0
  52. package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
  53. package/lib/hooks/useBackToTopButton.js +50 -0
  54. package/lib/hooks/useBackToTopButton.js.map +1 -0
  55. package/lib/hooks/useCodeWordWrap.d.ts +14 -0
  56. package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
  57. package/lib/hooks/useCodeWordWrap.js +41 -0
  58. package/lib/hooks/useCodeWordWrap.js.map +1 -0
  59. package/lib/hooks/useHideableNavbar.d.ts +7 -3
  60. package/lib/hooks/useHideableNavbar.d.ts.map +1 -1
  61. package/lib/hooks/useHideableNavbar.js +10 -9
  62. package/lib/hooks/useHideableNavbar.js.map +1 -1
  63. package/lib/hooks/useKeyboardNavigation.d.ts +11 -1
  64. package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -1
  65. package/lib/hooks/useKeyboardNavigation.js +11 -3
  66. package/lib/hooks/useKeyboardNavigation.js.map +1 -1
  67. package/lib/hooks/useLockBodyScroll.d.ts +5 -1
  68. package/lib/hooks/useLockBodyScroll.d.ts.map +1 -1
  69. package/lib/hooks/useLockBodyScroll.js +5 -1
  70. package/lib/hooks/useLockBodyScroll.js.map +1 -1
  71. package/lib/hooks/usePrismTheme.d.ts +6 -2
  72. package/lib/hooks/usePrismTheme.d.ts.map +1 -1
  73. package/lib/hooks/usePrismTheme.js +9 -6
  74. package/lib/hooks/usePrismTheme.js.map +1 -1
  75. package/lib/hooks/useSearchPage.d.ts +15 -4
  76. package/lib/hooks/useSearchPage.d.ts.map +1 -1
  77. package/lib/hooks/useSearchPage.js +4 -4
  78. package/lib/hooks/useSearchPage.js.map +1 -1
  79. package/lib/hooks/useSkipToContent.d.ts +25 -0
  80. package/lib/hooks/useSkipToContent.d.ts.map +1 -0
  81. package/lib/hooks/useSkipToContent.js +35 -0
  82. package/lib/hooks/useSkipToContent.js.map +1 -0
  83. package/lib/hooks/useTOCHighlight.d.ts +25 -0
  84. package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
  85. package/lib/{utils → hooks}/useTOCHighlight.js +18 -16
  86. package/lib/hooks/useTOCHighlight.js.map +1 -0
  87. package/lib/hooks/useWindowSize.d.ts +14 -1
  88. package/lib/hooks/useWindowSize.d.ts.map +1 -1
  89. package/lib/hooks/useWindowSize.js +14 -11
  90. package/lib/hooks/useWindowSize.js.map +1 -1
  91. package/lib/index.d.ts +31 -32
  92. package/lib/index.d.ts.map +1 -1
  93. package/lib/index.js +31 -26
  94. package/lib/index.js.map +1 -1
  95. package/lib/utils/ThemeClassNames.d.ts +7 -0
  96. package/lib/utils/ThemeClassNames.d.ts.map +1 -1
  97. package/lib/utils/ThemeClassNames.js +7 -4
  98. package/lib/utils/ThemeClassNames.js.map +1 -1
  99. package/lib/utils/codeBlockUtils.d.ts +57 -4
  100. package/lib/utils/codeBlockUtils.d.ts.map +1 -1
  101. package/lib/utils/codeBlockUtils.js +111 -80
  102. package/lib/utils/codeBlockUtils.js.map +1 -1
  103. package/lib/utils/docsUtils.d.ts +83 -18
  104. package/lib/utils/docsUtils.d.ts.map +1 -1
  105. package/lib/utils/docsUtils.js +143 -62
  106. package/lib/utils/docsUtils.js.map +1 -1
  107. package/lib/utils/footerUtils.d.ts +13 -0
  108. package/lib/utils/footerUtils.d.ts.map +1 -0
  109. package/lib/utils/footerUtils.js +14 -0
  110. package/lib/utils/footerUtils.js.map +1 -0
  111. package/lib/utils/generalUtils.d.ts +4 -1
  112. package/lib/utils/generalUtils.d.ts.map +1 -1
  113. package/lib/utils/generalUtils.js +6 -3
  114. package/lib/utils/generalUtils.js.map +1 -1
  115. package/lib/utils/historyUtils.d.ts +1 -7
  116. package/lib/utils/historyUtils.d.ts.map +1 -1
  117. package/lib/utils/historyUtils.js +10 -13
  118. package/lib/utils/historyUtils.js.map +1 -1
  119. package/lib/utils/jsUtils.d.ts +1 -1
  120. package/lib/utils/jsUtils.js +1 -1
  121. package/lib/utils/metadataUtils.d.ts +38 -0
  122. package/lib/utils/metadataUtils.d.ts.map +1 -0
  123. package/lib/utils/metadataUtils.js +61 -0
  124. package/lib/utils/metadataUtils.js.map +1 -0
  125. package/lib/utils/navbarUtils.d.ts +21 -0
  126. package/lib/utils/navbarUtils.d.ts.map +1 -0
  127. package/lib/utils/navbarUtils.js +32 -0
  128. package/lib/utils/navbarUtils.js.map +1 -0
  129. package/lib/utils/reactUtils.d.ts +21 -7
  130. package/lib/utils/reactUtils.d.ts.map +1 -1
  131. package/lib/utils/reactUtils.js +34 -6
  132. package/lib/utils/reactUtils.js.map +1 -1
  133. package/lib/utils/regexpUtils.d.ts +2 -1
  134. package/lib/utils/regexpUtils.d.ts.map +1 -1
  135. package/lib/utils/regexpUtils.js +2 -1
  136. package/lib/utils/regexpUtils.js.map +1 -1
  137. package/lib/utils/routesUtils.d.ts +19 -4
  138. package/lib/utils/routesUtils.d.ts.map +1 -1
  139. package/lib/utils/routesUtils.js +40 -19
  140. package/lib/utils/routesUtils.js.map +1 -1
  141. package/lib/utils/scrollUtils.d.ts +55 -25
  142. package/lib/utils/scrollUtils.d.ts.map +1 -1
  143. package/lib/utils/scrollUtils.js +85 -21
  144. package/lib/utils/scrollUtils.js.map +1 -1
  145. package/lib/utils/searchUtils.d.ts +12 -0
  146. package/lib/utils/searchUtils.d.ts.map +1 -1
  147. package/lib/utils/searchUtils.js +36 -0
  148. package/lib/utils/searchUtils.js.map +1 -1
  149. package/lib/utils/storageUtils.d.ts +12 -9
  150. package/lib/utils/storageUtils.d.ts.map +1 -1
  151. package/lib/utils/storageUtils.js +21 -13
  152. package/lib/utils/storageUtils.js.map +1 -1
  153. package/lib/utils/tagsUtils.d.ts +7 -8
  154. package/lib/utils/tagsUtils.d.ts.map +1 -1
  155. package/lib/utils/tagsUtils.js +8 -6
  156. package/lib/utils/tagsUtils.js.map +1 -1
  157. package/lib/utils/tocUtils.d.ts +17 -1
  158. package/lib/utils/tocUtils.d.ts.map +1 -1
  159. package/lib/utils/tocUtils.js +20 -9
  160. package/lib/utils/tocUtils.js.map +1 -1
  161. package/lib/utils/useAlternatePageUtils.d.ts +20 -1
  162. package/lib/utils/useAlternatePageUtils.d.ts.map +1 -1
  163. package/lib/utils/useAlternatePageUtils.js +6 -3
  164. package/lib/utils/useAlternatePageUtils.js.map +1 -1
  165. package/lib/utils/useLocalPathname.d.ts +5 -0
  166. package/lib/utils/useLocalPathname.d.ts.map +1 -1
  167. package/lib/utils/useLocalPathname.js +6 -4
  168. package/lib/utils/useLocalPathname.js.map +1 -1
  169. package/lib/utils/useLocationChange.d.ts +7 -5
  170. package/lib/utils/useLocationChange.d.ts.map +1 -1
  171. package/lib/utils/useLocationChange.js +6 -2
  172. package/lib/utils/useLocationChange.js.map +1 -1
  173. package/lib/utils/usePluralForm.d.ts +11 -0
  174. package/lib/utils/usePluralForm.d.ts.map +1 -1
  175. package/lib/utils/usePluralForm.js +19 -24
  176. package/lib/utils/usePluralForm.js.map +1 -1
  177. package/lib/utils/useThemeConfig.d.ts +28 -22
  178. package/lib/utils/useThemeConfig.d.ts.map +1 -1
  179. package/lib/utils/useThemeConfig.js +3 -0
  180. package/lib/utils/useThemeConfig.js.map +1 -1
  181. package/package.json +18 -11
  182. package/src/components/Collapsible/index.tsx +42 -24
  183. package/src/components/Details/index.tsx +15 -8
  184. package/src/components/Details/styles.module.css +4 -0
  185. package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +20 -22
  186. package/src/contexts/colorMode.tsx +199 -0
  187. package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
  188. package/src/contexts/docsPreferredVersion.tsx +253 -0
  189. package/src/contexts/docsSidebar.tsx +50 -0
  190. package/src/contexts/docsVersion.tsx +36 -0
  191. package/src/contexts/navbarMobileSidebar.tsx +99 -0
  192. package/src/contexts/navbarSecondaryMenu/content.tsx +110 -0
  193. package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
  194. package/src/{utils/tabGroupChoiceUtils.tsx → contexts/tabGroupChoice.tsx} +28 -33
  195. package/src/hooks/useBackToTopButton.ts +73 -0
  196. package/src/hooks/useCodeWordWrap.ts +56 -0
  197. package/src/hooks/useHideableNavbar.ts +12 -14
  198. package/src/hooks/useKeyboardNavigation.ts +11 -3
  199. package/src/hooks/useLockBodyScroll.ts +5 -2
  200. package/src/hooks/usePrismTheme.ts +10 -6
  201. package/src/hooks/useSearchPage.ts +18 -5
  202. package/src/hooks/useSkipToContent.ts +58 -0
  203. package/src/{utils → hooks}/useTOCHighlight.ts +24 -15
  204. package/src/hooks/useWindowSize.ts +14 -12
  205. package/src/index.ts +77 -57
  206. package/src/utils/ThemeClassNames.ts +10 -6
  207. package/src/utils/codeBlockUtils.ts +178 -92
  208. package/src/utils/docsUtils.tsx +205 -101
  209. package/src/utils/footerUtils.ts +18 -0
  210. package/src/utils/generalUtils.ts +6 -3
  211. package/src/utils/historyUtils.ts +11 -17
  212. package/src/utils/jsUtils.ts +1 -1
  213. package/src/utils/metadataUtils.tsx +115 -0
  214. package/src/utils/navbarUtils.tsx +45 -0
  215. package/src/utils/reactUtils.tsx +40 -7
  216. package/src/utils/regexpUtils.ts +2 -1
  217. package/src/utils/routesUtils.ts +58 -22
  218. package/src/utils/scrollUtils.tsx +120 -49
  219. package/src/utils/searchUtils.ts +51 -0
  220. package/src/utils/storageUtils.ts +23 -15
  221. package/src/utils/tagsUtils.ts +12 -10
  222. package/src/utils/tocUtils.ts +22 -11
  223. package/src/utils/useAlternatePageUtils.ts +17 -5
  224. package/src/utils/useLocalPathname.ts +6 -4
  225. package/src/utils/useLocationChange.ts +12 -10
  226. package/src/utils/usePluralForm.ts +27 -24
  227. package/src/utils/useThemeConfig.ts +23 -22
  228. package/yarn-error.log +20199 -0
  229. package/lib/utils/announcementBarUtils.d.ts.map +0 -1
  230. package/lib/utils/announcementBarUtils.js.map +0 -1
  231. package/lib/utils/colorModeUtils.d.ts +0 -18
  232. package/lib/utils/colorModeUtils.d.ts.map +0 -1
  233. package/lib/utils/colorModeUtils.js +0 -106
  234. package/lib/utils/colorModeUtils.js.map +0 -1
  235. package/lib/utils/docSidebarItemsExpandedState.d.ts +0 -17
  236. package/lib/utils/docSidebarItemsExpandedState.d.ts.map +0 -1
  237. package/lib/utils/docSidebarItemsExpandedState.js.map +0 -1
  238. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -22
  239. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +0 -1
  240. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -91
  241. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +0 -1
  242. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -14
  243. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +0 -1
  244. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -19
  245. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +0 -1
  246. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -14
  247. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +0 -1
  248. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
  249. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +0 -1
  250. package/lib/utils/mobileSecondaryMenu.d.ts +0 -21
  251. package/lib/utils/mobileSecondaryMenu.d.ts.map +0 -1
  252. package/lib/utils/mobileSecondaryMenu.js +0 -50
  253. package/lib/utils/mobileSecondaryMenu.js.map +0 -1
  254. package/lib/utils/pathUtils.d.ts +0 -8
  255. package/lib/utils/pathUtils.d.ts.map +0 -1
  256. package/lib/utils/pathUtils.js +0 -17
  257. package/lib/utils/pathUtils.js.map +0 -1
  258. package/lib/utils/tabGroupChoiceUtils.d.ts.map +0 -1
  259. package/lib/utils/tabGroupChoiceUtils.js.map +0 -1
  260. package/lib/utils/useContextualSearchFilters.d.ts +0 -12
  261. package/lib/utils/useContextualSearchFilters.d.ts.map +0 -1
  262. package/lib/utils/useContextualSearchFilters.js +0 -37
  263. package/lib/utils/useContextualSearchFilters.js.map +0 -1
  264. package/lib/utils/usePrevious.d.ts +0 -8
  265. package/lib/utils/usePrevious.d.ts.map +0 -1
  266. package/lib/utils/usePrevious.js +0 -16
  267. package/lib/utils/usePrevious.js.map +0 -1
  268. package/lib/utils/useTOCHighlight.d.ts +0 -14
  269. package/lib/utils/useTOCHighlight.d.ts.map +0 -1
  270. package/lib/utils/useTOCHighlight.js.map +0 -1
  271. package/src/utils/colorModeUtils.tsx +0 -156
  272. package/src/utils/docSidebarItemsExpandedState.tsx +0 -41
  273. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -167
  274. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -33
  275. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -70
  276. package/src/utils/mobileSecondaryMenu.tsx +0 -115
  277. package/src/utils/pathUtils.ts +0 -19
  278. package/src/utils/useContextualSearchFilters.ts +0 -53
  279. package/src/utils/usePrevious.ts +0 -19
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import {useMemo} from 'react';
9
- import type {TOCItem} from '@docusaurus/types';
9
+ import type {TOCItem} from '@docusaurus/mdx-loader';
10
10
 
11
11
  export type TOCTreeNode = {
12
12
  readonly value: string;
@@ -30,11 +30,11 @@ function treeifyTOC(flatTOC: readonly TOCItem[]): TOCTreeNode[] {
30
30
  const prevIndexForLevel = Array(7).fill(-1);
31
31
 
32
32
  headings.forEach((curr, currIndex) => {
33
- // take the last seen index for each ancestor level. the highest
34
- // index will be the direct ancestor of the current heading.
33
+ // Take the last seen index for each ancestor level. the highest index will
34
+ // be the direct ancestor of the current heading.
35
35
  const ancestorLevelIndexes = prevIndexForLevel.slice(2, curr.level);
36
36
  curr.parentIndex = Math.max(...ancestorLevelIndexes);
37
- // mark that curr.level was last seen at the current index
37
+ // Mark that curr.level was last seen at the current index.
38
38
  prevIndexForLevel[curr.level] = currIndex;
39
39
  });
40
40
 
@@ -44,7 +44,7 @@ function treeifyTOC(flatTOC: readonly TOCItem[]): TOCTreeNode[] {
44
44
  headings.forEach((heading) => {
45
45
  const {parentIndex, ...rest} = heading;
46
46
  if (parentIndex >= 0) {
47
- headings[parentIndex].children.push(rest);
47
+ headings[parentIndex]!.children.push(rest);
48
48
  } else {
49
49
  rootNodes.push(rest);
50
50
  }
@@ -52,6 +52,10 @@ function treeifyTOC(flatTOC: readonly TOCItem[]): TOCTreeNode[] {
52
52
  return rootNodes;
53
53
  }
54
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
+ */
55
59
  export function useTreeifiedTOC(toc: TOCItem[]): readonly TOCTreeNode[] {
56
60
  return useMemo(() => treeifyTOC(toc), [toc]);
57
61
  }
@@ -87,6 +91,18 @@ function filterTOC({
87
91
  });
88
92
  }
89
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
+ */
90
106
  export function useFilteredAndTreeifiedTOC({
91
107
  toc,
92
108
  minHeadingLevel,
@@ -97,12 +113,7 @@ export function useFilteredAndTreeifiedTOC({
97
113
  maxHeadingLevel: number;
98
114
  }): readonly TOCTreeNode[] {
99
115
  return useMemo(
100
- () =>
101
- // Note: we have to filter the TOC after it has been treeified. This is
102
- // mostly to ensure that weird TOC structures preserve their semantics.
103
- // For example, an h3-h2-h4 sequence should not be treeified as an h3 > h4
104
- // hierarchy with min=3, max=4, but should rather be [h3, h4]
105
- filterTOC({toc: treeifyTOC(toc), minHeadingLevel, maxHeadingLevel}),
116
+ () => filterTOC({toc: treeifyTOC(toc), minHeadingLevel, maxHeadingLevel}),
106
117
  [toc, minHeadingLevel, maxHeadingLevel],
107
118
  );
108
119
  }
@@ -8,12 +8,26 @@
8
8
  import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
9
9
  import {useLocation} from '@docusaurus/router';
10
10
 
11
- // Permits to obtain the url of the current page in another locale
12
- // Useful to generate hreflang meta headers etc...
13
- // See https://developers.google.com/search/docs/advanced/crawling/localized-versions
11
+ /**
12
+ * Permits to obtain the url of the current page in another locale, useful to
13
+ * generate hreflang meta headers etc...
14
+ *
15
+ * @see https://developers.google.com/search/docs/advanced/crawling/localized-versions
16
+ */
14
17
  export function useAlternatePageUtils(): {
18
+ /**
19
+ * Everything (pathname, base URL, etc.) is read from the context. Just tell
20
+ * it which locale to link to and it will give you the alternate link for the
21
+ * current page.
22
+ */
15
23
  createUrl: ({
24
+ /** The locale name to link to. */
16
25
  locale,
26
+ /**
27
+ * For hreflang SEO headers, we need it to be fully qualified (full
28
+ * protocol/domain/path...); but for locale dropdowns, using a pathname is
29
+ * good enough.
30
+ */
17
31
  fullyQualified,
18
32
  }: {
19
33
  locale: string;
@@ -46,8 +60,6 @@ export function useAlternatePageUtils(): {
46
60
  fullyQualified,
47
61
  }: {
48
62
  locale: string;
49
- // For hreflang SEO headers, we need it to be fully qualified (full
50
- // protocol/domain/path...) or locale dropdown, using a path is good enough
51
63
  fullyQualified: boolean;
52
64
  }) {
53
65
  return `${fullyQualified ? url : ''}${getLocalizedBaseUrl(
@@ -5,12 +5,14 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
9
8
  import {useLocation} from '@docusaurus/router';
9
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
10
10
 
11
- // Get the pathname of current route, without the optional site baseUrl
12
- // - /docs/myDoc => /docs/myDoc
13
- // - /baseUrl/docs/myDoc => /docs/myDoc
11
+ /**
12
+ * Get the pathname of current route, without the optional site baseUrl.
13
+ * - `/docs/myDoc` => `/docs/myDoc`
14
+ * - `/baseUrl/docs/myDoc` => `/docs/myDoc`
15
+ */
14
16
  export function useLocalPathname(): string {
15
17
  const {
16
18
  siteConfig: {baseUrl},
@@ -8,17 +8,19 @@
8
8
  import {useEffect} from 'react';
9
9
  import {useLocation} from '@docusaurus/router';
10
10
  import type {Location} from 'history';
11
- import {usePrevious} from './usePrevious';
12
- import {useDynamicCallback} from './reactUtils';
11
+ import {useDynamicCallback, usePrevious} from './reactUtils';
13
12
 
14
- type LocationChangeEvent = {
15
- location: Location;
16
- previousLocation: Location | undefined;
17
- };
18
-
19
- type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
20
-
21
- export function useLocationChange(onLocationChange: OnLocationChange): void {
13
+ /**
14
+ * Fires an effect when the location changes (which includes hash, query, etc.).
15
+ * Importantly, doesn't fire when there's no previous location: see
16
+ * https://github.com/facebook/docusaurus/pull/6696
17
+ */
18
+ export function useLocationChange(
19
+ onLocationChange: (locationChangeEvent: {
20
+ location: Location;
21
+ previousLocation: Location | undefined;
22
+ }) => void,
23
+ ): void {
22
24
  const location = useLocation();
23
25
  const previousLocation = usePrevious(location);
24
26
 
@@ -50,17 +50,17 @@ function createLocalePluralForms(locale: string): LocalePluralForms {
50
50
  }
51
51
 
52
52
  /**
53
- * Poor man's PluralSelector implementation, using an english fallback. We want
54
- * a lightweight, future-proof and good-enough solution. We don't want a perfect
55
- * and heavy solution.
53
+ * Poor man's `PluralSelector` implementation, using an English fallback. We
54
+ * want a lightweight, future-proof and good-enough solution. We don't want a
55
+ * perfect and heavy solution.
56
56
  *
57
57
  * Docusaurus classic theme has only 2 deeply nested labels requiring complex
58
- * plural rules. We don't want to use Intl + PluralRules polyfills + full ICU
59
- * syntax (react-intl) just for that.
58
+ * plural rules. We don't want to use `Intl` + `PluralRules` polyfills + full
59
+ * ICU syntax (react-intl) just for that.
60
60
  *
61
61
  * Notes:
62
- * - 2021: 92+% Browsers support Intl.PluralRules, and support will increase in
63
- * the future
62
+ * - 2021: 92+% Browsers support `Intl.PluralRules`, and support will increase
63
+ * in the future
64
64
  * - NodeJS >= 13 has full ICU support by default
65
65
  * - In case of "mismatch" between SSR and Browser ICU support, React keeps
66
66
  * working!
@@ -70,21 +70,13 @@ function useLocalePluralForms(): LocalePluralForms {
70
70
  i18n: {currentLocale},
71
71
  } = useDocusaurusContext();
72
72
  return useMemo(() => {
73
- // @ts-expect-error checking Intl.PluralRules in case browser doesn't
74
- // have it (e.g Safari 12-)
75
- if (Intl.PluralRules) {
76
- try {
77
- return createLocalePluralForms(currentLocale);
78
- } catch {
79
- console.error(`Failed to use Intl.PluralRules for locale "${currentLocale}".
80
- Docusaurus will fallback to a default/fallback (English) Intl.PluralRules implementation.
73
+ try {
74
+ return createLocalePluralForms(currentLocale);
75
+ } catch (err) {
76
+ console.error(`Failed to use Intl.PluralRules for locale "${currentLocale}".
77
+ Docusaurus will fallback to the default (English) implementation.
78
+ Error: ${(err as Error).message}
81
79
  `);
82
- return EnglishPluralForms;
83
- }
84
- } else {
85
- console.error(`Intl.PluralRules not available!
86
- Docusaurus will fallback to a default/fallback (English) Intl.PluralRules implementation.
87
- `);
88
80
  return EnglishPluralForms;
89
81
  }
90
82
  }, [currentLocale]);
@@ -99,21 +91,32 @@ function selectPluralMessage(
99
91
  const parts = pluralMessages.split(separator);
100
92
 
101
93
  if (parts.length === 1) {
102
- return parts[0];
94
+ return parts[0]!;
103
95
  }
104
96
  if (parts.length > localePluralForms.pluralForms.length) {
105
97
  console.error(
106
- `For locale=${localePluralForms.locale}, a maximum of ${localePluralForms.pluralForms.length} plural forms are expected (${localePluralForms.pluralForms}), but the message contains ${parts.length} plural forms: ${pluralMessages} `,
98
+ `For locale=${localePluralForms.locale}, a maximum of ${localePluralForms.pluralForms.length} plural forms are expected (${localePluralForms.pluralForms}), but the message contains ${parts.length}: ${pluralMessages}`,
107
99
  );
108
100
  }
109
101
  const pluralForm = localePluralForms.select(count);
110
102
  const pluralFormIndex = localePluralForms.pluralForms.indexOf(pluralForm);
111
103
  // In case of not enough plural form messages, we take the last one (other)
112
104
  // instead of returning undefined
113
- return parts[Math.min(pluralFormIndex, parts.length - 1)];
105
+ return parts[Math.min(pluralFormIndex, parts.length - 1)]!;
114
106
  }
115
107
 
108
+ /**
109
+ * Reads the current locale and returns an interface very similar to
110
+ * `Intl.PluralRules`.
111
+ */
116
112
  export function usePluralForm(): {
113
+ /**
114
+ * Give it a `count` and it will select the relevant message from
115
+ * `pluralMessages`. `pluralMessages` should be separated by `|`, and in the
116
+ * order of "zero", "one", "two", "few", "many", "other". The actual selection
117
+ * is done by `Intl.PluralRules`, which tells us all plurals the locale has
118
+ * and which plural we should use for `count`.
119
+ */
117
120
  selectMessage: (count: number, pluralMessages: string) => string;
118
121
  } {
119
122
  const localePluralForm = useLocalePluralForms();
@@ -7,8 +7,8 @@
7
7
 
8
8
  import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
9
9
  import type {PrismTheme} from 'prism-react-renderer';
10
- import type {CSSProperties} from 'react';
11
10
  import type {DeepPartial} from 'utility-types';
11
+ import type {MagicCommentConfig} from './codeBlockUtils';
12
12
 
13
13
  export type DocsVersionPersistence = 'localStorage' | 'none';
14
14
 
@@ -18,7 +18,7 @@ export type NavbarItem = {
18
18
  items?: NavbarItem[];
19
19
  label?: string;
20
20
  position?: 'left' | 'right';
21
- } & Record<string, unknown>;
21
+ } & {[key: string]: unknown};
22
22
 
23
23
  export type NavbarLogo = {
24
24
  src: string;
@@ -43,12 +43,6 @@ export type ColorModeConfig = {
43
43
  defaultMode: 'light' | 'dark';
44
44
  disableSwitch: boolean;
45
45
  respectPrefersColorScheme: boolean;
46
- switchConfig: {
47
- darkIcon: string;
48
- darkIconStyle: CSSProperties;
49
- lightIcon: string;
50
- lightIconStyle: CSSProperties;
51
- };
52
46
  };
53
47
 
54
48
  export type AnnouncementBarConfig = {
@@ -60,10 +54,11 @@ export type AnnouncementBarConfig = {
60
54
  };
61
55
 
62
56
  export type PrismConfig = {
63
- theme?: PrismTheme;
57
+ theme: PrismTheme;
64
58
  darkTheme?: PrismTheme;
65
59
  defaultLanguage?: string;
66
- additionalLanguages?: string[];
60
+ additionalLanguages: string[];
61
+ magicComments: MagicCommentConfig[];
67
62
  };
68
63
 
69
64
  export type FooterLinkItem = {
@@ -72,18 +67,20 @@ export type FooterLinkItem = {
72
67
  href?: string;
73
68
  html?: string;
74
69
  prependBaseUrlToHref?: string;
70
+ } & {[key: string]: unknown};
71
+
72
+ export type FooterLogo = {
73
+ alt?: string;
74
+ src: string;
75
+ srcDark?: string;
76
+ width?: string | number;
77
+ height?: string | number;
78
+ href?: string;
75
79
  };
76
80
 
77
81
  export type FooterBase = {
78
82
  style: 'light' | 'dark';
79
- logo?: {
80
- alt?: string;
81
- src?: string;
82
- srcDark?: string;
83
- width?: string | number;
84
- height?: string | number;
85
- href?: string;
86
- };
83
+ logo?: FooterLogo;
87
84
  copyright?: string;
88
85
  };
89
86
 
@@ -109,6 +106,10 @@ export type TableOfContents = {
109
106
  export type ThemeConfig = {
110
107
  docs: {
111
108
  versionPersistence: DocsVersionPersistence;
109
+ sidebar: {
110
+ hideable: boolean;
111
+ autoCollapseCategories: boolean;
112
+ };
112
113
  };
113
114
 
114
115
  // TODO we should complete this theme config type over time
@@ -121,17 +122,17 @@ export type ThemeConfig = {
121
122
  announcementBar?: AnnouncementBarConfig;
122
123
  prism: PrismConfig;
123
124
  footer?: Footer;
124
- hideableSidebar: boolean;
125
- autoCollapseSidebarCategories: boolean;
126
125
  image?: string;
127
- metadata: Array<Record<string, string>>;
128
- sidebarCollapsible: boolean;
126
+ metadata: Array<{[key: string]: string}>;
129
127
  tableOfContents: TableOfContents;
130
128
  };
131
129
 
132
130
  // User-provided theme config, unnormalized
133
131
  export type UserThemeConfig = DeepPartial<ThemeConfig>;
134
132
 
133
+ /**
134
+ * A convenient/more semantic way to get theme config from context.
135
+ */
135
136
  export function useThemeConfig(): ThemeConfig {
136
137
  return useDocusaurusContext().siteConfig.themeConfig as ThemeConfig;
137
138
  }