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): allow sorting blog posts through a options.sortPosts function hook #9840

Closed
wants to merge 16 commits into from
10 changes: 7 additions & 3 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,13 @@ export async function generateBlogPosts(
await Promise.all(blogSourceFiles.map(doProcessBlogSourceFile))
).filter(Boolean) as BlogPost[];

blogPosts.sort(
(a, b) => b.metadata.date.getTime() - a.metadata.date.getTime(),
);
if (typeof options.sortPosts === 'function') {
blogPosts.sort(options.sortPosts);
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
} else {
blogPosts.sort(
(a, b) => b.metadata.date.getTime() - a.metadata.date.getTime(),
);
}

if (options.sortPosts === 'ascending') {
return blogPosts.reverse();
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-content-blog/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ const PluginOptionSchema = Joi.object<PluginOptions>({
}).default(DEFAULT_OPTIONS.feedOptions),
authorsMapPath: Joi.string().default(DEFAULT_OPTIONS.authorsMapPath),
readingTime: Joi.function().default(() => DEFAULT_OPTIONS.readingTime),
sortPosts: Joi.string()
.valid('descending', 'ascending')
sortPosts: Joi.alternatives()
.try(Joi.string().valid('descending', 'ascending'), Joi.function())
.default(DEFAULT_OPTIONS.sortPosts),
}).default(DEFAULT_OPTIONS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ yarn workspace v1.22.19image` is a collocated image path, this entry will be the
/** A callback to customize the reading time number displayed. */
readingTime: ReadingTimeFunctionOption;
/** Governs the direction of blog post sorting. */
sortPosts: 'ascending' | 'descending';
sortPosts:
| 'ascending'
| 'descending'
| ((a: BlogPost, b: BlogPost) => number);
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
};

/**
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 @@ -73,7 +73,7 @@ Accepted fields:
| `feedOptions.description` | `string` | <code>\`$\{siteConfig.title} Blog\`</code> | Description of the feed. |
| `feedOptions.copyright` | `string` | `undefined` | Copyright message. |
| `feedOptions.language` | `string` (See [documentation](http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes) for possible values) | `undefined` | Language metadata of the feed. |
| `sortPosts` | <code>'descending' \| 'ascending' </code> | `'descending'` | Governs the direction of blog post sorting. |
| `sortPosts` | <code>'descending' \| 'ascending' \| function()</code> | `'descending'` | Governs the direction of blog post sorting. |
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved

```mdx-code-block
</APITable>
Expand Down