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.
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)

View file

@ -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),

View file

@ -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;