feat(content-blog): allow sorting posts in ascending order (#5787)

This commit is contained in:
Devtato 2021-11-09 18:25:43 +01:00 committed by GitHub
parent 8fba542d26
commit 1c024470e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 0 deletions

View file

@ -394,4 +394,15 @@ describe('loadBlog', () => {
truncated: false,
});
});
test('test ascending sort direction of blog post', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const normalOrder = await getBlogPosts(siteDir);
const reversedOrder = await getBlogPosts(siteDir, {
sortPosts: 'ascending',
});
expect(normalOrder.reverse().map((x) => x.metadata.date)).toEqual(
reversedOrder.map((x) => x.metadata.date),
);
});
});

View file

@ -294,6 +294,9 @@ export async function generateBlogPosts(
(a, b) => b.metadata.date.getTime() - a.metadata.date.getTime(),
);
if (options.sortPosts === 'ascending') {
return blogPosts.reverse();
}
return blogPosts;
}

View file

@ -42,6 +42,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
editLocalizedFiles: false,
authorsMapPath: 'authors.yml',
readingTime: ({content, defaultReadingTime}) => defaultReadingTime({content}),
sortPosts: 'descending',
};
export const PluginOptionSchema = Joi.object<PluginOptions>({
@ -115,4 +116,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),
sortPosts: Joi.string()
.valid('descending', 'ascending')
.default(DEFAULT_OPTIONS.sortPosts),
});

View file

@ -96,6 +96,7 @@ export type PluginOptions = RemarkAndRehypePluginOptions & {
admonitions: Record<string, unknown>;
authorsMapPath: string;
readingTime: ReadingTimeFunctionOption;
sortPosts: 'ascending' | 'descending';
};
// Options, as provided in the user config (before normalization)

View file

@ -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. |
| `sortPosts` | <code>'descending' \| 'ascending' </code> | `'descending'` | Governs the direction of blog post sorting. |
</small>