@docusaurus/theme-common 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 (95) hide show
  1. package/lib/index.d.ts +5 -6
  2. package/lib/index.d.ts.map +1 -1
  3. package/lib/index.js +18 -6
  4. package/lib/index.js.map +1 -1
  5. package/lib/internal.d.ts +2 -9
  6. package/lib/internal.d.ts.map +1 -1
  7. package/lib/internal.js +2 -9
  8. package/lib/internal.js.map +1 -1
  9. package/lib/translations/blogTranslations.d.ts +19 -0
  10. package/lib/translations/blogTranslations.d.ts.map +1 -0
  11. package/lib/translations/blogTranslations.js +48 -0
  12. package/lib/translations/blogTranslations.js.map +1 -0
  13. package/lib/{utils/unlistedUtils.d.ts → translations/contentVisibilityTranslations.d.ts} +3 -2
  14. package/lib/translations/contentVisibilityTranslations.d.ts.map +1 -0
  15. package/lib/translations/contentVisibilityTranslations.js +39 -0
  16. package/lib/translations/contentVisibilityTranslations.js.map +1 -0
  17. package/lib/utils/ThemeClassNames.d.ts +3 -0
  18. package/lib/utils/ThemeClassNames.d.ts.map +1 -1
  19. package/lib/utils/ThemeClassNames.js +3 -0
  20. package/lib/utils/ThemeClassNames.js.map +1 -1
  21. package/lib/utils/errorBoundaryUtils.d.ts +0 -1
  22. package/lib/utils/errorBoundaryUtils.d.ts.map +1 -1
  23. package/lib/utils/jsUtils.d.ts +1 -0
  24. package/lib/utils/jsUtils.d.ts.map +1 -1
  25. package/lib/utils/jsUtils.js +14 -0
  26. package/lib/utils/jsUtils.js.map +1 -1
  27. package/lib/utils/searchUtils.d.ts +0 -13
  28. package/lib/utils/searchUtils.d.ts.map +1 -1
  29. package/lib/utils/searchUtils.js +0 -39
  30. package/lib/utils/searchUtils.js.map +1 -1
  31. package/lib/utils/storageUtils.d.ts +0 -1
  32. package/lib/utils/storageUtils.d.ts.map +1 -1
  33. package/lib/utils/tabsUtils.d.ts +2 -2
  34. package/lib/utils/tabsUtils.d.ts.map +1 -1
  35. package/lib/utils/useThemeConfig.d.ts +5 -0
  36. package/lib/utils/useThemeConfig.d.ts.map +1 -1
  37. package/lib/utils/useThemeConfig.js.map +1 -1
  38. package/package.json +9 -11
  39. package/src/index.ts +27 -16
  40. package/src/internal.ts +8 -36
  41. package/src/translations/blogTranslations.tsx +79 -0
  42. package/src/{utils/unlistedUtils.tsx → translations/contentVisibilityTranslations.tsx} +25 -2
  43. package/src/types.d.ts +0 -3
  44. package/src/utils/ThemeClassNames.ts +3 -0
  45. package/src/utils/jsUtils.ts +18 -0
  46. package/src/utils/searchUtils.ts +0 -58
  47. package/src/utils/useThemeConfig.ts +6 -0
  48. package/lib/contexts/blogPost.d.ts +0 -33
  49. package/lib/contexts/blogPost.d.ts.map +0 -1
  50. package/lib/contexts/blogPost.js +0 -46
  51. package/lib/contexts/blogPost.js.map +0 -1
  52. package/lib/contexts/doc.d.ts +0 -30
  53. package/lib/contexts/doc.d.ts.map +0 -1
  54. package/lib/contexts/doc.js +0 -48
  55. package/lib/contexts/doc.js.map +0 -1
  56. package/lib/contexts/docSidebarItemsExpandedState.d.ts +0 -31
  57. package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +0 -1
  58. package/lib/contexts/docSidebarItemsExpandedState.js +0 -28
  59. package/lib/contexts/docSidebarItemsExpandedState.js.map +0 -1
  60. package/lib/contexts/docsPreferredVersion.d.ts +0 -30
  61. package/lib/contexts/docsPreferredVersion.d.ts.map +0 -1
  62. package/lib/contexts/docsPreferredVersion.js +0 -130
  63. package/lib/contexts/docsPreferredVersion.js.map +0 -1
  64. package/lib/contexts/docsSidebar.d.ts +0 -26
  65. package/lib/contexts/docsSidebar.d.ts.map +0 -1
  66. package/lib/contexts/docsSidebar.js +0 -30
  67. package/lib/contexts/docsSidebar.js.map +0 -1
  68. package/lib/contexts/docsVersion.d.ts +0 -20
  69. package/lib/contexts/docsVersion.d.ts.map +0 -1
  70. package/lib/contexts/docsVersion.js +0 -26
  71. package/lib/contexts/docsVersion.js.map +0 -1
  72. package/lib/utils/blogUtils.d.ts +0 -13
  73. package/lib/utils/blogUtils.d.ts.map +0 -1
  74. package/lib/utils/blogUtils.js +0 -24
  75. package/lib/utils/blogUtils.js.map +0 -1
  76. package/lib/utils/docsUtils.d.ts +0 -109
  77. package/lib/utils/docsUtils.d.ts.map +0 -1
  78. package/lib/utils/docsUtils.js +0 -276
  79. package/lib/utils/docsUtils.js.map +0 -1
  80. package/lib/utils/structuredDataUtils.d.ts +0 -12
  81. package/lib/utils/structuredDataUtils.d.ts.map +0 -1
  82. package/lib/utils/structuredDataUtils.js +0 -123
  83. package/lib/utils/structuredDataUtils.js.map +0 -1
  84. package/lib/utils/unlistedUtils.d.ts.map +0 -1
  85. package/lib/utils/unlistedUtils.js +0 -26
  86. package/lib/utils/unlistedUtils.js.map +0 -1
  87. package/src/contexts/blogPost.tsx +0 -80
  88. package/src/contexts/doc.tsx +0 -71
  89. package/src/contexts/docSidebarItemsExpandedState.tsx +0 -55
  90. package/src/contexts/docsPreferredVersion.tsx +0 -251
  91. package/src/contexts/docsSidebar.tsx +0 -50
  92. package/src/contexts/docsVersion.tsx +0 -36
  93. package/src/utils/blogUtils.ts +0 -32
  94. package/src/utils/docsUtils.tsx +0 -418
  95. package/src/utils/structuredDataUtils.ts +0 -177
@@ -1,55 +0,0 @@
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 '../utils/reactUtils';
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
- }
@@ -1,251 +0,0 @@
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 {
24
- useThemeConfig,
25
- type DocsVersionPersistence,
26
- } from '../utils/useThemeConfig';
27
- import {isDocsPluginEnabled} from '../utils/docsUtils';
28
- import {ReactContextError} from '../utils/reactUtils';
29
- import {createStorageSlot} from '../utils/storageUtils';
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
- if (isDocsPluginEnabled) {
181
- return (
182
- <DocsPreferredVersionContextProviderUnsafe>
183
- {children}
184
- </DocsPreferredVersionContextProviderUnsafe>
185
- );
186
- }
187
- return <>{children}</>;
188
- }
189
-
190
- function useDocsPreferredVersionContext(): ContextValue {
191
- const value = useContext(Context);
192
- if (!value) {
193
- throw new ReactContextError('DocsPreferredVersionContextProvider');
194
- }
195
- return value;
196
- }
197
-
198
- /**
199
- * Returns a read-write interface to a plugin's preferred version. The
200
- * "preferred version" is defined as the last version that the user visited.
201
- * For example, if a user is using v3, even when v4 is later published, the user
202
- * would still be browsing v3 docs when she opens the website next time. Note,
203
- * the `preferredVersion` attribute will always be `null` before mount.
204
- */
205
- export function useDocsPreferredVersion(
206
- pluginId: string | undefined = DEFAULT_PLUGIN_ID,
207
- ): {
208
- preferredVersion: GlobalVersion | null;
209
- savePreferredVersionName: (versionName: string) => void;
210
- } {
211
- const docsData = useDocsData(pluginId);
212
- const [state, api] = useDocsPreferredVersionContext();
213
-
214
- const {preferredVersionName} = state[pluginId]!;
215
-
216
- const preferredVersion =
217
- docsData.versions.find(
218
- (version) => version.name === preferredVersionName,
219
- ) ?? null;
220
-
221
- const savePreferredVersionName = useCallback(
222
- (versionName: string) => {
223
- api.savePreferredVersion(pluginId, versionName);
224
- },
225
- [api, pluginId],
226
- );
227
-
228
- return {preferredVersion, savePreferredVersionName};
229
- }
230
-
231
- export function useDocsPreferredVersionByPluginId(): {
232
- [pluginId: string]: GlobalVersion | null;
233
- } {
234
- const allDocsData = useAllDocsData();
235
- const [state] = useDocsPreferredVersionContext();
236
-
237
- function getPluginIdPreferredVersion(pluginId: string) {
238
- const docsData = allDocsData[pluginId]!;
239
- const {preferredVersionName} = state[pluginId]!;
240
-
241
- return (
242
- docsData.versions.find(
243
- (version) => version.name === preferredVersionName,
244
- ) ?? null
245
- );
246
- }
247
- const pluginIds = Object.keys(allDocsData);
248
- return Object.fromEntries(
249
- pluginIds.map((id) => [id, getPluginIdPreferredVersion(id)]),
250
- );
251
- }
@@ -1,50 +0,0 @@
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 '../utils/reactUtils';
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
- }
@@ -1,36 +0,0 @@
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, useContext} from 'react';
9
- import {ReactContextError} from '../utils/reactUtils';
10
- import type {PropVersionMetadata} from '@docusaurus/plugin-content-docs';
11
-
12
- const Context = React.createContext<PropVersionMetadata | null>(null);
13
-
14
- /**
15
- * Provide the current version's metadata to your children.
16
- */
17
- export function DocsVersionProvider({
18
- children,
19
- version,
20
- }: {
21
- children: ReactNode;
22
- version: PropVersionMetadata | null;
23
- }): JSX.Element {
24
- return <Context.Provider value={version}>{children}</Context.Provider>;
25
- }
26
-
27
- /**
28
- * Gets the version metadata of the current doc page.
29
- */
30
- export function useDocsVersion(): PropVersionMetadata {
31
- const version = useContext(Context);
32
- if (version === null) {
33
- throw new ReactContextError('DocsVersionProvider');
34
- }
35
- return version;
36
- }
@@ -1,32 +0,0 @@
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 {useMemo} from 'react';
9
- import {useLocation} from '@docusaurus/router';
10
- import {isSamePath} from './routesUtils';
11
- import type {BlogSidebarItem} from '@docusaurus/plugin-content-blog';
12
-
13
- function isVisible(item: BlogSidebarItem, pathname: string): boolean {
14
- if (item.unlisted && !isSamePath(item.permalink, pathname)) {
15
- return false;
16
- }
17
- return true;
18
- }
19
-
20
- /**
21
- * Return the visible blog sidebar items to display.
22
- * Unlisted items are filtered.
23
- */
24
- export function useVisibleBlogSidebarItems(
25
- items: BlogSidebarItem[],
26
- ): BlogSidebarItem[] {
27
- const {pathname} = useLocation();
28
- return useMemo(
29
- () => items.filter((item) => isVisible(item, pathname)),
30
- [items, pathname],
31
- );
32
- }