Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blog): add options.createFeedItems to filter/limit/transform feed items #8378

Merged
merged 19 commits into from Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

Large diffs are not rendered by default.

Expand Up @@ -143,4 +143,47 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => {
).toMatchSnapshot();
fsMock.mockClear();
});

it('filters to the first two entries', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const outDir = path.join(siteDir, 'build-snap');
const siteConfig = {
title: 'Hello',
baseUrl: '/myBaseUrl/',
url: 'https://docusaurus.io',
favicon: 'image/favicon.ico',
};

// Build is quite difficult to mock, so we built the blog beforehand and
// copied the output to the fixture...
await testGenerateFeeds(
{
siteDir,
siteConfig,
i18n: DefaultI18N,
outDir,
} as LoadContext,
{
path: 'blog',
routeBasePath: 'blog',
tagsBasePath: 'tags',
authorsMapPath: 'authors.yml',
include: DEFAULT_OPTIONS.include,
exclude: DEFAULT_OPTIONS.exclude,
feedOptions: {
type: [feedType],
copyright: 'Copyright',
filter: (items, index) => index < 2,
},
readingTime: ({content, defaultReadingTime}) =>
defaultReadingTime({content}),
truncateMarker: /<!--\s*truncate\s*-->/,
} as PluginOptions,
);

expect(
fsMock.mock.calls.map((call) => call[1] as string),
).toMatchSnapshot();
fsMock.mockClear();
});
});
5 changes: 4 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/feed.ts
Expand Up @@ -154,9 +154,12 @@ export async function createBlogFeedFiles({
locale: string;
}): Promise<void> {
const blogPosts = allBlogPosts.filter(shouldBeInFeed);
const filteredBlogPosts = options.feedOptions.filter
? blogPosts.filter(options.feedOptions.filter)
: blogPosts;
johnnyreilly marked this conversation as resolved.
Show resolved Hide resolved

const feed = await generateBlogFeed({
blogPosts,
blogPosts: filteredBlogPosts,
options,
siteConfig,
outDir,
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-blog/src/options.ts
Expand Up @@ -124,6 +124,7 @@ const PluginOptionSchema = Joi.object<PluginOptions>({
.default(DEFAULT_OPTIONS.feedOptions.copyright),
}),
language: Joi.string(),
filter: Joi.function(),
}).default(DEFAULT_OPTIONS.feedOptions),
authorsMapPath: Joi.string().default(DEFAULT_OPTIONS.authorsMapPath),
readingTime: Joi.function().default(() => DEFAULT_OPTIONS.readingTime),
Expand Down
Expand Up @@ -263,6 +263,8 @@ declare module '@docusaurus/plugin-content-blog' {
copyright: string;
/** Language of the feed. */
language?: string;
/** Filter that can be applied to reduce the number of entries */
filter?: (blogPost: BlogPost, index: number) => boolean;
};

/**
Expand Down
1 change: 1 addition & 0 deletions website/docs/blog.mdx
Expand Up @@ -511,6 +511,7 @@ type BlogOptions = {
description?: string;
copyright: string;
language?: string; // possible values: http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
filter?: (blogPost: BlogPost, index?: number) => boolean;
};
};
```
Expand Down
1 change: 1 addition & 0 deletions website/docusaurus.config.js
Expand Up @@ -341,6 +341,7 @@ const config = {
feedOptions: {
type: 'all',
copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`,
filter: (items, index) => index < 2,
},
blogSidebarCount: 'ALL',
blogSidebarTitle: 'All our posts',
Expand Down