From 7d0272fe4dea4c519b5fcbd9221ff2e3be13fcf7 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Tue, 17 Aug 2021 19:07:18 +0800 Subject: [PATCH] feat(plugin-blog): allow `'ALL'` as `postsPerPage` option value (#5354) * 'ALL' option Signed-off-by: Josh-Cena * Guard against zero posts Signed-off-by: Josh-Cena * Remove redundant code Signed-off-by: Josh-Cena --- .../docusaurus-plugin-content-blog/src/index.ts | 13 ++++++++++--- .../src/pluginOptionSchema.ts | 7 +++---- .../docusaurus-plugin-content-blog/src/types.ts | 2 +- website/docs/api/plugins/plugin-content-blog.md | 2 +- website/docs/blog.md | 4 ++-- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/index.ts b/packages/docusaurus-plugin-content-blog/src/index.ts index de672bae40..6789078fda 100644 --- a/packages/docusaurus-plugin-content-blog/src/index.ts +++ b/packages/docusaurus-plugin-content-blog/src/index.ts @@ -103,7 +103,12 @@ export default function pluginContentBlog( // Fetches blog contents and returns metadata for the necessary routes. async loadContent() { - const {postsPerPage, routeBasePath} = options; + const { + postsPerPage: postsPerPageOption, + routeBasePath, + blogDescription, + blogTitle, + } = options; const blogPosts: BlogPost[] = await generateBlogPosts( contentPaths, @@ -143,6 +148,8 @@ export default function pluginContentBlog( // Blog pagination routes. // Example: `/blog`, `/blog/page/1`, `/blog/page/2` const totalCount = blogPosts.length; + const postsPerPage = + postsPerPageOption === 'ALL' ? totalCount : postsPerPageOption; const numberOfPages = Math.ceil(totalCount / postsPerPage); const { siteConfig: {baseUrl = ''}, @@ -170,8 +177,8 @@ export default function pluginContentBlog( page < numberOfPages - 1 ? blogPaginationPermalink(page + 1) : null, - blogDescription: options.blogDescription, - blogTitle: options.blogTitle, + blogDescription, + blogTitle, }, items: blogPosts .slice(page * postsPerPage, (page + 1) * postsPerPage) diff --git a/packages/docusaurus-plugin-content-blog/src/pluginOptionSchema.ts b/packages/docusaurus-plugin-content-blog/src/pluginOptionSchema.ts index 3e79dafb27..247cb2bec6 100644 --- a/packages/docusaurus-plugin-content-blog/src/pluginOptionSchema.ts +++ b/packages/docusaurus-plugin-content-blog/src/pluginOptionSchema.ts @@ -47,9 +47,8 @@ export const PluginOptionSchema = Joi.object({ .default(DEFAULT_OPTIONS.routeBasePath), include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include), exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude), - postsPerPage: Joi.number() - .integer() - .min(1) + postsPerPage: Joi.alternatives() + .try(Joi.equal('ALL').required(), Joi.number().integer().min(1).required()) .default(DEFAULT_OPTIONS.postsPerPage), blogListComponent: Joi.string().default(DEFAULT_OPTIONS.blogListComponent), blogPostComponent: Joi.string().default(DEFAULT_OPTIONS.blogPostComponent), @@ -64,7 +63,7 @@ export const PluginOptionSchema = Joi.object({ .allow('') .default(DEFAULT_OPTIONS.blogDescription), blogSidebarCount: Joi.alternatives() - .try(Joi.equal('ALL').required(), Joi.number().required()) + .try(Joi.equal('ALL').required(), Joi.number().integer().min(0).required()) .default(DEFAULT_OPTIONS.blogSidebarCount), blogSidebarTitle: Joi.string().default(DEFAULT_OPTIONS.blogSidebarTitle), showReadingTime: Joi.bool().default(DEFAULT_OPTIONS.showReadingTime), diff --git a/packages/docusaurus-plugin-content-blog/src/types.ts b/packages/docusaurus-plugin-content-blog/src/types.ts index f4b59e305a..c7d3f3d3bb 100644 --- a/packages/docusaurus-plugin-content-blog/src/types.ts +++ b/packages/docusaurus-plugin-content-blog/src/types.ts @@ -35,7 +35,7 @@ export interface PluginOptions extends RemarkAndRehypePluginOptions { routeBasePath: string; include: string[]; exclude: string[]; - postsPerPage: number; + postsPerPage: number | 'ALL'; blogListComponent: string; blogPostComponent: string; blogTagsListComponent: string; diff --git a/website/docs/api/plugins/plugin-content-blog.md b/website/docs/api/plugins/plugin-content-blog.md index 75ad752840..4f55cd768c 100644 --- a/website/docs/api/plugins/plugin-content-blog.md +++ b/website/docs/api/plugins/plugin-content-blog.md @@ -38,7 +38,7 @@ Accepted fields: | `routeBasePath` | `string` | `'blog'` | URL route for the blog section of your site. **DO NOT** include a trailing slash. Use `/` to put the blog at root path. | | `include` | `string[]` | `['**/*.{md,mdx}']` | Matching files will be included and processed. | | `exclude` | `string[]` | _See example configuration_ | No route will be created for matching files. | -| `postsPerPage` | `number` | `10` | Number of posts to show per page in the listing page. | +| `postsPerPage` | number | 'ALL' | `10` | Number of posts to show per page in the listing page. Use `'ALL'` to display all posts on one listing page. | | `blogListComponent` | `string` | `'@theme/BlogListPage'` | Root component of the blog listing page. | | `blogPostComponent` | `string` | `'@theme/BlogPostPage'` | Root component of each blog post page. | | `blogTagsListComponent` | `string` | `'@theme/BlogTagsListPage'` | Root component of the tags list page | diff --git a/website/docs/blog.md b/website/docs/blog.md index 6e7184e0ee..8488920631 100644 --- a/website/docs/blog.md +++ b/website/docs/blog.md @@ -112,7 +112,7 @@ Not this. Or this. ``` -By default, 10 posts are shown on each blog list page, but you can control pagination with the `postsPerPage` option in the plugin configuration. You can also add meta description to the blog list page for better SEO: +By default, 10 posts are shown on each blog list page, but you can control pagination with the `postsPerPage` option in the plugin configuration. If you set `postsPerPage: 'ALL'`, pagination will be disabled and all posts will be displayed on the first page. You can also add meta description to the blog list page for better SEO: ```js title="docusaurus.config.js" module.exports = { @@ -125,7 +125,7 @@ module.exports = { // highlight-start blogTitle: 'Docusaurus blog!', blogDescription: 'A Docusaurus powered blog!', - postsPerPage: 20, + postsPerPage: 'ALL', // highlight-end }, },