Skip to content

Commit

Permalink
wip: config option, test doesn't
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Feb 24, 2024
1 parent 7a76d9b commit 29b29ae
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -237,6 +238,7 @@ describe('linkify', () => {
frontMatter: {},
authors: [],
formattedDate: '',
unlisted: false,
},
content: '',
},
Expand Down Expand Up @@ -295,3 +297,49 @@ describe('linkify', () => {
} as BlogBrokenMarkdownLink);
});
});

const defaultValuesForOtherKeys: Omit<BlogPostMetadata, 'date'> = {
source: '',
title: '',
formattedDate: '',
permalink: '',
description: '',
hasTruncateMarker: false,
authors: [],
frontMatter: {},
tags: [],
unlisted: false,
};
const createBlogPost = (args: Partial<BlogPost>): 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]);

Check failure on line 343 in packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts

View workflow job for this annotation

GitHub Actions / Windows Tests (18.0)

processBlogPosts › filter blogs

expect(received).toEqual(expected) // deep equality Expected: [{"content": "", "id": "", "metadata": {"authors": [], "date": 2024-01-01T00:00:00.000Z, "description": "", "formattedDate": "", "frontMatter": {}, "hasTruncateMarker": false, "permalink": "", "source": "", "tags": [], "title": "", "unlisted": false}}] Received: {} at Object.toEqual (packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts:343:32)
});
});
15 changes: 8 additions & 7 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,18 @@ export function linkify({
return newContent;
}

export async function applyProcessBlogPosts(
contentPaths: BlogContentPaths,
context: LoadContext,
options: PluginOptions,
): Promise<BlogPost[]> {
const blogPosts = await generateBlogPosts(contentPaths, context, options);
const {processBlogPosts} = options;
export async function applyProcessBlogPosts({
blogPosts,
processBlogPosts,
}: {
blogPosts: BlogPost[];
processBlogPosts: PluginOptions['processBlogPosts'];
}): Promise<BlogPost[]> {
const processedBlogPosts = processBlogPosts({blogPosts});

if (Array.isArray(processedBlogPosts)) {
return processedBlogPosts;
}

return blogPosts;
}
11 changes: 6 additions & 5 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
paginateBlogPosts,
shouldBeListed,
applyProcessBlogPosts,
generateBlogPosts,
} from './blogUtils';
import footnoteIDFixer from './remark/footnoteIDFixer';
import {translateContent, getTranslationFiles} from './translations';
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/api/plugins/plugin-content-blog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type CreateFeedItemsFn = (params: {
```ts
type ProcessBlogPostsFn = (params: {
blogPosts: BlogPost[];
}) => void | BlogPost[];
}) => Promise<void | BlogPost[]>;
```

### Example configuration {#ex-config}
Expand Down

0 comments on commit 29b29ae

Please sign in to comment.