From c156e91cafd090aa77f64b144fe58b545e0789a3 Mon Sep 17 00:00:00 2001 From: Billyyyyy3320 Date: Sat, 28 Sep 2019 13:35:07 +0800 Subject: [PATCH] feat: support configuring pagination globally (#20) * feat: accept global pagination config * refactor: remove lengthy definition * chore: update example * docs: update documentation --- docs/config/README.md | 15 +++++++++++++-- examples/blog/.vuepress/config.js | 14 ++++---------- src/node/handleOptions.ts | 11 ++++------- src/node/index.ts | 3 ++- src/node/interface/Options.ts | 1 + src/node/util.ts | 20 +++++++++++--------- 6 files changed, 35 insertions(+), 29 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index ce63b3c..e859a40 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -97,9 +97,10 @@ For more details about permalinks, please head to [Permalinks](https://v1.vuepre ### pagination - Type: `Pagination` -- Default: `{ lengthPerPage: 10 }` - Required: `false` +It can overwrite [globalPagination](./#globalpagination). + Please head to [Pagination Config](../pagination/README.md#config) section to get all available options. ## frontmatters @@ -173,7 +174,17 @@ Layout component name for scope page. ### pagination - Type: `Pagination` -- Default: `{ lengthPerPage: 10 }` +- Required: `false` + +It can overwrite [globalPagination](./#globalpagination). + +Please head to [Pagination Config](../pagination/README.md#config) section to get all available options. + +## globalPagination + +Pagination config for all directories and frontmatters. + +- Type: `Pagination` - Required: `false` Please head to [Pagination Config](../pagination/README.md#config) section to get all available options. diff --git a/examples/blog/.vuepress/config.js b/examples/blog/.vuepress/config.js index 7bf3e5c..2b4f0ad 100644 --- a/examples/blog/.vuepress/config.js +++ b/examples/blog/.vuepress/config.js @@ -21,9 +21,6 @@ module.exports = { // layout: 'IndexArchive', defaults to `Layout.vue` itemLayout: 'Post', itemPermalink: '/archive/:year/:month/:day/:slug', - pagination: { - lengthPerPage: 5, - }, }, ], frontmatters: [ @@ -33,9 +30,6 @@ module.exports = { path: '/tag/', // layout: 'Tag', defaults to `FrontmatterKey`. frontmatter: { title: 'Tag' }, - pagination: { - lengthPerPage: 3 - } }, { id: "location", @@ -43,11 +37,11 @@ module.exports = { path: '/location/', // layout: 'Location', defaults to `FrontmatterKey`. frontmatter: { title: 'Location' }, - pagination: { - lengthPerPage: 5 - }, } - ] + ], + globalPagination: { + lengthPerPage: 5 + } }], ], } diff --git a/src/node/handleOptions.ts b/src/node/handleOptions.ts index 92cf379..e0a4e80 100644 --- a/src/node/handleOptions.ts +++ b/src/node/handleOptions.ts @@ -24,7 +24,7 @@ export function handleOptions( options: BlogPluginOptions, ctx: VuePressContext, ) { - let { directories = [], frontmatters = [] } = options + let { directories = [], frontmatters = [], globalPagination = {} as PaginationConfig } = options /** * Validate the existence of directory specified by directory classifier. @@ -61,9 +61,7 @@ export function handleOptions( frontmatter, itemLayout = 'Post', itemPermalink = '/:year/:month/:day/:slug', - pagination = { - lengthPerPage: 10, - } as PaginationConfig, + pagination = {} as PaginationConfig, } = directory /** @@ -115,6 +113,7 @@ export function handleOptions( }, ...resolvePaginationConfig( ClassifierTypeEnum.Directory, + globalPagination, pagination, indexPath, ctx, @@ -135,9 +134,7 @@ export function handleOptions( layout: indexLayout, scopeLayout, frontmatter, - pagination = { - lengthPerPage: 10, - } as PaginationConfig, + pagination = {} as PaginationConfig, } = frontmatterPage if (!indexPath) { diff --git a/src/node/index.ts b/src/node/index.ts index 40b392a..4c4ac0e 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -111,6 +111,7 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => { }, ...resolvePaginationConfig( ClassifierTypeEnum.Frontmatter, + options.globalPagination, pagination, indexPath, ctx, @@ -215,7 +216,7 @@ function mapToString(map, unstringedKeys: string[] | boolean = []) { keys === true || (Array.isArray(keys) && keys.includes(key)) ? map[key] : JSON.stringify(map[key]) - },\n` + },\n` } str += '}' return str diff --git a/src/node/interface/Options.ts b/src/node/interface/Options.ts index 0d846de..12256c8 100644 --- a/src/node/interface/Options.ts +++ b/src/node/interface/Options.ts @@ -83,4 +83,5 @@ export interface FrontmatterClassifier { export interface BlogPluginOptions { directories: DirectoryClassifier[]; frontmatters: FrontmatterClassifier[]; + globalPagination: PaginationConfig } diff --git a/src/node/util.ts b/src/node/util.ts index 5a7ad1e..dca932b 100644 --- a/src/node/util.ts +++ b/src/node/util.ts @@ -40,13 +40,13 @@ export function logPages(title, pages) { data.push( ...pages.map(({ // @ts-ignore path, permalink, meta, pid, id, frontmatter }) => [ - // @ts-ignore // @ts-ignore - permalink || path || '', - JSON.stringify(meta) || '', - pid || '', - id || '', - JSON.stringify(frontmatter) || '', - ]), + // @ts-ignore // @ts-ignore + permalink || path || '', + JSON.stringify(meta) || '', + pid || '', + id || '', + JSON.stringify(frontmatter) || '', + ]), ) console.log(table(data)) console.log() @@ -55,8 +55,9 @@ export function logPages(title, pages) { export function resolvePaginationConfig( classifierType: ClassifierTypeEnum, - pagination = {} as PaginationConfig, - indexPath, + globalPagination: PaginationConfig, + pagination: PaginationConfig, + indexPath: string, ctx: VuePressContext, keys: string[] = [''], // ['js'] ) { @@ -86,6 +87,7 @@ export function resolvePaginationConfig( return prevTime - nextTime > 0 ? -1 : 1 }, }, + globalPagination, pagination, ) }