mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-17 19:16:58 +02:00
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:
parent
ee6882650e
commit
7d0272fe4d
5 changed files with 17 additions and 11 deletions
|
@ -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)
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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` | <code>number | '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. |
|
||||
| `blogPostComponent` | `string` | `'@theme/BlogPostPage'` | Root component of each blog post page. |
|
||||
| `blogTagsListComponent` | `string` | `'@theme/BlogTagsListPage'` | Root component of the tags list page |
|
||||
|
|
|
@ -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
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue