@docusaurus/theme-common 2.0.0-beta.12faed89d → 2.0.0-beta.13
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/{utils/useCollapsible.d.ts → components/Collapsible/index.d.ts} +9 -3
- package/lib/{utils/useCollapsible.js → components/Collapsible/index.js} +36 -14
- 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 +19 -5
- package/lib/index.js +14 -3
- package/lib/utils/ThemeClassNames.d.ts +36 -12
- package/lib/utils/ThemeClassNames.js +36 -3
- package/lib/utils/announcementBarUtils.d.ts +3 -3
- package/lib/utils/announcementBarUtils.js +14 -18
- 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 +2 -2
- package/lib/utils/mobileSecondaryMenu.js +3 -4
- 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/{src/utils/docsUtils.ts → lib/utils/useLocalPathname.d.ts} +1 -5
- package/lib/utils/useLocalPathname.js +16 -0
- package/lib/utils/useLocationChange.js +9 -11
- package/lib/utils/usePluralForm.js +1 -3
- package/lib/utils/usePrevious.js +3 -2
- package/lib/utils/useTOCHighlight.d.ts +14 -0
- package/lib/utils/useTOCHighlight.js +124 -0
- package/lib/utils/useThemeConfig.d.ts +20 -3
- package/package.json +18 -12
- package/src/{utils/useCollapsible.tsx → components/Collapsible/index.tsx} +70 -25
- package/src/components/Details/index.tsx +94 -0
- package/src/components/Details/styles.module.css +58 -0
- package/src/index.ts +54 -4
- 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 +20 -15
- 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 +9 -6
- 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/useContextualSearchFilters.ts +50 -0
- package/src/utils/useLocalPathname.ts +20 -0
- package/src/utils/useLocationChange.ts +10 -12
- package/src/utils/usePluralForm.ts +2 -3
- package/src/utils/usePrevious.ts +3 -2
- package/src/utils/useTOCHighlight.ts +179 -0
- package/src/utils/useThemeConfig.ts +19 -3
|
@@ -0,0 +1,197 @@
|
|
|
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 {TOCItem} from '@docusaurus/types';
|
|
9
|
+
import {filterTOC} from '../tocUtils';
|
|
10
|
+
|
|
11
|
+
describe('filterTOC', () => {
|
|
12
|
+
test('filter a toc with all heading levels', () => {
|
|
13
|
+
const toc: TOCItem[] = [
|
|
14
|
+
{
|
|
15
|
+
id: 'alpha',
|
|
16
|
+
level: 1,
|
|
17
|
+
value: 'alpha',
|
|
18
|
+
children: [
|
|
19
|
+
{
|
|
20
|
+
id: 'bravo',
|
|
21
|
+
level: 2,
|
|
22
|
+
value: 'Bravo',
|
|
23
|
+
children: [
|
|
24
|
+
{
|
|
25
|
+
id: 'charlie',
|
|
26
|
+
level: 3,
|
|
27
|
+
value: 'Charlie',
|
|
28
|
+
children: [
|
|
29
|
+
{
|
|
30
|
+
id: 'delta',
|
|
31
|
+
level: 4,
|
|
32
|
+
value: 'Delta',
|
|
33
|
+
children: [
|
|
34
|
+
{
|
|
35
|
+
id: 'echo',
|
|
36
|
+
level: 5,
|
|
37
|
+
value: 'Echo',
|
|
38
|
+
children: [
|
|
39
|
+
{
|
|
40
|
+
id: 'foxtrot',
|
|
41
|
+
level: 6,
|
|
42
|
+
value: 'Foxtrot',
|
|
43
|
+
children: [],
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 2})).toEqual([
|
|
58
|
+
{
|
|
59
|
+
id: 'bravo',
|
|
60
|
+
level: 2,
|
|
61
|
+
value: 'Bravo',
|
|
62
|
+
children: [],
|
|
63
|
+
},
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
expect(filterTOC({toc, minHeadingLevel: 3, maxHeadingLevel: 3})).toEqual([
|
|
67
|
+
{
|
|
68
|
+
id: 'charlie',
|
|
69
|
+
level: 3,
|
|
70
|
+
value: 'Charlie',
|
|
71
|
+
children: [],
|
|
72
|
+
},
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 3})).toEqual([
|
|
76
|
+
{
|
|
77
|
+
id: 'bravo',
|
|
78
|
+
level: 2,
|
|
79
|
+
value: 'Bravo',
|
|
80
|
+
children: [
|
|
81
|
+
{
|
|
82
|
+
id: 'charlie',
|
|
83
|
+
level: 3,
|
|
84
|
+
value: 'Charlie',
|
|
85
|
+
children: [],
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 4})).toEqual([
|
|
92
|
+
{
|
|
93
|
+
id: 'bravo',
|
|
94
|
+
level: 2,
|
|
95
|
+
value: 'Bravo',
|
|
96
|
+
children: [
|
|
97
|
+
{
|
|
98
|
+
id: 'charlie',
|
|
99
|
+
level: 3,
|
|
100
|
+
value: 'Charlie',
|
|
101
|
+
children: [
|
|
102
|
+
{
|
|
103
|
+
id: 'delta',
|
|
104
|
+
level: 4,
|
|
105
|
+
value: 'Delta',
|
|
106
|
+
children: [],
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// It's not 100% clear exactly how the TOC should behave under weird heading levels provided by the user
|
|
116
|
+
// Adding a test so that behavior stays the same over time
|
|
117
|
+
test('filter invalid heading levels (but possible) TOC', () => {
|
|
118
|
+
const toc: TOCItem[] = [
|
|
119
|
+
{
|
|
120
|
+
id: 'charlie',
|
|
121
|
+
level: 3,
|
|
122
|
+
value: 'Charlie',
|
|
123
|
+
children: [],
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
id: 'bravo',
|
|
127
|
+
level: 2,
|
|
128
|
+
value: 'Bravo',
|
|
129
|
+
children: [
|
|
130
|
+
{
|
|
131
|
+
id: 'delta',
|
|
132
|
+
level: 4,
|
|
133
|
+
value: 'Delta',
|
|
134
|
+
children: [],
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 2})).toEqual([
|
|
141
|
+
{
|
|
142
|
+
id: 'bravo',
|
|
143
|
+
level: 2,
|
|
144
|
+
value: 'Bravo',
|
|
145
|
+
children: [],
|
|
146
|
+
},
|
|
147
|
+
]);
|
|
148
|
+
|
|
149
|
+
expect(filterTOC({toc, minHeadingLevel: 3, maxHeadingLevel: 3})).toEqual([
|
|
150
|
+
{
|
|
151
|
+
id: 'charlie',
|
|
152
|
+
level: 3,
|
|
153
|
+
value: 'Charlie',
|
|
154
|
+
children: [],
|
|
155
|
+
},
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
expect(filterTOC({toc, minHeadingLevel: 4, maxHeadingLevel: 4})).toEqual([
|
|
159
|
+
{
|
|
160
|
+
id: 'delta',
|
|
161
|
+
level: 4,
|
|
162
|
+
value: 'Delta',
|
|
163
|
+
children: [],
|
|
164
|
+
},
|
|
165
|
+
]);
|
|
166
|
+
|
|
167
|
+
expect(filterTOC({toc, minHeadingLevel: 2, maxHeadingLevel: 3})).toEqual([
|
|
168
|
+
{
|
|
169
|
+
id: 'charlie',
|
|
170
|
+
level: 3,
|
|
171
|
+
value: 'Charlie',
|
|
172
|
+
children: [],
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
id: 'bravo',
|
|
176
|
+
level: 2,
|
|
177
|
+
value: 'Bravo',
|
|
178
|
+
children: [],
|
|
179
|
+
},
|
|
180
|
+
]);
|
|
181
|
+
|
|
182
|
+
expect(filterTOC({toc, minHeadingLevel: 3, maxHeadingLevel: 4})).toEqual([
|
|
183
|
+
{
|
|
184
|
+
id: 'charlie',
|
|
185
|
+
level: 3,
|
|
186
|
+
value: 'Charlie',
|
|
187
|
+
children: [],
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
id: 'delta',
|
|
191
|
+
level: 4,
|
|
192
|
+
value: 'Delta',
|
|
193
|
+
children: [],
|
|
194
|
+
},
|
|
195
|
+
]);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
@@ -14,7 +14,7 @@ import React, {
|
|
|
14
14
|
useContext,
|
|
15
15
|
createContext,
|
|
16
16
|
} from 'react';
|
|
17
|
-
import
|
|
17
|
+
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
18
18
|
import {createStorageSlot} from './storageUtils';
|
|
19
19
|
import {useThemeConfig} from './useThemeConfig';
|
|
20
20
|
|
|
@@ -33,21 +33,21 @@ const setDismissedInStorage = (bool: boolean) =>
|
|
|
33
33
|
AnnouncementBarDismissStorage.set(String(bool));
|
|
34
34
|
|
|
35
35
|
type AnnouncementBarAPI = {
|
|
36
|
-
readonly
|
|
36
|
+
readonly isActive: boolean;
|
|
37
37
|
readonly close: () => void;
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
|
|
41
41
|
const {announcementBar} = useThemeConfig();
|
|
42
|
-
const
|
|
42
|
+
const isBrowser = useIsBrowser();
|
|
43
43
|
|
|
44
|
-
const [isClosed, setClosed] = useState(() =>
|
|
45
|
-
|
|
44
|
+
const [isClosed, setClosed] = useState(() =>
|
|
45
|
+
isBrowser
|
|
46
46
|
? // On client navigation: init with localstorage value
|
|
47
47
|
isDismissedInStorage()
|
|
48
48
|
: // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
|
|
49
|
-
false
|
|
50
|
-
|
|
49
|
+
false,
|
|
50
|
+
);
|
|
51
51
|
// Update state after hydration
|
|
52
52
|
useEffect(() => {
|
|
53
53
|
setClosed(isDismissedInStorage());
|
|
@@ -83,26 +83,31 @@ const useAnnouncementBarContextValue = (): AnnouncementBarAPI => {
|
|
|
83
83
|
if (isNewAnnouncement || !isDismissedInStorage()) {
|
|
84
84
|
setClosed(false);
|
|
85
85
|
}
|
|
86
|
-
}, []);
|
|
86
|
+
}, [announcementBar]);
|
|
87
87
|
|
|
88
|
-
return useMemo(
|
|
89
|
-
|
|
90
|
-
isClosed,
|
|
88
|
+
return useMemo(
|
|
89
|
+
() => ({
|
|
90
|
+
isActive: !!announcementBar && !isClosed,
|
|
91
91
|
close: handleClose,
|
|
92
|
-
}
|
|
93
|
-
|
|
92
|
+
}),
|
|
93
|
+
[announcementBar, isClosed, handleClose],
|
|
94
|
+
);
|
|
94
95
|
};
|
|
95
96
|
|
|
96
97
|
const AnnouncementBarContext = createContext<AnnouncementBarAPI | null>(null);
|
|
97
98
|
|
|
98
|
-
export
|
|
99
|
+
export function AnnouncementBarProvider({
|
|
100
|
+
children,
|
|
101
|
+
}: {
|
|
102
|
+
children: ReactNode;
|
|
103
|
+
}): JSX.Element {
|
|
99
104
|
const value = useAnnouncementBarContextValue();
|
|
100
105
|
return (
|
|
101
106
|
<AnnouncementBarContext.Provider value={value}>
|
|
102
107
|
{children}
|
|
103
108
|
</AnnouncementBarContext.Provider>
|
|
104
109
|
);
|
|
105
|
-
}
|
|
110
|
+
}
|
|
106
111
|
|
|
107
112
|
export const useAnnouncementBar = (): AnnouncementBarAPI => {
|
|
108
113
|
const api = useContext(AnnouncementBarContext);
|
|
@@ -5,8 +5,159 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import rangeParser from 'parse-numeric-range';
|
|
9
|
+
import type {Language} from 'prism-react-renderer';
|
|
10
|
+
|
|
8
11
|
const codeBlockTitleRegex = /title=(["'])(.*?)\1/;
|
|
12
|
+
const highlightLinesRangeRegex = /{([\d,-]+)}/;
|
|
13
|
+
|
|
14
|
+
const commentTypes = ['js', 'jsBlock', 'jsx', 'python', 'html'] as const;
|
|
15
|
+
type CommentType = typeof commentTypes[number];
|
|
16
|
+
|
|
17
|
+
type CommentPattern = {
|
|
18
|
+
start: string;
|
|
19
|
+
end: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Supported types of highlight comments
|
|
23
|
+
const commentPatterns: Record<CommentType, CommentPattern> = {
|
|
24
|
+
js: {
|
|
25
|
+
start: '\\/\\/',
|
|
26
|
+
end: '',
|
|
27
|
+
},
|
|
28
|
+
jsBlock: {
|
|
29
|
+
start: '\\/\\*',
|
|
30
|
+
end: '\\*\\/',
|
|
31
|
+
},
|
|
32
|
+
jsx: {
|
|
33
|
+
start: '\\{\\s*\\/\\*',
|
|
34
|
+
end: '\\*\\/\\s*\\}',
|
|
35
|
+
},
|
|
36
|
+
python: {
|
|
37
|
+
start: '#',
|
|
38
|
+
end: '',
|
|
39
|
+
},
|
|
40
|
+
html: {
|
|
41
|
+
start: '<!--',
|
|
42
|
+
end: '-->',
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const magicCommentDirectives = [
|
|
47
|
+
'highlight-next-line',
|
|
48
|
+
'highlight-start',
|
|
49
|
+
'highlight-end',
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
const getMagicCommentDirectiveRegex = (
|
|
53
|
+
languages: readonly CommentType[] = commentTypes,
|
|
54
|
+
) => {
|
|
55
|
+
// to be more reliable, the opening and closing comment must match
|
|
56
|
+
const commentPattern = languages
|
|
57
|
+
.map((lang) => {
|
|
58
|
+
const {start, end} = commentPatterns[lang];
|
|
59
|
+
return `(?:${start}\\s*(${magicCommentDirectives.join('|')})\\s*${end})`;
|
|
60
|
+
})
|
|
61
|
+
.join('|');
|
|
62
|
+
// white space is allowed, but otherwise it should be on it's own line
|
|
63
|
+
return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// select comment styles based on language
|
|
67
|
+
const magicCommentDirectiveRegex = (lang: string) => {
|
|
68
|
+
switch (lang) {
|
|
69
|
+
case 'js':
|
|
70
|
+
case 'javascript':
|
|
71
|
+
case 'ts':
|
|
72
|
+
case 'typescript':
|
|
73
|
+
return getMagicCommentDirectiveRegex(['js', 'jsBlock']);
|
|
74
|
+
|
|
75
|
+
case 'jsx':
|
|
76
|
+
case 'tsx':
|
|
77
|
+
return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'jsx']);
|
|
78
|
+
|
|
79
|
+
case 'html':
|
|
80
|
+
return getMagicCommentDirectiveRegex(['js', 'jsBlock', 'html']);
|
|
81
|
+
|
|
82
|
+
case 'python':
|
|
83
|
+
case 'py':
|
|
84
|
+
return getMagicCommentDirectiveRegex(['python']);
|
|
85
|
+
|
|
86
|
+
default:
|
|
87
|
+
// all comment types
|
|
88
|
+
return getMagicCommentDirectiveRegex();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
9
91
|
|
|
10
92
|
export function parseCodeBlockTitle(metastring?: string): string {
|
|
11
93
|
return metastring?.match(codeBlockTitleRegex)?.[2] ?? '';
|
|
12
94
|
}
|
|
95
|
+
|
|
96
|
+
export function parseLanguage(className?: string): Language | undefined {
|
|
97
|
+
const languageClassName = className
|
|
98
|
+
?.split(' ')
|
|
99
|
+
.find((str) => str.startsWith('language-'));
|
|
100
|
+
return languageClassName?.replace(/language-/, '') as Language | undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param metastring The highlight range declared here starts at 1
|
|
105
|
+
* @returns Note: all line numbers start at 0, not 1
|
|
106
|
+
*/
|
|
107
|
+
export function parseLines(
|
|
108
|
+
content: string,
|
|
109
|
+
metastring?: string,
|
|
110
|
+
language?: Language,
|
|
111
|
+
): {
|
|
112
|
+
highlightLines: number[];
|
|
113
|
+
code: string;
|
|
114
|
+
} {
|
|
115
|
+
let code = content.replace(/\n$/, '');
|
|
116
|
+
// Highlighted lines specified in props: don't parse the content
|
|
117
|
+
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
|
118
|
+
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)![1];
|
|
119
|
+
const highlightLines = rangeParser(highlightLinesRange)
|
|
120
|
+
.filter((n) => n > 0)
|
|
121
|
+
.map((n) => n - 1);
|
|
122
|
+
return {highlightLines, code};
|
|
123
|
+
}
|
|
124
|
+
if (language === undefined) {
|
|
125
|
+
return {highlightLines: [], code};
|
|
126
|
+
}
|
|
127
|
+
const directiveRegex = magicCommentDirectiveRegex(language);
|
|
128
|
+
// go through line by line
|
|
129
|
+
const lines = code.split('\n');
|
|
130
|
+
let highlightBlockStart: number;
|
|
131
|
+
let highlightRange = '';
|
|
132
|
+
// loop through lines
|
|
133
|
+
for (let lineNumber = 0; lineNumber < lines.length; ) {
|
|
134
|
+
const line = lines[lineNumber];
|
|
135
|
+
const match = line.match(directiveRegex);
|
|
136
|
+
if (match !== null) {
|
|
137
|
+
const directive = match.slice(1).find((item) => item !== undefined);
|
|
138
|
+
switch (directive) {
|
|
139
|
+
case 'highlight-next-line':
|
|
140
|
+
highlightRange += `${lineNumber},`;
|
|
141
|
+
break;
|
|
142
|
+
|
|
143
|
+
case 'highlight-start':
|
|
144
|
+
highlightBlockStart = lineNumber;
|
|
145
|
+
break;
|
|
146
|
+
|
|
147
|
+
case 'highlight-end':
|
|
148
|
+
highlightRange += `${highlightBlockStart!}-${lineNumber - 1},`;
|
|
149
|
+
break;
|
|
150
|
+
|
|
151
|
+
default:
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
lines.splice(lineNumber, 1);
|
|
155
|
+
} else {
|
|
156
|
+
// lines without directives are unchanged
|
|
157
|
+
lineNumber += 1;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const highlightLines = rangeParser(highlightRange);
|
|
161
|
+
code = lines.join('\n');
|
|
162
|
+
return {highlightLines, code};
|
|
163
|
+
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
|
|
7
8
|
import React, {
|
|
8
9
|
createContext,
|
|
9
10
|
ReactNode,
|
|
@@ -15,7 +16,7 @@ import React, {
|
|
|
15
16
|
import {useThemeConfig, DocsVersionPersistence} from '../useThemeConfig';
|
|
16
17
|
import {isDocsPluginEnabled} from '../docsUtils';
|
|
17
18
|
|
|
18
|
-
import {useAllDocsData} from '@theme/hooks/useDocs';
|
|
19
|
+
import {useAllDocsData, GlobalPluginData} from '@theme/hooks/useDocs';
|
|
19
20
|
|
|
20
21
|
import DocsPreferredVersionStorage from './DocsPreferredVersionStorage';
|
|
21
22
|
|
|
@@ -54,7 +55,7 @@ function readStorageState({
|
|
|
54
55
|
}: {
|
|
55
56
|
pluginIds: string[];
|
|
56
57
|
versionPersistence: DocsVersionPersistence;
|
|
57
|
-
allDocsData:
|
|
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
|
});
|