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

Commit c156e91

Browse files
feat: support configuring pagination globally (#20)
* feat: accept global pagination config * refactor: remove lengthy definition * chore: update example * docs: update documentation
1 parent 3dd3a57 commit c156e91

File tree

6 files changed

+35
-29
lines changed

6 files changed

+35
-29
lines changed

Diff for: docs/config/README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ For more details about permalinks, please head to [Permalinks](https://v1.vuepre
9797
### pagination
9898

9999
- Type: `Pagination`
100-
- Default: `{ lengthPerPage: 10 }`
101100
- Required: `false`
102101

102+
It can overwrite [globalPagination](./#globalpagination).
103+
103104
Please head to [Pagination Config](../pagination/README.md#config) section to get all available options.
104105

105106
## frontmatters
@@ -173,7 +174,17 @@ Layout component name for scope page.
173174
### pagination
174175

175176
- Type: `Pagination`
176-
- Default: `{ lengthPerPage: 10 }`
177+
- Required: `false`
178+
179+
It can overwrite [globalPagination](./#globalpagination).
180+
181+
Please head to [Pagination Config](../pagination/README.md#config) section to get all available options.
182+
183+
## globalPagination
184+
185+
Pagination config for all directories and frontmatters.
186+
187+
- Type: `Pagination`
177188
- Required: `false`
178189

179190
Please head to [Pagination Config](../pagination/README.md#config) section to get all available options.

Diff for: examples/blog/.vuepress/config.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ module.exports = {
2121
// layout: 'IndexArchive', defaults to `Layout.vue`
2222
itemLayout: 'Post',
2323
itemPermalink: '/archive/:year/:month/:day/:slug',
24-
pagination: {
25-
lengthPerPage: 5,
26-
},
2724
},
2825
],
2926
frontmatters: [
@@ -33,21 +30,18 @@ module.exports = {
3330
path: '/tag/',
3431
// layout: 'Tag', defaults to `FrontmatterKey`.
3532
frontmatter: { title: 'Tag' },
36-
pagination: {
37-
lengthPerPage: 3
38-
}
3933
},
4034
{
4135
id: "location",
4236
keys: ['location'],
4337
path: '/location/',
4438
// layout: 'Location', defaults to `FrontmatterKey`.
4539
frontmatter: { title: 'Location' },
46-
pagination: {
47-
lengthPerPage: 5
48-
},
4940
}
50-
]
41+
],
42+
globalPagination: {
43+
lengthPerPage: 5
44+
}
5145
}],
5246
],
5347
}

Diff for: src/node/handleOptions.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function handleOptions(
2424
options: BlogPluginOptions,
2525
ctx: VuePressContext,
2626
) {
27-
let { directories = [], frontmatters = [] } = options
27+
let { directories = [], frontmatters = [], globalPagination = {} as PaginationConfig } = options
2828

2929
/**
3030
* Validate the existence of directory specified by directory classifier.
@@ -61,9 +61,7 @@ export function handleOptions(
6161
frontmatter,
6262
itemLayout = 'Post',
6363
itemPermalink = '/:year/:month/:day/:slug',
64-
pagination = {
65-
lengthPerPage: 10,
66-
} as PaginationConfig,
64+
pagination = {} as PaginationConfig,
6765
} = directory
6866

6967
/**
@@ -115,6 +113,7 @@ export function handleOptions(
115113
},
116114
...resolvePaginationConfig(
117115
ClassifierTypeEnum.Directory,
116+
globalPagination,
118117
pagination,
119118
indexPath,
120119
ctx,
@@ -135,9 +134,7 @@ export function handleOptions(
135134
layout: indexLayout,
136135
scopeLayout,
137136
frontmatter,
138-
pagination = {
139-
lengthPerPage: 10,
140-
} as PaginationConfig,
137+
pagination = {} as PaginationConfig,
141138
} = frontmatterPage
142139

143140
if (!indexPath) {

Diff for: src/node/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
111111
},
112112
...resolvePaginationConfig(
113113
ClassifierTypeEnum.Frontmatter,
114+
options.globalPagination,
114115
pagination,
115116
indexPath,
116117
ctx,
@@ -215,7 +216,7 @@ function mapToString(map, unstringedKeys: string[] | boolean = []) {
215216
keys === true || (Array.isArray(keys) && keys.includes(key))
216217
? map[key]
217218
: JSON.stringify(map[key])
218-
},\n`
219+
},\n`
219220
}
220221
str += '}'
221222
return str

Diff for: src/node/interface/Options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@ export interface FrontmatterClassifier {
8383
export interface BlogPluginOptions {
8484
directories: DirectoryClassifier[];
8585
frontmatters: FrontmatterClassifier[];
86+
globalPagination: PaginationConfig
8687
}

Diff for: src/node/util.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ export function logPages(title, pages) {
4040
data.push(
4141
...pages.map(({ // @ts-ignore
4242
path, permalink, meta, pid, id, frontmatter }) => [
43-
// @ts-ignore // @ts-ignore
44-
permalink || path || '',
45-
JSON.stringify(meta) || '',
46-
pid || '',
47-
id || '',
48-
JSON.stringify(frontmatter) || '',
49-
]),
43+
// @ts-ignore // @ts-ignore
44+
permalink || path || '',
45+
JSON.stringify(meta) || '',
46+
pid || '',
47+
id || '',
48+
JSON.stringify(frontmatter) || '',
49+
]),
5050
)
5151
console.log(table(data))
5252
console.log()
@@ -55,8 +55,9 @@ export function logPages(title, pages) {
5555

5656
export function resolvePaginationConfig(
5757
classifierType: ClassifierTypeEnum,
58-
pagination = {} as PaginationConfig,
59-
indexPath,
58+
globalPagination: PaginationConfig,
59+
pagination: PaginationConfig,
60+
indexPath: string,
6061
ctx: VuePressContext,
6162
keys: string[] = [''], // ['js']
6263
) {
@@ -86,6 +87,7 @@ export function resolvePaginationConfig(
8687
return prevTime - nextTime > 0 ? -1 : 1
8788
},
8889
},
90+
globalPagination,
8991
pagination,
9092
)
9193
}

0 commit comments

Comments
 (0)