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)