@docusaurus/plugin-content-blog 0.0.0-5995 → 0.0.0-6000

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.
@@ -0,0 +1,33 @@
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
+ import { type ReactNode } from 'react';
8
+ import type { PropBlogPostContent, BlogMetadata } from '@docusaurus/plugin-content-blog';
9
+ export declare function useBlogMetadata(): BlogMetadata;
10
+ /**
11
+ * The React context value returned by the `useBlogPost()` hook.
12
+ * It contains useful data related to the currently browsed blog post.
13
+ */
14
+ export type BlogPostContextValue = Pick<PropBlogPostContent, 'metadata' | 'frontMatter' | 'assets' | 'toc'> & {
15
+ readonly isBlogPostPage: boolean;
16
+ };
17
+ /**
18
+ * This is a very thin layer around the `content` received from the MDX loader.
19
+ * It provides metadata about the blog post to the children tree.
20
+ */
21
+ export declare function BlogPostProvider({ children, content, isBlogPostPage, }: {
22
+ children: ReactNode;
23
+ content: PropBlogPostContent;
24
+ isBlogPostPage?: boolean;
25
+ }): JSX.Element;
26
+ /**
27
+ * Returns the data of the currently browsed blog post. Gives access to
28
+ * front matter, metadata, TOC, etc.
29
+ * When swizzling a low-level component (e.g. the "Edit this page" link)
30
+ * and you need some extra metadata, you don't have to drill the props
31
+ * all the way through the component tree: simply use this hook instead.
32
+ */
33
+ export declare function useBlogPost(): BlogPostContextValue;
@@ -0,0 +1,54 @@
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
+ import React, { useMemo, useContext } from 'react';
8
+ import { ReactContextError } from '@docusaurus/theme-common/internal';
9
+ import useRouteContext from '@docusaurus/useRouteContext';
10
+ export function useBlogMetadata() {
11
+ const routeContext = useRouteContext();
12
+ const blogMetadata = routeContext?.data?.blogMetadata;
13
+ if (!blogMetadata) {
14
+ throw new Error("useBlogMetadata() can't be called on the current route because the blog metadata could not be found in route context");
15
+ }
16
+ return blogMetadata;
17
+ }
18
+ const Context = React.createContext(null);
19
+ /**
20
+ * Note: we don't use `PropBlogPostContent` as context value on purpose.
21
+ * Metadata is currently stored inside the MDX component, but we may want to
22
+ * change that in the future.
23
+ */
24
+ function useContextValue({ content, isBlogPostPage, }) {
25
+ return useMemo(() => ({
26
+ metadata: content.metadata,
27
+ frontMatter: content.frontMatter,
28
+ assets: content.assets,
29
+ toc: content.toc,
30
+ isBlogPostPage,
31
+ }), [content, isBlogPostPage]);
32
+ }
33
+ /**
34
+ * This is a very thin layer around the `content` received from the MDX loader.
35
+ * It provides metadata about the blog post to the children tree.
36
+ */
37
+ export function BlogPostProvider({ children, content, isBlogPostPage = false, }) {
38
+ const contextValue = useContextValue({ content, isBlogPostPage });
39
+ return <Context.Provider value={contextValue}>{children}</Context.Provider>;
40
+ }
41
+ /**
42
+ * Returns the data of the currently browsed blog post. Gives access to
43
+ * front matter, metadata, TOC, etc.
44
+ * When swizzling a low-level component (e.g. the "Edit this page" link)
45
+ * and you need some extra metadata, you don't have to drill the props
46
+ * all the way through the component tree: simply use this hook instead.
47
+ */
48
+ export function useBlogPost() {
49
+ const blogPost = useContext(Context);
50
+ if (blogPost === null) {
51
+ throw new ReactContextError('BlogPostProvider');
52
+ }
53
+ return blogPost;
54
+ }
@@ -4,5 +4,6 @@
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
- import type { BlogMetadata } from '@docusaurus/plugin-content-blog';
8
- export declare function useBlogMetadata(): BlogMetadata;
7
+ export { BlogPostProvider, type BlogPostContextValue, useBlogPost, useBlogMetadata, } from './contexts';
8
+ export { useBlogListPageStructuredData, useBlogPostStructuredData, } from './structuredDataUtils';
9
+ export { BlogSidebarItemList, groupBlogSidebarItemsByYear, useVisibleBlogSidebarItems, } from './sidebarUtils';
@@ -4,12 +4,6 @@
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
- import useRouteContext from '@docusaurus/useRouteContext';
8
- export function useBlogMetadata() {
9
- const routeContext = useRouteContext();
10
- const blogMetadata = routeContext?.data?.blogMetadata;
11
- if (!blogMetadata) {
12
- throw new Error("useBlogMetadata() can't be called on the current route because the blog metadata could not be found in route context");
13
- }
14
- return blogMetadata;
15
- }
7
+ export { BlogPostProvider, useBlogPost, useBlogMetadata, } from './contexts';
8
+ export { useBlogListPageStructuredData, useBlogPostStructuredData, } from './structuredDataUtils';
9
+ export { BlogSidebarItemList, groupBlogSidebarItemsByYear, useVisibleBlogSidebarItems, } from './sidebarUtils';
@@ -0,0 +1,21 @@
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
+ import { type ReactNode } from 'react';
8
+ import type { BlogSidebarItem } from '@docusaurus/plugin-content-blog';
9
+ /**
10
+ * Return the visible blog sidebar items to display.
11
+ * Unlisted items are filtered.
12
+ */
13
+ export declare function useVisibleBlogSidebarItems(items: BlogSidebarItem[]): BlogSidebarItem[];
14
+ export declare function groupBlogSidebarItemsByYear(items: BlogSidebarItem[]): [string, BlogSidebarItem[]][];
15
+ export declare function BlogSidebarItemList({ items, ulClassName, liClassName, linkClassName, linkActiveClassName, }: {
16
+ items: BlogSidebarItem[];
17
+ ulClassName?: string;
18
+ liClassName?: string;
19
+ linkClassName?: string;
20
+ linkActiveClassName?: string;
21
+ }): ReactNode;
@@ -0,0 +1,49 @@
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
+ import React, { useMemo } from 'react';
8
+ import { useLocation } from '@docusaurus/router';
9
+ import Link from '@docusaurus/Link';
10
+ import { groupBy } from '@docusaurus/theme-common';
11
+ import { isSamePath } from '@docusaurus/theme-common/internal';
12
+ function isVisible(item, pathname) {
13
+ if (item.unlisted && !isSamePath(item.permalink, pathname)) {
14
+ return false;
15
+ }
16
+ return true;
17
+ }
18
+ /**
19
+ * Return the visible blog sidebar items to display.
20
+ * Unlisted items are filtered.
21
+ */
22
+ export function useVisibleBlogSidebarItems(items) {
23
+ const { pathname } = useLocation();
24
+ return useMemo(() => items.filter((item) => isVisible(item, pathname)), [items, pathname]);
25
+ }
26
+ export function groupBlogSidebarItemsByYear(items) {
27
+ const groupedByYear = groupBy(items, (item) => {
28
+ return `${new Date(item.date).getFullYear()}`;
29
+ });
30
+ // "as" is safe here
31
+ // see https://github.com/microsoft/TypeScript/pull/56805#issuecomment-2196526425
32
+ const entries = Object.entries(groupedByYear);
33
+ // We have to use entries because of https://x.com/sebastienlorber/status/1806371668614369486
34
+ // Objects with string/number keys are automatically sorted asc...
35
+ // Even if keys are strings like "2024"
36
+ // We want descending order for years
37
+ // Alternative: using Map.groupBy (not affected by this "reordering")
38
+ entries.reverse();
39
+ return entries;
40
+ }
41
+ export function BlogSidebarItemList({ items, ulClassName, liClassName, linkClassName, linkActiveClassName, }) {
42
+ return (<ul className={ulClassName}>
43
+ {items.map((item) => (<li key={item.permalink} className={liClassName}>
44
+ <Link isNavLink to={item.permalink} className={linkClassName} activeClassName={linkActiveClassName}>
45
+ {item.title}
46
+ </Link>
47
+ </li>))}
48
+ </ul>);
49
+ }
@@ -0,0 +1,7 @@
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
+ export {};
@@ -0,0 +1,43 @@
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
+ import { groupBlogSidebarItemsByYear } from './sidebarUtils';
8
+ describe('groupBlogSidebarItemsByYear', () => {
9
+ const post1 = {
10
+ title: 'post1',
11
+ permalink: '/post1',
12
+ date: '2024-10-03',
13
+ unlisted: false,
14
+ };
15
+ const post2 = {
16
+ title: 'post2',
17
+ permalink: '/post2',
18
+ date: '2024-05-02',
19
+ unlisted: false,
20
+ };
21
+ const post3 = {
22
+ title: 'post3',
23
+ permalink: '/post3',
24
+ date: '2022-11-18',
25
+ unlisted: false,
26
+ };
27
+ it('can group items by year', () => {
28
+ const items = [post1, post2, post3];
29
+ const entries = groupBlogSidebarItemsByYear(items);
30
+ expect(entries).toEqual([
31
+ ['2024', [post1, post2]],
32
+ ['2022', [post3]],
33
+ ]);
34
+ });
35
+ it('always returns result in descending chronological order', () => {
36
+ const items = [post3, post1, post2];
37
+ const entries = groupBlogSidebarItemsByYear(items);
38
+ expect(entries).toEqual([
39
+ ['2024', [post1, post2]],
40
+ ['2022', [post3]],
41
+ ]);
42
+ });
43
+ });
@@ -0,0 +1,10 @@
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
+ import type { Props as BlogListPageStructuredDataProps } from '@theme/BlogListPage/StructuredData';
8
+ import type { Blog, BlogPosting, WithContext } from 'schema-dts';
9
+ export declare function useBlogListPageStructuredData(props: BlogListPageStructuredDataProps): WithContext<Blog>;
10
+ export declare function useBlogPostStructuredData(): WithContext<BlogPosting>;
@@ -0,0 +1,122 @@
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
+ import { useBaseUrlUtils } from '@docusaurus/useBaseUrl';
8
+ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
9
+ import { useBlogMetadata } from '@docusaurus/plugin-content-blog/client';
10
+ import { useBlogPost } from './contexts';
11
+ const convertDate = (dateMs) => new Date(dateMs).toISOString();
12
+ function getBlogPost(blogPostContent, siteConfig, withBaseUrl) {
13
+ const { assets, frontMatter, metadata } = blogPostContent;
14
+ const { date, title, description, lastUpdatedAt } = metadata;
15
+ const image = assets.image ?? frontMatter.image;
16
+ const keywords = frontMatter.keywords ?? [];
17
+ const blogUrl = `${siteConfig.url}${metadata.permalink}`;
18
+ const dateModified = lastUpdatedAt ? convertDate(lastUpdatedAt) : undefined;
19
+ return {
20
+ '@type': 'BlogPosting',
21
+ '@id': blogUrl,
22
+ mainEntityOfPage: blogUrl,
23
+ url: blogUrl,
24
+ headline: title,
25
+ name: title,
26
+ description,
27
+ datePublished: date,
28
+ ...(dateModified ? { dateModified } : {}),
29
+ ...getAuthor(metadata.authors),
30
+ ...getImage(image, withBaseUrl, title),
31
+ ...(keywords ? { keywords } : {}),
32
+ };
33
+ }
34
+ function getAuthor(authors) {
35
+ const authorsStructuredData = authors.map(createPersonStructuredData);
36
+ return {
37
+ author: authorsStructuredData.length === 1
38
+ ? authorsStructuredData[0]
39
+ : authorsStructuredData,
40
+ };
41
+ }
42
+ function getImage(image, withBaseUrl, title) {
43
+ return image
44
+ ? {
45
+ image: createImageStructuredData({
46
+ imageUrl: withBaseUrl(image, { absolute: true }),
47
+ caption: `title image for the blog post: ${title}`,
48
+ }),
49
+ }
50
+ : {};
51
+ }
52
+ export function useBlogListPageStructuredData(props) {
53
+ const { siteConfig } = useDocusaurusContext();
54
+ const { withBaseUrl } = useBaseUrlUtils();
55
+ const { metadata: { blogDescription, blogTitle, permalink }, } = props;
56
+ const url = `${siteConfig.url}${permalink}`;
57
+ // details on structured data support: https://schema.org/Blog
58
+ return {
59
+ '@context': 'https://schema.org',
60
+ '@type': 'Blog',
61
+ '@id': url,
62
+ mainEntityOfPage: url,
63
+ headline: blogTitle,
64
+ description: blogDescription,
65
+ blogPost: props.items.map((blogItem) => getBlogPost(blogItem.content, siteConfig, withBaseUrl)),
66
+ };
67
+ }
68
+ export function useBlogPostStructuredData() {
69
+ const blogMetadata = useBlogMetadata();
70
+ const { assets, metadata } = useBlogPost();
71
+ const { siteConfig } = useDocusaurusContext();
72
+ const { withBaseUrl } = useBaseUrlUtils();
73
+ const { date, title, description, frontMatter, lastUpdatedAt } = metadata;
74
+ const image = assets.image ?? frontMatter.image;
75
+ const keywords = frontMatter.keywords ?? [];
76
+ const dateModified = lastUpdatedAt ? convertDate(lastUpdatedAt) : undefined;
77
+ const url = `${siteConfig.url}${metadata.permalink}`;
78
+ // details on structured data support: https://schema.org/BlogPosting
79
+ // BlogPosting is one of the structured data types that Google explicitly
80
+ // supports: https://developers.google.com/search/docs/appearance/structured-data/article#structured-data-type-definitions
81
+ return {
82
+ '@context': 'https://schema.org',
83
+ '@type': 'BlogPosting',
84
+ '@id': url,
85
+ mainEntityOfPage: url,
86
+ url,
87
+ headline: title,
88
+ name: title,
89
+ description,
90
+ datePublished: date,
91
+ ...(dateModified ? { dateModified } : {}),
92
+ ...getAuthor(metadata.authors),
93
+ ...getImage(image, withBaseUrl, title),
94
+ ...(keywords ? { keywords } : {}),
95
+ isPartOf: {
96
+ '@type': 'Blog',
97
+ '@id': `${siteConfig.url}${blogMetadata.blogBasePath}`,
98
+ name: blogMetadata.blogTitle,
99
+ },
100
+ };
101
+ }
102
+ /** @returns A {@link https://schema.org/Person} constructed from the {@link Author} */
103
+ function createPersonStructuredData(author) {
104
+ return {
105
+ '@type': 'Person',
106
+ ...(author.name ? { name: author.name } : {}),
107
+ ...(author.title ? { description: author.title } : {}),
108
+ ...(author.url ? { url: author.url } : {}),
109
+ ...(author.email ? { email: author.email } : {}),
110
+ ...(author.imageURL ? { image: author.imageURL } : {}),
111
+ };
112
+ }
113
+ /** @returns A {@link https://schema.org/ImageObject} */
114
+ function createImageStructuredData({ imageUrl, caption, }) {
115
+ return {
116
+ '@type': 'ImageObject',
117
+ '@id': imageUrl,
118
+ url: imageUrl,
119
+ contentUrl: imageUrl,
120
+ caption,
121
+ };
122
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-content-blog",
3
- "version": "0.0.0-5995",
3
+ "version": "0.0.0-6000",
4
4
  "description": "Blog plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/plugin-content-blog.d.ts",
@@ -31,13 +31,14 @@
31
31
  },
32
32
  "license": "MIT",
33
33
  "dependencies": {
34
- "@docusaurus/core": "0.0.0-5995",
35
- "@docusaurus/logger": "0.0.0-5995",
36
- "@docusaurus/mdx-loader": "0.0.0-5995",
37
- "@docusaurus/types": "0.0.0-5995",
38
- "@docusaurus/utils": "0.0.0-5995",
39
- "@docusaurus/utils-common": "0.0.0-5995",
40
- "@docusaurus/utils-validation": "0.0.0-5995",
34
+ "@docusaurus/core": "0.0.0-6000",
35
+ "@docusaurus/logger": "0.0.0-6000",
36
+ "@docusaurus/mdx-loader": "0.0.0-6000",
37
+ "@docusaurus/theme-common": "0.0.0-6000",
38
+ "@docusaurus/types": "0.0.0-6000",
39
+ "@docusaurus/utils": "0.0.0-6000",
40
+ "@docusaurus/utils-common": "0.0.0-6000",
41
+ "@docusaurus/utils-validation": "0.0.0-6000",
41
42
  "cheerio": "^1.0.0-rc.12",
42
43
  "feed": "^4.2.2",
43
44
  "fs-extra": "^11.1.1",
@@ -59,5 +60,5 @@
59
60
  "devDependencies": {
60
61
  "@total-typescript/shoehorn": "^0.1.2"
61
62
  },
62
- "gitHead": "c12bf10b9832a94916891f384814d62c4f49fd34"
63
+ "gitHead": "10f44ae468ae6751fa53eb8af6441265f0e5075c"
63
64
  }
@@ -0,0 +1,95 @@
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, type ReactNode, useContext} from 'react';
9
+ import {ReactContextError} from '@docusaurus/theme-common/internal';
10
+ import useRouteContext from '@docusaurus/useRouteContext';
11
+
12
+ import type {
13
+ PropBlogPostContent,
14
+ BlogMetadata,
15
+ } from '@docusaurus/plugin-content-blog';
16
+
17
+ export function useBlogMetadata(): BlogMetadata {
18
+ const routeContext = useRouteContext();
19
+ const blogMetadata = routeContext?.data?.blogMetadata;
20
+ if (!blogMetadata) {
21
+ throw new Error(
22
+ "useBlogMetadata() can't be called on the current route because the blog metadata could not be found in route context",
23
+ );
24
+ }
25
+ return blogMetadata as BlogMetadata;
26
+ }
27
+
28
+ /**
29
+ * The React context value returned by the `useBlogPost()` hook.
30
+ * It contains useful data related to the currently browsed blog post.
31
+ */
32
+ export type BlogPostContextValue = Pick<
33
+ PropBlogPostContent,
34
+ 'metadata' | 'frontMatter' | 'assets' | 'toc'
35
+ > & {
36
+ readonly isBlogPostPage: boolean;
37
+ };
38
+
39
+ const Context = React.createContext<BlogPostContextValue | null>(null);
40
+
41
+ /**
42
+ * Note: we don't use `PropBlogPostContent` as context value on purpose.
43
+ * Metadata is currently stored inside the MDX component, but we may want to
44
+ * change that in the future.
45
+ */
46
+ function useContextValue({
47
+ content,
48
+ isBlogPostPage,
49
+ }: {
50
+ content: PropBlogPostContent;
51
+ isBlogPostPage: boolean;
52
+ }): BlogPostContextValue {
53
+ return useMemo(
54
+ () => ({
55
+ metadata: content.metadata,
56
+ frontMatter: content.frontMatter,
57
+ assets: content.assets,
58
+ toc: content.toc,
59
+ isBlogPostPage,
60
+ }),
61
+ [content, isBlogPostPage],
62
+ );
63
+ }
64
+
65
+ /**
66
+ * This is a very thin layer around the `content` received from the MDX loader.
67
+ * It provides metadata about the blog post to the children tree.
68
+ */
69
+ export function BlogPostProvider({
70
+ children,
71
+ content,
72
+ isBlogPostPage = false,
73
+ }: {
74
+ children: ReactNode;
75
+ content: PropBlogPostContent;
76
+ isBlogPostPage?: boolean;
77
+ }): JSX.Element {
78
+ const contextValue = useContextValue({content, isBlogPostPage});
79
+ return <Context.Provider value={contextValue}>{children}</Context.Provider>;
80
+ }
81
+
82
+ /**
83
+ * Returns the data of the currently browsed blog post. Gives access to
84
+ * front matter, metadata, TOC, etc.
85
+ * When swizzling a low-level component (e.g. the "Edit this page" link)
86
+ * and you need some extra metadata, you don't have to drill the props
87
+ * all the way through the component tree: simply use this hook instead.
88
+ */
89
+ export function useBlogPost(): BlogPostContextValue {
90
+ const blogPost = useContext(Context);
91
+ if (blogPost === null) {
92
+ throw new ReactContextError('BlogPostProvider');
93
+ }
94
+ return blogPost;
95
+ }
@@ -0,0 +1,24 @@
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
+ export {
9
+ BlogPostProvider,
10
+ type BlogPostContextValue,
11
+ useBlogPost,
12
+ useBlogMetadata,
13
+ } from './contexts';
14
+
15
+ export {
16
+ useBlogListPageStructuredData,
17
+ useBlogPostStructuredData,
18
+ } from './structuredDataUtils';
19
+
20
+ export {
21
+ BlogSidebarItemList,
22
+ groupBlogSidebarItemsByYear,
23
+ useVisibleBlogSidebarItems,
24
+ } from './sidebarUtils';
@@ -0,0 +1,52 @@
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 {groupBlogSidebarItemsByYear} from './sidebarUtils';
9
+ import type {BlogSidebarItem} from '@docusaurus/plugin-content-blog';
10
+
11
+ describe('groupBlogSidebarItemsByYear', () => {
12
+ const post1: BlogSidebarItem = {
13
+ title: 'post1',
14
+ permalink: '/post1',
15
+ date: '2024-10-03',
16
+ unlisted: false,
17
+ };
18
+
19
+ const post2: BlogSidebarItem = {
20
+ title: 'post2',
21
+ permalink: '/post2',
22
+ date: '2024-05-02',
23
+ unlisted: false,
24
+ };
25
+
26
+ const post3: BlogSidebarItem = {
27
+ title: 'post3',
28
+ permalink: '/post3',
29
+ date: '2022-11-18',
30
+ unlisted: false,
31
+ };
32
+
33
+ it('can group items by year', () => {
34
+ const items: BlogSidebarItem[] = [post1, post2, post3];
35
+ const entries = groupBlogSidebarItemsByYear(items);
36
+
37
+ expect(entries).toEqual([
38
+ ['2024', [post1, post2]],
39
+ ['2022', [post3]],
40
+ ]);
41
+ });
42
+
43
+ it('always returns result in descending chronological order', () => {
44
+ const items: BlogSidebarItem[] = [post3, post1, post2];
45
+ const entries = groupBlogSidebarItemsByYear(items);
46
+
47
+ expect(entries).toEqual([
48
+ ['2024', [post1, post2]],
49
+ ['2022', [post3]],
50
+ ]);
51
+ });
52
+ });
@@ -0,0 +1,85 @@
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} from 'react';
9
+ import {useLocation} from '@docusaurus/router';
10
+ import Link from '@docusaurus/Link';
11
+ import {groupBy} from '@docusaurus/theme-common';
12
+ import {isSamePath} from '@docusaurus/theme-common/internal';
13
+ import type {BlogSidebarItem} from '@docusaurus/plugin-content-blog';
14
+
15
+ function isVisible(item: BlogSidebarItem, pathname: string): boolean {
16
+ if (item.unlisted && !isSamePath(item.permalink, pathname)) {
17
+ return false;
18
+ }
19
+ return true;
20
+ }
21
+
22
+ /**
23
+ * Return the visible blog sidebar items to display.
24
+ * Unlisted items are filtered.
25
+ */
26
+ export function useVisibleBlogSidebarItems(
27
+ items: BlogSidebarItem[],
28
+ ): BlogSidebarItem[] {
29
+ const {pathname} = useLocation();
30
+ return useMemo(
31
+ () => items.filter((item) => isVisible(item, pathname)),
32
+ [items, pathname],
33
+ );
34
+ }
35
+
36
+ export function groupBlogSidebarItemsByYear(
37
+ items: BlogSidebarItem[],
38
+ ): [string, BlogSidebarItem[]][] {
39
+ const groupedByYear = groupBy(items, (item) => {
40
+ return `${new Date(item.date).getFullYear()}`;
41
+ });
42
+ // "as" is safe here
43
+ // see https://github.com/microsoft/TypeScript/pull/56805#issuecomment-2196526425
44
+ const entries = Object.entries(groupedByYear) as [
45
+ string,
46
+ BlogSidebarItem[],
47
+ ][];
48
+ // We have to use entries because of https://x.com/sebastienlorber/status/1806371668614369486
49
+ // Objects with string/number keys are automatically sorted asc...
50
+ // Even if keys are strings like "2024"
51
+ // We want descending order for years
52
+ // Alternative: using Map.groupBy (not affected by this "reordering")
53
+ entries.reverse();
54
+ return entries;
55
+ }
56
+
57
+ export function BlogSidebarItemList({
58
+ items,
59
+ ulClassName,
60
+ liClassName,
61
+ linkClassName,
62
+ linkActiveClassName,
63
+ }: {
64
+ items: BlogSidebarItem[];
65
+ ulClassName?: string;
66
+ liClassName?: string;
67
+ linkClassName?: string;
68
+ linkActiveClassName?: string;
69
+ }): ReactNode {
70
+ return (
71
+ <ul className={ulClassName}>
72
+ {items.map((item) => (
73
+ <li key={item.permalink} className={liClassName}>
74
+ <Link
75
+ isNavLink
76
+ to={item.permalink}
77
+ className={linkClassName}
78
+ activeClassName={linkActiveClassName}>
79
+ {item.title}
80
+ </Link>
81
+ </li>
82
+ ))}
83
+ </ul>
84
+ );
85
+ }
@@ -0,0 +1,178 @@
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';
13
+
14
+ import type {
15
+ Blog,
16
+ BlogPosting,
17
+ WithContext,
18
+ Person,
19
+ ImageObject,
20
+ } from 'schema-dts';
21
+ import type {
22
+ Author,
23
+ PropBlogPostContent,
24
+ } from '@docusaurus/plugin-content-blog';
25
+ import type {DocusaurusConfig} from '@docusaurus/types';
26
+
27
+ const convertDate = (dateMs: number) => new Date(dateMs).toISOString();
28
+
29
+ function getBlogPost(
30
+ blogPostContent: PropBlogPostContent,
31
+ siteConfig: DocusaurusConfig,
32
+ withBaseUrl: BaseUrlUtils['withBaseUrl'],
33
+ ): BlogPosting {
34
+ const {assets, frontMatter, metadata} = blogPostContent;
35
+ const {date, title, description, lastUpdatedAt} = metadata;
36
+
37
+ const image = assets.image ?? frontMatter.image;
38
+ const keywords = frontMatter.keywords ?? [];
39
+
40
+ const blogUrl = `${siteConfig.url}${metadata.permalink}`;
41
+
42
+ const dateModified = lastUpdatedAt ? convertDate(lastUpdatedAt) : undefined;
43
+
44
+ return {
45
+ '@type': 'BlogPosting',
46
+ '@id': blogUrl,
47
+ mainEntityOfPage: blogUrl,
48
+ url: blogUrl,
49
+ headline: title,
50
+ name: title,
51
+ description,
52
+ datePublished: date,
53
+ ...(dateModified ? {dateModified} : {}),
54
+ ...getAuthor(metadata.authors),
55
+ ...getImage(image, withBaseUrl, title),
56
+ ...(keywords ? {keywords} : {}),
57
+ };
58
+ }
59
+
60
+ function getAuthor(authors: Author[]) {
61
+ const authorsStructuredData = authors.map(createPersonStructuredData);
62
+ return {
63
+ author:
64
+ authorsStructuredData.length === 1
65
+ ? authorsStructuredData[0]
66
+ : authorsStructuredData,
67
+ };
68
+ }
69
+
70
+ function getImage(
71
+ image: string | undefined,
72
+ withBaseUrl: BaseUrlUtils['withBaseUrl'],
73
+ title: string,
74
+ ) {
75
+ return image
76
+ ? {
77
+ image: createImageStructuredData({
78
+ imageUrl: withBaseUrl(image, {absolute: true}),
79
+ caption: `title image for the blog post: ${title}`,
80
+ }),
81
+ }
82
+ : {};
83
+ }
84
+
85
+ export function useBlogListPageStructuredData(
86
+ props: BlogListPageStructuredDataProps,
87
+ ): WithContext<Blog> {
88
+ const {siteConfig} = useDocusaurusContext();
89
+ const {withBaseUrl} = useBaseUrlUtils();
90
+
91
+ const {
92
+ metadata: {blogDescription, blogTitle, permalink},
93
+ } = props;
94
+
95
+ const url = `${siteConfig.url}${permalink}`;
96
+
97
+ // details on structured data support: https://schema.org/Blog
98
+ return {
99
+ '@context': 'https://schema.org',
100
+ '@type': 'Blog',
101
+ '@id': url,
102
+ mainEntityOfPage: url,
103
+ headline: blogTitle,
104
+ description: blogDescription,
105
+ blogPost: props.items.map((blogItem) =>
106
+ getBlogPost(blogItem.content, siteConfig, withBaseUrl),
107
+ ),
108
+ };
109
+ }
110
+
111
+ export function useBlogPostStructuredData(): WithContext<BlogPosting> {
112
+ const blogMetadata = useBlogMetadata();
113
+ const {assets, metadata} = useBlogPost();
114
+ const {siteConfig} = useDocusaurusContext();
115
+ const {withBaseUrl} = useBaseUrlUtils();
116
+
117
+ const {date, title, description, frontMatter, lastUpdatedAt} = metadata;
118
+
119
+ const image = assets.image ?? frontMatter.image;
120
+ const keywords = frontMatter.keywords ?? [];
121
+
122
+ const dateModified = lastUpdatedAt ? convertDate(lastUpdatedAt) : undefined;
123
+
124
+ const url = `${siteConfig.url}${metadata.permalink}`;
125
+
126
+ // details on structured data support: https://schema.org/BlogPosting
127
+ // BlogPosting is one of the structured data types that Google explicitly
128
+ // supports: https://developers.google.com/search/docs/appearance/structured-data/article#structured-data-type-definitions
129
+ return {
130
+ '@context': 'https://schema.org',
131
+ '@type': 'BlogPosting',
132
+ '@id': url,
133
+ mainEntityOfPage: url,
134
+ url,
135
+ headline: title,
136
+ name: title,
137
+ description,
138
+ datePublished: date,
139
+ ...(dateModified ? {dateModified} : {}),
140
+ ...getAuthor(metadata.authors),
141
+ ...getImage(image, withBaseUrl, title),
142
+ ...(keywords ? {keywords} : {}),
143
+ isPartOf: {
144
+ '@type': 'Blog',
145
+ '@id': `${siteConfig.url}${blogMetadata.blogBasePath}`,
146
+ name: blogMetadata.blogTitle,
147
+ },
148
+ };
149
+ }
150
+
151
+ /** @returns A {@link https://schema.org/Person} constructed from the {@link Author} */
152
+ function createPersonStructuredData(author: Author): Person {
153
+ return {
154
+ '@type': 'Person',
155
+ ...(author.name ? {name: author.name} : {}),
156
+ ...(author.title ? {description: author.title} : {}),
157
+ ...(author.url ? {url: author.url} : {}),
158
+ ...(author.email ? {email: author.email} : {}),
159
+ ...(author.imageURL ? {image: author.imageURL} : {}),
160
+ };
161
+ }
162
+
163
+ /** @returns A {@link https://schema.org/ImageObject} */
164
+ function createImageStructuredData({
165
+ imageUrl,
166
+ caption,
167
+ }: {
168
+ imageUrl: string;
169
+ caption: string;
170
+ }): ImageObject {
171
+ return {
172
+ '@type': 'ImageObject',
173
+ '@id': imageUrl,
174
+ url: imageUrl,
175
+ contentUrl: imageUrl,
176
+ caption,
177
+ };
178
+ }
@@ -1,20 +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 useRouteContext from '@docusaurus/useRouteContext';
9
- import type {BlogMetadata} from '@docusaurus/plugin-content-blog';
10
-
11
- export function useBlogMetadata(): BlogMetadata {
12
- const routeContext = useRouteContext();
13
- const blogMetadata = routeContext?.data?.blogMetadata;
14
- if (!blogMetadata) {
15
- throw new Error(
16
- "useBlogMetadata() can't be called on the current route because the blog metadata could not be found in route context",
17
- );
18
- }
19
- return blogMetadata as BlogMetadata;
20
- }