@docusaurus/theme-common 2.0.0-beta.17 → 2.0.0-beta.18
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.
- package/lib/components/Collapsible/index.d.ts +32 -4
- package/lib/components/Collapsible/index.d.ts.map +1 -1
- package/lib/components/Collapsible/index.js +12 -3
- package/lib/components/Collapsible/index.js.map +1 -1
- package/lib/components/Details/index.d.ts +6 -1
- package/lib/components/Details/index.d.ts.map +1 -1
- package/lib/components/Details/index.js +8 -4
- package/lib/components/Details/index.js.map +1 -1
- package/lib/{utils/announcementBarUtils.d.ts → contexts/announcementBar.d.ts} +7 -3
- package/lib/contexts/announcementBar.d.ts.map +1 -0
- package/lib/{utils/announcementBarUtils.js → contexts/announcementBar.js} +11 -11
- package/lib/contexts/announcementBar.js.map +1 -0
- package/lib/{utils/colorModeUtils.d.ts → contexts/colorMode.d.ts} +12 -3
- package/lib/contexts/colorMode.d.ts.map +1 -0
- package/lib/contexts/colorMode.js +114 -0
- package/lib/contexts/colorMode.js.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
- package/lib/{utils → contexts}/docSidebarItemsExpandedState.js +9 -4
- package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
- package/lib/contexts/docsPreferredVersion.d.ts +28 -0
- package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
- package/lib/contexts/docsPreferredVersion.js +125 -0
- package/lib/contexts/docsPreferredVersion.js.map +1 -0
- package/lib/contexts/docsSidebar.d.ts +20 -0
- package/lib/contexts/docsSidebar.d.ts.map +1 -0
- package/lib/contexts/docsSidebar.js +29 -0
- package/lib/contexts/docsSidebar.js.map +1 -0
- package/lib/contexts/docsVersion.d.ts +20 -0
- package/lib/contexts/docsVersion.d.ts.map +1 -0
- package/lib/contexts/docsVersion.js +26 -0
- package/lib/contexts/docsVersion.js.map +1 -0
- package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
- package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
- package/lib/contexts/navbarMobileSidebar.js +56 -0
- package/lib/contexts/navbarMobileSidebar.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu.d.ts +38 -0
- package/lib/contexts/navbarSecondaryMenu.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu.js +93 -0
- package/lib/contexts/navbarSecondaryMenu.js.map +1 -0
- package/lib/{utils/tabGroupChoiceUtils.d.ts → contexts/tabGroupChoice.d.ts} +5 -3
- package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
- package/lib/{utils/tabGroupChoiceUtils.js → contexts/tabGroupChoice.js} +14 -20
- package/lib/contexts/tabGroupChoice.js.map +1 -0
- package/lib/hooks/useHideableNavbar.d.ts +7 -3
- package/lib/hooks/useHideableNavbar.d.ts.map +1 -1
- package/lib/hooks/useHideableNavbar.js +8 -6
- package/lib/hooks/useHideableNavbar.js.map +1 -1
- package/lib/hooks/useKeyboardNavigation.d.ts +11 -1
- package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -1
- package/lib/hooks/useKeyboardNavigation.js +11 -3
- package/lib/hooks/useKeyboardNavigation.js.map +1 -1
- package/lib/hooks/useLockBodyScroll.d.ts +5 -1
- package/lib/hooks/useLockBodyScroll.d.ts.map +1 -1
- package/lib/hooks/useLockBodyScroll.js +5 -1
- package/lib/hooks/useLockBodyScroll.js.map +1 -1
- package/lib/hooks/usePrismTheme.d.ts +5 -1
- package/lib/hooks/usePrismTheme.d.ts.map +1 -1
- package/lib/hooks/usePrismTheme.js +8 -4
- package/lib/hooks/usePrismTheme.js.map +1 -1
- package/lib/hooks/useSearchPage.d.ts +15 -4
- package/lib/hooks/useSearchPage.d.ts.map +1 -1
- package/lib/hooks/useSearchPage.js +3 -2
- package/lib/hooks/useSearchPage.js.map +1 -1
- package/lib/hooks/useTOCHighlight.d.ts +25 -0
- package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
- package/lib/{utils → hooks}/useTOCHighlight.js +13 -9
- package/lib/hooks/useTOCHighlight.js.map +1 -0
- package/lib/hooks/useWindowSize.d.ts +14 -1
- package/lib/hooks/useWindowSize.d.ts.map +1 -1
- package/lib/hooks/useWindowSize.js +14 -11
- package/lib/hooks/useWindowSize.js.map +1 -1
- package/lib/index.d.ts +26 -30
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +26 -24
- package/lib/index.js.map +1 -1
- package/lib/utils/ThemeClassNames.d.ts +7 -0
- package/lib/utils/ThemeClassNames.d.ts.map +1 -1
- package/lib/utils/ThemeClassNames.js +7 -4
- package/lib/utils/ThemeClassNames.js.map +1 -1
- package/lib/utils/codeBlockUtils.d.ts +25 -2
- package/lib/utils/codeBlockUtils.d.ts.map +1 -1
- package/lib/utils/codeBlockUtils.js +36 -35
- package/lib/utils/codeBlockUtils.js.map +1 -1
- package/lib/utils/docsUtils.d.ts +25 -18
- package/lib/utils/docsUtils.d.ts.map +1 -1
- package/lib/utils/docsUtils.js +38 -56
- package/lib/utils/docsUtils.js.map +1 -1
- package/lib/utils/footerUtils.d.ts +13 -0
- package/lib/utils/footerUtils.d.ts.map +1 -0
- package/lib/utils/footerUtils.js +14 -0
- package/lib/utils/footerUtils.js.map +1 -0
- package/lib/utils/generalUtils.d.ts +4 -1
- package/lib/utils/generalUtils.d.ts.map +1 -1
- package/lib/utils/generalUtils.js +6 -3
- package/lib/utils/generalUtils.js.map +1 -1
- package/lib/utils/historyUtils.d.ts +1 -7
- package/lib/utils/historyUtils.d.ts.map +1 -1
- package/lib/utils/historyUtils.js +10 -13
- package/lib/utils/historyUtils.js.map +1 -1
- package/lib/utils/jsUtils.d.ts +1 -1
- package/lib/utils/jsUtils.js +1 -1
- package/lib/utils/metadataUtils.d.ts +38 -0
- package/lib/utils/metadataUtils.d.ts.map +1 -0
- package/lib/utils/metadataUtils.js +61 -0
- package/lib/utils/metadataUtils.js.map +1 -0
- package/lib/utils/navbarUtils.d.ts +21 -0
- package/lib/utils/navbarUtils.d.ts.map +1 -0
- package/lib/utils/navbarUtils.js +30 -0
- package/lib/utils/navbarUtils.js.map +1 -0
- package/lib/utils/reactUtils.d.ts +16 -5
- package/lib/utils/reactUtils.d.ts.map +1 -1
- package/lib/utils/reactUtils.js +28 -7
- package/lib/utils/reactUtils.js.map +1 -1
- package/lib/utils/regexpUtils.d.ts +2 -1
- package/lib/utils/regexpUtils.d.ts.map +1 -1
- package/lib/utils/regexpUtils.js +2 -1
- package/lib/utils/regexpUtils.js.map +1 -1
- package/lib/utils/routesUtils.d.ts +14 -2
- package/lib/utils/routesUtils.d.ts.map +1 -1
- package/lib/utils/routesUtils.js +20 -7
- package/lib/utils/routesUtils.js.map +1 -1
- package/lib/utils/scrollUtils.d.ts +32 -26
- package/lib/utils/scrollUtils.d.ts.map +1 -1
- package/lib/utils/scrollUtils.js +30 -17
- package/lib/utils/scrollUtils.js.map +1 -1
- package/lib/utils/searchUtils.d.ts +12 -0
- package/lib/utils/searchUtils.d.ts.map +1 -1
- package/lib/utils/searchUtils.js +34 -0
- package/lib/utils/searchUtils.js.map +1 -1
- package/lib/utils/storageUtils.d.ts +10 -7
- package/lib/utils/storageUtils.d.ts.map +1 -1
- package/lib/utils/storageUtils.js +20 -12
- package/lib/utils/storageUtils.js.map +1 -1
- package/lib/utils/tagsUtils.d.ts +5 -2
- package/lib/utils/tagsUtils.d.ts.map +1 -1
- package/lib/utils/tagsUtils.js +7 -4
- package/lib/utils/tagsUtils.js.map +1 -1
- package/lib/utils/tocUtils.d.ts +16 -0
- package/lib/utils/tocUtils.d.ts.map +1 -1
- package/lib/utils/tocUtils.js +17 -6
- package/lib/utils/tocUtils.js.map +1 -1
- package/lib/utils/useAlternatePageUtils.d.ts +20 -1
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -1
- package/lib/utils/useAlternatePageUtils.js +6 -3
- package/lib/utils/useAlternatePageUtils.js.map +1 -1
- package/lib/utils/useLocalPathname.d.ts +5 -0
- package/lib/utils/useLocalPathname.d.ts.map +1 -1
- package/lib/utils/useLocalPathname.js +6 -4
- package/lib/utils/useLocalPathname.js.map +1 -1
- package/lib/utils/useLocationChange.d.ts +7 -5
- package/lib/utils/useLocationChange.d.ts.map +1 -1
- package/lib/utils/useLocationChange.js +6 -2
- package/lib/utils/useLocationChange.js.map +1 -1
- package/lib/utils/usePluralForm.d.ts +11 -0
- package/lib/utils/usePluralForm.d.ts.map +1 -1
- package/lib/utils/usePluralForm.js +19 -24
- package/lib/utils/usePluralForm.js.map +1 -1
- package/lib/utils/useThemeConfig.d.ts +21 -11
- package/lib/utils/useThemeConfig.d.ts.map +1 -1
- package/lib/utils/useThemeConfig.js +3 -0
- package/lib/utils/useThemeConfig.js.map +1 -1
- package/package.json +8 -9
- package/src/components/Collapsible/index.tsx +40 -22
- package/src/components/Details/index.tsx +11 -6
- package/src/{utils/announcementBarUtils.tsx → contexts/announcementBar.tsx} +17 -18
- package/src/contexts/colorMode.tsx +176 -0
- package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
- package/src/contexts/docsPreferredVersion.tsx +250 -0
- package/src/contexts/docsSidebar.tsx +42 -0
- package/src/contexts/docsVersion.tsx +36 -0
- package/src/contexts/navbarMobileSidebar.tsx +99 -0
- package/src/contexts/navbarSecondaryMenu.tsx +170 -0
- package/src/{utils/tabGroupChoiceUtils.tsx → contexts/tabGroupChoice.tsx} +21 -28
- package/src/hooks/useHideableNavbar.ts +11 -11
- package/src/hooks/useKeyboardNavigation.ts +11 -3
- package/src/hooks/useLockBodyScroll.ts +5 -2
- package/src/hooks/usePrismTheme.ts +8 -4
- package/src/hooks/useSearchPage.ts +18 -5
- package/src/{utils → hooks}/useTOCHighlight.ts +21 -12
- package/src/hooks/useWindowSize.ts +14 -12
- package/src/index.ts +68 -56
- package/src/utils/ThemeClassNames.ts +10 -6
- package/src/utils/codeBlockUtils.ts +49 -47
- package/src/utils/docsUtils.tsx +48 -99
- package/src/utils/footerUtils.ts +18 -0
- package/src/utils/generalUtils.ts +6 -3
- package/src/utils/historyUtils.ts +11 -17
- package/src/utils/jsUtils.ts +1 -1
- package/src/utils/metadataUtils.tsx +115 -0
- package/src/utils/navbarUtils.tsx +40 -0
- package/src/utils/reactUtils.tsx +31 -8
- package/src/utils/regexpUtils.ts +2 -1
- package/src/utils/routesUtils.ts +27 -8
- package/src/utils/scrollUtils.tsx +44 -45
- package/src/utils/searchUtils.ts +49 -0
- package/src/utils/storageUtils.ts +21 -13
- package/src/utils/tagsUtils.ts +14 -7
- package/src/utils/tocUtils.ts +18 -7
- package/src/utils/useAlternatePageUtils.ts +17 -5
- package/src/utils/useLocalPathname.ts +6 -4
- package/src/utils/useLocationChange.ts +12 -10
- package/src/utils/usePluralForm.ts +27 -24
- package/src/utils/useThemeConfig.ts +16 -11
- package/lib/utils/announcementBarUtils.d.ts.map +0 -1
- package/lib/utils/announcementBarUtils.js.map +0 -1
- package/lib/utils/colorModeUtils.d.ts.map +0 -1
- package/lib/utils/colorModeUtils.js +0 -107
- package/lib/utils/colorModeUtils.js.map +0 -1
- package/lib/utils/docSidebarItemsExpandedState.d.ts +0 -17
- package/lib/utils/docSidebarItemsExpandedState.d.ts.map +0 -1
- package/lib/utils/docSidebarItemsExpandedState.js.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -22
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -92
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -14
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts.map +0 -1
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -19
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js.map +0 -1
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -14
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts.map +0 -1
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js.map +0 -1
- package/lib/utils/mobileSecondaryMenu.d.ts +0 -21
- package/lib/utils/mobileSecondaryMenu.d.ts.map +0 -1
- package/lib/utils/mobileSecondaryMenu.js +0 -51
- package/lib/utils/mobileSecondaryMenu.js.map +0 -1
- package/lib/utils/pathUtils.d.ts +0 -8
- package/lib/utils/pathUtils.d.ts.map +0 -1
- package/lib/utils/pathUtils.js +0 -14
- package/lib/utils/pathUtils.js.map +0 -1
- package/lib/utils/tabGroupChoiceUtils.d.ts.map +0 -1
- package/lib/utils/tabGroupChoiceUtils.js.map +0 -1
- package/lib/utils/useContextualSearchFilters.d.ts +0 -12
- package/lib/utils/useContextualSearchFilters.d.ts.map +0 -1
- package/lib/utils/useContextualSearchFilters.js +0 -36
- package/lib/utils/useContextualSearchFilters.js.map +0 -1
- package/lib/utils/usePrevious.d.ts +0 -8
- package/lib/utils/usePrevious.d.ts.map +0 -1
- package/lib/utils/usePrevious.js +0 -16
- package/lib/utils/usePrevious.js.map +0 -1
- package/lib/utils/useTOCHighlight.d.ts +0 -14
- package/lib/utils/useTOCHighlight.d.ts.map +0 -1
- package/lib/utils/useTOCHighlight.js.map +0 -1
- package/src/utils/colorModeUtils.tsx +0 -158
- package/src/utils/docSidebarItemsExpandedState.tsx +0 -40
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -166
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -33
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -70
- package/src/utils/mobileSecondaryMenu.tsx +0 -114
- package/src/utils/pathUtils.ts +0 -19
- package/src/utils/useContextualSearchFilters.ts +0 -53
- package/src/utils/usePrevious.ts +0 -19
|
@@ -9,14 +9,16 @@ import {useState, useCallback, useRef} from 'react';
|
|
|
9
9
|
import {useLocationChange} from '../utils/useLocationChange';
|
|
10
10
|
import {useScrollPosition} from '../utils/scrollUtils';
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Wires the imperative logic of a hideable navbar.
|
|
14
|
+
* @param hideOnScroll If `false`, this hook is basically a no-op.
|
|
15
|
+
*/
|
|
16
|
+
export function useHideableNavbar(hideOnScroll: boolean): {
|
|
17
|
+
/** A ref to the navbar component. Plug this into the actual element. */
|
|
13
18
|
readonly navbarRef: (node: HTMLElement | null) => void;
|
|
19
|
+
/** If `false`, the navbar component should not be rendered. */
|
|
14
20
|
readonly isNavbarVisible: boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default function useHideableNavbar(
|
|
18
|
-
hideOnScroll: boolean,
|
|
19
|
-
): UseHideableNavbarReturns {
|
|
21
|
+
} {
|
|
20
22
|
const [isNavbarVisible, setIsNavbarVisible] = useState(hideOnScroll);
|
|
21
23
|
const isFocusedAnchor = useRef(false);
|
|
22
24
|
const navbarHeight = useRef(0);
|
|
@@ -33,7 +35,8 @@ export default function useHideableNavbar(
|
|
|
33
35
|
|
|
34
36
|
const scrollTop = currentPosition.scrollY;
|
|
35
37
|
|
|
36
|
-
//
|
|
38
|
+
// Needed mostly for handling rubber band scrolling.
|
|
39
|
+
// See https://github.com/facebook/docusaurus/pull/5721
|
|
37
40
|
if (scrollTop < navbarHeight.current) {
|
|
38
41
|
setIsNavbarVisible(true);
|
|
39
42
|
return;
|
|
@@ -70,8 +73,5 @@ export default function useHideableNavbar(
|
|
|
70
73
|
setIsNavbarVisible(true);
|
|
71
74
|
});
|
|
72
75
|
|
|
73
|
-
return {
|
|
74
|
-
navbarRef,
|
|
75
|
-
isNavbarVisible,
|
|
76
|
-
};
|
|
76
|
+
return {navbarRef, isNavbarVisible};
|
|
77
77
|
}
|
|
@@ -11,9 +11,17 @@ import './styles.css';
|
|
|
11
11
|
|
|
12
12
|
export const keyboardFocusedClassName = 'navigation-with-keyboard';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Side-effect that adds the `keyboardFocusedClassName` to the body element when
|
|
16
|
+
* the keyboard has been pressed, or removes it when the mouse is clicked.
|
|
17
|
+
*
|
|
18
|
+
* The presence of this class name signals that the user may be using keyboard
|
|
19
|
+
* for navigation, and the theme **must** add focus outline when this class name
|
|
20
|
+
* is present. (And optionally not if it's absent, for design purposes)
|
|
21
|
+
*
|
|
22
|
+
* Inspired by https://hackernoon.com/removing-that-ugly-focus-ring-and-keeping-it-too-6c8727fefcd2
|
|
23
|
+
*/
|
|
24
|
+
export function useKeyboardNavigation(): void {
|
|
17
25
|
useEffect(() => {
|
|
18
26
|
function handleOutlineStyles(e: MouseEvent | KeyboardEvent) {
|
|
19
27
|
if (e.type === 'keydown' && (e as KeyboardEvent).key === 'Tab') {
|
|
@@ -7,10 +7,13 @@
|
|
|
7
7
|
|
|
8
8
|
import {useEffect} from 'react';
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Side-effect that locks the document body's scroll throughout the lifetime of
|
|
12
|
+
* the containing component. e.g. when the mobile sidebar is expanded.
|
|
13
|
+
*/
|
|
14
|
+
export function useLockBodyScroll(lock: boolean = true): void {
|
|
11
15
|
useEffect(() => {
|
|
12
16
|
document.body.style.overflow = lock ? 'hidden' : 'visible';
|
|
13
|
-
|
|
14
17
|
return () => {
|
|
15
18
|
document.body.style.overflow = 'visible';
|
|
16
19
|
};
|
|
@@ -6,15 +6,19 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import defaultTheme from 'prism-react-renderer/themes/palenight';
|
|
9
|
-
import {useColorMode} from '../
|
|
9
|
+
import {useColorMode} from '../contexts/colorMode';
|
|
10
10
|
import {useThemeConfig} from '../utils/useThemeConfig';
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Returns a color-mode-dependent Prism theme: whatever the user specified in
|
|
14
|
+
* the config. Falls back to `palenight`.
|
|
15
|
+
*/
|
|
16
|
+
export function usePrismTheme(): typeof defaultTheme {
|
|
13
17
|
const {prism} = useThemeConfig();
|
|
14
|
-
const {
|
|
18
|
+
const {colorMode} = useColorMode();
|
|
15
19
|
const lightModeTheme = prism.theme || defaultTheme;
|
|
16
20
|
const darkModeTheme = prism.darkTheme || lightModeTheme;
|
|
17
|
-
const prismTheme =
|
|
21
|
+
const prismTheme = colorMode === 'dark' ? darkModeTheme : lightModeTheme;
|
|
18
22
|
|
|
19
23
|
return prismTheme;
|
|
20
24
|
}
|
|
@@ -11,13 +11,24 @@ import {useCallback, useEffect, useState} from 'react';
|
|
|
11
11
|
|
|
12
12
|
const SEARCH_PARAM_QUERY = 'q';
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
/** Some utility functions around search queries. */
|
|
15
|
+
export function useSearchPage(): {
|
|
16
|
+
/**
|
|
17
|
+
* Works hand-in-hand with `setSearchQuery`; whatever the user has inputted
|
|
18
|
+
* into the search box.
|
|
19
|
+
*/
|
|
15
20
|
searchQuery: string;
|
|
21
|
+
/**
|
|
22
|
+
* Set a new search query. In addition to updating `searchQuery`, this handle
|
|
23
|
+
* also mutates the location and appends the query.
|
|
24
|
+
*/
|
|
16
25
|
setSearchQuery: (newSearchQuery: string) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Given a query, this handle generates the corresponding search page link,
|
|
28
|
+
* with base URL prepended.
|
|
29
|
+
*/
|
|
17
30
|
generateSearchPageLink: (targetSearchQuery: string) => string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export default function useSearchPage(): UseSearchPageReturn {
|
|
31
|
+
} {
|
|
21
32
|
const history = useHistory();
|
|
22
33
|
const {
|
|
23
34
|
siteConfig: {baseUrl},
|
|
@@ -54,7 +65,9 @@ export default function useSearchPage(): UseSearchPageReturn {
|
|
|
54
65
|
const generateSearchPageLink = useCallback(
|
|
55
66
|
(targetSearchQuery: string) =>
|
|
56
67
|
// Refer to https://github.com/facebook/docusaurus/pull/2838
|
|
57
|
-
`${baseUrl}search
|
|
68
|
+
`${baseUrl}search?${SEARCH_PARAM_QUERY}=${encodeURIComponent(
|
|
69
|
+
targetSearchQuery,
|
|
70
|
+
)}`,
|
|
58
71
|
[baseUrl],
|
|
59
72
|
);
|
|
60
73
|
|
|
@@ -6,15 +6,15 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import {useEffect, useRef} from 'react';
|
|
9
|
-
import {useThemeConfig} from '
|
|
9
|
+
import {useThemeConfig} from '../utils/useThemeConfig';
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
(or add them to ThemeClassNames?)
|
|
14
|
-
*/
|
|
11
|
+
// TODO make the hardcoded theme-classic classnames configurable (or add them
|
|
12
|
+
// to ThemeClassNames?)
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
/**
|
|
15
|
+
* If the anchor has no height and is just a "marker" in the DOM; we'll use the
|
|
16
|
+
* parent (normally the link text) rect boundaries instead
|
|
17
|
+
*/
|
|
18
18
|
function getVisibleBoundingClientRect(element: HTMLElement): DOMRect {
|
|
19
19
|
const rect = element.getBoundingClientRect();
|
|
20
20
|
const hasNoHeight = rect.top === rect.bottom;
|
|
@@ -26,7 +26,7 @@ function getVisibleBoundingClientRect(element: HTMLElement): DOMRect {
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Considering we divide viewport into 2 zones of each 50vh, this returns true
|
|
29
|
-
* if an element is in the first zone (
|
|
29
|
+
* if an element is in the first zone (i.e., appear in viewport, near the top)
|
|
30
30
|
*/
|
|
31
31
|
function isInViewportTopHalf(boundingRect: DOMRect) {
|
|
32
32
|
return boundingRect.top > 0 && boundingRect.bottom < window.innerHeight / 2;
|
|
@@ -83,7 +83,7 @@ function getActiveAnchor(
|
|
|
83
83
|
}
|
|
84
84
|
// no anchor under viewport top? (ie we are at the bottom of the page)
|
|
85
85
|
// => highlight the last anchor found
|
|
86
|
-
return anchors[anchors.length - 1];
|
|
86
|
+
return anchors[anchors.length - 1] ?? null;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
function getLinkAnchorValue(link: HTMLAnchorElement): string {
|
|
@@ -116,15 +116,24 @@ function useAnchorTopOffsetRef() {
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
export type TOCHighlightConfig = {
|
|
119
|
+
/** A class name that all TOC links share. */
|
|
119
120
|
linkClassName: string;
|
|
121
|
+
/** The class name applied to the active (highlighted) link. */
|
|
120
122
|
linkActiveClassName: string;
|
|
123
|
+
/**
|
|
124
|
+
* The minimum heading level that the TOC includes. Only headings that are in
|
|
125
|
+
* this range will be eligible as "active heading".
|
|
126
|
+
*/
|
|
121
127
|
minHeadingLevel: number;
|
|
128
|
+
/** @see {@link TOCHighlightConfig.minHeadingLevel} */
|
|
122
129
|
maxHeadingLevel: number;
|
|
123
130
|
};
|
|
124
131
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
132
|
+
/**
|
|
133
|
+
* Side-effect that applies the active class name to the TOC heading that the
|
|
134
|
+
* user is currently viewing. Disabled when `config` is undefined.
|
|
135
|
+
*/
|
|
136
|
+
export function useTOCHighlight(config: TOCHighlightConfig | undefined): void {
|
|
128
137
|
const lastActiveLinkRef = useRef<HTMLAnchorElement | undefined>(undefined);
|
|
129
138
|
|
|
130
139
|
const anchorTopOffsetRef = useAnchorTopOffsetRef();
|
|
@@ -12,12 +12,6 @@ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
|
12
12
|
const windowSizes = {
|
|
13
13
|
desktop: 'desktop',
|
|
14
14
|
mobile: 'mobile',
|
|
15
|
-
|
|
16
|
-
// This "ssr" value is very important to handle hydration FOUC / layout shifts
|
|
17
|
-
// You have to handle server-rendering explicitly on the call-site
|
|
18
|
-
// On the server, you may need to render BOTH the mobile/desktop elements (and
|
|
19
|
-
// hide one of them with mediaquery)
|
|
20
|
-
// We don't return "undefined" on purpose, to make it more explicit
|
|
21
15
|
ssr: 'ssr',
|
|
22
16
|
} as const;
|
|
23
17
|
|
|
@@ -34,14 +28,22 @@ function getWindowSize() {
|
|
|
34
28
|
: windowSizes.mobile;
|
|
35
29
|
}
|
|
36
30
|
|
|
37
|
-
// Simulate the SSR window size in dev, so that potential hydration FOUC/layout
|
|
38
|
-
// shift problems can be seen in dev too!
|
|
39
31
|
const DevSimulateSSR = process.env.NODE_ENV === 'development' && true;
|
|
40
32
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Gets the current window size as an enum value. We don't want it to return the
|
|
35
|
+
* actual width value, so that it only re-renders once a breakpoint is crossed.
|
|
36
|
+
*
|
|
37
|
+
* It may return `"ssr"`, which is very important to handle hydration FOUC or
|
|
38
|
+
* layout shifts. You have to handle it explicitly upfront. On the server, you
|
|
39
|
+
* may need to render BOTH the mobile/desktop elements (and hide one of them
|
|
40
|
+
* with mediaquery). We don't return `undefined` on purpose, to make it more
|
|
41
|
+
* explicit.
|
|
42
|
+
*
|
|
43
|
+
* In development mode, this hook will still return `"ssr"` for one second, to
|
|
44
|
+
* catch potential layout shifts, similar to strict mode calling effects twice.
|
|
45
|
+
*/
|
|
46
|
+
export function useWindowSize(): WindowSize {
|
|
45
47
|
const [windowSize, setWindowSize] = useState<WindowSize>(() => {
|
|
46
48
|
if (DevSimulateSSR) {
|
|
47
49
|
return 'ssr';
|
package/src/index.ts
CHANGED
|
@@ -5,46 +5,46 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
export {
|
|
8
|
+
export {
|
|
9
|
+
useThemeConfig,
|
|
10
|
+
type ThemeConfig,
|
|
11
|
+
type UserThemeConfig,
|
|
12
|
+
type Navbar,
|
|
13
|
+
type NavbarItem,
|
|
14
|
+
type NavbarLogo,
|
|
15
|
+
type MultiColumnFooter,
|
|
16
|
+
type SimpleFooter,
|
|
17
|
+
type Footer,
|
|
18
|
+
type FooterLogo,
|
|
19
|
+
type FooterLinkItem,
|
|
20
|
+
type ColorModeConfig,
|
|
21
|
+
} from './utils/useThemeConfig';
|
|
9
22
|
export {
|
|
10
23
|
DocSidebarItemsExpandedStateProvider,
|
|
11
24
|
useDocSidebarItemsExpandedState,
|
|
12
|
-
} from './
|
|
13
|
-
|
|
14
|
-
export
|
|
15
|
-
ThemeConfig,
|
|
16
|
-
UserThemeConfig,
|
|
17
|
-
Navbar,
|
|
18
|
-
NavbarItem,
|
|
19
|
-
NavbarLogo,
|
|
20
|
-
MultiColumnFooter,
|
|
21
|
-
SimpleFooter,
|
|
22
|
-
Footer,
|
|
23
|
-
FooterLinkItem,
|
|
24
|
-
ColorModeConfig,
|
|
25
|
-
} from './utils/useThemeConfig';
|
|
25
|
+
} from './contexts/docSidebarItemsExpandedState';
|
|
26
|
+
export {DocsVersionProvider, useDocsVersion} from './contexts/docsVersion';
|
|
27
|
+
export {DocsSidebarProvider, useDocsSidebar} from './contexts/docsSidebar';
|
|
26
28
|
|
|
27
29
|
export {createStorageSlot, listStorageKeys} from './utils/storageUtils';
|
|
28
30
|
|
|
29
31
|
export {useAlternatePageUtils} from './utils/useAlternatePageUtils';
|
|
30
32
|
|
|
31
|
-
export {useContextualSearchFilters} from './utils/useContextualSearchFilters';
|
|
32
|
-
|
|
33
33
|
export {
|
|
34
34
|
parseCodeBlockTitle,
|
|
35
35
|
parseLanguage,
|
|
36
36
|
parseLines,
|
|
37
37
|
} from './utils/codeBlockUtils';
|
|
38
38
|
|
|
39
|
-
export {
|
|
39
|
+
export {
|
|
40
|
+
docVersionSearchTag,
|
|
41
|
+
DEFAULT_SEARCH_TAG,
|
|
42
|
+
useContextualSearchFilters,
|
|
43
|
+
} from './utils/searchUtils';
|
|
40
44
|
|
|
41
45
|
export {
|
|
42
46
|
isDocsPluginEnabled,
|
|
43
|
-
DocsVersionProvider,
|
|
44
|
-
useDocsVersion,
|
|
45
47
|
useDocById,
|
|
46
|
-
DocsSidebarProvider,
|
|
47
|
-
useDocsSidebar,
|
|
48
48
|
findSidebarCategory,
|
|
49
49
|
findFirstCategoryLink,
|
|
50
50
|
useCurrentSidebarCategory,
|
|
@@ -52,57 +52,46 @@ export {
|
|
|
52
52
|
useSidebarBreadcrumbs,
|
|
53
53
|
} from './utils/docsUtils';
|
|
54
54
|
|
|
55
|
-
export {isSamePath} from './utils/pathUtils';
|
|
56
|
-
|
|
57
55
|
export {useTitleFormatter} from './utils/generalUtils';
|
|
58
56
|
|
|
59
57
|
export {usePluralForm} from './utils/usePluralForm';
|
|
60
58
|
|
|
61
59
|
export {useLocationChange} from './utils/useLocationChange';
|
|
62
60
|
|
|
63
|
-
export {usePrevious} from './utils/usePrevious';
|
|
64
|
-
|
|
65
61
|
export {useCollapsible, Collapsible} from './components/Collapsible';
|
|
66
|
-
export type {
|
|
67
|
-
UseCollapsibleConfig,
|
|
68
|
-
UseCollapsibleReturns,
|
|
69
|
-
} from './components/Collapsible';
|
|
70
62
|
|
|
71
|
-
export {
|
|
72
|
-
export type {DetailsProps} from './components/Details';
|
|
73
|
-
|
|
74
|
-
export {
|
|
75
|
-
MobileSecondaryMenuProvider,
|
|
76
|
-
MobileSecondaryMenuFiller,
|
|
77
|
-
useMobileSecondaryMenuRenderer,
|
|
78
|
-
} from './utils/mobileSecondaryMenu';
|
|
79
|
-
export type {MobileSecondaryMenuComponent} from './utils/mobileSecondaryMenu';
|
|
63
|
+
export {Details, type DetailsProps} from './components/Details';
|
|
80
64
|
|
|
81
65
|
export {
|
|
82
66
|
useDocsPreferredVersion,
|
|
83
67
|
useDocsPreferredVersionByPluginId,
|
|
84
|
-
|
|
68
|
+
DocsPreferredVersionContextProvider,
|
|
69
|
+
} from './contexts/docsPreferredVersion';
|
|
85
70
|
|
|
86
71
|
export {duplicates, uniq} from './utils/jsUtils';
|
|
87
72
|
|
|
88
|
-
export {DocsPreferredVersionContextProvider} from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
|
|
89
|
-
|
|
90
73
|
export {ThemeClassNames} from './utils/ThemeClassNames';
|
|
91
74
|
|
|
92
75
|
export {
|
|
93
76
|
AnnouncementBarProvider,
|
|
94
77
|
useAnnouncementBar,
|
|
95
|
-
} from './
|
|
78
|
+
} from './contexts/announcementBar';
|
|
96
79
|
|
|
97
80
|
export {useLocalPathname} from './utils/useLocalPathname';
|
|
98
81
|
|
|
99
|
-
export {
|
|
100
|
-
|
|
82
|
+
export {
|
|
83
|
+
translateTagsPageTitle,
|
|
84
|
+
listTagsByLetters,
|
|
85
|
+
type TagLetterEntry,
|
|
86
|
+
type TagsListItem,
|
|
87
|
+
} from './utils/tagsUtils';
|
|
101
88
|
|
|
102
89
|
export {useHistoryPopHandler} from './utils/historyUtils';
|
|
103
90
|
|
|
104
|
-
export {
|
|
105
|
-
|
|
91
|
+
export {
|
|
92
|
+
useTOCHighlight,
|
|
93
|
+
type TOCHighlightConfig,
|
|
94
|
+
} from './hooks/useTOCHighlight';
|
|
106
95
|
|
|
107
96
|
export {
|
|
108
97
|
useFilteredAndTreeifiedTOC,
|
|
@@ -110,6 +99,8 @@ export {
|
|
|
110
99
|
type TOCTreeNode,
|
|
111
100
|
} from './utils/tocUtils';
|
|
112
101
|
|
|
102
|
+
export {isMultiColumnFooterLinks} from './utils/footerUtils';
|
|
103
|
+
|
|
113
104
|
export {
|
|
114
105
|
ScrollControllerProvider,
|
|
115
106
|
useScrollController,
|
|
@@ -120,25 +111,46 @@ export {
|
|
|
120
111
|
export {
|
|
121
112
|
useIsomorphicLayoutEffect,
|
|
122
113
|
useDynamicCallback,
|
|
114
|
+
usePrevious,
|
|
123
115
|
ReactContextError,
|
|
124
116
|
} from './utils/reactUtils';
|
|
125
117
|
|
|
126
118
|
export {isRegexpStringMatch} from './utils/regexpUtils';
|
|
127
119
|
|
|
128
|
-
export {useHomePageRoute} from './utils/routesUtils';
|
|
120
|
+
export {useHomePageRoute, isSamePath} from './utils/routesUtils';
|
|
121
|
+
|
|
122
|
+
export {
|
|
123
|
+
PageMetadata,
|
|
124
|
+
HtmlClassNameProvider,
|
|
125
|
+
PluginHtmlClassNameProvider,
|
|
126
|
+
} from './utils/metadataUtils';
|
|
127
|
+
|
|
128
|
+
export {
|
|
129
|
+
useColorMode,
|
|
130
|
+
ColorModeProvider,
|
|
131
|
+
type ColorMode,
|
|
132
|
+
} from './contexts/colorMode';
|
|
133
|
+
|
|
134
|
+
export {splitNavbarItems, NavbarProvider} from './utils/navbarUtils';
|
|
129
135
|
|
|
130
|
-
export {useColorMode, ColorModeProvider} from './utils/colorModeUtils';
|
|
131
136
|
export {
|
|
132
137
|
useTabGroupChoice,
|
|
133
138
|
TabGroupChoiceProvider,
|
|
134
|
-
} from './
|
|
139
|
+
} from './contexts/tabGroupChoice';
|
|
140
|
+
|
|
141
|
+
export {useNavbarMobileSidebar} from './contexts/navbarMobileSidebar';
|
|
142
|
+
export {
|
|
143
|
+
useNavbarSecondaryMenu,
|
|
144
|
+
NavbarSecondaryMenuFiller,
|
|
145
|
+
type NavbarSecondaryMenuComponent,
|
|
146
|
+
} from './contexts/navbarSecondaryMenu';
|
|
135
147
|
|
|
136
|
-
export {
|
|
148
|
+
export {useHideableNavbar} from './hooks/useHideableNavbar';
|
|
137
149
|
export {
|
|
138
|
-
|
|
150
|
+
useKeyboardNavigation,
|
|
139
151
|
keyboardFocusedClassName,
|
|
140
152
|
} from './hooks/useKeyboardNavigation';
|
|
141
|
-
export {
|
|
142
|
-
export {
|
|
143
|
-
export {
|
|
144
|
-
export {
|
|
153
|
+
export {usePrismTheme} from './hooks/usePrismTheme';
|
|
154
|
+
export {useLockBodyScroll} from './hooks/useLockBodyScroll';
|
|
155
|
+
export {useWindowSize} from './hooks/useWindowSize';
|
|
156
|
+
export {useSearchPage} from './hooks/useSearchPage';
|
|
@@ -5,10 +5,13 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
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
8
|
// Please do not modify the classnames! This is a breaking change, and annoying
|
|
11
9
|
// for users!
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* These class names are used to style page layouts in Docusaurus, meant to be
|
|
13
|
+
* targeted by user-provided custom CSS selectors.
|
|
14
|
+
*/
|
|
12
15
|
export const ThemeClassNames = {
|
|
13
16
|
page: {
|
|
14
17
|
blogListPage: 'blog-list-page',
|
|
@@ -17,8 +20,8 @@ export const ThemeClassNames = {
|
|
|
17
20
|
blogTagPostListPage: 'blog-tags-post-list-page',
|
|
18
21
|
|
|
19
22
|
docsDocPage: 'docs-doc-page',
|
|
20
|
-
docsTagsListPage: 'docs-tags-list-page',
|
|
21
|
-
docsTagDocListPage: 'docs-tags-doc-list-page',
|
|
23
|
+
docsTagsListPage: 'docs-tags-list-page',
|
|
24
|
+
docsTagDocListPage: 'docs-tags-doc-list-page',
|
|
22
25
|
|
|
23
26
|
mdxPage: 'mdx-page',
|
|
24
27
|
},
|
|
@@ -29,8 +32,9 @@ export const ThemeClassNames = {
|
|
|
29
32
|
mdxPages: 'mdx-wrapper',
|
|
30
33
|
},
|
|
31
34
|
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Follows the naming convention "theme-{blog,doc,version,page}?-<suffix>"
|
|
37
|
+
*/
|
|
34
38
|
common: {
|
|
35
39
|
editThisPage: 'theme-edit-this-page',
|
|
36
40
|
lastUpdated: 'theme-last-updated',
|
|
@@ -8,49 +8,26 @@
|
|
|
8
8
|
import rangeParser from 'parse-numeric-range';
|
|
9
9
|
|
|
10
10
|
const codeBlockTitleRegex = /title=(?<quote>["'])(?<title>.*?)\1/;
|
|
11
|
-
const highlightLinesRangeRegex =
|
|
12
|
-
|
|
13
|
-
const commentTypes = ['js', 'jsBlock', 'jsx', 'python', 'html'] as const;
|
|
14
|
-
type CommentType = typeof commentTypes[number];
|
|
15
|
-
|
|
16
|
-
type CommentPattern = {
|
|
17
|
-
start: string;
|
|
18
|
-
end: string;
|
|
19
|
-
};
|
|
11
|
+
const highlightLinesRangeRegex = /\{(?<range>[\d,-]+)\}/;
|
|
20
12
|
|
|
21
13
|
// Supported types of highlight comments
|
|
22
|
-
const commentPatterns
|
|
23
|
-
js: {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
start: '\\/\\*',
|
|
29
|
-
end: '\\*\\/',
|
|
30
|
-
},
|
|
31
|
-
jsx: {
|
|
32
|
-
start: '\\{\\s*\\/\\*',
|
|
33
|
-
end: '\\*\\/\\s*\\}',
|
|
34
|
-
},
|
|
35
|
-
python: {
|
|
36
|
-
start: '#',
|
|
37
|
-
end: '',
|
|
38
|
-
},
|
|
39
|
-
html: {
|
|
40
|
-
start: '<!--',
|
|
41
|
-
end: '-->',
|
|
42
|
-
},
|
|
14
|
+
const commentPatterns = {
|
|
15
|
+
js: {start: '\\/\\/', end: ''},
|
|
16
|
+
jsBlock: {start: '\\/\\*', end: '\\*\\/'},
|
|
17
|
+
jsx: {start: '\\{\\s*\\/\\*', end: '\\*\\/\\s*\\}'},
|
|
18
|
+
python: {start: '#', end: ''},
|
|
19
|
+
html: {start: '<!--', end: '-->'},
|
|
43
20
|
};
|
|
44
21
|
|
|
22
|
+
type CommentType = keyof typeof commentPatterns;
|
|
23
|
+
|
|
45
24
|
const magicCommentDirectives = [
|
|
46
25
|
'highlight-next-line',
|
|
47
26
|
'highlight-start',
|
|
48
27
|
'highlight-end',
|
|
49
28
|
];
|
|
50
29
|
|
|
51
|
-
|
|
52
|
-
languages: readonly CommentType[] = commentTypes,
|
|
53
|
-
) => {
|
|
30
|
+
function getCommentPattern(languages: CommentType[]) {
|
|
54
31
|
// to be more reliable, the opening and closing comment must match
|
|
55
32
|
const commentPattern = languages
|
|
56
33
|
.map((lang) => {
|
|
@@ -60,38 +37,45 @@ const getMagicCommentDirectiveRegex = (
|
|
|
60
37
|
.join('|');
|
|
61
38
|
// white space is allowed, but otherwise it should be on it's own line
|
|
62
39
|
return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
|
|
63
|
-
}
|
|
40
|
+
}
|
|
64
41
|
|
|
65
|
-
|
|
66
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Select comment styles based on language
|
|
44
|
+
*/
|
|
45
|
+
function getAllMagicCommentDirectiveStyles(lang: string) {
|
|
67
46
|
switch (lang) {
|
|
68
47
|
case 'js':
|
|
69
48
|
case 'javascript':
|
|
70
49
|
case 'ts':
|
|
71
50
|
case 'typescript':
|
|
72
|
-
return
|
|
51
|
+
return getCommentPattern(['js', 'jsBlock']);
|
|
73
52
|
|
|
74
53
|
case 'jsx':
|
|
75
54
|
case 'tsx':
|
|
76
|
-
return
|
|
55
|
+
return getCommentPattern(['js', 'jsBlock', 'jsx']);
|
|
77
56
|
|
|
78
57
|
case 'html':
|
|
79
|
-
return
|
|
58
|
+
return getCommentPattern(['js', 'jsBlock', 'html']);
|
|
80
59
|
|
|
81
60
|
case 'python':
|
|
82
61
|
case 'py':
|
|
83
|
-
return
|
|
62
|
+
return getCommentPattern(['python']);
|
|
84
63
|
|
|
85
64
|
default:
|
|
86
65
|
// all comment types
|
|
87
|
-
return
|
|
66
|
+
return getCommentPattern(Object.keys(commentPatterns) as CommentType[]);
|
|
88
67
|
}
|
|
89
|
-
}
|
|
68
|
+
}
|
|
90
69
|
|
|
91
70
|
export function parseCodeBlockTitle(metastring?: string): string {
|
|
92
71
|
return metastring?.match(codeBlockTitleRegex)?.groups!.title ?? '';
|
|
93
72
|
}
|
|
94
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Gets the language name from the class name (set by MDX).
|
|
76
|
+
* e.g. `"language-javascript"` => `"javascript"`.
|
|
77
|
+
* Returns undefined if there is no language class name.
|
|
78
|
+
*/
|
|
95
79
|
export function parseLanguage(className: string): string | undefined {
|
|
96
80
|
const languageClassName = className
|
|
97
81
|
.split(' ')
|
|
@@ -100,22 +84,40 @@ export function parseLanguage(className: string): string | undefined {
|
|
|
100
84
|
}
|
|
101
85
|
|
|
102
86
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
87
|
+
* Parses the code content, strips away any magic comments, and returns the
|
|
88
|
+
* clean content and the highlighted lines marked by the comments or metastring.
|
|
89
|
+
*
|
|
90
|
+
* If the metastring contains highlight range, the `content` will be returned
|
|
91
|
+
* as-is without any parsing.
|
|
92
|
+
*
|
|
93
|
+
* @param content The raw code with magic comments. Trailing newline will be
|
|
94
|
+
* trimmed upfront.
|
|
95
|
+
* @param metastring The full metastring, as received from MDX. Highlight range
|
|
96
|
+
* declared here starts at 1.
|
|
97
|
+
* @param language Language of the code block, used to determine which kinds of
|
|
98
|
+
* magic comment styles to enable.
|
|
105
99
|
*/
|
|
106
100
|
export function parseLines(
|
|
107
101
|
content: string,
|
|
108
102
|
metastring?: string,
|
|
109
103
|
language?: string,
|
|
110
104
|
): {
|
|
105
|
+
/**
|
|
106
|
+
* The highlighted lines, 0-indexed. e.g. `[0, 1, 4]` means the 1st, 2nd, and
|
|
107
|
+
* 5th lines are highlighted.
|
|
108
|
+
*/
|
|
111
109
|
highlightLines: number[];
|
|
110
|
+
/**
|
|
111
|
+
* The clean code without any magic comments (only if highlight range isn't
|
|
112
|
+
* present in the metastring).
|
|
113
|
+
*/
|
|
112
114
|
code: string;
|
|
113
115
|
} {
|
|
114
116
|
let code = content.replace(/\n$/, '');
|
|
115
117
|
// Highlighted lines specified in props: don't parse the content
|
|
116
118
|
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
|
117
119
|
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)!
|
|
118
|
-
.groups!.range
|
|
120
|
+
.groups!.range!;
|
|
119
121
|
const highlightLines = rangeParser(highlightLinesRange)
|
|
120
122
|
.filter((n) => n > 0)
|
|
121
123
|
.map((n) => n - 1);
|
|
@@ -124,14 +126,14 @@ export function parseLines(
|
|
|
124
126
|
if (language === undefined) {
|
|
125
127
|
return {highlightLines: [], code};
|
|
126
128
|
}
|
|
127
|
-
const directiveRegex =
|
|
129
|
+
const directiveRegex = getAllMagicCommentDirectiveStyles(language);
|
|
128
130
|
// go through line by line
|
|
129
131
|
const lines = code.split('\n');
|
|
130
132
|
let highlightBlockStart: number;
|
|
131
133
|
let highlightRange = '';
|
|
132
134
|
// loop through lines
|
|
133
135
|
for (let lineNumber = 0; lineNumber < lines.length; ) {
|
|
134
|
-
const line = lines[lineNumber]
|
|
136
|
+
const line = lines[lineNumber]!;
|
|
135
137
|
const match = line.match(directiveRegex);
|
|
136
138
|
if (match !== null) {
|
|
137
139
|
const directive = match.slice(1).find((item) => item !== undefined);
|