@docusaurus/plugin-content-docs 3.4.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/lib/categoryGeneratedIndex.d.ts +0 -1
  2. package/lib/categoryGeneratedIndex.js +1 -2
  3. package/lib/cli.d.ts +0 -1
  4. package/lib/cli.js +1 -2
  5. package/lib/client/doc.d.ts +29 -0
  6. package/lib/client/doc.js +47 -0
  7. package/lib/client/docSidebarItemsExpandedState.d.ts +30 -0
  8. package/lib/client/docSidebarItemsExpandedState.js +27 -0
  9. package/lib/client/docsClientUtils.js +16 -8
  10. package/lib/client/docsPreferredVersion.d.ts +29 -0
  11. package/lib/client/docsPreferredVersion.js +124 -0
  12. package/lib/client/docsSearch.d.ts +19 -0
  13. package/lib/client/docsSearch.js +39 -0
  14. package/lib/client/docsSearch.test.d.ts +7 -0
  15. package/lib/client/docsSearch.test.js +12 -0
  16. package/lib/client/docsSidebar.d.ts +25 -0
  17. package/lib/client/docsSidebar.js +29 -0
  18. package/lib/client/docsUtils.d.ts +106 -0
  19. package/lib/client/docsUtils.js +273 -0
  20. package/lib/client/docsVersion.d.ts +19 -0
  21. package/lib/client/docsVersion.js +25 -0
  22. package/lib/client/index.d.ts +8 -0
  23. package/lib/client/index.js +8 -0
  24. package/lib/docs.d.ts +0 -1
  25. package/lib/docs.js +8 -8
  26. package/lib/frontMatter.d.ts +0 -1
  27. package/lib/frontMatter.js +2 -2
  28. package/lib/globalData.js +1 -2
  29. package/lib/index.d.ts +0 -1
  30. package/lib/index.js +3 -2
  31. package/lib/numberPrefix.d.ts +0 -1
  32. package/lib/numberPrefix.js +3 -3
  33. package/lib/options.d.ts +0 -1
  34. package/lib/options.js +4 -2
  35. package/lib/props.d.ts +0 -1
  36. package/lib/props.js +5 -6
  37. package/lib/routes.d.ts +0 -1
  38. package/lib/routes.js +2 -3
  39. package/lib/sidebars/generator.js +3 -0
  40. package/lib/sidebars/index.d.ts +0 -1
  41. package/lib/sidebars/index.js +4 -4
  42. package/lib/sidebars/normalization.js +2 -3
  43. package/lib/sidebars/postProcessor.js +1 -2
  44. package/lib/sidebars/processor.js +1 -2
  45. package/lib/sidebars/types.d.ts +1 -1
  46. package/lib/sidebars/utils.d.ts +0 -1
  47. package/lib/sidebars/utils.js +13 -14
  48. package/lib/sidebars/validation.js +3 -3
  49. package/lib/slug.d.ts +0 -1
  50. package/lib/slug.js +1 -1
  51. package/lib/translations.d.ts +0 -1
  52. package/lib/translations.js +2 -3
  53. package/lib/types.d.ts +0 -1
  54. package/lib/versions/files.d.ts +0 -1
  55. package/lib/versions/files.js +7 -8
  56. package/lib/versions/index.d.ts +0 -1
  57. package/lib/versions/index.js +8 -9
  58. package/lib/versions/validation.d.ts +0 -1
  59. package/lib/versions/validation.js +3 -4
  60. package/package.json +11 -10
  61. package/src/client/doc.tsx +71 -0
  62. package/src/client/docSidebarItemsExpandedState.tsx +55 -0
  63. package/src/client/docsClientUtils.ts +17 -8
  64. package/src/client/docsPreferredVersion.tsx +248 -0
  65. package/src/client/docsSearch.test.ts +14 -0
  66. package/src/client/docsSearch.ts +57 -0
  67. package/src/client/docsSidebar.tsx +50 -0
  68. package/src/client/docsUtils.tsx +415 -0
  69. package/src/client/docsVersion.tsx +36 -0
  70. package/src/client/index.ts +39 -0
  71. package/src/index.ts +2 -0
  72. package/src/options.ts +3 -0
  73. package/src/sidebars/generator.ts +3 -0
  74. package/src/sidebars/postProcessor.ts +1 -0
  75. package/src/sidebars/types.ts +1 -0
  76. package/src/sidebars/validation.ts +1 -0
@@ -0,0 +1,55 @@
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, {type ReactNode, useMemo, useState, useContext} from 'react';
9
+ import {ReactContextError} from '@docusaurus/theme-common/internal';
10
+
11
+ type ContextValue = {
12
+ /**
13
+ * The item that the user last opened, `null` when there's none open. On
14
+ * initial render, it will always be `null`, which doesn't necessarily mean
15
+ * there's no category open (can have 0, 1, or many being initially open).
16
+ */
17
+ expandedItem: number | null;
18
+ /**
19
+ * Set the currently expanded item, when the user opens one. Set the value to
20
+ * `null` when the user closes an open category.
21
+ */
22
+ setExpandedItem: (a: number | null) => void;
23
+ };
24
+
25
+ const EmptyContext: unique symbol = Symbol('EmptyContext');
26
+ const Context = React.createContext<ContextValue | typeof EmptyContext>(
27
+ EmptyContext,
28
+ );
29
+
30
+ /**
31
+ * Should be used to wrap one sidebar category level. This provider syncs the
32
+ * expanded states of all sibling categories, and categories can choose to
33
+ * collapse itself if another one is expanded.
34
+ */
35
+ export function DocSidebarItemsExpandedStateProvider({
36
+ children,
37
+ }: {
38
+ children: ReactNode;
39
+ }): JSX.Element {
40
+ const [expandedItem, setExpandedItem] = useState<number | null>(null);
41
+ const contextValue = useMemo(
42
+ () => ({expandedItem, setExpandedItem}),
43
+ [expandedItem],
44
+ );
45
+
46
+ return <Context.Provider value={contextValue}>{children}</Context.Provider>;
47
+ }
48
+
49
+ export function useDocSidebarItemsExpandedState(): ContextValue {
50
+ const value = useContext(Context);
51
+ if (value === EmptyContext) {
52
+ throw new ReactContextError('DocSidebarItemsExpandedStateProvider');
53
+ }
54
+ return value;
55
+ }
@@ -63,14 +63,23 @@ export function getActiveVersion(
63
63
  data: GlobalPluginData,
64
64
  pathname: string,
65
65
  ): GlobalVersion | undefined {
66
- const lastVersion = getLatestVersion(data);
67
- // Last version is a route like /docs/*,
68
- // we need to match it last or it would match /docs/version-1.0/* as well
69
- const orderedVersionsMetadata = [
70
- ...data.versions.filter((version) => version !== lastVersion),
71
- lastVersion,
72
- ];
73
- return orderedVersionsMetadata.find(
66
+ // Sort paths so that a match-all version like /docs/* is matched last
67
+ // Otherwise /docs/* would match /docs/1.0.0/* routes
68
+ // This is simplified but similar to the core sortRoutes() logic
69
+ const sortedVersions = [...data.versions].sort((a, b) => {
70
+ if (a.path === b.path) {
71
+ return 0;
72
+ }
73
+ if (a.path.includes(b.path)) {
74
+ return -1;
75
+ }
76
+ if (b.path.includes(a.path)) {
77
+ return 1;
78
+ }
79
+ return 0;
80
+ });
81
+
82
+ return sortedVersions.find(
74
83
  (version) =>
75
84
  !!matchPath(pathname, {
76
85
  path: version.path,
@@ -0,0 +1,248 @@
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
+ useContext,
10
+ useEffect,
11
+ useMemo,
12
+ useState,
13
+ useCallback,
14
+ type ReactNode,
15
+ } from 'react';
16
+ import {
17
+ useAllDocsData,
18
+ useDocsData,
19
+ type GlobalPluginData,
20
+ type GlobalVersion,
21
+ } from '@docusaurus/plugin-content-docs/client';
22
+ import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';
23
+ import {useThemeConfig, type ThemeConfig} from '@docusaurus/theme-common';
24
+ import {
25
+ ReactContextError,
26
+ createStorageSlot,
27
+ } from '@docusaurus/theme-common/internal';
28
+
29
+ type DocsVersionPersistence = ThemeConfig['docs']['versionPersistence'];
30
+
31
+ const storageKey = (pluginId: string) => `docs-preferred-version-${pluginId}`;
32
+
33
+ const DocsPreferredVersionStorage = {
34
+ save: (
35
+ pluginId: string,
36
+ persistence: DocsVersionPersistence,
37
+ versionName: string,
38
+ ): void => {
39
+ createStorageSlot(storageKey(pluginId), {persistence}).set(versionName);
40
+ },
41
+
42
+ read: (
43
+ pluginId: string,
44
+ persistence: DocsVersionPersistence,
45
+ ): string | null =>
46
+ createStorageSlot(storageKey(pluginId), {persistence}).get(),
47
+
48
+ clear: (pluginId: string, persistence: DocsVersionPersistence): void => {
49
+ createStorageSlot(storageKey(pluginId), {persistence}).del();
50
+ },
51
+ };
52
+
53
+ type DocsPreferredVersionName = string | null;
54
+
55
+ /** State for a single docs plugin instance */
56
+ type DocsPreferredVersionPluginState = {
57
+ preferredVersionName: DocsPreferredVersionName;
58
+ };
59
+
60
+ /**
61
+ * We need to store the state in storage globally, with one preferred version
62
+ * per docs plugin instance.
63
+ */
64
+ type DocsPreferredVersionState = {
65
+ [pluginId: string]: DocsPreferredVersionPluginState;
66
+ };
67
+
68
+ /**
69
+ * Initial state is always null as we can't read local storage from node SSR
70
+ */
71
+ const getInitialState = (pluginIds: string[]): DocsPreferredVersionState =>
72
+ Object.fromEntries(pluginIds.map((id) => [id, {preferredVersionName: null}]));
73
+
74
+ /**
75
+ * Read storage for all docs plugins, assigning each doc plugin a preferred
76
+ * version (if found)
77
+ */
78
+ function readStorageState({
79
+ pluginIds,
80
+ versionPersistence,
81
+ allDocsData,
82
+ }: {
83
+ pluginIds: string[];
84
+ versionPersistence: DocsVersionPersistence;
85
+ allDocsData: {[pluginId: string]: GlobalPluginData};
86
+ }): DocsPreferredVersionState {
87
+ /**
88
+ * The storage value we read might be stale, and belong to a version that does
89
+ * not exist in the site anymore. In such case, we remove the storage value to
90
+ * avoid downstream errors.
91
+ */
92
+ function restorePluginState(
93
+ pluginId: string,
94
+ ): DocsPreferredVersionPluginState {
95
+ const preferredVersionNameUnsafe = DocsPreferredVersionStorage.read(
96
+ pluginId,
97
+ versionPersistence,
98
+ );
99
+ const pluginData = allDocsData[pluginId]!;
100
+ const versionExists = pluginData.versions.some(
101
+ (version) => version.name === preferredVersionNameUnsafe,
102
+ );
103
+ if (versionExists) {
104
+ return {preferredVersionName: preferredVersionNameUnsafe};
105
+ }
106
+ DocsPreferredVersionStorage.clear(pluginId, versionPersistence);
107
+ return {preferredVersionName: null};
108
+ }
109
+ return Object.fromEntries(
110
+ pluginIds.map((id) => [id, restorePluginState(id)]),
111
+ );
112
+ }
113
+
114
+ function useVersionPersistence(): DocsVersionPersistence {
115
+ return useThemeConfig().docs.versionPersistence;
116
+ }
117
+
118
+ type ContextValue = [
119
+ state: DocsPreferredVersionState,
120
+ api: {
121
+ savePreferredVersion: (pluginId: string, versionName: string) => void;
122
+ },
123
+ ];
124
+
125
+ const Context = React.createContext<ContextValue | null>(null);
126
+
127
+ function useContextValue(): ContextValue {
128
+ const allDocsData = useAllDocsData();
129
+ const versionPersistence = useVersionPersistence();
130
+ const pluginIds = useMemo(() => Object.keys(allDocsData), [allDocsData]);
131
+
132
+ // Initial state is empty, as we can't read browser storage in node/SSR
133
+ const [state, setState] = useState(() => getInitialState(pluginIds));
134
+
135
+ // On mount, we set the state read from browser storage
136
+ useEffect(() => {
137
+ setState(readStorageState({allDocsData, versionPersistence, pluginIds}));
138
+ }, [allDocsData, versionPersistence, pluginIds]);
139
+
140
+ // The API that we expose to consumer hooks (memo for constant object)
141
+ const api = useMemo(() => {
142
+ function savePreferredVersion(pluginId: string, versionName: string) {
143
+ DocsPreferredVersionStorage.save(
144
+ pluginId,
145
+ versionPersistence,
146
+ versionName,
147
+ );
148
+ setState((s) => ({
149
+ ...s,
150
+ [pluginId]: {preferredVersionName: versionName},
151
+ }));
152
+ }
153
+
154
+ return {
155
+ savePreferredVersion,
156
+ };
157
+ }, [versionPersistence]);
158
+
159
+ return [state, api];
160
+ }
161
+
162
+ function DocsPreferredVersionContextProviderUnsafe({
163
+ children,
164
+ }: {
165
+ children: ReactNode;
166
+ }): JSX.Element {
167
+ const value = useContextValue();
168
+ return <Context.Provider value={value}>{children}</Context.Provider>;
169
+ }
170
+
171
+ /**
172
+ * This is a maybe-layer. If the docs plugin is not enabled, this provider is a
173
+ * simple pass-through.
174
+ */
175
+ export function DocsPreferredVersionContextProvider({
176
+ children,
177
+ }: {
178
+ children: ReactNode;
179
+ }): JSX.Element {
180
+ return (
181
+ <DocsPreferredVersionContextProviderUnsafe>
182
+ {children}
183
+ </DocsPreferredVersionContextProviderUnsafe>
184
+ );
185
+ }
186
+
187
+ function useDocsPreferredVersionContext(): ContextValue {
188
+ const value = useContext(Context);
189
+ if (!value) {
190
+ throw new ReactContextError('DocsPreferredVersionContextProvider');
191
+ }
192
+ return value;
193
+ }
194
+
195
+ /**
196
+ * Returns a read-write interface to a plugin's preferred version. The
197
+ * "preferred version" is defined as the last version that the user visited.
198
+ * For example, if a user is using v3, even when v4 is later published, the user
199
+ * would still be browsing v3 docs when she opens the website next time. Note,
200
+ * the `preferredVersion` attribute will always be `null` before mount.
201
+ */
202
+ export function useDocsPreferredVersion(
203
+ pluginId: string | undefined = DEFAULT_PLUGIN_ID,
204
+ ): {
205
+ preferredVersion: GlobalVersion | null;
206
+ savePreferredVersionName: (versionName: string) => void;
207
+ } {
208
+ const docsData = useDocsData(pluginId);
209
+ const [state, api] = useDocsPreferredVersionContext();
210
+
211
+ const {preferredVersionName} = state[pluginId]!;
212
+
213
+ const preferredVersion =
214
+ docsData.versions.find(
215
+ (version) => version.name === preferredVersionName,
216
+ ) ?? null;
217
+
218
+ const savePreferredVersionName = useCallback(
219
+ (versionName: string) => {
220
+ api.savePreferredVersion(pluginId, versionName);
221
+ },
222
+ [api, pluginId],
223
+ );
224
+
225
+ return {preferredVersion, savePreferredVersionName};
226
+ }
227
+
228
+ export function useDocsPreferredVersionByPluginId(): {
229
+ [pluginId: string]: GlobalVersion | null;
230
+ } {
231
+ const allDocsData = useAllDocsData();
232
+ const [state] = useDocsPreferredVersionContext();
233
+
234
+ function getPluginIdPreferredVersion(pluginId: string) {
235
+ const docsData = allDocsData[pluginId]!;
236
+ const {preferredVersionName} = state[pluginId]!;
237
+
238
+ return (
239
+ docsData.versions.find(
240
+ (version) => version.name === preferredVersionName,
241
+ ) ?? null
242
+ );
243
+ }
244
+ const pluginIds = Object.keys(allDocsData);
245
+ return Object.fromEntries(
246
+ pluginIds.map((id) => [id, getPluginIdPreferredVersion(id)]),
247
+ );
248
+ }
@@ -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
+
8
+ import {getDocsVersionSearchTag} from './docsSearch';
9
+
10
+ describe('getDocsVersionSearchTag', () => {
11
+ it('works', () => {
12
+ expect(getDocsVersionSearchTag('foo', 'bar')).toBe('docs-foo-bar');
13
+ });
14
+ });
@@ -0,0 +1,57 @@
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 {
9
+ useAllDocsData,
10
+ useActivePluginAndVersion,
11
+ } from '@docusaurus/plugin-content-docs/client';
12
+ import {useDocsPreferredVersionByPluginId} from './docsPreferredVersion';
13
+
14
+ /** The search tag to append as each doc's metadata. */
15
+ export function getDocsVersionSearchTag(
16
+ pluginId: string,
17
+ versionName: string,
18
+ ): string {
19
+ return `docs-${pluginId}-${versionName}`;
20
+ }
21
+
22
+ /**
23
+ * Gets the relevant docs tags to search.
24
+ * This is the logic that powers the contextual search feature.
25
+ *
26
+ * If user is browsing Android 1.4 docs, he'll get presented with:
27
+ * - Android '1.4' docs
28
+ * - iOS 'preferred | latest' docs
29
+ *
30
+ * The result is generic and not coupled to Algolia/DocSearch on purpose.
31
+ */
32
+ export function useDocsContextualSearchTags(): string[] {
33
+ const allDocsData = useAllDocsData();
34
+ const activePluginAndVersion = useActivePluginAndVersion();
35
+ const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
36
+
37
+ // This can't use more specialized hooks because we are mapping over all
38
+ // plugin instances.
39
+ function getDocPluginTags(pluginId: string) {
40
+ const activeVersion =
41
+ activePluginAndVersion?.activePlugin.pluginId === pluginId
42
+ ? activePluginAndVersion.activeVersion
43
+ : undefined;
44
+
45
+ const preferredVersion = docsPreferredVersionByPluginId[pluginId];
46
+
47
+ const latestVersion = allDocsData[pluginId]!.versions.find(
48
+ (v) => v.isLast,
49
+ )!;
50
+
51
+ const version = activeVersion ?? preferredVersion ?? latestVersion;
52
+
53
+ return getDocsVersionSearchTag(pluginId, version.name);
54
+ }
55
+
56
+ return [...Object.keys(allDocsData).map(getDocPluginTags)];
57
+ }
@@ -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 React, {useMemo, useContext, type ReactNode} from 'react';
9
+ import {ReactContextError} from '@docusaurus/theme-common/internal';
10
+ import type {PropSidebar} from '@docusaurus/plugin-content-docs';
11
+
12
+ // Using a Symbol because null is a valid context value (a doc with no sidebar)
13
+ // Inspired by https://github.com/jamiebuilds/unstated-next/blob/master/src/unstated-next.tsx
14
+ const EmptyContext: unique symbol = Symbol('EmptyContext');
15
+
16
+ type ContextValue = {name: string; items: PropSidebar};
17
+
18
+ const Context = React.createContext<ContextValue | null | typeof EmptyContext>(
19
+ EmptyContext,
20
+ );
21
+
22
+ /**
23
+ * Provide the current sidebar to your children.
24
+ */
25
+ export function DocsSidebarProvider({
26
+ children,
27
+ name,
28
+ items,
29
+ }: {
30
+ children: ReactNode;
31
+ name: string | undefined;
32
+ items: PropSidebar | undefined;
33
+ }): JSX.Element {
34
+ const stableValue: ContextValue | null = useMemo(
35
+ () => (name && items ? {name, items} : null),
36
+ [name, items],
37
+ );
38
+ return <Context.Provider value={stableValue}>{children}</Context.Provider>;
39
+ }
40
+
41
+ /**
42
+ * Gets the sidebar that's currently displayed, or `null` if there isn't one
43
+ */
44
+ export function useDocsSidebar(): ContextValue | null {
45
+ const value = useContext(Context);
46
+ if (value === EmptyContext) {
47
+ throw new ReactContextError('DocsSidebarProvider');
48
+ }
49
+ return value;
50
+ }