@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,94 @@
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, {ComponentProps, ReactElement, useRef, useState} from 'react';
9
+ import useIsBrowser from '@docusaurus/useIsBrowser';
10
+ import clsx from 'clsx';
11
+ import {useCollapsible, Collapsible} from '../Collapsible';
12
+ import styles from './styles.module.css';
13
+
14
+ function isInSummary(node: HTMLElement | null): boolean {
15
+ if (!node) {
16
+ return false;
17
+ }
18
+ return node.tagName === 'SUMMARY' || isInSummary(node.parentElement);
19
+ }
20
+
21
+ function hasParent(node: HTMLElement | null, parent: HTMLElement): boolean {
22
+ if (!node) {
23
+ return false;
24
+ }
25
+ return node === parent || hasParent(node.parentElement, parent);
26
+ }
27
+
28
+ export type DetailsProps = {
29
+ summary?: ReactElement;
30
+ } & ComponentProps<'details'>;
31
+
32
+ function Details({summary, children, ...props}: DetailsProps): JSX.Element {
33
+ const isBrowser = useIsBrowser();
34
+ const detailsRef = useRef<HTMLDetailsElement>(null);
35
+
36
+ const {collapsed, setCollapsed} = useCollapsible({
37
+ initialState: !props.open,
38
+ });
39
+ // We use a separate prop because it must be set only after animation completes
40
+ // Otherwise close anim won't work
41
+ const [open, setOpen] = useState(props.open);
42
+
43
+ return (
44
+ <details
45
+ {...props}
46
+ ref={detailsRef}
47
+ open={open}
48
+ data-collapsed={collapsed}
49
+ className={clsx(
50
+ styles.details,
51
+ {[styles.isBrowser]: isBrowser},
52
+ props.className,
53
+ )}
54
+ onMouseDown={(e) => {
55
+ const target = e.target as HTMLElement;
56
+ // Prevent a double-click to highlight summary text
57
+ if (isInSummary(target) && e.detail > 1) {
58
+ e.preventDefault();
59
+ }
60
+ }}
61
+ onClick={(e) => {
62
+ e.stopPropagation(); // For isolation of multiple nested details/summary
63
+ const target = e.target as HTMLElement;
64
+ const shouldToggle =
65
+ isInSummary(target) && hasParent(target, detailsRef.current!);
66
+ if (!shouldToggle) {
67
+ return;
68
+ }
69
+ e.preventDefault();
70
+ if (collapsed) {
71
+ setCollapsed(false);
72
+ setOpen(true);
73
+ } else {
74
+ setCollapsed(true);
75
+ // setOpen(false); // Don't do this, it breaks close animation!
76
+ }
77
+ }}>
78
+ {summary}
79
+
80
+ <Collapsible
81
+ lazy={false} // Content might matter for SEO in this case
82
+ collapsed={collapsed}
83
+ disableSSRStyle // Allows component to work fine even with JS disabled!
84
+ onCollapseTransitionEnd={(newCollapsed) => {
85
+ setCollapsed(newCollapsed);
86
+ setOpen(!newCollapsed);
87
+ }}>
88
+ <div className={styles.collapsibleContent}>{children}</div>
89
+ </Collapsible>
90
+ </details>
91
+ );
92
+ }
93
+
94
+ export default Details;
@@ -0,0 +1,58 @@
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
+ CSS variables, meant to be overriden by final theme
10
+ */
11
+ .details {
12
+ --docusaurus-details-summary-arrow-size: 0.38rem;
13
+ --docusaurus-details-transition: transform 200ms ease;
14
+ --docusaurus-details-decoration-color: grey;
15
+ }
16
+
17
+ .details > summary {
18
+ position: relative;
19
+ cursor: pointer;
20
+ list-style: none;
21
+ padding-left: 1rem;
22
+ }
23
+
24
+ /* TODO: deprecation, need to remove this after Safari will support `::marker` */
25
+ .details > summary::-webkit-details-marker {
26
+ display: none;
27
+ }
28
+
29
+ .details > summary:before {
30
+ position: absolute;
31
+ top: 0.45rem;
32
+ left: 0;
33
+
34
+ /* CSS-only Arrow */
35
+ content: '';
36
+ border-width: var(--docusaurus-details-summary-arrow-size);
37
+ border-style: solid;
38
+ border-color: transparent transparent transparent
39
+ var(--docusaurus-details-decoration-color);
40
+
41
+ /* Arrow rotation anim */
42
+ transform: rotate(0deg);
43
+ transition: var(--docusaurus-details-transition);
44
+ transform-origin: calc(var(--docusaurus-details-summary-arrow-size) / 2) 50%;
45
+ }
46
+
47
+ /* When JS disabled/failed to load: we use the open property for arrow animation: */
48
+ .details[open]:not(.isBrowser) > summary:before,
49
+ /* When JS works: we use the data-attribute for arrow animation */
50
+ .details[data-collapsed='false'].isBrowser > summary:before {
51
+ transform: rotate(90deg);
52
+ }
53
+
54
+ .collapsibleContent {
55
+ margin-top: 1rem;
56
+ border-top: 1px solid var(--docusaurus-details-decoration-color);
57
+ padding-top: 1rem;
58
+ }
package/src/index.ts CHANGED
@@ -9,23 +9,43 @@ export {useThemeConfig} from './utils/useThemeConfig';
9
9
 
10
10
  export type {
11
11
  ThemeConfig,
12
+ UserThemeConfig,
12
13
  Navbar,
13
14
  NavbarItem,
14
15
  NavbarLogo,
16
+ MultiColumnFooter,
17
+ SimpleFooter,
15
18
  Footer,
16
- FooterLinks,
17
19
  FooterLinkItem,
20
+ ColorModeConfig,
18
21
  } from './utils/useThemeConfig';
19
22
 
20
23
  export {createStorageSlot, listStorageKeys} from './utils/storageUtils';
21
24
 
22
25
  export {useAlternatePageUtils} from './utils/useAlternatePageUtils';
23
26
 
24
- export {parseCodeBlockTitle} from './utils/codeBlockUtils';
27
+ export {useContextualSearchFilters} from './utils/useContextualSearchFilters';
28
+
29
+ export {
30
+ parseCodeBlockTitle,
31
+ parseLanguage,
32
+ parseLines,
33
+ } from './utils/codeBlockUtils';
25
34
 
26
35
  export {docVersionSearchTag, DEFAULT_SEARCH_TAG} from './utils/searchUtils';
27
36
 
28
- export {isDocsPluginEnabled} from './utils/docsUtils';
37
+ export {
38
+ isDocsPluginEnabled,
39
+ DocsVersionProvider,
40
+ useDocsVersion,
41
+ useDocById,
42
+ DocsSidebarProvider,
43
+ useDocsSidebar,
44
+ findSidebarCategory,
45
+ findFirstCategoryLink,
46
+ useCurrentSidebarCategory,
47
+ isActiveSidebarItem,
48
+ } from './utils/docsUtils';
29
49
 
30
50
  export {isSamePath} from './utils/pathUtils';
31
51
 
@@ -37,11 +57,14 @@ export {useLocationChange} from './utils/useLocationChange';
37
57
 
38
58
  export {usePrevious} from './utils/usePrevious';
39
59
 
40
- export {useCollapsible, Collapsible} from './utils/useCollapsible';
60
+ export {useCollapsible, Collapsible} from './components/Collapsible';
41
61
  export type {
42
62
  UseCollapsibleConfig,
43
63
  UseCollapsibleReturns,
44
- } from './utils/useCollapsible';
64
+ } from './components/Collapsible';
65
+
66
+ export {default as Details} from './components/Details';
67
+ export type {DetailsProps} from './components/Details';
45
68
 
46
69
  export {
47
70
  MobileSecondaryMenuProvider,
@@ -55,6 +78,8 @@ export {
55
78
  useDocsPreferredVersionByPluginId,
56
79
  } from './utils/docsPreferredVersion/useDocsPreferredVersion';
57
80
 
81
+ export {duplicates, uniq} from './utils/jsUtils';
82
+
58
83
  export {DocsPreferredVersionContextProvider} from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
59
84
 
60
85
  export {ThemeClassNames} from './utils/ThemeClassNames';
@@ -63,3 +88,29 @@ export {
63
88
  AnnouncementBarProvider,
64
89
  useAnnouncementBar,
65
90
  } from './utils/announcementBarUtils';
91
+
92
+ export {useLocalPathname} from './utils/useLocalPathname';
93
+
94
+ export {translateTagsPageTitle, listTagsByLetters} from './utils/tagsUtils';
95
+ export type {TagLetterEntry} from './utils/tagsUtils';
96
+
97
+ export {useHistoryPopHandler} from './utils/historyUtils';
98
+
99
+ export {default as useTOCHighlight} from './utils/useTOCHighlight';
100
+ export type {TOCHighlightConfig} from './utils/useTOCHighlight';
101
+
102
+ export {useTOCFilter} from './utils/tocUtils';
103
+
104
+ export {
105
+ ScrollControllerProvider,
106
+ useScrollController,
107
+ useScrollPosition,
108
+ useScrollPositionBlocker,
109
+ } from './utils/scrollUtils';
110
+
111
+ export {
112
+ useIsomorphicLayoutEffect,
113
+ useDynamicCallback,
114
+ } from './utils/reactUtils';
115
+
116
+ export {isRegexpStringMatch} from './utils/regexpUtils';
package/src/types.d.ts CHANGED
@@ -5,8 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- /* eslint-disable import/no-duplicates */
9
- /* eslint-disable spaced-comment */
10
8
  /// <reference types="@docusaurus/module-type-aliases" />
11
9
  /// <reference types="@docusaurus/plugin-content-blog" />
12
10
  /// <reference types="@docusaurus/plugin-content-docs" />
@@ -6,19 +6,58 @@
6
6
  */
7
7
 
8
8
  // These class names are used to style page layouts in Docusaurus
9
+ // Those are meant to be targeted by user-provided custom CSS selectors
10
+ // /!\ Please do not modify the classnames! This is a breaking change, and annoying for users!
9
11
  export const ThemeClassNames = {
10
12
  page: {
11
13
  blogListPage: 'blog-list-page',
12
14
  blogPostPage: 'blog-post-page',
13
15
  blogTagsListPage: 'blog-tags-list-page',
14
- blogTagsPostPage: 'blog-tags-post-page',
15
- docPage: 'doc-page',
16
+ blogTagPostListPage: 'blog-tags-post-list-page',
17
+
18
+ docsDocPage: 'docs-doc-page',
19
+ docsTagsListPage: 'docs-tags-list-page', // List of tags
20
+ docsTagDocListPage: 'docs-tags-doc-list-page', // Docs for a tag
21
+
16
22
  mdxPage: 'mdx-page',
17
23
  },
18
24
  wrapper: {
19
25
  main: 'main-wrapper',
20
26
  blogPages: 'blog-wrapper',
21
- docPages: 'docs-wrapper',
27
+ docsPages: 'docs-wrapper',
22
28
  mdxPages: 'mdx-wrapper',
23
29
  },
24
- };
30
+
31
+ // /!\ Please keep the naming convention consistent!
32
+ // Something like: "theme-{blog,doc,version,page}?-<suffix>"
33
+ common: {
34
+ editThisPage: 'theme-edit-this-page',
35
+ lastUpdated: 'theme-last-updated',
36
+ backToTopButton: 'theme-back-to-top-button',
37
+ codeBlock: 'theme-code-block',
38
+ },
39
+ layout: {
40
+ // TODO add other stable classNames here
41
+ },
42
+ docs: {
43
+ docVersionBanner: 'theme-doc-version-banner',
44
+ docVersionBadge: 'theme-doc-version-badge',
45
+ docMarkdown: 'theme-doc-markdown',
46
+ docTocMobile: 'theme-doc-toc-mobile',
47
+ docTocDesktop: 'theme-doc-toc-desktop',
48
+ docFooter: 'theme-doc-footer',
49
+ docFooterTagsRow: 'theme-doc-footer-tags-row',
50
+ docFooterEditMetaRow: 'theme-doc-footer-edit-meta-row',
51
+ docSidebarMenu: 'theme-doc-sidebar-menu',
52
+ docSidebarItemCategory: 'theme-doc-sidebar-item-category',
53
+ docSidebarItemLink: 'theme-doc-sidebar-item-link',
54
+ docSidebarItemCategoryLevel: (level: number) =>
55
+ `theme-doc-sidebar-item-category-level-${level}` as const,
56
+ docSidebarItemLinkLevel: (level: number) =>
57
+ `theme-doc-sidebar-item-link-level-${level}` as const,
58
+ // TODO add other stable classNames here
59
+ },
60
+ blog: {
61
+ // TODO add other stable classNames here
62
+ },
63
+ } as const;
@@ -14,7 +14,7 @@ import React, {
14
14
  useContext,
15
15
  createContext,
16
16
  } from 'react';
17
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
17
+ import useIsBrowser from '@docusaurus/useIsBrowser';
18
18
  import {createStorageSlot} from './storageUtils';
19
19
  import {useThemeConfig} from './useThemeConfig';
20
20
 
@@ -33,21 +33,21 @@ const setDismissedInStorage = (bool: boolean) =>
33
33
  AnnouncementBarDismissStorage.set(String(bool));
34
34
 
35
35
  type AnnouncementBarAPI = {
36
- readonly isClosed: boolean;
36
+ readonly isActive: boolean;
37
37
  readonly close: () => void;
38
38
  };
39
39
 
40
40
  const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
41
41
  const {announcementBar} = useThemeConfig();
42
- const {isClient} = useDocusaurusContext();
42
+ const isBrowser = useIsBrowser();
43
43
 
44
- const [isClosed, setClosed] = useState(() => {
45
- return isClient
44
+ const [isClosed, setClosed] = useState(() =>
45
+ isBrowser
46
46
  ? // On client navigation: init with localstorage value
47
47
  isDismissedInStorage()
48
48
  : // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
49
- false;
50
- });
49
+ false,
50
+ );
51
51
  // Update state after hydration
52
52
  useEffect(() => {
53
53
  setClosed(isDismissedInStorage());
@@ -83,26 +83,31 @@ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
83
83
  if (isNewAnnouncement || !isDismissedInStorage()) {
84
84
  setClosed(false);
85
85
  }
86
- }, []);
86
+ }, [announcementBar]);
87
87
 
88
- return useMemo(() => {
89
- return {
90
- isClosed,
88
+ return useMemo(
89
+ () => ({
90
+ isActive: !!announcementBar && !isClosed,
91
91
  close: handleClose,
92
- };
93
- }, [isClosed]);
92
+ }),
93
+ [announcementBar, isClosed, handleClose],
94
+ );
94
95
  };
95
96
 
96
97
  const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
97
98
 
98
- export const AnnouncementBarProvider = ({children}: {children: ReactNode}) => {
99
+ export function AnnouncementBarProvider({
100
+ children,
101
+ }: {
102
+ children: ReactNode;
103
+ }): JSX.Element {
99
104
  const value = useAnnouncementBarContextValue();
100
105
  return (
101
106
  <AnnouncementBarContext.Provider value={value}>
102
107
  {children}
103
108
  </AnnouncementBarContext.Provider>
104
109
  );
105
- };
110
+ }
106
111
 
107
112
  export const useAnnouncementBar = (): AnnouncementBarAPI => {
108
113
  const api = useContext(AnnouncementBarContext);
@@ -5,8 +5,159 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
+ import rangeParser from 'parse-numeric-range';
9
+ import type {Language} from 'prism-react-renderer';
10
+
8
11
  const codeBlockTitleRegex = /title=(["'])(.*?)\1/;
12
+ const highlightLinesRangeRegex = /{([\d,-]+)}/;
13
+
14
+ const commentTypes = ['js', 'jsBlock', 'jsx', 'python', 'html'] as const;
15
+ type CommentType = typeof commentTypes[number];
16
+
17
+ type CommentPattern = {
18
+ start: string;
19
+ end: string;
20
+ };
21
+
22
+ // Supported types of highlight comments
23
+ const commentPatterns: Record<CommentType, CommentPattern> = {
24
+ js: {
25
+ start: '\\/\\/',
26
+ end: '',
27
+ },
28
+ jsBlock: {
29
+ start: '\\/\\*',
30
+ end: '\\*\\/',
31
+ },
32
+ jsx: {
33
+ start: '\\{\\s*\\/\\*',
34
+ end: '\\*\\/\\s*\\}',
35
+ },
36
+ python: {
37
+ start: '#',
38
+ end: '',
39
+ },
40
+ html: {
41
+ start: '<!--',
42
+ end: '-->',
43
+ },
44
+ };
45
+
46
+ const magicCommentDirectives = [
47
+ 'highlight-next-line',
48
+ 'highlight-start',
49
+ 'highlight-end',
50
+ ];
51
+
52
+ const getMagicCommentDirectiveRegex = (
53
+ languages: readonly CommentType[] = commentTypes,
54
+ ) => {
55
+ // to be more reliable, the opening and closing comment must match
56
+ const commentPattern = languages
57
+ .map((lang) => {
58
+ const {start, end} = commentPatterns[lang];
59
+ return `(?:${start}\\s*(${magicCommentDirectives.join('|')})\\s*${end})`;
60
+ })
61
+ .join('|');
62
+ // white space is allowed, but otherwise it should be on it's own line
63
+ return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
64
+ };
65
+
66
+ // select comment styles based on language
67
+ const magicCommentDirectiveRegex = (lang: string) => {
68
+ switch (lang) {
69
+ case 'js':
70
+ case 'javascript':
71
+ case 'ts':
72
+ case 'typescript':
73
+ return getMagicCommentDirectiveRegex(['js', 'jsBlock']);
74
+
75
+ case 'jsx':
76
+ case 'tsx':
77
+ return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'jsx']);
78
+
79
+ case 'html':
80
+ return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'html']);
81
+
82
+ case 'python':
83
+ case 'py':
84
+ return getMagicCommentDirectiveRegex(['python']);
85
+
86
+ default:
87
+ // all comment types
88
+ return getMagicCommentDirectiveRegex();
89
+ }
90
+ };
9
91
 
10
92
  export function parseCodeBlockTitle(metastring?: string): string {
11
93
  return metastring?.match(codeBlockTitleRegex)?.[2] ?? '';
12
94
  }
95
+
96
+ export function parseLanguage(className?: string): Language | undefined {
97
+ const languageClassName = className
98
+ ?.split(' ')
99
+ .find((str) => str.startsWith('language-'));
100
+ return languageClassName?.replace(/language-/, '') as Language | undefined;
101
+ }
102
+
103
+ /**
104
+ * @param metastring The highlight range declared here starts at 1
105
+ * @returns Note: all line numbers start at 0, not 1
106
+ */
107
+ export function parseLines(
108
+ content: string,
109
+ metastring?: string,
110
+ language?: Language,
111
+ ): {
112
+ highlightLines: number[];
113
+ code: string;
114
+ } {
115
+ let code = content.replace(/\n$/, '');
116
+ // Highlighted lines specified in props: don't parse the content
117
+ if (metastring && highlightLinesRangeRegex.test(metastring)) {
118
+ const highlightLinesRange = metastring.match(highlightLinesRangeRegex)![1];
119
+ const highlightLines = rangeParser(highlightLinesRange)
120
+ .filter((n) => n > 0)
121
+ .map((n) => n - 1);
122
+ return {highlightLines, code};
123
+ }
124
+ if (language === undefined) {
125
+ return {highlightLines: [], code};
126
+ }
127
+ const directiveRegex = magicCommentDirectiveRegex(language);
128
+ // go through line by line
129
+ const lines = code.split('\n');
130
+ let highlightBlockStart: number;
131
+ let highlightRange = '';
132
+ // loop through lines
133
+ for (let lineNumber = 0; lineNumber < lines.length; ) {
134
+ const line = lines[lineNumber];
135
+ const match = line.match(directiveRegex);
136
+ if (match !== null) {
137
+ const directive = match.slice(1).find((item) => item !== undefined);
138
+ switch (directive) {
139
+ case 'highlight-next-line':
140
+ highlightRange += `${lineNumber},`;
141
+ break;
142
+
143
+ case 'highlight-start':
144
+ highlightBlockStart = lineNumber;
145
+ break;
146
+
147
+ case 'highlight-end':
148
+ highlightRange += `${highlightBlockStart!}-${lineNumber - 1},`;
149
+ break;
150
+
151
+ default:
152
+ break;
153
+ }
154
+ lines.splice(lineNumber, 1);
155
+ } else {
156
+ // lines without directives are unchanged
157
+ lineNumber += 1;
158
+ }
159
+ }
160
+ const highlightLines = rangeParser(highlightRange);
161
+ code = lines.join('\n');
162
+ return {highlightLines, code};
163
+ }
@@ -4,6 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
+
7
8
  import React, {
8
9
  createContext,
9
10
  ReactNode,
@@ -15,7 +16,7 @@ import React, {
15
16
  import {useThemeConfig, DocsVersionPersistence} from '../useThemeConfig';
16
17
  import {isDocsPluginEnabled} from '../docsUtils';
17
18
 
18
- import {useAllDocsData} from '@theme/hooks/useDocs';
19
+ import {useAllDocsData, GlobalPluginData} from '@theme/hooks/useDocs';
19
20
 
20
21
  import DocsPreferredVersionStorage from './DocsPreferredVersionStorage';
21
22
 
@@ -54,7 +55,7 @@ function readStorageState({
54
55
  }: {
55
56
  pluginIds: string[];
56
57
  versionPersistence: DocsVersionPersistence;
57
- allDocsData: any; // TODO find a way to type it :(
58
+ allDocsData: Record<string, GlobalPluginData>;
58
59
  }): DocsPreferredVersionState {
59
60
  // The storage value we read might be stale,
60
61
  // and belong to a version that does not exist in the site anymore
@@ -68,7 +69,7 @@ function readStorageState({
68
69
  );
69
70
  const pluginData = allDocsData[pluginId];
70
71
  const versionExists = pluginData.versions.some(
71
- (version: any) => version.name === preferredVersionNameUnsafe,
72
+ (version) => version.name === preferredVersionNameUnsafe,
72
73
  );
73
74
  if (versionExists) {
74
75
  return {preferredVersionName: preferredVersionNameUnsafe};
@@ -120,7 +121,7 @@ function useContextValue() {
120
121
  return {
121
122
  savePreferredVersion,
122
123
  };
123
- }, [setState]);
124
+ }, [versionPersistence]);
124
125
 
125
126
  return [state, api] as const;
126
127
  }
@@ -132,7 +133,7 @@ const Context = createContext<DocsPreferredVersionContextValue | null>(null);
132
133
  export function DocsPreferredVersionContextProvider({
133
134
  children,
134
135
  }: {
135
- children: ReactNode;
136
+ children: JSX.Element;
136
137
  }): JSX.Element {
137
138
  if (isDocsPluginEnabled) {
138
139
  return (
@@ -141,7 +142,7 @@ export function DocsPreferredVersionContextProvider({
141
142
  </DocsPreferredVersionContextProviderUnsafe>
142
143
  );
143
144
  } else {
144
- return <>{children}</>;
145
+ return children;
145
146
  }
146
147
  }
147
148
 
@@ -22,9 +22,8 @@ const DocsPreferredVersionStorage = {
22
22
  read: (
23
23
  pluginId: string,
24
24
  persistence: DocsVersionPersistence,
25
- ): string | null => {
26
- return createStorageSlot(storageKey(pluginId), {persistence}).get();
27
- },
25
+ ): string | null =>
26
+ createStorageSlot(storageKey(pluginId), {persistence}).get(),
28
27
 
29
28
  clear: (pluginId: string, persistence: DocsVersionPersistence): void => {
30
29
  createStorageSlot(storageKey(pluginId), {persistence}).del();