diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts index d96f1228c897..fff1adb3955e 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts @@ -14,10 +14,11 @@ import { linkify, getSourceToPermalink, paginateBlogPosts, + applyProcessBlogPosts, type LinkifyParams, } from '../blogUtils'; import type {BlogBrokenMarkdownLink, BlogContentPaths} from '../types'; -import type {BlogPost} from '@docusaurus/plugin-content-blog'; +import type {BlogPost, BlogPostMetadata} from '@docusaurus/plugin-content-blog'; describe('truncate', () => { it('truncates texts', () => { @@ -237,6 +238,7 @@ describe('linkify', () => { frontMatter: {}, authors: [], formattedDate: '', + unlisted: false, }, content: '', }, @@ -295,3 +297,49 @@ describe('linkify', () => { } as BlogBrokenMarkdownLink); }); }); + +const defaultValuesForOtherKeys: Omit = { + source: '', + title: '', + formattedDate: '', + permalink: '', + description: '', + hasTruncateMarker: false, + authors: [], + frontMatter: {}, + tags: [], + unlisted: false, +}; +const createBlogPost = (args: Partial): BlogPost => ({ + id: '', + metadata: { + date: new Date(), + ...defaultValuesForOtherKeys, + ...args.metadata, + }, + content: args.content || '', +}); + +describe('processBlogPosts', () => { + const blogPost2022 = createBlogPost({ + metadata: {date: new Date('2022-01-01'), ...defaultValuesForOtherKeys}, + }); + const blogPost2023 = createBlogPost({ + metadata: {date: new Date('2023-01-01'), ...defaultValuesForOtherKeys}, + }); + const blogPost2024 = createBlogPost({ + metadata: {date: new Date('2024-01-01'), ...defaultValuesForOtherKeys}, + }); + + it('filter blogs', async () => { + const processedBlogPosts = applyProcessBlogPosts({ + blogPosts: [blogPost2022, blogPost2023, blogPost2024], + processBlogPosts: ({blogPosts}: {blogPosts: BlogPost[]}) => + blogPosts.filter( + (blogPost) => blogPost.metadata.date.getFullYear() === 2024, + ), + }); + + expect(processedBlogPosts).toEqual([blogPost2024]); + }); +}); diff --git a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts index 194971e985c0..4d9911c15ef5 100644 --- a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts +++ b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts @@ -447,17 +447,18 @@ export function linkify({ return newContent; } -export async function applyProcessBlogPosts( - contentPaths: BlogContentPaths, - context: LoadContext, - options: PluginOptions, -): Promise { - const blogPosts = await generateBlogPosts(contentPaths, context, options); - const {processBlogPosts} = options; +export async function applyProcessBlogPosts({ + blogPosts, + processBlogPosts, +}: { + blogPosts: BlogPost[]; + processBlogPosts: PluginOptions['processBlogPosts']; +}): Promise { const processedBlogPosts = processBlogPosts({blogPosts}); if (Array.isArray(processedBlogPosts)) { return processedBlogPosts; } + return blogPosts; } diff --git a/packages/docusaurus-plugin-content-blog/src/index.ts b/packages/docusaurus-plugin-content-blog/src/index.ts index 20981fb1aebd..eba91487779c 100644 --- a/packages/docusaurus-plugin-content-blog/src/index.ts +++ b/packages/docusaurus-plugin-content-blog/src/index.ts @@ -25,6 +25,7 @@ import { paginateBlogPosts, shouldBeListed, applyProcessBlogPosts, + generateBlogPosts, } from './blogUtils'; import footnoteIDFixer from './remark/footnoteIDFixer'; import {translateContent, getTranslationFiles} from './translations'; @@ -113,11 +114,11 @@ export default async function pluginContentBlog( const baseBlogUrl = normalizeUrl([baseUrl, routeBasePath]); const blogTagsListPath = normalizeUrl([baseBlogUrl, tagsBasePath]); - const blogPosts = await applyProcessBlogPosts( - contentPaths, - context, - options, - ); + let blogPosts = await generateBlogPosts(contentPaths, context, options); + blogPosts = await applyProcessBlogPosts({ + blogPosts, + processBlogPosts: options.processBlogPosts, + }); const listedBlogPosts = blogPosts.filter(shouldBeListed); if (!blogPosts.length) { diff --git a/website/docs/api/plugins/plugin-content-blog.mdx b/website/docs/api/plugins/plugin-content-blog.mdx index 7821f6f418e9..bb47401f6a76 100644 --- a/website/docs/api/plugins/plugin-content-blog.mdx +++ b/website/docs/api/plugins/plugin-content-blog.mdx @@ -137,7 +137,7 @@ type CreateFeedItemsFn = (params: { ```ts type ProcessBlogPostsFn = (params: { blogPosts: BlogPost[]; -}) => void | BlogPost[]; +}) => Promise; ``` ### Example configuration {#ex-config}