@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,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import React, {
|
|
9
|
+
useState,
|
|
10
|
+
useEffect,
|
|
11
|
+
useCallback,
|
|
12
|
+
useMemo,
|
|
13
|
+
ReactNode,
|
|
14
|
+
useContext,
|
|
15
|
+
createContext,
|
|
16
|
+
} from 'react';
|
|
17
|
+
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
18
|
+
import {createStorageSlot} from './storageUtils';
|
|
19
|
+
import {useThemeConfig} from './useThemeConfig';
|
|
20
|
+
|
|
21
|
+
export const AnnouncementBarDismissStorageKey =
|
|
22
|
+
'docusaurus.announcement.dismiss';
|
|
23
|
+
const AnnouncementBarIdStorageKey = 'docusaurus.announcement.id';
|
|
24
|
+
|
|
25
|
+
const AnnouncementBarDismissStorage = createStorageSlot(
|
|
26
|
+
AnnouncementBarDismissStorageKey,
|
|
27
|
+
);
|
|
28
|
+
const IdStorage = createStorageSlot(AnnouncementBarIdStorageKey);
|
|
29
|
+
|
|
30
|
+
const isDismissedInStorage = () =>
|
|
31
|
+
AnnouncementBarDismissStorage.get() === 'true';
|
|
32
|
+
const setDismissedInStorage = (bool: boolean) =>
|
|
33
|
+
AnnouncementBarDismissStorage.set(String(bool));
|
|
34
|
+
|
|
35
|
+
type AnnouncementBarAPI = {
|
|
36
|
+
readonly isActive: boolean;
|
|
37
|
+
readonly close: () => void;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
|
|
41
|
+
const {announcementBar} = useThemeConfig();
|
|
42
|
+
const isBrowser = useIsBrowser();
|
|
43
|
+
|
|
44
|
+
const [isClosed, setClosed] = useState(() =>
|
|
45
|
+
isBrowser
|
|
46
|
+
? // On client navigation: init with localstorage value
|
|
47
|
+
isDismissedInStorage()
|
|
48
|
+
: // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
|
|
49
|
+
false,
|
|
50
|
+
);
|
|
51
|
+
// Update state after hydration
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
setClosed(isDismissedInStorage());
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
const handleClose = useCallback(() => {
|
|
57
|
+
setDismissedInStorage(true);
|
|
58
|
+
setClosed(true);
|
|
59
|
+
}, []);
|
|
60
|
+
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (!announcementBar) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const {id} = announcementBar;
|
|
66
|
+
|
|
67
|
+
let viewedId = IdStorage.get();
|
|
68
|
+
|
|
69
|
+
// retrocompatibility due to spelling mistake of default id
|
|
70
|
+
// see https://github.com/facebook/docusaurus/issues/3338
|
|
71
|
+
if (viewedId === 'annoucement-bar') {
|
|
72
|
+
viewedId = 'announcement-bar';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const isNewAnnouncement = id !== viewedId;
|
|
76
|
+
|
|
77
|
+
IdStorage.set(id);
|
|
78
|
+
|
|
79
|
+
if (isNewAnnouncement) {
|
|
80
|
+
setDismissedInStorage(false);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (isNewAnnouncement || !isDismissedInStorage()) {
|
|
84
|
+
setClosed(false);
|
|
85
|
+
}
|
|
86
|
+
}, [announcementBar]);
|
|
87
|
+
|
|
88
|
+
return useMemo(
|
|
89
|
+
() => ({
|
|
90
|
+
isActive: !!announcementBar && !isClosed,
|
|
91
|
+
close: handleClose,
|
|
92
|
+
}),
|
|
93
|
+
[announcementBar, isClosed, handleClose],
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
|
|
98
|
+
|
|
99
|
+
export function AnnouncementBarProvider({
|
|
100
|
+
children,
|
|
101
|
+
}: {
|
|
102
|
+
children: ReactNode;
|
|
103
|
+
}): JSX.Element {
|
|
104
|
+
const value = useAnnouncementBarContextValue();
|
|
105
|
+
return (
|
|
106
|
+
<AnnouncementBarContext.Provider value={value}>
|
|
107
|
+
{children}
|
|
108
|
+
</AnnouncementBarContext.Provider>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export const useAnnouncementBar = (): AnnouncementBarAPI => {
|
|
113
|
+
const api = useContext(AnnouncementBarContext);
|
|
114
|
+
if (!api) {
|
|
115
|
+
throw new Error(
|
|
116
|
+
'useAnnouncementBar(): AnnouncementBar not found in React context: make sure to use the AnnouncementBarProvider on top of the tree',
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return api;
|
|
120
|
+
};
|
|
@@ -5,8 +5,159 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import rangeParser from 'parse-numeric-range';
|
|
9
|
+
import type {Language} from 'prism-react-renderer';
|
|
10
|
+
|
|
8
11
|
const codeBlockTitleRegex = /title=(["'])(.*?)\1/;
|
|
12
|
+
const highlightLinesRangeRegex = /{([\d,-]+)}/;
|
|
13
|
+
|
|
14
|
+
const commentTypes = ['js', 'jsBlock', 'jsx', 'python', 'html'] as const;
|
|
15
|
+
type CommentType = typeof commentTypes[number];
|
|
16
|
+
|
|
17
|
+
type CommentPattern = {
|
|
18
|
+
start: string;
|
|
19
|
+
end: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Supported types of highlight comments
|
|
23
|
+
const commentPatterns: Record<CommentType, CommentPattern> = {
|
|
24
|
+
js: {
|
|
25
|
+
start: '\\/\\/',
|
|
26
|
+
end: '',
|
|
27
|
+
},
|
|
28
|
+
jsBlock: {
|
|
29
|
+
start: '\\/\\*',
|
|
30
|
+
end: '\\*\\/',
|
|
31
|
+
},
|
|
32
|
+
jsx: {
|
|
33
|
+
start: '\\{\\s*\\/\\*',
|
|
34
|
+
end: '\\*\\/\\s*\\}',
|
|
35
|
+
},
|
|
36
|
+
python: {
|
|
37
|
+
start: '#',
|
|
38
|
+
end: '',
|
|
39
|
+
},
|
|
40
|
+
html: {
|
|
41
|
+
start: '<!--',
|
|
42
|
+
end: '-->',
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const magicCommentDirectives = [
|
|
47
|
+
'highlight-next-line',
|
|
48
|
+
'highlight-start',
|
|
49
|
+
'highlight-end',
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
const getMagicCommentDirectiveRegex = (
|
|
53
|
+
languages: readonly CommentType[] = commentTypes,
|
|
54
|
+
) => {
|
|
55
|
+
// to be more reliable, the opening and closing comment must match
|
|
56
|
+
const commentPattern = languages
|
|
57
|
+
.map((lang) => {
|
|
58
|
+
const {start, end} = commentPatterns[lang];
|
|
59
|
+
return `(?:${start}\\s*(${magicCommentDirectives.join('|')})\\s*${end})`;
|
|
60
|
+
})
|
|
61
|
+
.join('|');
|
|
62
|
+
// white space is allowed, but otherwise it should be on it's own line
|
|
63
|
+
return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// select comment styles based on language
|
|
67
|
+
const magicCommentDirectiveRegex = (lang: string) => {
|
|
68
|
+
switch (lang) {
|
|
69
|
+
case 'js':
|
|
70
|
+
case 'javascript':
|
|
71
|
+
case 'ts':
|
|
72
|
+
case 'typescript':
|
|
73
|
+
return getMagicCommentDirectiveRegex(['js', 'jsBlock']);
|
|
74
|
+
|
|
75
|
+
case 'jsx':
|
|
76
|
+
case 'tsx':
|
|
77
|
+
return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'jsx']);
|
|
78
|
+
|
|
79
|
+
case 'html':
|
|
80
|
+
return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'html']);
|
|
81
|
+
|
|
82
|
+
case 'python':
|
|
83
|
+
case 'py':
|
|
84
|
+
return getMagicCommentDirectiveRegex(['python']);
|
|
85
|
+
|
|
86
|
+
default:
|
|
87
|
+
// all comment types
|
|
88
|
+
return getMagicCommentDirectiveRegex();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
9
91
|
|
|
10
92
|
export function parseCodeBlockTitle(metastring?: string): string {
|
|
11
93
|
return metastring?.match(codeBlockTitleRegex)?.[2] ?? '';
|
|
12
94
|
}
|
|
95
|
+
|
|
96
|
+
export function parseLanguage(className?: string): Language | undefined {
|
|
97
|
+
const languageClassName = className
|
|
98
|
+
?.split(' ')
|
|
99
|
+
.find((str) => str.startsWith('language-'));
|
|
100
|
+
return languageClassName?.replace(/language-/, '') as Language | undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param metastring The highlight range declared here starts at 1
|
|
105
|
+
* @returns Note: all line numbers start at 0, not 1
|
|
106
|
+
*/
|
|
107
|
+
export function parseLines(
|
|
108
|
+
content: string,
|
|
109
|
+
metastring?: string,
|
|
110
|
+
language?: Language,
|
|
111
|
+
): {
|
|
112
|
+
highlightLines: number[];
|
|
113
|
+
code: string;
|
|
114
|
+
} {
|
|
115
|
+
let code = content.replace(/\n$/, '');
|
|
116
|
+
// Highlighted lines specified in props: don't parse the content
|
|
117
|
+
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
|
118
|
+
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)![1];
|
|
119
|
+
const highlightLines = rangeParser(highlightLinesRange)
|
|
120
|
+
.filter((n) => n > 0)
|
|
121
|
+
.map((n) => n - 1);
|
|
122
|
+
return {highlightLines, code};
|
|
123
|
+
}
|
|
124
|
+
if (language === undefined) {
|
|
125
|
+
return {highlightLines: [], code};
|
|
126
|
+
}
|
|
127
|
+
const directiveRegex = magicCommentDirectiveRegex(language);
|
|
128
|
+
// go through line by line
|
|
129
|
+
const lines = code.split('\n');
|
|
130
|
+
let highlightBlockStart: number;
|
|
131
|
+
let highlightRange = '';
|
|
132
|
+
// loop through lines
|
|
133
|
+
for (let lineNumber = 0; lineNumber < lines.length; ) {
|
|
134
|
+
const line = lines[lineNumber];
|
|
135
|
+
const match = line.match(directiveRegex);
|
|
136
|
+
if (match !== null) {
|
|
137
|
+
const directive = match.slice(1).find((item) => item !== undefined);
|
|
138
|
+
switch (directive) {
|
|
139
|
+
case 'highlight-next-line':
|
|
140
|
+
highlightRange += `${lineNumber},`;
|
|
141
|
+
break;
|
|
142
|
+
|
|
143
|
+
case 'highlight-start':
|
|
144
|
+
highlightBlockStart = lineNumber;
|
|
145
|
+
break;
|
|
146
|
+
|
|
147
|
+
case 'highlight-end':
|
|
148
|
+
highlightRange += `${highlightBlockStart!}-${lineNumber - 1},`;
|
|
149
|
+
break;
|
|
150
|
+
|
|
151
|
+
default:
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
lines.splice(lineNumber, 1);
|
|
155
|
+
} else {
|
|
156
|
+
// lines without directives are unchanged
|
|
157
|
+
lineNumber += 1;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const highlightLines = rangeParser(highlightRange);
|
|
161
|
+
code = lines.join('\n');
|
|
162
|
+
return {highlightLines, code};
|
|
163
|
+
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
|
|
7
8
|
import React, {
|
|
8
9
|
createContext,
|
|
9
10
|
ReactNode,
|
|
@@ -15,7 +16,7 @@ import React, {
|
|
|
15
16
|
import {useThemeConfig, DocsVersionPersistence} from '../useThemeConfig';
|
|
16
17
|
import {isDocsPluginEnabled} from '../docsUtils';
|
|
17
18
|
|
|
18
|
-
import {useAllDocsData} from '@theme/hooks/useDocs';
|
|
19
|
+
import {useAllDocsData, GlobalPluginData} from '@theme/hooks/useDocs';
|
|
19
20
|
|
|
20
21
|
import DocsPreferredVersionStorage from './DocsPreferredVersionStorage';
|
|
21
22
|
|
|
@@ -54,7 +55,7 @@ function readStorageState({
|
|
|
54
55
|
}: {
|
|
55
56
|
pluginIds: string[];
|
|
56
57
|
versionPersistence: DocsVersionPersistence;
|
|
57
|
-
allDocsData:
|
|
58
|
+
allDocsData: Record<string, GlobalPluginData>;
|
|
58
59
|
}): DocsPreferredVersionState {
|
|
59
60
|
// The storage value we read might be stale,
|
|
60
61
|
// and belong to a version that does not exist in the site anymore
|
|
@@ -68,7 +69,7 @@ function readStorageState({
|
|
|
68
69
|
);
|
|
69
70
|
const pluginData = allDocsData[pluginId];
|
|
70
71
|
const versionExists = pluginData.versions.some(
|
|
71
|
-
(version
|
|
72
|
+
(version) => version.name === preferredVersionNameUnsafe,
|
|
72
73
|
);
|
|
73
74
|
if (versionExists) {
|
|
74
75
|
return {preferredVersionName: preferredVersionNameUnsafe};
|
|
@@ -120,7 +121,7 @@ function useContextValue() {
|
|
|
120
121
|
return {
|
|
121
122
|
savePreferredVersion,
|
|
122
123
|
};
|
|
123
|
-
}, [
|
|
124
|
+
}, [versionPersistence]);
|
|
124
125
|
|
|
125
126
|
return [state, api] as const;
|
|
126
127
|
}
|
|
@@ -132,7 +133,7 @@ const Context = createContext<DocsPreferredVersionContextValue | null>(null);
|
|
|
132
133
|
export function DocsPreferredVersionContextProvider({
|
|
133
134
|
children,
|
|
134
135
|
}: {
|
|
135
|
-
children:
|
|
136
|
+
children: JSX.Element;
|
|
136
137
|
}): JSX.Element {
|
|
137
138
|
if (isDocsPluginEnabled) {
|
|
138
139
|
return (
|
|
@@ -141,7 +142,7 @@ export function DocsPreferredVersionContextProvider({
|
|
|
141
142
|
</DocsPreferredVersionContextProviderUnsafe>
|
|
142
143
|
);
|
|
143
144
|
} else {
|
|
144
|
-
return
|
|
145
|
+
return children;
|
|
145
146
|
}
|
|
146
147
|
}
|
|
147
148
|
|
|
@@ -22,9 +22,8 @@ const DocsPreferredVersionStorage = {
|
|
|
22
22
|
read: (
|
|
23
23
|
pluginId: string,
|
|
24
24
|
persistence: DocsVersionPersistence,
|
|
25
|
-
): string | null =>
|
|
26
|
-
|
|
27
|
-
},
|
|
25
|
+
): string | null =>
|
|
26
|
+
createStorageSlot(storageKey(pluginId), {persistence}).get(),
|
|
28
27
|
|
|
29
28
|
clear: (pluginId: string, persistence: DocsVersionPersistence): void => {
|
|
30
29
|
createStorageSlot(storageKey(pluginId), {persistence}).del();
|
|
@@ -4,40 +4,43 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
|
|
7
8
|
import {useCallback} from 'react';
|
|
8
9
|
import {useDocsPreferredVersionContext} from './DocsPreferredVersionProvider';
|
|
9
|
-
import {useAllDocsData, useDocsData} from '@theme/hooks/useDocs';
|
|
10
|
+
import {useAllDocsData, useDocsData, GlobalVersion} from '@theme/hooks/useDocs';
|
|
10
11
|
|
|
11
12
|
import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';
|
|
12
13
|
|
|
13
|
-
// TODO improve typing
|
|
14
|
-
|
|
15
14
|
// Note, the preferredVersion attribute will always be null before mount
|
|
16
15
|
export function useDocsPreferredVersion(
|
|
17
16
|
pluginId: string | undefined = DEFAULT_PLUGIN_ID,
|
|
18
|
-
) {
|
|
17
|
+
): {
|
|
18
|
+
preferredVersion: GlobalVersion | null | undefined;
|
|
19
|
+
savePreferredVersionName: (versionName: string) => void;
|
|
20
|
+
} {
|
|
19
21
|
const docsData = useDocsData(pluginId);
|
|
20
22
|
const [state, api] = useDocsPreferredVersionContext();
|
|
21
23
|
|
|
22
24
|
const {preferredVersionName} = state[pluginId];
|
|
23
25
|
|
|
24
26
|
const preferredVersion = preferredVersionName
|
|
25
|
-
? docsData.versions.find(
|
|
26
|
-
(version: any) => version.name === preferredVersionName,
|
|
27
|
-
)
|
|
27
|
+
? docsData.versions.find((version) => version.name === preferredVersionName)
|
|
28
28
|
: null;
|
|
29
29
|
|
|
30
30
|
const savePreferredVersionName = useCallback(
|
|
31
31
|
(versionName: string) => {
|
|
32
32
|
api.savePreferredVersion(pluginId, versionName);
|
|
33
33
|
},
|
|
34
|
-
[api],
|
|
34
|
+
[api, pluginId],
|
|
35
35
|
);
|
|
36
36
|
|
|
37
37
|
return {preferredVersion, savePreferredVersionName} as const;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
export function useDocsPreferredVersionByPluginId(): Record<
|
|
40
|
+
export function useDocsPreferredVersionByPluginId(): Record<
|
|
41
|
+
string,
|
|
42
|
+
GlobalVersion | null | undefined
|
|
43
|
+
> {
|
|
41
44
|
const allDocsData = useAllDocsData();
|
|
42
45
|
const [state] = useDocsPreferredVersionContext();
|
|
43
46
|
|
|
@@ -47,17 +50,14 @@ export function useDocsPreferredVersionByPluginId(): Record<string, any> {
|
|
|
47
50
|
|
|
48
51
|
return preferredVersionName
|
|
49
52
|
? docsData.versions.find(
|
|
50
|
-
(version
|
|
53
|
+
(version) => version.name === preferredVersionName,
|
|
51
54
|
)
|
|
52
55
|
: null;
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
const pluginIds = Object.keys(allDocsData);
|
|
56
59
|
|
|
57
|
-
const result: Record<
|
|
58
|
-
string,
|
|
59
|
-
any // TODO find a way to type this properly!
|
|
60
|
-
> = {};
|
|
60
|
+
const result: Record<string, GlobalVersion | null | undefined> = {};
|
|
61
61
|
pluginIds.forEach((pluginId) => {
|
|
62
62
|
result[pluginId] = getPluginIdPreferredVersion(pluginId);
|
|
63
63
|
});
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import React, {createContext, ReactNode, useContext} from 'react';
|
|
9
|
+
import {useAllDocsData} from '@theme/hooks/useDocs';
|
|
10
|
+
import {
|
|
11
|
+
PropSidebar,
|
|
12
|
+
PropSidebarItem,
|
|
13
|
+
PropSidebarItemCategory,
|
|
14
|
+
PropVersionDoc,
|
|
15
|
+
PropVersionMetadata,
|
|
16
|
+
} from '@docusaurus/plugin-content-docs';
|
|
17
|
+
import {isSamePath} from './pathUtils';
|
|
18
|
+
import {useLocation} from '@docusaurus/router';
|
|
19
|
+
|
|
20
|
+
// TODO not ideal, see also "useDocs"
|
|
21
|
+
export const isDocsPluginEnabled: boolean = !!useAllDocsData;
|
|
22
|
+
|
|
23
|
+
// Using a Symbol because null is a valid context value (a doc can have no sidebar)
|
|
24
|
+
// Inspired by https://github.com/jamiebuilds/unstated-next/blob/master/src/unstated-next.tsx
|
|
25
|
+
const EmptyContextValue: unique symbol = Symbol('EmptyContext');
|
|
26
|
+
|
|
27
|
+
const DocsVersionContext = createContext<
|
|
28
|
+
PropVersionMetadata | typeof EmptyContextValue
|
|
29
|
+
>(EmptyContextValue);
|
|
30
|
+
|
|
31
|
+
export function DocsVersionProvider({
|
|
32
|
+
children,
|
|
33
|
+
version,
|
|
34
|
+
}: {
|
|
35
|
+
children: ReactNode;
|
|
36
|
+
version: PropVersionMetadata | typeof EmptyContextValue;
|
|
37
|
+
}): JSX.Element {
|
|
38
|
+
return (
|
|
39
|
+
<DocsVersionContext.Provider value={version}>
|
|
40
|
+
{children}
|
|
41
|
+
</DocsVersionContext.Provider>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function useDocsVersion(): PropVersionMetadata {
|
|
46
|
+
const version = useContext(DocsVersionContext);
|
|
47
|
+
if (version === EmptyContextValue) {
|
|
48
|
+
throw new Error('This hook requires usage of <DocsVersionProvider>');
|
|
49
|
+
}
|
|
50
|
+
return version;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function useDocById(id: string): PropVersionDoc;
|
|
54
|
+
export function useDocById(id: string | undefined): PropVersionDoc | undefined;
|
|
55
|
+
export function useDocById(id: string | undefined): PropVersionDoc | undefined {
|
|
56
|
+
const version = useDocsVersion();
|
|
57
|
+
if (!id) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
const doc = version.docs[id];
|
|
61
|
+
if (!doc) {
|
|
62
|
+
throw new Error(`no version doc found by id=${id}`);
|
|
63
|
+
}
|
|
64
|
+
return doc;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const DocsSidebarContext = createContext<
|
|
68
|
+
PropSidebar | null | typeof EmptyContextValue
|
|
69
|
+
>(EmptyContextValue);
|
|
70
|
+
|
|
71
|
+
export function DocsSidebarProvider({
|
|
72
|
+
children,
|
|
73
|
+
sidebar,
|
|
74
|
+
}: {
|
|
75
|
+
children: ReactNode;
|
|
76
|
+
sidebar: PropSidebar | null;
|
|
77
|
+
}): JSX.Element {
|
|
78
|
+
return (
|
|
79
|
+
<DocsSidebarContext.Provider value={sidebar}>
|
|
80
|
+
{children}
|
|
81
|
+
</DocsSidebarContext.Provider>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function useDocsSidebar(): PropSidebar | null {
|
|
86
|
+
const sidebar = useContext(DocsSidebarContext);
|
|
87
|
+
if (sidebar === EmptyContextValue) {
|
|
88
|
+
throw new Error('This hook requires usage of <DocsSidebarProvider>');
|
|
89
|
+
}
|
|
90
|
+
return sidebar;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Use the components props and the sidebar in context
|
|
94
|
+
// to get back the related sidebar category that we want to render
|
|
95
|
+
export function findSidebarCategory(
|
|
96
|
+
sidebar: PropSidebar,
|
|
97
|
+
predicate: (category: PropSidebarItemCategory) => boolean,
|
|
98
|
+
): PropSidebarItemCategory | undefined {
|
|
99
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
100
|
+
for (const item of sidebar) {
|
|
101
|
+
if (item.type === 'category') {
|
|
102
|
+
if (predicate(item)) {
|
|
103
|
+
return item;
|
|
104
|
+
} else {
|
|
105
|
+
const subItem = findSidebarCategory(item.items, predicate);
|
|
106
|
+
if (subItem) {
|
|
107
|
+
return subItem;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// If a category card has no link => link to the first subItem having a link
|
|
116
|
+
export function findFirstCategoryLink(
|
|
117
|
+
item: PropSidebarItemCategory,
|
|
118
|
+
): string | undefined {
|
|
119
|
+
if (item.href) {
|
|
120
|
+
return item.href;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
124
|
+
for (const subItem of item.items) {
|
|
125
|
+
if (subItem.type === 'link') {
|
|
126
|
+
return subItem.href;
|
|
127
|
+
}
|
|
128
|
+
if (subItem.type === 'category') {
|
|
129
|
+
const categoryLink = findFirstCategoryLink(subItem);
|
|
130
|
+
if (categoryLink) {
|
|
131
|
+
return categoryLink;
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`Unexpected category item type for ${JSON.stringify(subItem)}`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function useCurrentSidebarCategory(): PropSidebarItemCategory {
|
|
143
|
+
const {pathname} = useLocation();
|
|
144
|
+
const sidebar = useDocsSidebar();
|
|
145
|
+
if (!sidebar) {
|
|
146
|
+
throw new Error('Unexpected: cant find current sidebar in context');
|
|
147
|
+
}
|
|
148
|
+
const category = findSidebarCategory(sidebar, (item) =>
|
|
149
|
+
isSamePath(item.href, pathname),
|
|
150
|
+
);
|
|
151
|
+
if (!category) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
`Unexpected: sidebar category could not be found for pathname='${pathname}'.
|
|
154
|
+
Hook useCurrentSidebarCategory() should only be used on Category pages`,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
return category;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function containsActiveSidebarItem(
|
|
161
|
+
items: PropSidebarItem[],
|
|
162
|
+
activePath: string,
|
|
163
|
+
): boolean {
|
|
164
|
+
return items.some((subItem) => isActiveSidebarItem(subItem, activePath));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function isActiveSidebarItem(
|
|
168
|
+
item: PropSidebarItem,
|
|
169
|
+
activePath: string,
|
|
170
|
+
): boolean {
|
|
171
|
+
const isActive = (testedPath: string | undefined) =>
|
|
172
|
+
typeof testedPath !== 'undefined' && isSamePath(testedPath, activePath);
|
|
173
|
+
|
|
174
|
+
if (item.type === 'link') {
|
|
175
|
+
return isActive(item.href);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (item.type === 'category') {
|
|
179
|
+
return (
|
|
180
|
+
isActive(item.href) || containsActiveSidebarItem(item.items, activePath)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
|
|
7
8
|
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
8
9
|
|
|
9
10
|
export const useTitleFormatter = (title?: string | undefined): string => {
|
|
10
|
-
const {siteConfig
|
|
11
|
-
const {title: siteTitle, titleDelimiter
|
|
11
|
+
const {siteConfig} = useDocusaurusContext();
|
|
12
|
+
const {title: siteTitle, titleDelimiter} = siteConfig;
|
|
12
13
|
return title && title.trim().length
|
|
13
14
|
? `${title.trim()} ${titleDelimiter} ${siteTitle}`
|
|
14
15
|
: siteTitle;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {useEffect, useRef} from 'react';
|
|
9
|
+
import {useHistory} from '@docusaurus/router';
|
|
10
|
+
import type {Location, Action} from '@docusaurus/history';
|
|
11
|
+
|
|
12
|
+
type HistoryBlockHandler = (location: Location, action: Action) => void | false;
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
Permits to register a handler that will be called on history actions (pop,push,replace)
|
|
16
|
+
If the handler returns false, the navigation transition will be blocked/cancelled
|
|
17
|
+
*/
|
|
18
|
+
export function useHistoryActionHandler(handler: HistoryBlockHandler): void {
|
|
19
|
+
const {block} = useHistory();
|
|
20
|
+
|
|
21
|
+
// Avoid stale closure issues without triggering useless re-renders
|
|
22
|
+
const lastHandlerRef = useRef(handler);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
lastHandlerRef.current = handler;
|
|
25
|
+
}, [handler]);
|
|
26
|
+
|
|
27
|
+
useEffect(
|
|
28
|
+
() =>
|
|
29
|
+
// See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
|
|
30
|
+
block((location, action) => lastHandlerRef.current(location, action)),
|
|
31
|
+
[block, lastHandlerRef],
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
Permits to register a handler that will be called on history pop navigation (backward/forward)
|
|
37
|
+
If the handler returns false, the backward/forward transition will be blocked
|
|
38
|
+
|
|
39
|
+
Unfortunately there's no good way to detect the "direction" (backward/forward) of the POP event.
|
|
40
|
+
*/
|
|
41
|
+
export function useHistoryPopHandler(handler: HistoryBlockHandler): void {
|
|
42
|
+
useHistoryActionHandler((location, action) => {
|
|
43
|
+
if (action === 'POP') {
|
|
44
|
+
// Eventually block navigation if handler returns false
|
|
45
|
+
return handler(location, action);
|
|
46
|
+
}
|
|
47
|
+
// Don't block other navigation actions
|
|
48
|
+
return undefined;
|
|
49
|
+
});
|
|
50
|
+
}
|