feat(plugin-blog): allow 'ALL' as postsPerPage option value (#5354)

* 'ALL' option

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Guard against zero posts

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Remove redundant code

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
Joshua Chen 2021-08-17 19:07:18 +08:00 committed by GitHub
parent ee6882650e
commit 7d0272fe4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 11 deletions

View file

@ -103,7 +103,12 @@ export default function pluginContentBlog(
// Fetches blog contents and returns metadata for the necessary routes. // Fetches blog contents and returns metadata for the necessary routes.
async loadContent() { async loadContent() {
const {postsPerPage, routeBasePath} = options; const {
postsPerPage: postsPerPageOption,
routeBasePath,
blogDescription,
blogTitle,
} = options;
const blogPosts: BlogPost[] = await generateBlogPosts( const blogPosts: BlogPost[] = await generateBlogPosts(
contentPaths, contentPaths,
@ -143,6 +148,8 @@ export default function pluginContentBlog(
// Blog pagination routes. // Blog pagination routes.
// Example: `/blog`, `/blog/page/1`, `/blog/page/2` // Example: `/blog`, `/blog/page/1`, `/blog/page/2`
const totalCount = blogPosts.length; const totalCount = blogPosts.length;
const postsPerPage =
postsPerPageOption === 'ALL' ? totalCount : postsPerPageOption;
const numberOfPages = Math.ceil(totalCount / postsPerPage); const numberOfPages = Math.ceil(totalCount / postsPerPage);
const { const {
siteConfig: {baseUrl = ''}, siteConfig: {baseUrl = ''},
@ -170,8 +177,8 @@ export default function pluginContentBlog(
page < numberOfPages - 1 page < numberOfPages - 1
? blogPaginationPermalink(page + 1) ? blogPaginationPermalink(page + 1)
: null, : null,
blogDescription: options.blogDescription, blogDescription,
blogTitle: options.blogTitle, blogTitle,
}, },
items: blogPosts items: blogPosts
.slice(page * postsPerPage, (page + 1) * postsPerPage) .slice(page * postsPerPage, (page + 1) * postsPerPage)

View file

@ -47,9 +47,8 @@ export const PluginOptionSchema = Joi.object({
.default(DEFAULT_OPTIONS.routeBasePath), .default(DEFAULT_OPTIONS.routeBasePath),
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include), include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude), exclude: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.exclude),
postsPerPage: Joi.number() postsPerPage: Joi.alternatives()
.integer() .try(Joi.equal('ALL').required(), Joi.number().integer().min(1).required())
.min(1)
.default(DEFAULT_OPTIONS.postsPerPage), .default(DEFAULT_OPTIONS.postsPerPage),
blogListComponent: Joi.string().default(DEFAULT_OPTIONS.blogListComponent), blogListComponent: Joi.string().default(DEFAULT_OPTIONS.blogListComponent),
blogPostComponent: Joi.string().default(DEFAULT_OPTIONS.blogPostComponent), blogPostComponent: Joi.string().default(DEFAULT_OPTIONS.blogPostComponent),
@ -64,7 +63,7 @@ export const PluginOptionSchema = Joi.object({
.allow('') .allow('')
.default(DEFAULT_OPTIONS.blogDescription), .default(DEFAULT_OPTIONS.blogDescription),
blogSidebarCount: Joi.alternatives() 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), .default(DEFAULT_OPTIONS.blogSidebarCount),
blogSidebarTitle: Joi.string().default(DEFAULT_OPTIONS.blogSidebarTitle), blogSidebarTitle: Joi.string().default(DEFAULT_OPTIONS.blogSidebarTitle),
showReadingTime: Joi.bool().default(DEFAULT_OPTIONS.showReadingTime), showReadingTime: Joi.bool().default(DEFAULT_OPTIONS.showReadingTime),

View file

@ -35,7 +35,7 @@ export interface PluginOptions extends RemarkAndRehypePluginOptions {
routeBasePath: string; routeBasePath: string;
include: string[]; include: string[];
exclude: string[]; exclude: string[];
postsPerPage: number; postsPerPage: number | 'ALL';
blogListComponent: string; blogListComponent: string;
blogPostComponent: string; blogPostComponent: string;
blogTagsListComponent: string; blogTagsListComponent: string;

View file

@ -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. | | `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. | | `include` | `string[]` | `['**/*.{md,mdx}']` | Matching files will be included and processed. |
| `exclude` | `string[]` | _See example configuration_ | No route will be created for matching files. | | `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` | <code>number &#124; 'ALL'</code> | `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. | | `blogListComponent` | `string` | `'@theme/BlogListPage'` | Root component of the blog listing page. |
| `blogPostComponent` | `string` | `'@theme/BlogPostPage'` | Root component of each blog post page. | | `blogPostComponent` | `string` | `'@theme/BlogPostPage'` | Root component of each blog post page. |
| `blogTagsListComponent` | `string` | `'@theme/BlogTagsListPage'` | Root component of the tags list page | | `blogTagsListComponent` | `string` | `'@theme/BlogTagsListPage'` | Root component of the tags list page |

View file

@ -112,7 +112,7 @@ Not this.
Or 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" ```js title="docusaurus.config.js"
module.exports = { module.exports = {
@ -125,7 +125,7 @@ module.exports = {
// highlight-start // highlight-start
blogTitle: 'Docusaurus blog!', blogTitle: 'Docusaurus blog!',
blogDescription: 'A Docusaurus powered blog!', blogDescription: 'A Docusaurus powered blog!',
postsPerPage: 20, postsPerPage: 'ALL',
// highlight-end // highlight-end
}, },
}, },