Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
feat: support configuring pagination globally (#20)
Browse files Browse the repository at this point in the history
* feat: accept global pagination config

* refactor: remove lengthy definition

* chore: update example

* docs: update documentation
  • Loading branch information
billyyyyy3320 authored Sep 28, 2019
1 parent 3dd3a57 commit c156e91
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
15 changes: 13 additions & 2 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
14 changes: 4 additions & 10 deletions examples/blog/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ module.exports = {
// layout: 'IndexArchive', defaults to `Layout.vue`
itemLayout: 'Post',
itemPermalink: '/archive/:year/:month/:day/:slug',
pagination: {
lengthPerPage: 5,
},
},
],
frontmatters: [
Expand All @@ -33,21 +30,18 @@ module.exports = {
path: '/tag/',
// layout: 'Tag', defaults to `FrontmatterKey`.
frontmatter: { title: 'Tag' },
pagination: {
lengthPerPage: 3
}
},
{
id: "location",
keys: ['location'],
path: '/location/',
// layout: 'Location', defaults to `FrontmatterKey`.
frontmatter: { title: 'Location' },
pagination: {
lengthPerPage: 5
},
}
]
],
globalPagination: {
lengthPerPage: 5
}
}],
],
}
11 changes: 4 additions & 7 deletions src/node/handleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -61,9 +61,7 @@ export function handleOptions(
frontmatter,
itemLayout = 'Post',
itemPermalink = '/:year/:month/:day/:slug',
pagination = {
lengthPerPage: 10,
} as PaginationConfig,
pagination = {} as PaginationConfig,
} = directory

/**
Expand Down Expand Up @@ -115,6 +113,7 @@ export function handleOptions(
},
...resolvePaginationConfig(
ClassifierTypeEnum.Directory,
globalPagination,
pagination,
indexPath,
ctx,
Expand All @@ -135,9 +134,7 @@ export function handleOptions(
layout: indexLayout,
scopeLayout,
frontmatter,
pagination = {
lengthPerPage: 10,
} as PaginationConfig,
pagination = {} as PaginationConfig,
} = frontmatterPage

if (!indexPath) {
Expand Down
3 changes: 2 additions & 1 deletion src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
},
...resolvePaginationConfig(
ClassifierTypeEnum.Frontmatter,
options.globalPagination,
pagination,
indexPath,
ctx,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/node/interface/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ export interface FrontmatterClassifier {
export interface BlogPluginOptions {
directories: DirectoryClassifier[];
frontmatters: FrontmatterClassifier[];
globalPagination: PaginationConfig
}
20 changes: 11 additions & 9 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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']
) {
Expand Down Expand Up @@ -86,6 +87,7 @@ export function resolvePaginationConfig(
return prevTime - nextTime > 0 ? -1 : 1
},
},
globalPagination,
pagination,
)
}
Expand Down

0 comments on commit c156e91

Please sign in to comment.