@docusaurus/theme-common 2.0.0-beta.1 → 2.0.0-beta.10

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 (94) hide show
  1. package/copyUntypedFiles.js +20 -0
  2. package/lib/.tsbuildinfo +1 -1
  3. package/lib/components/Collapsible/index.d.ts +35 -0
  4. package/lib/components/Collapsible/index.js +139 -0
  5. package/lib/components/Details/index.d.ts +12 -0
  6. package/lib/components/Details/index.js +64 -0
  7. package/lib/components/Details/styles.module.css +58 -0
  8. package/lib/index.d.ts +24 -4
  9. package/lib/index.js +18 -3
  10. package/lib/utils/ThemeClassNames.d.ts +36 -12
  11. package/lib/utils/ThemeClassNames.js +36 -3
  12. package/lib/utils/announcementBarUtils.d.ts +17 -0
  13. package/lib/utils/announcementBarUtils.js +69 -0
  14. package/lib/utils/codeBlockUtils.d.ts +10 -0
  15. package/lib/utils/codeBlockUtils.js +119 -0
  16. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +2 -2
  17. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +2 -2
  18. package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +1 -3
  19. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +11 -3
  20. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
  21. package/lib/utils/docsUtils.d.ts +20 -0
  22. package/lib/utils/docsUtils.js +106 -0
  23. package/lib/utils/generalUtils.d.ts +6 -0
  24. package/lib/utils/generalUtils.js +2 -2
  25. package/lib/utils/historyUtils.d.ts +11 -0
  26. package/lib/utils/historyUtils.js +39 -0
  27. package/lib/utils/jsUtils.d.ts +19 -0
  28. package/lib/utils/jsUtils.js +25 -0
  29. package/lib/utils/mobileSecondaryMenu.d.ts +20 -0
  30. package/lib/utils/mobileSecondaryMenu.js +49 -0
  31. package/lib/utils/pathUtils.js +1 -3
  32. package/lib/utils/reactUtils.d.ts +9 -0
  33. package/lib/utils/reactUtils.js +26 -0
  34. package/lib/utils/regexpUtils.d.ts +10 -0
  35. package/lib/utils/regexpUtils.js +16 -0
  36. package/lib/utils/scrollUtils.d.ts +52 -0
  37. package/lib/utils/scrollUtils.js +135 -0
  38. package/lib/utils/storageUtils.d.ts +4 -0
  39. package/lib/utils/storageUtils.js +29 -3
  40. package/lib/utils/tagsUtils.d.ts +18 -0
  41. package/lib/utils/tagsUtils.js +33 -0
  42. package/lib/utils/tocUtils.d.ts +15 -0
  43. package/lib/utils/tocUtils.js +34 -0
  44. package/lib/utils/useContextualSearchFilters.d.ts +11 -0
  45. package/lib/utils/useContextualSearchFilters.js +36 -0
  46. package/lib/utils/{useChangeRoute.d.ts → useLocalPathname.d.ts} +1 -1
  47. package/lib/utils/useLocalPathname.js +16 -0
  48. package/lib/utils/useLocationChange.d.ts +14 -0
  49. package/lib/utils/useLocationChange.js +23 -0
  50. package/lib/utils/usePluralForm.js +1 -3
  51. package/{src/utils/docsUtils.ts → lib/utils/usePrevious.d.ts} +1 -5
  52. package/lib/utils/usePrevious.js +15 -0
  53. package/lib/utils/useTOCHighlight.d.ts +14 -0
  54. package/lib/utils/useTOCHighlight.js +124 -0
  55. package/lib/utils/useThemeConfig.d.ts +21 -3
  56. package/package.json +18 -12
  57. package/src/components/Collapsible/index.tsx +242 -0
  58. package/src/components/Details/index.tsx +94 -0
  59. package/src/components/Details/styles.module.css +58 -0
  60. package/src/index.ts +73 -3
  61. package/src/types.d.ts +0 -2
  62. package/src/utils/ThemeClassNames.ts +42 -4
  63. package/src/utils/__tests__/codeBlockUtils.test.ts +2 -2
  64. package/src/utils/__tests__/docsUtils.test.tsx +331 -0
  65. package/src/utils/__tests__/jsUtils.test.ts +33 -0
  66. package/src/utils/__tests__/tagUtils.test.ts +66 -0
  67. package/src/utils/__tests__/tocUtils.test.ts +197 -0
  68. package/src/utils/announcementBarUtils.tsx +120 -0
  69. package/src/utils/codeBlockUtils.ts +151 -0
  70. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +7 -6
  71. package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +2 -3
  72. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +14 -14
  73. package/src/utils/docsUtils.tsx +185 -0
  74. package/src/utils/generalUtils.ts +3 -2
  75. package/src/utils/historyUtils.ts +50 -0
  76. package/src/utils/jsUtils.ts +33 -0
  77. package/src/utils/mobileSecondaryMenu.tsx +114 -0
  78. package/src/utils/pathUtils.ts +2 -3
  79. package/src/utils/reactUtils.tsx +34 -0
  80. package/src/utils/regexpUtils.ts +23 -0
  81. package/src/utils/scrollUtils.tsx +237 -0
  82. package/src/utils/storageUtils.ts +27 -4
  83. package/src/utils/tagsUtils.ts +48 -0
  84. package/src/utils/tocUtils.ts +55 -0
  85. package/src/utils/useAlternatePageUtils.ts +9 -1
  86. package/src/utils/useContextualSearchFilters.ts +50 -0
  87. package/src/utils/useLocalPathname.ts +20 -0
  88. package/src/utils/useLocationChange.ts +35 -0
  89. package/src/utils/usePluralForm.ts +5 -4
  90. package/src/utils/usePrevious.ts +19 -0
  91. package/src/utils/useTOCHighlight.ts +179 -0
  92. package/src/utils/useThemeConfig.ts +20 -3
  93. package/lib/utils/useChangeRoute.js +0 -18
  94. package/src/utils/useChangeRoute.ts +0 -21
@@ -0,0 +1,64 @@
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
+ import React, { useRef, useState } from 'react';
8
+ import useIsBrowser from '@docusaurus/useIsBrowser';
9
+ import clsx from 'clsx';
10
+ import { useCollapsible, Collapsible } from '../Collapsible';
11
+ import styles from './styles.module.css';
12
+ function isInSummary(node) {
13
+ if (!node) {
14
+ return false;
15
+ }
16
+ return node.tagName === 'SUMMARY' || isInSummary(node.parentElement);
17
+ }
18
+ function hasParent(node, parent) {
19
+ if (!node) {
20
+ return false;
21
+ }
22
+ return node === parent || hasParent(node.parentElement, parent);
23
+ }
24
+ function Details({ summary, children, ...props }) {
25
+ const isBrowser = useIsBrowser();
26
+ const detailsRef = useRef(null);
27
+ const { collapsed, setCollapsed } = useCollapsible({
28
+ initialState: !props.open,
29
+ });
30
+ // We use a separate prop because it must be set only after animation completes
31
+ // Otherwise close anim won't work
32
+ const [open, setOpen] = useState(props.open);
33
+ return (React.createElement("details", { ...props, ref: detailsRef, open: open, "data-collapsed": collapsed, className: clsx(styles.details, { [styles.isBrowser]: isBrowser }, props.className), onMouseDown: (e) => {
34
+ const target = e.target;
35
+ // Prevent a double-click to highlight summary text
36
+ if (isInSummary(target) && e.detail > 1) {
37
+ e.preventDefault();
38
+ }
39
+ }, onClick: (e) => {
40
+ e.stopPropagation(); // For isolation of multiple nested details/summary
41
+ const target = e.target;
42
+ const shouldToggle = isInSummary(target) && hasParent(target, detailsRef.current);
43
+ if (!shouldToggle) {
44
+ return;
45
+ }
46
+ e.preventDefault();
47
+ if (collapsed) {
48
+ setCollapsed(false);
49
+ setOpen(true);
50
+ }
51
+ else {
52
+ setCollapsed(true);
53
+ // setOpen(false); // Don't do this, it breaks close animation!
54
+ }
55
+ } },
56
+ summary,
57
+ React.createElement(Collapsible, { lazy: false, collapsed: collapsed, disableSSRStyle // Allows component to work fine even with JS disabled!
58
+ : true, onCollapseTransitionEnd: (newCollapsed) => {
59
+ setCollapsed(newCollapsed);
60
+ setOpen(!newCollapsed);
61
+ } },
62
+ React.createElement("div", { className: styles.collapsibleContent }, children))));
63
+ }
64
+ 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/lib/index.d.ts CHANGED
@@ -5,16 +5,36 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  export { useThemeConfig } from './utils/useThemeConfig';
8
- export type { ThemeConfig, Navbar, NavbarItem, NavbarLogo, Footer, FooterLinks, FooterLinkItem, } from './utils/useThemeConfig';
8
+ export type { ThemeConfig, UserThemeConfig, Navbar, NavbarItem, NavbarLogo, Footer, FooterLinks, FooterLinkItem, ColorModeConfig, } from './utils/useThemeConfig';
9
9
  export { createStorageSlot, listStorageKeys } from './utils/storageUtils';
10
10
  export { useAlternatePageUtils } from './utils/useAlternatePageUtils';
11
- export { parseCodeBlockTitle } from './utils/codeBlockUtils';
11
+ export { useContextualSearchFilters } from './utils/useContextualSearchFilters';
12
+ export { parseCodeBlockTitle, parseLanguage, parseLines, } from './utils/codeBlockUtils';
12
13
  export { docVersionSearchTag, DEFAULT_SEARCH_TAG } from './utils/searchUtils';
13
- export { isDocsPluginEnabled } from './utils/docsUtils';
14
+ export { isDocsPluginEnabled, DocsVersionProvider, useDocsVersion, useDocById, DocsSidebarProvider, useDocsSidebar, findSidebarCategory, findFirstCategoryLink, useCurrentSidebarCategory, isActiveSidebarItem, } from './utils/docsUtils';
14
15
  export { isSamePath } from './utils/pathUtils';
15
16
  export { useTitleFormatter } from './utils/generalUtils';
16
17
  export { usePluralForm } from './utils/usePluralForm';
17
- export { useChangeRoute } from './utils/useChangeRoute';
18
+ export { useLocationChange } from './utils/useLocationChange';
19
+ export { usePrevious } from './utils/usePrevious';
20
+ export { useCollapsible, Collapsible } from './components/Collapsible';
21
+ export type { UseCollapsibleConfig, UseCollapsibleReturns, } from './components/Collapsible';
22
+ export { default as Details } from './components/Details';
23
+ export type { DetailsProps } from './components/Details';
24
+ export { MobileSecondaryMenuProvider, MobileSecondaryMenuFiller, useMobileSecondaryMenuRenderer, } from './utils/mobileSecondaryMenu';
25
+ export type { MobileSecondaryMenuComponent } from './utils/mobileSecondaryMenu';
18
26
  export { useDocsPreferredVersion, useDocsPreferredVersionByPluginId, } from './utils/docsPreferredVersion/useDocsPreferredVersion';
27
+ export { duplicates, uniq } from './utils/jsUtils';
19
28
  export { DocsPreferredVersionContextProvider } from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
20
29
  export { ThemeClassNames } from './utils/ThemeClassNames';
30
+ export { AnnouncementBarProvider, useAnnouncementBar, } from './utils/announcementBarUtils';
31
+ export { useLocalPathname } from './utils/useLocalPathname';
32
+ export { translateTagsPageTitle, listTagsByLetters } from './utils/tagsUtils';
33
+ export type { TagLetterEntry } from './utils/tagsUtils';
34
+ export { useHistoryPopHandler } from './utils/historyUtils';
35
+ export { default as useTOCHighlight } from './utils/useTOCHighlight';
36
+ export type { TOCHighlightConfig } from './utils/useTOCHighlight';
37
+ export { useTOCFilter } from './utils/tocUtils';
38
+ export { ScrollControllerProvider, useScrollController, useScrollPosition, useScrollPositionBlocker, } from './utils/scrollUtils';
39
+ export { useIsomorphicLayoutEffect, useDynamicCallback, } from './utils/reactUtils';
40
+ export { isRegexpStringMatch } from './utils/regexpUtils';
package/lib/index.js CHANGED
@@ -7,13 +7,28 @@
7
7
  export { useThemeConfig } from './utils/useThemeConfig';
8
8
  export { createStorageSlot, listStorageKeys } from './utils/storageUtils';
9
9
  export { useAlternatePageUtils } from './utils/useAlternatePageUtils';
10
- export { parseCodeBlockTitle } from './utils/codeBlockUtils';
10
+ export { useContextualSearchFilters } from './utils/useContextualSearchFilters';
11
+ export { parseCodeBlockTitle, parseLanguage, parseLines, } from './utils/codeBlockUtils';
11
12
  export { docVersionSearchTag, DEFAULT_SEARCH_TAG } from './utils/searchUtils';
12
- export { isDocsPluginEnabled } from './utils/docsUtils';
13
+ export { isDocsPluginEnabled, DocsVersionProvider, useDocsVersion, useDocById, DocsSidebarProvider, useDocsSidebar, findSidebarCategory, findFirstCategoryLink, useCurrentSidebarCategory, isActiveSidebarItem, } from './utils/docsUtils';
13
14
  export { isSamePath } from './utils/pathUtils';
14
15
  export { useTitleFormatter } from './utils/generalUtils';
15
16
  export { usePluralForm } from './utils/usePluralForm';
16
- export { useChangeRoute } from './utils/useChangeRoute';
17
+ export { useLocationChange } from './utils/useLocationChange';
18
+ export { usePrevious } from './utils/usePrevious';
19
+ export { useCollapsible, Collapsible } from './components/Collapsible';
20
+ export { default as Details } from './components/Details';
21
+ export { MobileSecondaryMenuProvider, MobileSecondaryMenuFiller, useMobileSecondaryMenuRenderer, } from './utils/mobileSecondaryMenu';
17
22
  export { useDocsPreferredVersion, useDocsPreferredVersionByPluginId, } from './utils/docsPreferredVersion/useDocsPreferredVersion';
23
+ export { duplicates, uniq } from './utils/jsUtils';
18
24
  export { DocsPreferredVersionContextProvider } from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
19
25
  export { ThemeClassNames } from './utils/ThemeClassNames';
26
+ export { AnnouncementBarProvider, useAnnouncementBar, } from './utils/announcementBarUtils';
27
+ export { useLocalPathname } from './utils/useLocalPathname';
28
+ export { translateTagsPageTitle, listTagsByLetters } from './utils/tagsUtils';
29
+ export { useHistoryPopHandler } from './utils/historyUtils';
30
+ export { default as useTOCHighlight } from './utils/useTOCHighlight';
31
+ export { useTOCFilter } from './utils/tocUtils';
32
+ export { ScrollControllerProvider, useScrollController, useScrollPosition, useScrollPositionBlocker, } from './utils/scrollUtils';
33
+ export { useIsomorphicLayoutEffect, useDynamicCallback, } from './utils/reactUtils';
34
+ export { isRegexpStringMatch } from './utils/regexpUtils';
@@ -5,18 +5,42 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  export declare const ThemeClassNames: {
8
- page: {
9
- blogListPage: string;
10
- blogPostPage: string;
11
- blogTagsListPage: string;
12
- blogTagsPostPage: string;
13
- docPage: string;
14
- mdxPage: string;
8
+ readonly page: {
9
+ readonly blogListPage: "blog-list-page";
10
+ readonly blogPostPage: "blog-post-page";
11
+ readonly blogTagsListPage: "blog-tags-list-page";
12
+ readonly blogTagPostListPage: "blog-tags-post-list-page";
13
+ readonly docsDocPage: "docs-doc-page";
14
+ readonly docsTagsListPage: "docs-tags-list-page";
15
+ readonly docsTagDocListPage: "docs-tags-doc-list-page";
16
+ readonly mdxPage: "mdx-page";
15
17
  };
16
- wrapper: {
17
- main: string;
18
- blogPages: string;
19
- docPages: string;
20
- mdxPages: string;
18
+ readonly wrapper: {
19
+ readonly main: "main-wrapper";
20
+ readonly blogPages: "blog-wrapper";
21
+ readonly docsPages: "docs-wrapper";
22
+ readonly mdxPages: "mdx-wrapper";
21
23
  };
24
+ readonly common: {
25
+ readonly editThisPage: "theme-edit-this-page";
26
+ readonly lastUpdated: "theme-last-updated";
27
+ readonly backToTopButton: "theme-back-to-top-button";
28
+ };
29
+ readonly layout: {};
30
+ readonly docs: {
31
+ readonly docVersionBanner: "theme-doc-version-banner";
32
+ readonly docVersionBadge: "theme-doc-version-badge";
33
+ readonly docMarkdown: "theme-doc-markdown";
34
+ readonly docTocMobile: "theme-doc-toc-mobile";
35
+ readonly docTocDesktop: "theme-doc-toc-desktop";
36
+ readonly docFooter: "theme-doc-footer";
37
+ readonly docFooterTagsRow: "theme-doc-footer-tags-row";
38
+ readonly docFooterEditMetaRow: "theme-doc-footer-edit-meta-row";
39
+ readonly docSidebarMenu: "theme-doc-sidebar-menu";
40
+ readonly docSidebarItemCategory: "theme-doc-sidebar-item-category";
41
+ readonly docSidebarItemLink: "theme-doc-sidebar-item-link";
42
+ readonly docSidebarItemCategoryLevel: (level: number) => `theme-doc-sidebar-item-category-level-${number}`;
43
+ readonly docSidebarItemLinkLevel: (level: number) => `theme-doc-sidebar-item-link-level-${number}`;
44
+ };
45
+ readonly blog: {};
22
46
  };
@@ -5,19 +5,52 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  // These class names are used to style page layouts in Docusaurus
8
+ // Those are meant to be targeted by user-provided custom CSS selectors
9
+ // /!\ Please do not modify the classnames! This is a breaking change, and annoying for users!
8
10
  export const ThemeClassNames = {
9
11
  page: {
10
12
  blogListPage: 'blog-list-page',
11
13
  blogPostPage: 'blog-post-page',
12
14
  blogTagsListPage: 'blog-tags-list-page',
13
- blogTagsPostPage: 'blog-tags-post-page',
14
- docPage: 'doc-page',
15
+ blogTagPostListPage: 'blog-tags-post-list-page',
16
+ docsDocPage: 'docs-doc-page',
17
+ docsTagsListPage: 'docs-tags-list-page',
18
+ docsTagDocListPage: 'docs-tags-doc-list-page',
15
19
  mdxPage: 'mdx-page',
16
20
  },
17
21
  wrapper: {
18
22
  main: 'main-wrapper',
19
23
  blogPages: 'blog-wrapper',
20
- docPages: 'docs-wrapper',
24
+ docsPages: 'docs-wrapper',
21
25
  mdxPages: 'mdx-wrapper',
22
26
  },
27
+ // /!\ Please keep the naming convention consistent!
28
+ // Something like: "theme-{blog,doc,version,page}?-<suffix>"
29
+ common: {
30
+ editThisPage: 'theme-edit-this-page',
31
+ lastUpdated: 'theme-last-updated',
32
+ backToTopButton: 'theme-back-to-top-button',
33
+ },
34
+ layout: {
35
+ // TODO add other stable classNames here
36
+ },
37
+ docs: {
38
+ docVersionBanner: 'theme-doc-version-banner',
39
+ docVersionBadge: 'theme-doc-version-badge',
40
+ docMarkdown: 'theme-doc-markdown',
41
+ docTocMobile: 'theme-doc-toc-mobile',
42
+ docTocDesktop: 'theme-doc-toc-desktop',
43
+ docFooter: 'theme-doc-footer',
44
+ docFooterTagsRow: 'theme-doc-footer-tags-row',
45
+ docFooterEditMetaRow: 'theme-doc-footer-edit-meta-row',
46
+ docSidebarMenu: 'theme-doc-sidebar-menu',
47
+ docSidebarItemCategory: 'theme-doc-sidebar-item-category',
48
+ docSidebarItemLink: 'theme-doc-sidebar-item-link',
49
+ docSidebarItemCategoryLevel: (level) => `theme-doc-sidebar-item-category-level-${level}`,
50
+ docSidebarItemLinkLevel: (level) => `theme-doc-sidebar-item-link-level-${level}`,
51
+ // TODO add other stable classNames here
52
+ },
53
+ blog: {
54
+ // TODO add other stable classNames here
55
+ },
23
56
  };
@@ -0,0 +1,17 @@
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
+ import { ReactNode } from 'react';
8
+ export declare const AnnouncementBarDismissStorageKey = "docusaurus.announcement.dismiss";
9
+ declare type AnnouncementBarAPI = {
10
+ readonly isActive: boolean;
11
+ readonly close: () => void;
12
+ };
13
+ export declare function AnnouncementBarProvider({ children, }: {
14
+ children: ReactNode;
15
+ }): JSX.Element;
16
+ export declare const useAnnouncementBar: () => AnnouncementBarAPI;
17
+ export {};
@@ -0,0 +1,69 @@
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
+ import React, { useState, useEffect, useCallback, useMemo, useContext, createContext, } from 'react';
8
+ import useIsBrowser from '@docusaurus/useIsBrowser';
9
+ import { createStorageSlot } from './storageUtils';
10
+ import { useThemeConfig } from './useThemeConfig';
11
+ export const AnnouncementBarDismissStorageKey = 'docusaurus.announcement.dismiss';
12
+ const AnnouncementBarIdStorageKey = 'docusaurus.announcement.id';
13
+ const AnnouncementBarDismissStorage = createStorageSlot(AnnouncementBarDismissStorageKey);
14
+ const IdStorage = createStorageSlot(AnnouncementBarIdStorageKey);
15
+ const isDismissedInStorage = () => AnnouncementBarDismissStorage.get() === 'true';
16
+ const setDismissedInStorage = (bool) => AnnouncementBarDismissStorage.set(String(bool));
17
+ const useAnnouncementBarContextValue = () => {
18
+ const { announcementBar } = useThemeConfig();
19
+ const isBrowser = useIsBrowser();
20
+ const [isClosed, setClosed] = useState(() => isBrowser
21
+ ? // On client navigation: init with localstorage value
22
+ isDismissedInStorage()
23
+ : // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
24
+ false);
25
+ // Update state after hydration
26
+ useEffect(() => {
27
+ setClosed(isDismissedInStorage());
28
+ }, []);
29
+ const handleClose = useCallback(() => {
30
+ setDismissedInStorage(true);
31
+ setClosed(true);
32
+ }, []);
33
+ useEffect(() => {
34
+ if (!announcementBar) {
35
+ return;
36
+ }
37
+ const { id } = announcementBar;
38
+ let viewedId = IdStorage.get();
39
+ // retrocompatibility due to spelling mistake of default id
40
+ // see https://github.com/facebook/docusaurus/issues/3338
41
+ if (viewedId === 'annoucement-bar') {
42
+ viewedId = 'announcement-bar';
43
+ }
44
+ const isNewAnnouncement = id !== viewedId;
45
+ IdStorage.set(id);
46
+ if (isNewAnnouncement) {
47
+ setDismissedInStorage(false);
48
+ }
49
+ if (isNewAnnouncement || !isDismissedInStorage()) {
50
+ setClosed(false);
51
+ }
52
+ }, [announcementBar]);
53
+ return useMemo(() => ({
54
+ isActive: !!announcementBar && !isClosed,
55
+ close: handleClose,
56
+ }), [announcementBar, isClosed, handleClose]);
57
+ };
58
+ const AnnouncementBarContext = createContext(null);
59
+ export function AnnouncementBarProvider({ children, }) {
60
+ const value = useAnnouncementBarContextValue();
61
+ return (React.createElement(AnnouncementBarContext.Provider, { value: value }, children));
62
+ }
63
+ export const useAnnouncementBar = () => {
64
+ const api = useContext(AnnouncementBarContext);
65
+ if (!api) {
66
+ throw new Error('useAnnouncementBar(): AnnouncementBar not found in React context: make sure to use the AnnouncementBarProvider on top of the tree');
67
+ }
68
+ return api;
69
+ };
@@ -4,4 +4,14 @@
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
+ import type { Language } from 'prism-react-renderer';
7
8
  export declare function parseCodeBlockTitle(metastring?: string): string;
9
+ export declare function parseLanguage(className?: string): Language | undefined;
10
+ /**
11
+ * @param metastring The highlight range declared here starts at 1
12
+ * @returns Note: all line numbers start at 0, not 1
13
+ */
14
+ export declare function parseLines(content: string, metastring?: string, language?: Language): {
15
+ highlightLines: number[];
16
+ code: string;
17
+ };
@@ -4,8 +4,127 @@
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
+ import rangeParser from 'parse-numeric-range';
7
8
  const codeBlockTitleRegex = /title=(["'])(.*?)\1/;
9
+ const highlightLinesRangeRegex = /{([\d,-]+)}/;
10
+ const commentTypes = ['js', 'jsBlock', 'jsx', 'python', 'html'];
11
+ // Supported types of highlight comments
12
+ const commentPatterns = {
13
+ js: {
14
+ start: '\\/\\/',
15
+ end: '',
16
+ },
17
+ jsBlock: {
18
+ start: '\\/\\*',
19
+ end: '\\*\\/',
20
+ },
21
+ jsx: {
22
+ start: '\\{\\s*\\/\\*',
23
+ end: '\\*\\/\\s*\\}',
24
+ },
25
+ python: {
26
+ start: '#',
27
+ end: '',
28
+ },
29
+ html: {
30
+ start: '<!--',
31
+ end: '-->',
32
+ },
33
+ };
34
+ const magicCommentDirectives = [
35
+ 'highlight-next-line',
36
+ 'highlight-start',
37
+ 'highlight-end',
38
+ ];
39
+ const getMagicCommentDirectiveRegex = (languages = commentTypes) => {
40
+ // to be more reliable, the opening and closing comment must match
41
+ const commentPattern = languages
42
+ .map((lang) => {
43
+ const { start, end } = commentPatterns[lang];
44
+ return `(?:${start}\\s*(${magicCommentDirectives.join('|')})\\s*${end})`;
45
+ })
46
+ .join('|');
47
+ // white space is allowed, but otherwise it should be on it's own line
48
+ return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
49
+ };
50
+ // select comment styles based on language
51
+ const magicCommentDirectiveRegex = (lang) => {
52
+ switch (lang) {
53
+ case 'js':
54
+ case 'javascript':
55
+ case 'ts':
56
+ case 'typescript':
57
+ return getMagicCommentDirectiveRegex(['js', 'jsBlock']);
58
+ case 'jsx':
59
+ case 'tsx':
60
+ return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'jsx']);
61
+ case 'html':
62
+ return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'html']);
63
+ case 'python':
64
+ case 'py':
65
+ return getMagicCommentDirectiveRegex(['python']);
66
+ default:
67
+ // all comment types
68
+ return getMagicCommentDirectiveRegex();
69
+ }
70
+ };
8
71
  export function parseCodeBlockTitle(metastring) {
9
72
  var _a, _b;
10
73
  return (_b = (_a = metastring === null || metastring === void 0 ? void 0 : metastring.match(codeBlockTitleRegex)) === null || _a === void 0 ? void 0 : _a[2]) !== null && _b !== void 0 ? _b : '';
11
74
  }
75
+ export function parseLanguage(className) {
76
+ const languageClassName = className === null || className === void 0 ? void 0 : className.split(' ').find((str) => str.startsWith('language-'));
77
+ return languageClassName === null || languageClassName === void 0 ? void 0 : languageClassName.replace(/language-/, '');
78
+ }
79
+ /**
80
+ * @param metastring The highlight range declared here starts at 1
81
+ * @returns Note: all line numbers start at 0, not 1
82
+ */
83
+ export function parseLines(content, metastring, language) {
84
+ let code = content.replace(/\n$/, '');
85
+ // Highlighted lines specified in props: don't parse the content
86
+ if (metastring && highlightLinesRangeRegex.test(metastring)) {
87
+ const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
88
+ const highlightLines = rangeParser(highlightLinesRange)
89
+ .filter((n) => n > 0)
90
+ .map((n) => n - 1);
91
+ return { highlightLines, code };
92
+ }
93
+ if (language === undefined) {
94
+ return { highlightLines: [], code };
95
+ }
96
+ const directiveRegex = magicCommentDirectiveRegex(language);
97
+ // go through line by line
98
+ const lines = code.split('\n');
99
+ let highlightBlockStart;
100
+ let highlightRange = '';
101
+ // loop through lines
102
+ for (let lineNumber = 0; lineNumber < lines.length;) {
103
+ const line = lines[lineNumber];
104
+ const match = line.match(directiveRegex);
105
+ if (match !== null) {
106
+ const directive = match.slice(1).find((item) => item !== undefined);
107
+ switch (directive) {
108
+ case 'highlight-next-line':
109
+ highlightRange += `${lineNumber},`;
110
+ break;
111
+ case 'highlight-start':
112
+ highlightBlockStart = lineNumber;
113
+ break;
114
+ case 'highlight-end':
115
+ highlightRange += `${highlightBlockStart}-${lineNumber - 1},`;
116
+ break;
117
+ default:
118
+ break;
119
+ }
120
+ lines.splice(lineNumber, 1);
121
+ }
122
+ else {
123
+ // lines without directives are unchanged
124
+ lineNumber += 1;
125
+ }
126
+ }
127
+ const highlightLines = rangeParser(highlightRange);
128
+ code = lines.join('\n');
129
+ return { highlightLines, code };
130
+ }
@@ -4,7 +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
- import { ReactNode } from 'react';
7
+ /// <reference types="react" />
8
8
  declare type DocsPreferredVersionName = string | null;
9
9
  declare type DocsPreferredVersionPluginState = {
10
10
  preferredVersionName: DocsPreferredVersionName;
@@ -15,7 +15,7 @@ declare function useContextValue(): readonly [DocsPreferredVersionState, {
15
15
  }];
16
16
  declare type DocsPreferredVersionContextValue = ReturnType<typeof useContextValue>;
17
17
  export declare function DocsPreferredVersionContextProvider({ children, }: {
18
- children: ReactNode;
18
+ children: JSX.Element;
19
19
  }): JSX.Element;
20
20
  export declare function useDocsPreferredVersionContext(): DocsPreferredVersionContextValue;
21
21
  export {};
@@ -69,7 +69,7 @@ function useContextValue() {
69
69
  return {
70
70
  savePreferredVersion,
71
71
  };
72
- }, [setState]);
72
+ }, [versionPersistence]);
73
73
  return [state, api];
74
74
  }
75
75
  const Context = createContext(null);
@@ -78,7 +78,7 @@ export function DocsPreferredVersionContextProvider({ children, }) {
78
78
  return (React.createElement(DocsPreferredVersionContextProviderUnsafe, null, children));
79
79
  }
80
80
  else {
81
- return React.createElement(React.Fragment, null, children);
81
+ return children;
82
82
  }
83
83
  }
84
84
  function DocsPreferredVersionContextProviderUnsafe({ children, }) {
@@ -10,9 +10,7 @@ const DocsPreferredVersionStorage = {
10
10
  save: (pluginId, persistence, versionName) => {
11
11
  createStorageSlot(storageKey(pluginId), { persistence }).set(versionName);
12
12
  },
13
- read: (pluginId, persistence) => {
14
- return createStorageSlot(storageKey(pluginId), { persistence }).get();
15
- },
13
+ read: (pluginId, persistence) => createStorageSlot(storageKey(pluginId), { persistence }).get(),
16
14
  clear: (pluginId, persistence) => {
17
15
  createStorageSlot(storageKey(pluginId), { persistence }).del();
18
16
  },
@@ -1,5 +1,13 @@
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
+ /// <reference types="@docusaurus/plugin-content-docs" />
8
+ import { GlobalVersion } from '@theme/hooks/useDocs';
1
9
  export declare function useDocsPreferredVersion(pluginId?: string | undefined): {
2
- readonly preferredVersion: any;
3
- readonly savePreferredVersionName: (versionName: string) => void;
10
+ preferredVersion: GlobalVersion | null | undefined;
11
+ savePreferredVersionName: (versionName: string) => void;
4
12
  };
5
- export declare function useDocsPreferredVersionByPluginId(): Record<string, any>;
13
+ export declare function useDocsPreferredVersionByPluginId(): Record<string, GlobalVersion | null | undefined>;
@@ -8,7 +8,6 @@ import { useCallback } from 'react';
8
8
  import { useDocsPreferredVersionContext } from './DocsPreferredVersionProvider';
9
9
  import { useAllDocsData, useDocsData } from '@theme/hooks/useDocs';
10
10
  import { DEFAULT_PLUGIN_ID } from '@docusaurus/constants';
11
- // TODO improve typing
12
11
  // Note, the preferredVersion attribute will always be null before mount
13
12
  export function useDocsPreferredVersion(pluginId = DEFAULT_PLUGIN_ID) {
14
13
  const docsData = useDocsData(pluginId);
@@ -19,7 +18,7 @@ export function useDocsPreferredVersion(pluginId = DEFAULT_PLUGIN_ID) {
19
18
  : null;
20
19
  const savePreferredVersionName = useCallback((versionName) => {
21
20
  api.savePreferredVersion(pluginId, versionName);
22
- }, [api]);
21
+ }, [api, pluginId]);
23
22
  return { preferredVersion, savePreferredVersionName };
24
23
  }
25
24
  export function useDocsPreferredVersionByPluginId() {
@@ -4,4 +4,24 @@
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
+ import { ReactNode } from 'react';
8
+ import { PropSidebar, PropSidebarItem, PropSidebarItemCategory, PropVersionDoc, PropVersionMetadata } from '@docusaurus/plugin-content-docs';
7
9
  export declare const isDocsPluginEnabled: boolean;
10
+ declare const EmptyContextValue: unique symbol;
11
+ export declare function DocsVersionProvider({ children, version, }: {
12
+ children: ReactNode;
13
+ version: PropVersionMetadata | typeof EmptyContextValue;
14
+ }): JSX.Element;
15
+ export declare function useDocsVersion(): PropVersionMetadata;
16
+ export declare function useDocById(id: string): PropVersionDoc;
17
+ export declare function useDocById(id: string | undefined): PropVersionDoc | undefined;
18
+ export declare function DocsSidebarProvider({ children, sidebar, }: {
19
+ children: ReactNode;
20
+ sidebar: PropSidebar | null;
21
+ }): JSX.Element;
22
+ export declare function useDocsSidebar(): PropSidebar | null;
23
+ export declare function findSidebarCategory(sidebar: PropSidebar, predicate: (category: PropSidebarItemCategory) => boolean): PropSidebarItemCategory | undefined;
24
+ export declare function findFirstCategoryLink(item: PropSidebarItemCategory): string | undefined;
25
+ export declare function useCurrentSidebarCategory(): PropSidebarItemCategory;
26
+ export declare function isActiveSidebarItem(item: PropSidebarItem, activePath: string): boolean;
27
+ export {};