chore(blog): refactor blog Content, remove useless `blogListPaginated` attribute (#11562)

This commit is contained in:
Sébastien Lorber 2025-11-20 12:52:29 +01:00 committed by GitHub
parent 7880f26a07
commit 37530aaafb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 41 additions and 114 deletions

View File

@ -24,24 +24,7 @@ exports[`getContentTranslationFiles returns translation files matching snapshot
exports[`translateContent falls back when translation is incomplete 1`] = `
{
"blogListPaginated": [
{
"items": [
"hello",
],
"metadata": {
"blogDescription": "Someone's random blog",
"blogTitle": "My blog",
"nextPage": undefined,
"page": 1,
"permalink": "/",
"postsPerPage": 10,
"previousPage": undefined,
"totalCount": 1,
"totalPages": 1,
},
},
],
"blogDescription": "Someone's random blog",
"blogPosts": [
{
"content": "",
@ -63,29 +46,13 @@ exports[`translateContent falls back when translation is incomplete 1`] = `
"blogSidebarTitle": "All my posts",
"blogTags": {},
"blogTagsListPath": "/tags",
"blogTitle": "My blog",
}
`;
exports[`translateContent returns translated loaded 1`] = `
{
"blogListPaginated": [
{
"items": [
"hello",
],
"metadata": {
"blogDescription": "Someone's random blog (translated)",
"blogTitle": "My blog (translated)",
"nextPage": undefined,
"page": 1,
"permalink": "/",
"postsPerPage": 10,
"previousPage": undefined,
"totalCount": 1,
"totalPages": 1,
},
},
],
"blogDescription": "Someone's random blog (translated)",
"blogPosts": [
{
"content": "",
@ -107,5 +74,6 @@ exports[`translateContent returns translated loaded 1`] = `
"blogSidebarTitle": "All my posts (translated)",
"blogTags": {},
"blogTagsListPath": "/tags",
"blogTitle": "My blog (translated)",
}
`;

View File

@ -638,10 +638,7 @@ describe('blog plugin', () => {
},
DefaultI18N,
);
const {blogPosts, blogTags, blogListPaginated} =
(await plugin.loadContent!())!;
expect(blogListPaginated).toHaveLength(3);
const {blogPosts, blogTags} = (await plugin.loadContent!())!;
expect(Object.keys(blogTags)).toHaveLength(2);
expect(blogTags).toMatchSnapshot();

View File

@ -6,6 +6,7 @@
*/
import {updateTranslationFileMessages} from '@docusaurus/utils';
import {fromPartial} from '@total-typescript/shoehorn';
import {getTranslationFiles, translateContent} from '../translations';
import {DEFAULT_OPTIONS} from '../options';
import type {
@ -16,13 +17,13 @@ import type {
const sampleBlogOptions: PluginOptions = {
...DEFAULT_OPTIONS,
blogSidebarTitle: 'All my posts',
blogTitle: 'My blog',
blogDescription: "Someone's random blog",
blogSidebarTitle: 'All my posts',
};
const sampleBlogPosts: BlogPost[] = [
{
fromPartial({
id: 'hello',
metadata: {
permalink: '/blog/2021/06/19/hello',
@ -37,27 +38,13 @@ const sampleBlogPosts: BlogPost[] = [
unlisted: false,
},
content: '',
},
}),
];
const sampleBlogContent: BlogContent = {
blogTitle: sampleBlogOptions.blogTitle,
blogDescription: sampleBlogOptions.blogDescription,
blogSidebarTitle: sampleBlogOptions.blogSidebarTitle,
blogListPaginated: [
{
items: ['hello'],
metadata: {
permalink: '/',
page: 1,
postsPerPage: 10,
totalPages: 1,
totalCount: 1,
previousPage: undefined,
nextPage: undefined,
blogTitle: sampleBlogOptions.blogTitle,
blogDescription: sampleBlogOptions.blogDescription,
},
},
],
blogPosts: sampleBlogPosts,
blogTags: {},
blogTagsListPath: '/tags',

View File

@ -25,7 +25,6 @@ import {getTagsFilePathsToWatch} from '@docusaurus/utils-validation';
import {createMDXLoaderItem} from '@docusaurus/mdx-loader';
import {
getBlogTags,
paginateBlogPosts,
shouldBeListed,
applyProcessBlogPosts,
generateBlogPosts,
@ -45,7 +44,6 @@ import type {
Assets,
BlogTags,
BlogContent,
BlogPaginated,
} from '@docusaurus/plugin-content-blog';
import type {RuleSetRule, RuleSetUseItem} from 'webpack';
@ -260,9 +258,10 @@ export default async function pluginContentBlog(
if (!blogPosts.length) {
return {
blogTitle,
blogDescription,
blogSidebarTitle,
blogPosts: [],
blogListPaginated: [],
blogTags: {},
blogTagsListPath,
authorsMap,
@ -291,15 +290,6 @@ export default async function pluginContentBlog(
}
});
const blogListPaginated: BlogPaginated[] = paginateBlogPosts({
blogPosts: listedBlogPosts,
blogTitle,
blogDescription,
postsPerPageOption,
basePageUrl: baseBlogUrl,
pageBasePath,
});
const blogTags: BlogTags = getBlogTags({
blogPosts,
postsPerPageOption,
@ -309,9 +299,10 @@ export default async function pluginContentBlog(
});
return {
blogTitle,
blogDescription,
blogSidebarTitle,
blogPosts,
blogListPaginated,
blogTags,
blogTagsListPath,
authorsMap,

View File

@ -583,9 +583,10 @@ declare module '@docusaurus/plugin-content-blog' {
export type AuthorsMap = {[authorKey: string]: AuthorWithKey};
export type BlogContent = {
blogSidebarTitle: string;
blogTitle: string; // for translation purposes
blogDescription: string; // for translation purposes
blogSidebarTitle: string; // for translation purposes
blogPosts: BlogPost[];
blogListPaginated: BlogPaginated[];
blogTags: BlogTags;
blogTagsListPath: string;
authorsMap?: AuthorsMap;

View File

@ -67,27 +67,24 @@ export async function buildAllRoutes({
blogArchiveComponent,
routeBasePath,
archiveBasePath,
blogTitle,
authorsBasePath,
postsPerPage,
blogDescription,
pageBasePath,
} = options;
const pluginId = options.id!;
const {createData} = actions;
const {
blogTitle,
blogDescription,
blogSidebarTitle,
blogPosts,
blogListPaginated,
blogTags,
blogTagsListPath,
authorsMap,
} = content;
const authorsListPath = normalizeUrl([
baseUrl,
routeBasePath,
authorsBasePath,
]);
const blogBasePath = normalizeUrl([baseUrl, routeBasePath]);
const authorsListPath = normalizeUrl([blogBasePath, authorsBasePath]);
const listedBlogPosts = blogPosts.filter(shouldBeListed);
@ -119,7 +116,7 @@ export async function buildAllRoutes({
async function createBlogMetadataModule() {
const blogMetadata: BlogMetadata = {
blogBasePath: normalizeUrl([baseUrl, routeBasePath]),
blogBasePath,
blogTitle,
authorsListPath,
};
@ -156,7 +153,7 @@ export async function buildAllRoutes({
if (archiveBasePath && listedBlogPosts.length) {
return [
{
path: normalizeUrl([baseUrl, routeBasePath, archiveBasePath]),
path: normalizeUrl([blogBasePath, archiveBasePath]),
component: blogArchiveComponent,
exact: true,
props: {
@ -210,6 +207,15 @@ export async function buildAllRoutes({
}
function createBlogPostsPaginatedRoutes(): RouteConfig[] {
const blogListPaginated = paginateBlogPosts({
blogPosts: listedBlogPosts,
blogTitle,
blogDescription,
postsPerPageOption: postsPerPage,
basePageUrl: blogBasePath,
pageBasePath,
});
return blogListPaginated.map((paginated) => {
return {
path: paginated.metadata.permalink,

View File

@ -5,30 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/
import type {TranslationFileContent, TranslationFile} from '@docusaurus/types';
import type {
PluginOptions,
BlogContent,
BlogPaginated,
} from '@docusaurus/plugin-content-blog';
function translateListPage(
blogListPaginated: BlogPaginated[],
translations: TranslationFileContent,
) {
return blogListPaginated.map((page) => {
const {items, metadata} = page;
return {
items,
metadata: {
...metadata,
blogTitle: translations.title?.message ?? page.metadata.blogTitle,
blogDescription:
translations.description?.message ?? page.metadata.blogDescription,
},
};
});
}
import type {TranslationFile} from '@docusaurus/types';
import type {PluginOptions, BlogContent} from '@docusaurus/plugin-content-blog';
export function getTranslationFiles(options: PluginOptions): TranslationFile[] {
return [
@ -56,14 +34,13 @@ export function translateContent(
content: BlogContent,
translationFiles: TranslationFile[],
): BlogContent {
const {content: optionsTranslations} = translationFiles[0]!;
const {content: translations} = translationFiles[0]!;
return {
...content,
blogTitle: translations.title?.message ?? content.blogTitle,
blogDescription:
translations.description?.message ?? content.blogDescription,
blogSidebarTitle:
optionsTranslations['sidebar.title']?.message ?? content.blogSidebarTitle,
blogListPaginated: translateListPage(
content.blogListPaginated,
optionsTranslations,
),
translations['sidebar.title']?.message ?? content.blogSidebarTitle,
};
}