Skip to content

Commit 37530aa

Browse files
authored
chore(blog): refactor blog Content, remove useless blogListPaginated attribute (#11562)
1 parent 7880f26 commit 37530aa

File tree

7 files changed

+41
-114
lines changed

7 files changed

+41
-114
lines changed

packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/translations.test.ts.snap

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,7 @@ exports[`getContentTranslationFiles returns translation files matching snapshot
2424

2525
exports[`translateContent falls back when translation is incomplete 1`] = `
2626
{
27-
"blogListPaginated": [
28-
{
29-
"items": [
30-
"hello",
31-
],
32-
"metadata": {
33-
"blogDescription": "Someone's random blog",
34-
"blogTitle": "My blog",
35-
"nextPage": undefined,
36-
"page": 1,
37-
"permalink": "/",
38-
"postsPerPage": 10,
39-
"previousPage": undefined,
40-
"totalCount": 1,
41-
"totalPages": 1,
42-
},
43-
},
44-
],
27+
"blogDescription": "Someone's random blog",
4528
"blogPosts": [
4629
{
4730
"content": "",
@@ -63,29 +46,13 @@ exports[`translateContent falls back when translation is incomplete 1`] = `
6346
"blogSidebarTitle": "All my posts",
6447
"blogTags": {},
6548
"blogTagsListPath": "/tags",
49+
"blogTitle": "My blog",
6650
}
6751
`;
6852

6953
exports[`translateContent returns translated loaded 1`] = `
7054
{
71-
"blogListPaginated": [
72-
{
73-
"items": [
74-
"hello",
75-
],
76-
"metadata": {
77-
"blogDescription": "Someone's random blog (translated)",
78-
"blogTitle": "My blog (translated)",
79-
"nextPage": undefined,
80-
"page": 1,
81-
"permalink": "/",
82-
"postsPerPage": 10,
83-
"previousPage": undefined,
84-
"totalCount": 1,
85-
"totalPages": 1,
86-
},
87-
},
88-
],
55+
"blogDescription": "Someone's random blog (translated)",
8956
"blogPosts": [
9057
{
9158
"content": "",
@@ -107,5 +74,6 @@ exports[`translateContent returns translated loaded 1`] = `
10774
"blogSidebarTitle": "All my posts (translated)",
10875
"blogTags": {},
10976
"blogTagsListPath": "/tags",
77+
"blogTitle": "My blog (translated)",
11078
}
11179
`;

packages/docusaurus-plugin-content-blog/src/__tests__/index.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,7 @@ describe('blog plugin', () => {
638638
},
639639
DefaultI18N,
640640
);
641-
const {blogPosts, blogTags, blogListPaginated} =
642-
(await plugin.loadContent!())!;
643-
644-
expect(blogListPaginated).toHaveLength(3);
641+
const {blogPosts, blogTags} = (await plugin.loadContent!())!;
645642

646643
expect(Object.keys(blogTags)).toHaveLength(2);
647644
expect(blogTags).toMatchSnapshot();

packages/docusaurus-plugin-content-blog/src/__tests__/translations.test.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import {updateTranslationFileMessages} from '@docusaurus/utils';
9+
import {fromPartial} from '@total-typescript/shoehorn';
910
import {getTranslationFiles, translateContent} from '../translations';
1011
import {DEFAULT_OPTIONS} from '../options';
1112
import type {
@@ -16,13 +17,13 @@ import type {
1617

1718
const sampleBlogOptions: PluginOptions = {
1819
...DEFAULT_OPTIONS,
19-
blogSidebarTitle: 'All my posts',
2020
blogTitle: 'My blog',
2121
blogDescription: "Someone's random blog",
22+
blogSidebarTitle: 'All my posts',
2223
};
2324

2425
const sampleBlogPosts: BlogPost[] = [
25-
{
26+
fromPartial({
2627
id: 'hello',
2728
metadata: {
2829
permalink: '/blog/2021/06/19/hello',
@@ -37,27 +38,13 @@ const sampleBlogPosts: BlogPost[] = [
3738
unlisted: false,
3839
},
3940
content: '',
40-
},
41+
}),
4142
];
4243

4344
const sampleBlogContent: BlogContent = {
45+
blogTitle: sampleBlogOptions.blogTitle,
46+
blogDescription: sampleBlogOptions.blogDescription,
4447
blogSidebarTitle: sampleBlogOptions.blogSidebarTitle,
45-
blogListPaginated: [
46-
{
47-
items: ['hello'],
48-
metadata: {
49-
permalink: '/',
50-
page: 1,
51-
postsPerPage: 10,
52-
totalPages: 1,
53-
totalCount: 1,
54-
previousPage: undefined,
55-
nextPage: undefined,
56-
blogTitle: sampleBlogOptions.blogTitle,
57-
blogDescription: sampleBlogOptions.blogDescription,
58-
},
59-
},
60-
],
6148
blogPosts: sampleBlogPosts,
6249
blogTags: {},
6350
blogTagsListPath: '/tags',

packages/docusaurus-plugin-content-blog/src/index.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {getTagsFilePathsToWatch} from '@docusaurus/utils-validation';
2525
import {createMDXLoaderItem} from '@docusaurus/mdx-loader';
2626
import {
2727
getBlogTags,
28-
paginateBlogPosts,
2928
shouldBeListed,
3029
applyProcessBlogPosts,
3130
generateBlogPosts,
@@ -45,7 +44,6 @@ import type {
4544
Assets,
4645
BlogTags,
4746
BlogContent,
48-
BlogPaginated,
4947
} from '@docusaurus/plugin-content-blog';
5048
import type {RuleSetRule, RuleSetUseItem} from 'webpack';
5149

@@ -260,9 +258,10 @@ export default async function pluginContentBlog(
260258

261259
if (!blogPosts.length) {
262260
return {
261+
blogTitle,
262+
blogDescription,
263263
blogSidebarTitle,
264264
blogPosts: [],
265-
blogListPaginated: [],
266265
blogTags: {},
267266
blogTagsListPath,
268267
authorsMap,
@@ -291,15 +290,6 @@ export default async function pluginContentBlog(
291290
}
292291
});
293292

294-
const blogListPaginated: BlogPaginated[] = paginateBlogPosts({
295-
blogPosts: listedBlogPosts,
296-
blogTitle,
297-
blogDescription,
298-
postsPerPageOption,
299-
basePageUrl: baseBlogUrl,
300-
pageBasePath,
301-
});
302-
303293
const blogTags: BlogTags = getBlogTags({
304294
blogPosts,
305295
postsPerPageOption,
@@ -309,9 +299,10 @@ export default async function pluginContentBlog(
309299
});
310300

311301
return {
302+
blogTitle,
303+
blogDescription,
312304
blogSidebarTitle,
313305
blogPosts,
314-
blogListPaginated,
315306
blogTags,
316307
blogTagsListPath,
317308
authorsMap,

packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,10 @@ declare module '@docusaurus/plugin-content-blog' {
583583
export type AuthorsMap = {[authorKey: string]: AuthorWithKey};
584584

585585
export type BlogContent = {
586-
blogSidebarTitle: string;
586+
blogTitle: string; // for translation purposes
587+
blogDescription: string; // for translation purposes
588+
blogSidebarTitle: string; // for translation purposes
587589
blogPosts: BlogPost[];
588-
blogListPaginated: BlogPaginated[];
589590
blogTags: BlogTags;
590591
blogTagsListPath: string;
591592
authorsMap?: AuthorsMap;

packages/docusaurus-plugin-content-blog/src/routes.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,24 @@ export async function buildAllRoutes({
6767
blogArchiveComponent,
6868
routeBasePath,
6969
archiveBasePath,
70-
blogTitle,
7170
authorsBasePath,
7271
postsPerPage,
73-
blogDescription,
72+
pageBasePath,
7473
} = options;
7574
const pluginId = options.id!;
7675
const {createData} = actions;
7776
const {
77+
blogTitle,
78+
blogDescription,
7879
blogSidebarTitle,
7980
blogPosts,
80-
blogListPaginated,
8181
blogTags,
8282
blogTagsListPath,
8383
authorsMap,
8484
} = content;
8585

86-
const authorsListPath = normalizeUrl([
87-
baseUrl,
88-
routeBasePath,
89-
authorsBasePath,
90-
]);
86+
const blogBasePath = normalizeUrl([baseUrl, routeBasePath]);
87+
const authorsListPath = normalizeUrl([blogBasePath, authorsBasePath]);
9188

9289
const listedBlogPosts = blogPosts.filter(shouldBeListed);
9390

@@ -119,7 +116,7 @@ export async function buildAllRoutes({
119116

120117
async function createBlogMetadataModule() {
121118
const blogMetadata: BlogMetadata = {
122-
blogBasePath: normalizeUrl([baseUrl, routeBasePath]),
119+
blogBasePath,
123120
blogTitle,
124121
authorsListPath,
125122
};
@@ -156,7 +153,7 @@ export async function buildAllRoutes({
156153
if (archiveBasePath && listedBlogPosts.length) {
157154
return [
158155
{
159-
path: normalizeUrl([baseUrl, routeBasePath, archiveBasePath]),
156+
path: normalizeUrl([blogBasePath, archiveBasePath]),
160157
component: blogArchiveComponent,
161158
exact: true,
162159
props: {
@@ -210,6 +207,15 @@ export async function buildAllRoutes({
210207
}
211208

212209
function createBlogPostsPaginatedRoutes(): RouteConfig[] {
210+
const blogListPaginated = paginateBlogPosts({
211+
blogPosts: listedBlogPosts,
212+
blogTitle,
213+
blogDescription,
214+
postsPerPageOption: postsPerPage,
215+
basePageUrl: blogBasePath,
216+
pageBasePath,
217+
});
218+
213219
return blogListPaginated.map((paginated) => {
214220
return {
215221
path: paginated.metadata.permalink,

packages/docusaurus-plugin-content-blog/src/translations.ts

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import type {TranslationFileContent, TranslationFile} from '@docusaurus/types';
9-
import type {
10-
PluginOptions,
11-
BlogContent,
12-
BlogPaginated,
13-
} from '@docusaurus/plugin-content-blog';
14-
15-
function translateListPage(
16-
blogListPaginated: BlogPaginated[],
17-
translations: TranslationFileContent,
18-
) {
19-
return blogListPaginated.map((page) => {
20-
const {items, metadata} = page;
21-
return {
22-
items,
23-
metadata: {
24-
...metadata,
25-
blogTitle: translations.title?.message ?? page.metadata.blogTitle,
26-
blogDescription:
27-
translations.description?.message ?? page.metadata.blogDescription,
28-
},
29-
};
30-
});
31-
}
8+
import type {TranslationFile} from '@docusaurus/types';
9+
import type {PluginOptions, BlogContent} from '@docusaurus/plugin-content-blog';
3210

3311
export function getTranslationFiles(options: PluginOptions): TranslationFile[] {
3412
return [
@@ -56,14 +34,13 @@ export function translateContent(
5634
content: BlogContent,
5735
translationFiles: TranslationFile[],
5836
): BlogContent {
59-
const {content: optionsTranslations} = translationFiles[0]!;
37+
const {content: translations} = translationFiles[0]!;
6038
return {
6139
...content,
40+
blogTitle: translations.title?.message ?? content.blogTitle,
41+
blogDescription:
42+
translations.description?.message ?? content.blogDescription,
6243
blogSidebarTitle:
63-
optionsTranslations['sidebar.title']?.message ?? content.blogSidebarTitle,
64-
blogListPaginated: translateListPage(
65-
content.blogListPaginated,
66-
optionsTranslations,
67-
),
44+
translations['sidebar.title']?.message ?? content.blogSidebarTitle,
6845
};
6946
}

0 commit comments

Comments
 (0)