@docusaurus/theme-common 3.3.2 → 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 (103) hide show
  1. package/lib/contexts/announcementBar.d.ts +0 -1
  2. package/lib/contexts/announcementBar.d.ts.map +1 -1
  3. package/lib/contexts/announcementBar.js +4 -4
  4. package/lib/contexts/announcementBar.js.map +1 -1
  5. package/lib/index.d.ts +5 -6
  6. package/lib/index.d.ts.map +1 -1
  7. package/lib/index.js +18 -6
  8. package/lib/index.js.map +1 -1
  9. package/lib/internal.d.ts +2 -9
  10. package/lib/internal.d.ts.map +1 -1
  11. package/lib/internal.js +2 -9
  12. package/lib/internal.js.map +1 -1
  13. package/lib/translations/blogTranslations.d.ts +19 -0
  14. package/lib/translations/blogTranslations.d.ts.map +1 -0
  15. package/lib/translations/blogTranslations.js +48 -0
  16. package/lib/translations/blogTranslations.js.map +1 -0
  17. package/lib/{utils/unlistedUtils.d.ts → translations/contentVisibilityTranslations.d.ts} +3 -2
  18. package/lib/translations/contentVisibilityTranslations.d.ts.map +1 -0
  19. package/lib/translations/contentVisibilityTranslations.js +39 -0
  20. package/lib/translations/contentVisibilityTranslations.js.map +1 -0
  21. package/lib/utils/ThemeClassNames.d.ts +3 -0
  22. package/lib/utils/ThemeClassNames.d.ts.map +1 -1
  23. package/lib/utils/ThemeClassNames.js +3 -0
  24. package/lib/utils/ThemeClassNames.js.map +1 -1
  25. package/lib/utils/errorBoundaryUtils.d.ts +0 -1
  26. package/lib/utils/errorBoundaryUtils.d.ts.map +1 -1
  27. package/lib/utils/jsUtils.d.ts +1 -0
  28. package/lib/utils/jsUtils.d.ts.map +1 -1
  29. package/lib/utils/jsUtils.js +14 -0
  30. package/lib/utils/jsUtils.js.map +1 -1
  31. package/lib/utils/searchUtils.d.ts +0 -13
  32. package/lib/utils/searchUtils.d.ts.map +1 -1
  33. package/lib/utils/searchUtils.js +0 -39
  34. package/lib/utils/searchUtils.js.map +1 -1
  35. package/lib/utils/storageUtils.d.ts +3 -4
  36. package/lib/utils/storageUtils.d.ts.map +1 -1
  37. package/lib/utils/storageUtils.js +7 -3
  38. package/lib/utils/storageUtils.js.map +1 -1
  39. package/lib/utils/tabsUtils.d.ts +2 -2
  40. package/lib/utils/tabsUtils.d.ts.map +1 -1
  41. package/lib/utils/useThemeConfig.d.ts +5 -0
  42. package/lib/utils/useThemeConfig.d.ts.map +1 -1
  43. package/lib/utils/useThemeConfig.js.map +1 -1
  44. package/package.json +9 -11
  45. package/src/contexts/announcementBar.tsx +4 -6
  46. package/src/index.ts +27 -16
  47. package/src/internal.ts +8 -36
  48. package/src/translations/blogTranslations.tsx +79 -0
  49. package/src/{utils/unlistedUtils.tsx → translations/contentVisibilityTranslations.tsx} +25 -2
  50. package/src/types.d.ts +0 -3
  51. package/src/utils/ThemeClassNames.ts +3 -0
  52. package/src/utils/jsUtils.ts +18 -0
  53. package/src/utils/searchUtils.ts +0 -58
  54. package/src/utils/storageUtils.ts +8 -4
  55. package/src/utils/useThemeConfig.ts +6 -0
  56. package/lib/contexts/blogPost.d.ts +0 -33
  57. package/lib/contexts/blogPost.d.ts.map +0 -1
  58. package/lib/contexts/blogPost.js +0 -46
  59. package/lib/contexts/blogPost.js.map +0 -1
  60. package/lib/contexts/doc.d.ts +0 -30
  61. package/lib/contexts/doc.d.ts.map +0 -1
  62. package/lib/contexts/doc.js +0 -48
  63. package/lib/contexts/doc.js.map +0 -1
  64. package/lib/contexts/docSidebarItemsExpandedState.d.ts +0 -31
  65. package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +0 -1
  66. package/lib/contexts/docSidebarItemsExpandedState.js +0 -28
  67. package/lib/contexts/docSidebarItemsExpandedState.js.map +0 -1
  68. package/lib/contexts/docsPreferredVersion.d.ts +0 -30
  69. package/lib/contexts/docsPreferredVersion.d.ts.map +0 -1
  70. package/lib/contexts/docsPreferredVersion.js +0 -130
  71. package/lib/contexts/docsPreferredVersion.js.map +0 -1
  72. package/lib/contexts/docsSidebar.d.ts +0 -26
  73. package/lib/contexts/docsSidebar.d.ts.map +0 -1
  74. package/lib/contexts/docsSidebar.js +0 -30
  75. package/lib/contexts/docsSidebar.js.map +0 -1
  76. package/lib/contexts/docsVersion.d.ts +0 -20
  77. package/lib/contexts/docsVersion.d.ts.map +0 -1
  78. package/lib/contexts/docsVersion.js +0 -26
  79. package/lib/contexts/docsVersion.js.map +0 -1
  80. package/lib/utils/blogUtils.d.ts +0 -13
  81. package/lib/utils/blogUtils.d.ts.map +0 -1
  82. package/lib/utils/blogUtils.js +0 -24
  83. package/lib/utils/blogUtils.js.map +0 -1
  84. package/lib/utils/docsUtils.d.ts +0 -109
  85. package/lib/utils/docsUtils.d.ts.map +0 -1
  86. package/lib/utils/docsUtils.js +0 -276
  87. package/lib/utils/docsUtils.js.map +0 -1
  88. package/lib/utils/structuredDataUtils.d.ts +0 -12
  89. package/lib/utils/structuredDataUtils.d.ts.map +0 -1
  90. package/lib/utils/structuredDataUtils.js +0 -123
  91. package/lib/utils/structuredDataUtils.js.map +0 -1
  92. package/lib/utils/unlistedUtils.d.ts.map +0 -1
  93. package/lib/utils/unlistedUtils.js +0 -26
  94. package/lib/utils/unlistedUtils.js.map +0 -1
  95. package/src/contexts/blogPost.tsx +0 -80
  96. package/src/contexts/doc.tsx +0 -71
  97. package/src/contexts/docSidebarItemsExpandedState.tsx +0 -55
  98. package/src/contexts/docsPreferredVersion.tsx +0 -251
  99. package/src/contexts/docsSidebar.tsx +0 -50
  100. package/src/contexts/docsVersion.tsx +0 -36
  101. package/src/utils/blogUtils.ts +0 -32
  102. package/src/utils/docsUtils.tsx +0 -418
  103. package/src/utils/structuredDataUtils.ts +0 -177
@@ -1,418 +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 {matchPath, useLocation} from '@docusaurus/router';
10
- import renderRoutes from '@docusaurus/renderRoutes';
11
- import {
12
- useAllDocsData,
13
- useActivePlugin,
14
- useActiveDocContext,
15
- useLatestVersion,
16
- type GlobalVersion,
17
- type GlobalSidebar,
18
- type GlobalDoc,
19
- } from '@docusaurus/plugin-content-docs/client';
20
- import type {Props as DocRootProps} from '@theme/DocRoot';
21
- import {useDocsPreferredVersion} from '../contexts/docsPreferredVersion';
22
- import {useDocsVersion} from '../contexts/docsVersion';
23
- import {useDocsSidebar} from '../contexts/docsSidebar';
24
- import {uniq} from './jsUtils';
25
- import {isSamePath} from './routesUtils';
26
- import type {
27
- PropSidebar,
28
- PropSidebarItem,
29
- PropSidebarItemCategory,
30
- PropVersionDoc,
31
- PropSidebarBreadcrumbsItem,
32
- } from '@docusaurus/plugin-content-docs';
33
-
34
- // TODO not ideal, see also "useDocs"
35
- export const isDocsPluginEnabled: boolean = !!useAllDocsData;
36
-
37
- /**
38
- * A null-safe way to access a doc's data by ID in the active version.
39
- */
40
- export function useDocById(id: string): PropVersionDoc;
41
- /**
42
- * A null-safe way to access a doc's data by ID in the active version.
43
- */
44
- export function useDocById(id: string | undefined): PropVersionDoc | undefined;
45
- export function useDocById(id: string | undefined): PropVersionDoc | undefined {
46
- const version = useDocsVersion();
47
- if (!id) {
48
- return undefined;
49
- }
50
- const doc = version.docs[id];
51
- if (!doc) {
52
- throw new Error(`no version doc found by id=${id}`);
53
- }
54
- return doc;
55
- }
56
-
57
- /**
58
- * Pure function, similar to `Array#find`, but works on the sidebar tree.
59
- */
60
- export function findSidebarCategory(
61
- sidebar: PropSidebar,
62
- predicate: (category: PropSidebarItemCategory) => boolean,
63
- ): PropSidebarItemCategory | undefined {
64
- for (const item of sidebar) {
65
- if (item.type === 'category') {
66
- if (predicate(item)) {
67
- return item;
68
- }
69
- const subItem = findSidebarCategory(item.items, predicate);
70
- if (subItem) {
71
- return subItem;
72
- }
73
- }
74
- }
75
- return undefined;
76
- }
77
-
78
- /**
79
- * Best effort to assign a link to a sidebar category. If the category doesn't
80
- * have a link itself, we link to the first sub item with a link.
81
- */
82
- export function findFirstSidebarItemCategoryLink(
83
- item: PropSidebarItemCategory,
84
- ): string | undefined {
85
- if (item.href && !item.linkUnlisted) {
86
- return item.href;
87
- }
88
-
89
- for (const subItem of item.items) {
90
- const link = findFirstSidebarItemLink(subItem);
91
- if (link) {
92
- return link;
93
- }
94
- }
95
- return undefined;
96
- }
97
-
98
- /**
99
- * Best effort to assign a link to a sidebar item.
100
- */
101
- export function findFirstSidebarItemLink(
102
- item: PropSidebarItem,
103
- ): string | undefined {
104
- if (item.type === 'link' && !item.unlisted) {
105
- return item.href;
106
- }
107
- if (item.type === 'category') {
108
- return findFirstSidebarItemCategoryLink(item);
109
- }
110
- // Other items types, like "html"
111
- return undefined;
112
- }
113
-
114
- /**
115
- * Gets the category associated with the current location. Should only be used
116
- * on category index pages.
117
- */
118
- export function useCurrentSidebarCategory(): PropSidebarItemCategory {
119
- const {pathname} = useLocation();
120
- const sidebar = useDocsSidebar();
121
- if (!sidebar) {
122
- throw new Error('Unexpected: cant find current sidebar in context');
123
- }
124
- const categoryBreadcrumbs = getSidebarBreadcrumbs({
125
- sidebarItems: sidebar.items,
126
- pathname,
127
- onlyCategories: true,
128
- });
129
- const deepestCategory = categoryBreadcrumbs.slice(-1)[0];
130
- if (!deepestCategory) {
131
- throw new Error(
132
- `${pathname} is not associated with a category. useCurrentSidebarCategory() should only be used on category index pages.`,
133
- );
134
- }
135
- return deepestCategory;
136
- }
137
-
138
- const isActive = (testedPath: string | undefined, activePath: string) =>
139
- typeof testedPath !== 'undefined' && isSamePath(testedPath, activePath);
140
- const containsActiveSidebarItem = (
141
- items: PropSidebarItem[],
142
- activePath: string,
143
- ) => items.some((subItem) => isActiveSidebarItem(subItem, activePath));
144
-
145
- /**
146
- * Checks if a sidebar item should be active, based on the active path.
147
- */
148
- export function isActiveSidebarItem(
149
- item: PropSidebarItem,
150
- activePath: string,
151
- ): boolean {
152
- if (item.type === 'link') {
153
- return isActive(item.href, activePath);
154
- }
155
-
156
- if (item.type === 'category') {
157
- return (
158
- isActive(item.href, activePath) ||
159
- containsActiveSidebarItem(item.items, activePath)
160
- );
161
- }
162
-
163
- return false;
164
- }
165
-
166
- export function isVisibleSidebarItem(
167
- item: PropSidebarItem,
168
- activePath: string,
169
- ): boolean {
170
- switch (item.type) {
171
- case 'category':
172
- return (
173
- isActiveSidebarItem(item, activePath) ||
174
- item.items.some((subItem) => isVisibleSidebarItem(subItem, activePath))
175
- );
176
- case 'link':
177
- // An unlisted item remains visible if it is active
178
- return !item.unlisted || isActiveSidebarItem(item, activePath);
179
- default:
180
- return true;
181
- }
182
- }
183
-
184
- export function useVisibleSidebarItems(
185
- items: readonly PropSidebarItem[],
186
- activePath: string,
187
- ): PropSidebarItem[] {
188
- return useMemo(
189
- () => items.filter((item) => isVisibleSidebarItem(item, activePath)),
190
- [items, activePath],
191
- );
192
- }
193
-
194
- function getSidebarBreadcrumbs(param: {
195
- sidebarItems: PropSidebar;
196
- pathname: string;
197
- onlyCategories: true;
198
- }): PropSidebarItemCategory[];
199
-
200
- function getSidebarBreadcrumbs(param: {
201
- sidebarItems: PropSidebar;
202
- pathname: string;
203
- }): PropSidebarBreadcrumbsItem[];
204
-
205
- /**
206
- * Get the sidebar the breadcrumbs for a given pathname
207
- * Ordered from top to bottom
208
- */
209
- function getSidebarBreadcrumbs({
210
- sidebarItems,
211
- pathname,
212
- onlyCategories = false,
213
- }: {
214
- sidebarItems: PropSidebar;
215
- pathname: string;
216
- onlyCategories?: boolean;
217
- }): PropSidebarBreadcrumbsItem[] {
218
- const breadcrumbs: PropSidebarBreadcrumbsItem[] = [];
219
-
220
- function extract(items: PropSidebarItem[]) {
221
- for (const item of items) {
222
- if (
223
- (item.type === 'category' &&
224
- (isSamePath(item.href, pathname) || extract(item.items))) ||
225
- (item.type === 'link' && isSamePath(item.href, pathname))
226
- ) {
227
- const filtered = onlyCategories && item.type !== 'category';
228
- if (!filtered) {
229
- breadcrumbs.unshift(item);
230
- }
231
- return true;
232
- }
233
- }
234
-
235
- return false;
236
- }
237
-
238
- extract(sidebarItems);
239
-
240
- return breadcrumbs;
241
- }
242
-
243
- /**
244
- * Gets the breadcrumbs of the current doc page, based on its sidebar location.
245
- * Returns `null` if there's no sidebar or breadcrumbs are disabled.
246
- */
247
- export function useSidebarBreadcrumbs(): PropSidebarBreadcrumbsItem[] | null {
248
- const sidebar = useDocsSidebar();
249
- const {pathname} = useLocation();
250
- const breadcrumbsOption = useActivePlugin()?.pluginData.breadcrumbs;
251
- if (breadcrumbsOption === false || !sidebar) {
252
- return null;
253
- }
254
- return getSidebarBreadcrumbs({sidebarItems: sidebar.items, pathname});
255
- }
256
-
257
- /**
258
- * "Version candidates" are mostly useful for the layout components, which must
259
- * be able to work on all pages. For example, if a user has `{ type: "doc",
260
- * docId: "intro" }` as a navbar item, which version does that refer to? We
261
- * believe that it could refer to at most three version candidates:
262
- *
263
- * 1. The **active version**, the one that the user is currently browsing. See
264
- * {@link useActiveDocContext}.
265
- * 2. The **preferred version**, the one that the user last visited. See
266
- * {@link useDocsPreferredVersion}.
267
- * 3. The **latest version**, the "default". See {@link useLatestVersion}.
268
- *
269
- * @param docsPluginId The plugin ID to get versions from.
270
- * @returns An array of 1~3 versions with priorities defined above, guaranteed
271
- * to be unique and non-sparse. Will be memoized, hence stable for deps array.
272
- */
273
- export function useDocsVersionCandidates(
274
- docsPluginId?: string,
275
- ): [GlobalVersion, ...GlobalVersion[]] {
276
- const {activeVersion} = useActiveDocContext(docsPluginId);
277
- const {preferredVersion} = useDocsPreferredVersion(docsPluginId);
278
- const latestVersion = useLatestVersion(docsPluginId);
279
- return useMemo(
280
- () =>
281
- uniq(
282
- [activeVersion, preferredVersion, latestVersion].filter(Boolean),
283
- ) as [GlobalVersion, ...GlobalVersion[]],
284
- [activeVersion, preferredVersion, latestVersion],
285
- );
286
- }
287
-
288
- /**
289
- * The layout components, like navbar items, must be able to work on all pages,
290
- * even on non-doc ones where there's no version context, so a sidebar ID could
291
- * be ambiguous. This hook would always return a sidebar to be linked to. See
292
- * also {@link useDocsVersionCandidates} for how this selection is done.
293
- *
294
- * @throws This hook throws if a sidebar with said ID is not found.
295
- */
296
- export function useLayoutDocsSidebar(
297
- sidebarId: string,
298
- docsPluginId?: string,
299
- ): GlobalSidebar {
300
- const versions = useDocsVersionCandidates(docsPluginId);
301
- return useMemo(() => {
302
- const allSidebars = versions.flatMap((version) =>
303
- version.sidebars ? Object.entries(version.sidebars) : [],
304
- );
305
- const sidebarEntry = allSidebars.find(
306
- (sidebar) => sidebar[0] === sidebarId,
307
- );
308
- if (!sidebarEntry) {
309
- throw new Error(
310
- `Can't find any sidebar with id "${sidebarId}" in version${
311
- versions.length > 1 ? 's' : ''
312
- } ${versions.map((version) => version.name).join(', ')}".
313
- Available sidebar ids are:
314
- - ${allSidebars.map((entry) => entry[0]).join('\n- ')}`,
315
- );
316
- }
317
- return sidebarEntry[1];
318
- }, [sidebarId, versions]);
319
- }
320
-
321
- /**
322
- * The layout components, like navbar items, must be able to work on all pages,
323
- * even on non-doc ones where there's no version context, so a doc ID could be
324
- * ambiguous. This hook would always return a doc to be linked to. See also
325
- * {@link useDocsVersionCandidates} for how this selection is done.
326
- *
327
- * @throws This hook throws if a doc with said ID is not found.
328
- */
329
- export function useLayoutDoc(
330
- docId: string,
331
- docsPluginId?: string,
332
- ): GlobalDoc | null {
333
- const versions = useDocsVersionCandidates(docsPluginId);
334
- return useMemo(() => {
335
- const allDocs = versions.flatMap((version) => version.docs);
336
- const doc = allDocs.find((versionDoc) => versionDoc.id === docId);
337
- if (!doc) {
338
- const isDraft = versions
339
- .flatMap((version) => version.draftIds)
340
- .includes(docId);
341
- // Drafts should be silently filtered instead of throwing
342
- if (isDraft) {
343
- return null;
344
- }
345
- throw new Error(
346
- `Couldn't find any doc with id "${docId}" in version${
347
- versions.length > 1 ? 's' : ''
348
- } "${versions.map((version) => version.name).join(', ')}".
349
- Available doc ids are:
350
- - ${uniq(allDocs.map((versionDoc) => versionDoc.id)).join('\n- ')}`,
351
- );
352
- }
353
- return doc;
354
- }, [docId, versions]);
355
- }
356
-
357
- // TODO later read version/route directly from context
358
- /**
359
- * The docs plugin creates nested routes, with the top-level route providing the
360
- * version metadata, and the subroutes creating individual doc pages. This hook
361
- * will match the current location against all known sub-routes.
362
- *
363
- * @param props The props received by `@theme/DocRoot`
364
- * @returns The data of the relevant document at the current location, or `null`
365
- * if no document associated with the current location can be found.
366
- */
367
- export function useDocRootMetadata({route}: DocRootProps): null | {
368
- /** The element that should be rendered at the current location. */
369
- docElement: JSX.Element;
370
- /**
371
- * The name of the sidebar associated with the current doc. `sidebarName` and
372
- * `sidebarItems` correspond to the value of {@link useDocsSidebar}.
373
- */
374
- sidebarName: string | undefined;
375
- /** The items of the sidebar associated with the current doc. */
376
- sidebarItems: PropSidebar | undefined;
377
- } {
378
- const location = useLocation();
379
- const versionMetadata = useDocsVersion();
380
- const docRoutes = route.routes!;
381
- const currentDocRoute = docRoutes.find((docRoute) =>
382
- matchPath(location.pathname, docRoute),
383
- );
384
- if (!currentDocRoute) {
385
- return null;
386
- }
387
-
388
- // For now, the sidebarName is added as route config: not ideal!
389
- const sidebarName = currentDocRoute.sidebar as string;
390
-
391
- const sidebarItems = sidebarName
392
- ? versionMetadata.docsSidebars[sidebarName]
393
- : undefined;
394
-
395
- const docElement = renderRoutes(docRoutes);
396
-
397
- return {
398
- docElement,
399
- sidebarName,
400
- sidebarItems,
401
- };
402
- }
403
-
404
- /**
405
- * Filter items we don't want to display on the doc card list view
406
- * @param items
407
- */
408
- export function filterDocCardListItems(
409
- items: PropSidebarItem[],
410
- ): PropSidebarItem[] {
411
- return items.filter((item) => {
412
- const canHaveLink = item.type === 'category' || item.type === 'link';
413
- if (canHaveLink) {
414
- return !!findFirstSidebarItemLink(item);
415
- }
416
- return true;
417
- });
418
- }
@@ -1,177 +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 {useBaseUrlUtils, type BaseUrlUtils} from '@docusaurus/useBaseUrl';
9
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
10
- import {useBlogMetadata} from '@docusaurus/plugin-content-blog/client';
11
- import type {Props as BlogListPageStructuredDataProps} from '@theme/BlogListPage/StructuredData';
12
- import {useBlogPost} from '../contexts/blogPost';
13
- import type {
14
- Blog,
15
- BlogPosting,
16
- WithContext,
17
- Person,
18
- ImageObject,
19
- } from 'schema-dts';
20
- import type {
21
- Author,
22
- PropBlogPostContent,
23
- } from '@docusaurus/plugin-content-blog';
24
- import type {DocusaurusConfig} from '@docusaurus/types';
25
-
26
- const convertDate = (dateMs: number) => new Date(dateMs).toISOString();
27
-
28
- function getBlogPost(
29
- blogPostContent: PropBlogPostContent,
30
- siteConfig: DocusaurusConfig,
31
- withBaseUrl: BaseUrlUtils['withBaseUrl'],
32
- ): BlogPosting {
33
- const {assets, frontMatter, metadata} = blogPostContent;
34
- const {date, title, description, lastUpdatedAt} = metadata;
35
-
36
- const image = assets.image ?? frontMatter.image;
37
- const keywords = frontMatter.keywords ?? [];
38
-
39
- const blogUrl = `${siteConfig.url}${metadata.permalink}`;
40
-
41
- const dateModified = lastUpdatedAt ? convertDate(lastUpdatedAt) : undefined;
42
-
43
- return {
44
- '@type': 'BlogPosting',
45
- '@id': blogUrl,
46
- mainEntityOfPage: blogUrl,
47
- url: blogUrl,
48
- headline: title,
49
- name: title,
50
- description,
51
- datePublished: date,
52
- ...(dateModified ? {dateModified} : {}),
53
- ...getAuthor(metadata.authors),
54
- ...getImage(image, withBaseUrl, title),
55
- ...(keywords ? {keywords} : {}),
56
- };
57
- }
58
-
59
- function getAuthor(authors: Author[]) {
60
- const authorsStructuredData = authors.map(createPersonStructuredData);
61
- return {
62
- author:
63
- authorsStructuredData.length === 1
64
- ? authorsStructuredData[0]
65
- : authorsStructuredData,
66
- };
67
- }
68
-
69
- function getImage(
70
- image: string | undefined,
71
- withBaseUrl: BaseUrlUtils['withBaseUrl'],
72
- title: string,
73
- ) {
74
- return image
75
- ? {
76
- image: createImageStructuredData({
77
- imageUrl: withBaseUrl(image, {absolute: true}),
78
- caption: `title image for the blog post: ${title}`,
79
- }),
80
- }
81
- : {};
82
- }
83
-
84
- export function useBlogListPageStructuredData(
85
- props: BlogListPageStructuredDataProps,
86
- ): WithContext<Blog> {
87
- const {siteConfig} = useDocusaurusContext();
88
- const {withBaseUrl} = useBaseUrlUtils();
89
-
90
- const {
91
- metadata: {blogDescription, blogTitle, permalink},
92
- } = props;
93
-
94
- const url = `${siteConfig.url}${permalink}`;
95
-
96
- // details on structured data support: https://schema.org/Blog
97
- return {
98
- '@context': 'https://schema.org',
99
- '@type': 'Blog',
100
- '@id': url,
101
- mainEntityOfPage: url,
102
- headline: blogTitle,
103
- description: blogDescription,
104
- blogPost: props.items.map((blogItem) =>
105
- getBlogPost(blogItem.content, siteConfig, withBaseUrl),
106
- ),
107
- };
108
- }
109
-
110
- export function useBlogPostStructuredData(): WithContext<BlogPosting> {
111
- const blogMetadata = useBlogMetadata();
112
- const {assets, metadata} = useBlogPost();
113
- const {siteConfig} = useDocusaurusContext();
114
- const {withBaseUrl} = useBaseUrlUtils();
115
-
116
- const {date, title, description, frontMatter, lastUpdatedAt} = metadata;
117
-
118
- const image = assets.image ?? frontMatter.image;
119
- const keywords = frontMatter.keywords ?? [];
120
-
121
- const dateModified = lastUpdatedAt ? convertDate(lastUpdatedAt) : undefined;
122
-
123
- const url = `${siteConfig.url}${metadata.permalink}`;
124
-
125
- // details on structured data support: https://schema.org/BlogPosting
126
- // BlogPosting is one of the structured data types that Google explicitly
127
- // supports: https://developers.google.com/search/docs/appearance/structured-data/article#structured-data-type-definitions
128
- return {
129
- '@context': 'https://schema.org',
130
- '@type': 'BlogPosting',
131
- '@id': url,
132
- mainEntityOfPage: url,
133
- url,
134
- headline: title,
135
- name: title,
136
- description,
137
- datePublished: date,
138
- ...(dateModified ? {dateModified} : {}),
139
- ...getAuthor(metadata.authors),
140
- ...getImage(image, withBaseUrl, title),
141
- ...(keywords ? {keywords} : {}),
142
- isPartOf: {
143
- '@type': 'Blog',
144
- '@id': `${siteConfig.url}${blogMetadata.blogBasePath}`,
145
- name: blogMetadata.blogTitle,
146
- },
147
- };
148
- }
149
-
150
- /** @returns A {@link https://schema.org/Person} constructed from the {@link Author} */
151
- function createPersonStructuredData(author: Author): Person {
152
- return {
153
- '@type': 'Person',
154
- ...(author.name ? {name: author.name} : {}),
155
- ...(author.title ? {description: author.title} : {}),
156
- ...(author.url ? {url: author.url} : {}),
157
- ...(author.email ? {email: author.email} : {}),
158
- ...(author.imageURL ? {image: author.imageURL} : {}),
159
- };
160
- }
161
-
162
- /** @returns A {@link https://schema.org/ImageObject} */
163
- function createImageStructuredData({
164
- imageUrl,
165
- caption,
166
- }: {
167
- imageUrl: string;
168
- caption: string;
169
- }): ImageObject {
170
- return {
171
- '@type': 'ImageObject',
172
- '@id': imageUrl,
173
- url: imageUrl,
174
- contentUrl: imageUrl,
175
- caption,
176
- };
177
- }