Skip to content

Commit

Permalink
feat: sorting blog posts is now customizable in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
cerkiewny committed Oct 25, 2021
1 parent 5d20219 commit e6baf77
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,17 @@ describe('loadBlog', () => {
truncated: false,
});
});

test('custom ordering of blog posts', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const normalOrder = await getBlogPosts(siteDir);
const reversedOrder = await getBlogPosts(siteDir, {
customBlogSortingFunction: ({lhs, rhs}) => {
return lhs.metadata.date.getTime() - rhs.metadata.date.getTime();
},
});
expect(normalOrder.reverse().map((x) => x.metadata.date)).toEqual(
reversedOrder.map((x) => x.metadata.date),
);
});
});
4 changes: 1 addition & 3 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,7 @@ export async function generateBlogPosts(
),
);

blogPosts.sort(
(a, b) => b.metadata.date.getTime() - a.metadata.date.getTime(),
);
blogPosts.sort((a, b) => options.customBlogSortingFunction({lhs: a, rhs: b}));

return blogPosts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const DEFAULT_OPTIONS: PluginOptions = {
editLocalizedFiles: false,
authorsMapPath: 'authors.yml',
readingTime: ({content, defaultReadingTime}) => defaultReadingTime({content}),
customBlogSortingFunction: ({lhs, rhs}) => {
return rhs.metadata.date.getTime() - lhs.metadata.date.getTime();
},
};

export const PluginOptionSchema = Joi.object<PluginOptions>({
Expand Down Expand Up @@ -115,4 +118,7 @@ export const PluginOptionSchema = Joi.object<PluginOptions>({
}).default(DEFAULT_OPTIONS.feedOptions),
authorsMapPath: Joi.string().default(DEFAULT_OPTIONS.authorsMapPath),
readingTime: Joi.function().default(() => DEFAULT_OPTIONS.readingTime),
customBlogSortingFunction: Joi.function().default(
() => DEFAULT_OPTIONS.customBlogSortingFunction,
),
});
6 changes: 6 additions & 0 deletions packages/docusaurus-plugin-content-blog/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export type ReadingTimeFunctionOption = (
},
) => number | undefined;

export type BlogSortingFunctionOption = (params: {
lhs: BlogPost;
rhs: BlogPost;
}) => number;

export type PluginOptions = RemarkAndRehypePluginOptions & {
id?: string;
path: string;
Expand Down Expand Up @@ -96,6 +101,7 @@ export type PluginOptions = RemarkAndRehypePluginOptions & {
admonitions: Record<string, unknown>;
authorsMapPath: string;
readingTime: ReadingTimeFunctionOption;
customBlogSortingFunction: BlogSortingFunctionOption;
};

// Options, as provided in the user config (before normalization)
Expand Down
6 changes: 6 additions & 0 deletions website/docs/api/plugins/plugin-content-blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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. |
| `customBlogSortingFunction` | `BlogSortingFunctionOption` | chronologically sorting function | A callback to customize the blog post sorting. |

</small>

Expand Down Expand Up @@ -86,6 +87,11 @@ type ReadingTimeFunctionOption = (params: {
frontMatter: BlogPostFrontMatter & Record<string, unknown>;
defaultReadingTime: ReadingTimeFunction;
}) => number | undefined;

type BlogSortingFunctionOption = (params: {
lhs: BlogPost;
rhs: BlogPost;
}) => number;
```

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

0 comments on commit e6baf77

Please sign in to comment.