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
11 changes: 8 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,14 @@ 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') {
console.log('options:', options);
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();
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-blog/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const PluginOptionSchema = Joi.object<PluginOptions>({
authorsMapPath: Joi.string().default(DEFAULT_OPTIONS.authorsMapPath),
readingTime: Joi.function().default(() => DEFAULT_OPTIONS.readingTime),
sortPosts: Joi.string()
.valid('descending', 'ascending')
.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
7 changes: 6 additions & 1 deletion website/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import PrismDark from './src/utils/prismDark';
import type {Config} from '@docusaurus/types';
import type * as Preset from '@docusaurus/preset-classic';
import type {Options as DocsOptions} from '@docusaurus/plugin-content-docs';
import type {Options as BlogOptions} from '@docusaurus/plugin-content-blog';
import type {
Options as BlogOptions,
BlogPost,
} from '@docusaurus/plugin-content-blog';
import type {Options as PageOptions} from '@docusaurus/plugin-content-pages';
import type {Options as IdealImageOptions} from '@docusaurus/plugin-ideal-image';
import type {Options as ClientRedirectsOptions} from '@docusaurus/plugin-client-redirects';
Expand Down Expand Up @@ -441,6 +444,8 @@ export default async function createConfigAsync() {
blog: {
// routeBasePath: '/',
path: 'blog',
sortPosts: (a: BlogPost, b: BlogPost): number =>
b.metadata.date.getTime() - a.metadata.date.getTime(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test this by applying this to the dogfood blog plugin.

For example you can write a blog post with an old date, and make it appear in 1st position thanks to a front matter field?

editUrl: ({locale, blogDirPath, blogPath}) => {
if (locale !== defaultLocale) {
return `https://crowdin.com/project/docusaurus-v2/${locale}`;
Expand Down