@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.
- package/copyUntypedFiles.js +20 -0
- package/lib/.tsbuildinfo +1 -1
- package/lib/components/Collapsible/index.d.ts +35 -0
- package/lib/components/Collapsible/index.js +139 -0
- package/lib/components/Details/index.d.ts +12 -0
- package/lib/components/Details/index.js +64 -0
- package/lib/components/Details/styles.module.css +58 -0
- package/lib/index.d.ts +24 -4
- package/lib/index.js +18 -3
- package/lib/utils/ThemeClassNames.d.ts +36 -12
- package/lib/utils/ThemeClassNames.js +36 -3
- package/lib/utils/announcementBarUtils.d.ts +17 -0
- package/lib/utils/announcementBarUtils.js +69 -0
- package/lib/utils/codeBlockUtils.d.ts +10 -0
- package/lib/utils/codeBlockUtils.js +119 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +2 -2
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +2 -2
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +1 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +11 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
- package/lib/utils/docsUtils.d.ts +20 -0
- package/lib/utils/docsUtils.js +106 -0
- package/lib/utils/generalUtils.d.ts +6 -0
- package/lib/utils/generalUtils.js +2 -2
- package/lib/utils/historyUtils.d.ts +11 -0
- package/lib/utils/historyUtils.js +39 -0
- package/lib/utils/jsUtils.d.ts +19 -0
- package/lib/utils/jsUtils.js +25 -0
- package/lib/utils/mobileSecondaryMenu.d.ts +20 -0
- package/lib/utils/mobileSecondaryMenu.js +49 -0
- package/lib/utils/pathUtils.js +1 -3
- package/lib/utils/reactUtils.d.ts +9 -0
- package/lib/utils/reactUtils.js +26 -0
- package/lib/utils/regexpUtils.d.ts +10 -0
- package/lib/utils/regexpUtils.js +16 -0
- package/lib/utils/scrollUtils.d.ts +52 -0
- package/lib/utils/scrollUtils.js +135 -0
- package/lib/utils/storageUtils.d.ts +4 -0
- package/lib/utils/storageUtils.js +29 -3
- package/lib/utils/tagsUtils.d.ts +18 -0
- package/lib/utils/tagsUtils.js +33 -0
- package/lib/utils/tocUtils.d.ts +15 -0
- package/lib/utils/tocUtils.js +34 -0
- package/lib/utils/useContextualSearchFilters.d.ts +11 -0
- package/lib/utils/useContextualSearchFilters.js +36 -0
- package/lib/utils/{useChangeRoute.d.ts → useLocalPathname.d.ts} +1 -1
- package/lib/utils/useLocalPathname.js +16 -0
- package/lib/utils/useLocationChange.d.ts +14 -0
- package/lib/utils/useLocationChange.js +23 -0
- package/lib/utils/usePluralForm.js +1 -3
- package/{src/utils/docsUtils.ts → lib/utils/usePrevious.d.ts} +1 -5
- package/lib/utils/usePrevious.js +15 -0
- package/lib/utils/useTOCHighlight.d.ts +14 -0
- package/lib/utils/useTOCHighlight.js +124 -0
- package/lib/utils/useThemeConfig.d.ts +21 -3
- package/package.json +18 -12
- package/src/components/Collapsible/index.tsx +242 -0
- package/src/components/Details/index.tsx +94 -0
- package/src/components/Details/styles.module.css +58 -0
- package/src/index.ts +73 -3
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +42 -4
- package/src/utils/__tests__/codeBlockUtils.test.ts +2 -2
- package/src/utils/__tests__/docsUtils.test.tsx +331 -0
- package/src/utils/__tests__/jsUtils.test.ts +33 -0
- package/src/utils/__tests__/tagUtils.test.ts +66 -0
- package/src/utils/__tests__/tocUtils.test.ts +197 -0
- package/src/utils/announcementBarUtils.tsx +120 -0
- package/src/utils/codeBlockUtils.ts +151 -0
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +7 -6
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +2 -3
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +14 -14
- package/src/utils/docsUtils.tsx +185 -0
- package/src/utils/generalUtils.ts +3 -2
- package/src/utils/historyUtils.ts +50 -0
- package/src/utils/jsUtils.ts +33 -0
- package/src/utils/mobileSecondaryMenu.tsx +114 -0
- package/src/utils/pathUtils.ts +2 -3
- package/src/utils/reactUtils.tsx +34 -0
- package/src/utils/regexpUtils.ts +23 -0
- package/src/utils/scrollUtils.tsx +237 -0
- package/src/utils/storageUtils.ts +27 -4
- package/src/utils/tagsUtils.ts +48 -0
- package/src/utils/tocUtils.ts +55 -0
- package/src/utils/useAlternatePageUtils.ts +9 -1
- package/src/utils/useContextualSearchFilters.ts +50 -0
- package/src/utils/useLocalPathname.ts +20 -0
- package/src/utils/useLocationChange.ts +35 -0
- package/src/utils/usePluralForm.ts +5 -4
- package/src/utils/usePrevious.ts +19 -0
- package/src/utils/useTOCHighlight.ts +179 -0
- package/src/utils/useThemeConfig.ts +20 -3
- package/lib/utils/useChangeRoute.js +0 -18
- package/src/utils/useChangeRoute.ts +0 -21
|
@@ -0,0 +1,33 @@
|
|
|
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 { translate } from '@docusaurus/Translate';
|
|
8
|
+
export const translateTagsPageTitle = () => translate({
|
|
9
|
+
id: 'theme.tags.tagsPageTitle',
|
|
10
|
+
message: 'Tags',
|
|
11
|
+
description: 'The title of the tag list page',
|
|
12
|
+
});
|
|
13
|
+
function getTagLetter(tag) {
|
|
14
|
+
return tag[0].toUpperCase();
|
|
15
|
+
}
|
|
16
|
+
export function listTagsByLetters(tags) {
|
|
17
|
+
// Group by letters
|
|
18
|
+
const groups = {};
|
|
19
|
+
Object.values(tags).forEach((tag) => {
|
|
20
|
+
var _a;
|
|
21
|
+
const letter = getTagLetter(tag.name);
|
|
22
|
+
groups[letter] = (_a = groups[letter]) !== null && _a !== void 0 ? _a : [];
|
|
23
|
+
groups[letter].push(tag);
|
|
24
|
+
});
|
|
25
|
+
return (Object.entries(groups)
|
|
26
|
+
// Sort letters
|
|
27
|
+
.sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
|
|
28
|
+
.map(([letter, letterTags]) => {
|
|
29
|
+
// Sort tags inside a letter
|
|
30
|
+
const sortedTags = letterTags.sort((tag1, tag2) => tag1.name.localeCompare(tag2.name));
|
|
31
|
+
return { letter, tags: sortedTags };
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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 { TOCItem } from '@docusaurus/types';
|
|
8
|
+
declare type FilterTOCParam = {
|
|
9
|
+
toc: readonly TOCItem[];
|
|
10
|
+
minHeadingLevel: number;
|
|
11
|
+
maxHeadingLevel: number;
|
|
12
|
+
};
|
|
13
|
+
export declare function filterTOC({ toc, minHeadingLevel, maxHeadingLevel, }: FilterTOCParam): TOCItem[];
|
|
14
|
+
export declare function useTOCFilter({ toc, minHeadingLevel, maxHeadingLevel, }: FilterTOCParam): readonly TOCItem[];
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
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 { useMemo } from 'react';
|
|
8
|
+
export function filterTOC({ toc, minHeadingLevel, maxHeadingLevel, }) {
|
|
9
|
+
function isValid(item) {
|
|
10
|
+
return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
|
|
11
|
+
}
|
|
12
|
+
return toc.flatMap((item) => {
|
|
13
|
+
const filteredChildren = filterTOC({
|
|
14
|
+
toc: item.children,
|
|
15
|
+
minHeadingLevel,
|
|
16
|
+
maxHeadingLevel,
|
|
17
|
+
});
|
|
18
|
+
if (isValid(item)) {
|
|
19
|
+
return [
|
|
20
|
+
{
|
|
21
|
+
...item,
|
|
22
|
+
children: filteredChildren,
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return filteredChildren;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
// Memoize potentially expensive filtering logic
|
|
32
|
+
export function useTOCFilter({ toc, minHeadingLevel, maxHeadingLevel, }) {
|
|
33
|
+
return useMemo(() => filterTOC({ toc, minHeadingLevel, maxHeadingLevel }), [toc, minHeadingLevel, maxHeadingLevel]);
|
|
34
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
export declare type useContextualSearchFiltersReturns = {
|
|
8
|
+
locale: string;
|
|
9
|
+
tags: string[];
|
|
10
|
+
};
|
|
11
|
+
export declare function useContextualSearchFilters(): useContextualSearchFiltersReturns;
|
|
@@ -0,0 +1,36 @@
|
|
|
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 { useAllDocsData, useActivePluginAndVersion } from '@theme/hooks/useDocs';
|
|
8
|
+
import { useDocsPreferredVersionByPluginId } from './docsPreferredVersion/useDocsPreferredVersion';
|
|
9
|
+
import { docVersionSearchTag, DEFAULT_SEARCH_TAG } from './searchUtils';
|
|
10
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
11
|
+
// We may want to support multiple search engines, don't couple that to Algolia/DocSearch
|
|
12
|
+
// Maybe users will want to use its own search engine solution
|
|
13
|
+
export function useContextualSearchFilters() {
|
|
14
|
+
const { i18n } = useDocusaurusContext();
|
|
15
|
+
const allDocsData = useAllDocsData();
|
|
16
|
+
const activePluginAndVersion = useActivePluginAndVersion();
|
|
17
|
+
const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
|
|
18
|
+
function getDocPluginTags(pluginId) {
|
|
19
|
+
var _a, _b;
|
|
20
|
+
const activeVersion = ((_a = activePluginAndVersion === null || activePluginAndVersion === void 0 ? void 0 : activePluginAndVersion.activePlugin) === null || _a === void 0 ? void 0 : _a.pluginId) === pluginId
|
|
21
|
+
? activePluginAndVersion.activeVersion
|
|
22
|
+
: undefined;
|
|
23
|
+
const preferredVersion = docsPreferredVersionByPluginId[pluginId];
|
|
24
|
+
const latestVersion = allDocsData[pluginId].versions.find((v) => v.isLast);
|
|
25
|
+
const version = (_b = activeVersion !== null && activeVersion !== void 0 ? activeVersion : preferredVersion) !== null && _b !== void 0 ? _b : latestVersion;
|
|
26
|
+
return docVersionSearchTag(pluginId, version.name);
|
|
27
|
+
}
|
|
28
|
+
const tags = [
|
|
29
|
+
DEFAULT_SEARCH_TAG,
|
|
30
|
+
...Object.keys(allDocsData).map(getDocPluginTags),
|
|
31
|
+
];
|
|
32
|
+
return {
|
|
33
|
+
locale: i18n.currentLocale,
|
|
34
|
+
tags,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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 useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
8
|
+
import { useLocation } from '@docusaurus/router';
|
|
9
|
+
// Get the pathname of current route, without the optional site baseUrl
|
|
10
|
+
// - /docs/myDoc => /docs/myDoc
|
|
11
|
+
// - /baseUrl/docs/myDoc => /docs/myDoc
|
|
12
|
+
export function useLocalPathname() {
|
|
13
|
+
const { siteConfig: { baseUrl }, } = useDocusaurusContext();
|
|
14
|
+
const { pathname } = useLocation();
|
|
15
|
+
return pathname.replace(baseUrl, '/');
|
|
16
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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 { Location } from '@docusaurus/history';
|
|
8
|
+
declare type LocationChangeEvent = {
|
|
9
|
+
location: Location;
|
|
10
|
+
previousLocation: Location | undefined;
|
|
11
|
+
};
|
|
12
|
+
declare type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
|
|
13
|
+
export declare function useLocationChange(onLocationChange: OnLocationChange): void;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
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 { useEffect } from 'react';
|
|
8
|
+
import { useLocation } from '@docusaurus/router';
|
|
9
|
+
import { usePrevious } from './usePrevious';
|
|
10
|
+
import { useDynamicCallback } from './reactUtils';
|
|
11
|
+
export function useLocationChange(onLocationChange) {
|
|
12
|
+
const location = useLocation();
|
|
13
|
+
const previousLocation = usePrevious(location);
|
|
14
|
+
const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (location !== previousLocation) {
|
|
17
|
+
onLocationChangeDynamic({
|
|
18
|
+
location,
|
|
19
|
+
previousLocation,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}, [onLocationChangeDynamic, location, previousLocation]);
|
|
23
|
+
}
|
|
@@ -87,8 +87,6 @@ function selectPluralMessage(pluralMessages, count, localePluralForms) {
|
|
|
87
87
|
export function usePluralForm() {
|
|
88
88
|
const localePluralForm = useLocalePluralForms();
|
|
89
89
|
return {
|
|
90
|
-
selectMessage: (count, pluralMessages) =>
|
|
91
|
-
return selectPluralMessage(pluralMessages, count, localePluralForm);
|
|
92
|
-
},
|
|
90
|
+
selectMessage: (count, pluralMessages) => selectPluralMessage(pluralMessages, count, localePluralForm),
|
|
93
91
|
};
|
|
94
92
|
}
|
|
@@ -4,8 +4,4 @@
|
|
|
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
|
-
|
|
8
|
-
import {useAllDocsData} from '@theme/hooks/useDocs';
|
|
9
|
-
|
|
10
|
-
// TODO not ideal, see also "useDocs"
|
|
11
|
-
export const isDocsPluginEnabled: boolean = !!useAllDocsData;
|
|
7
|
+
export declare function usePrevious<T>(value: T): T | undefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
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 { useRef } from 'react';
|
|
8
|
+
import { useIsomorphicLayoutEffect } from './reactUtils';
|
|
9
|
+
export function usePrevious(value) {
|
|
10
|
+
const ref = useRef();
|
|
11
|
+
useIsomorphicLayoutEffect(() => {
|
|
12
|
+
ref.current = value;
|
|
13
|
+
});
|
|
14
|
+
return ref.current;
|
|
15
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
export declare type TOCHighlightConfig = {
|
|
8
|
+
linkClassName: string;
|
|
9
|
+
linkActiveClassName: string;
|
|
10
|
+
minHeadingLevel: number;
|
|
11
|
+
maxHeadingLevel: number;
|
|
12
|
+
};
|
|
13
|
+
declare function useTOCHighlight(config: TOCHighlightConfig | undefined): void;
|
|
14
|
+
export default useTOCHighlight;
|
|
@@ -0,0 +1,124 @@
|
|
|
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 { useEffect, useRef } from 'react';
|
|
8
|
+
import { useThemeConfig } from './useThemeConfig';
|
|
9
|
+
/*
|
|
10
|
+
TODO make the hardcoded theme-classic classnames configurable
|
|
11
|
+
(or add them to ThemeClassNames?)
|
|
12
|
+
*/
|
|
13
|
+
// If the anchor has no height and is just a "marker" in the dom; we'll use the parent (normally the link text) rect boundaries instead
|
|
14
|
+
function getVisibleBoundingClientRect(element) {
|
|
15
|
+
const rect = element.getBoundingClientRect();
|
|
16
|
+
const hasNoHeight = rect.top === rect.bottom;
|
|
17
|
+
if (hasNoHeight) {
|
|
18
|
+
return getVisibleBoundingClientRect(element.parentNode);
|
|
19
|
+
}
|
|
20
|
+
return rect;
|
|
21
|
+
}
|
|
22
|
+
// Considering we divide viewport into 2 zones of each 50vh
|
|
23
|
+
// This returns true if an element is in the first zone (ie, appear in viewport, near the top)
|
|
24
|
+
function isInViewportTopHalf(boundingRect) {
|
|
25
|
+
return boundingRect.top > 0 && boundingRect.bottom < window.innerHeight / 2;
|
|
26
|
+
}
|
|
27
|
+
function getAnchors({ minHeadingLevel, maxHeadingLevel, }) {
|
|
28
|
+
const selectors = [];
|
|
29
|
+
for (let i = minHeadingLevel; i <= maxHeadingLevel; i += 1) {
|
|
30
|
+
selectors.push(`h${i}.anchor`);
|
|
31
|
+
}
|
|
32
|
+
return Array.from(document.querySelectorAll(selectors.join()));
|
|
33
|
+
}
|
|
34
|
+
function getActiveAnchor(anchors, { anchorTopOffset, }) {
|
|
35
|
+
var _a;
|
|
36
|
+
// Naming is hard
|
|
37
|
+
// The "nextVisibleAnchor" is the first anchor that appear under the viewport top boundary
|
|
38
|
+
// Note: it does not mean this anchor is visible yet, but if user continues scrolling down, it will be the first to become visible
|
|
39
|
+
const nextVisibleAnchor = anchors.find((anchor) => {
|
|
40
|
+
const boundingRect = getVisibleBoundingClientRect(anchor);
|
|
41
|
+
return boundingRect.top >= anchorTopOffset;
|
|
42
|
+
});
|
|
43
|
+
if (nextVisibleAnchor) {
|
|
44
|
+
const boundingRect = getVisibleBoundingClientRect(nextVisibleAnchor);
|
|
45
|
+
// If anchor is in the top half of the viewport: it is the one we consider "active"
|
|
46
|
+
// (unless it's too close to the top and and soon to be scrolled outside viewport)
|
|
47
|
+
if (isInViewportTopHalf(boundingRect)) {
|
|
48
|
+
return nextVisibleAnchor;
|
|
49
|
+
}
|
|
50
|
+
// If anchor is in the bottom half of the viewport, or under the viewport, we consider the active anchor is the previous one
|
|
51
|
+
// This is because the main text appearing in the user screen mostly belong to the previous anchor
|
|
52
|
+
else {
|
|
53
|
+
// Returns null for the first anchor, see https://github.com/facebook/docusaurus/issues/5318
|
|
54
|
+
return (_a = anchors[anchors.indexOf(nextVisibleAnchor) - 1]) !== null && _a !== void 0 ? _a : null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// no anchor under viewport top? (ie we are at the bottom of the page)
|
|
58
|
+
// => highlight the last anchor found
|
|
59
|
+
else {
|
|
60
|
+
return anchors[anchors.length - 1];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function getLinkAnchorValue(link) {
|
|
64
|
+
return decodeURIComponent(link.href.substring(link.href.indexOf('#') + 1));
|
|
65
|
+
}
|
|
66
|
+
function getLinks(linkClassName) {
|
|
67
|
+
return Array.from(document.getElementsByClassName(linkClassName));
|
|
68
|
+
}
|
|
69
|
+
function getNavbarHeight() {
|
|
70
|
+
// Not ideal to obtain actual height this way
|
|
71
|
+
// Using TS ! (not ?) because otherwise a bad selector would be un-noticed
|
|
72
|
+
return document.querySelector('.navbar').clientHeight;
|
|
73
|
+
}
|
|
74
|
+
function useAnchorTopOffsetRef() {
|
|
75
|
+
const anchorTopOffsetRef = useRef(0);
|
|
76
|
+
const { navbar: { hideOnScroll }, } = useThemeConfig();
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
anchorTopOffsetRef.current = hideOnScroll ? 0 : getNavbarHeight();
|
|
79
|
+
}, [hideOnScroll]);
|
|
80
|
+
return anchorTopOffsetRef;
|
|
81
|
+
}
|
|
82
|
+
function useTOCHighlight(config) {
|
|
83
|
+
const lastActiveLinkRef = useRef(undefined);
|
|
84
|
+
const anchorTopOffsetRef = useAnchorTopOffsetRef();
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if (!config) {
|
|
87
|
+
// no-op, highlighting is disabled
|
|
88
|
+
return () => { };
|
|
89
|
+
}
|
|
90
|
+
const { linkClassName, linkActiveClassName, minHeadingLevel, maxHeadingLevel, } = config;
|
|
91
|
+
function updateLinkActiveClass(link, active) {
|
|
92
|
+
var _a;
|
|
93
|
+
if (active) {
|
|
94
|
+
if (lastActiveLinkRef.current && lastActiveLinkRef.current !== link) {
|
|
95
|
+
(_a = lastActiveLinkRef.current) === null || _a === void 0 ? void 0 : _a.classList.remove(linkActiveClassName);
|
|
96
|
+
}
|
|
97
|
+
link.classList.add(linkActiveClassName);
|
|
98
|
+
lastActiveLinkRef.current = link;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
link.classList.remove(linkActiveClassName);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function updateActiveLink() {
|
|
105
|
+
const links = getLinks(linkClassName);
|
|
106
|
+
const anchors = getAnchors({ minHeadingLevel, maxHeadingLevel });
|
|
107
|
+
const activeAnchor = getActiveAnchor(anchors, {
|
|
108
|
+
anchorTopOffset: anchorTopOffsetRef.current,
|
|
109
|
+
});
|
|
110
|
+
const activeLink = links.find((link) => activeAnchor && activeAnchor.id === getLinkAnchorValue(link));
|
|
111
|
+
links.forEach((link) => {
|
|
112
|
+
updateLinkActiveClass(link, link === activeLink);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
document.addEventListener('scroll', updateActiveLink);
|
|
116
|
+
document.addEventListener('resize', updateActiveLink);
|
|
117
|
+
updateActiveLink();
|
|
118
|
+
return () => {
|
|
119
|
+
document.removeEventListener('scroll', updateActiveLink);
|
|
120
|
+
document.removeEventListener('resize', updateActiveLink);
|
|
121
|
+
};
|
|
122
|
+
}, [config, anchorTopOffsetRef]);
|
|
123
|
+
}
|
|
124
|
+
export default useTOCHighlight;
|
|
@@ -1,15 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
1
7
|
import { PrismTheme } from 'prism-react-renderer';
|
|
2
8
|
import { CSSProperties } from 'react';
|
|
9
|
+
import { DeepPartial } from 'utility-types';
|
|
3
10
|
export declare type DocsVersionPersistence = 'localStorage' | 'none';
|
|
4
11
|
export declare type NavbarItem = {
|
|
5
12
|
type?: string | undefined;
|
|
6
13
|
items?: NavbarItem[];
|
|
7
14
|
label?: string;
|
|
8
15
|
position?: 'left' | 'right';
|
|
9
|
-
}
|
|
16
|
+
} & Record<string, unknown>;
|
|
10
17
|
export declare type NavbarLogo = {
|
|
11
18
|
src: string;
|
|
12
19
|
srcDark?: string;
|
|
20
|
+
width?: string | number;
|
|
21
|
+
height?: string | number;
|
|
13
22
|
href?: string;
|
|
14
23
|
target?: string;
|
|
15
24
|
alt?: string;
|
|
@@ -62,11 +71,17 @@ export declare type Footer = {
|
|
|
62
71
|
alt?: string;
|
|
63
72
|
src?: string;
|
|
64
73
|
srcDark?: string;
|
|
74
|
+
width?: string | number;
|
|
75
|
+
height?: string | number;
|
|
65
76
|
href?: string;
|
|
66
77
|
};
|
|
67
78
|
copyright?: string;
|
|
68
79
|
links: FooterLinks[];
|
|
69
80
|
};
|
|
81
|
+
export declare type TableOfContents = {
|
|
82
|
+
minHeadingLevel: number;
|
|
83
|
+
maxHeadingLevel: number;
|
|
84
|
+
};
|
|
70
85
|
export declare type ThemeConfig = {
|
|
71
86
|
docs: {
|
|
72
87
|
versionPersistence: DocsVersionPersistence;
|
|
@@ -77,7 +92,10 @@ export declare type ThemeConfig = {
|
|
|
77
92
|
prism: PrismConfig;
|
|
78
93
|
footer?: Footer;
|
|
79
94
|
hideableSidebar: boolean;
|
|
80
|
-
image
|
|
81
|
-
|
|
95
|
+
image?: string;
|
|
96
|
+
metadata: Array<Record<string, string>>;
|
|
97
|
+
sidebarCollapsible: boolean;
|
|
98
|
+
tableOfContents: TableOfContents;
|
|
82
99
|
};
|
|
100
|
+
export declare type UserThemeConfig = DeepPartial<ThemeConfig>;
|
|
83
101
|
export declare function useThemeConfig(): ThemeConfig;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/theme-common",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.10",
|
|
4
4
|
"description": "Common code for Docusaurus themes.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "tsc",
|
|
9
|
-
"watch": "tsc --watch"
|
|
8
|
+
"build": "node copyUntypedFiles.js && tsc",
|
|
9
|
+
"watch": "node copyUntypedFiles.js && tsc --watch"
|
|
10
10
|
},
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|
|
@@ -18,23 +18,29 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/
|
|
22
|
-
"@docusaurus/plugin-content-
|
|
23
|
-
"@docusaurus/plugin-content-
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
21
|
+
"@docusaurus/plugin-content-blog": "2.0.0-beta.10",
|
|
22
|
+
"@docusaurus/plugin-content-docs": "2.0.0-beta.10",
|
|
23
|
+
"@docusaurus/plugin-content-pages": "2.0.0-beta.10",
|
|
24
|
+
"clsx": "^1.1.1",
|
|
25
|
+
"fs-extra": "^10.0.0",
|
|
26
|
+
"parse-numeric-range": "^1.3.0",
|
|
27
|
+
"tslib": "^2.3.1",
|
|
28
|
+
"utility-types": "^3.10.0"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
|
29
|
-
"@docusaurus/module-type-aliases": "2.0.0-beta.
|
|
31
|
+
"@docusaurus/module-type-aliases": "2.0.0-beta.10",
|
|
32
|
+
"@docusaurus/types": "2.0.0-beta.10",
|
|
33
|
+
"@testing-library/react-hooks": "^7.0.2",
|
|
34
|
+
"lodash": "^4.17.20"
|
|
30
35
|
},
|
|
31
36
|
"peerDependencies": {
|
|
37
|
+
"@docusaurus/core": "2.0.0-beta.9",
|
|
32
38
|
"prism-react-renderer": "^1.2.1",
|
|
33
39
|
"react": "^16.8.4 || ^17.0.0",
|
|
34
40
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
35
41
|
},
|
|
36
42
|
"engines": {
|
|
37
|
-
"node": ">=
|
|
43
|
+
"node": ">=14"
|
|
38
44
|
},
|
|
39
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "6f4869af5721435d6995e63e2f24ac41646e34ea"
|
|
40
46
|
}
|