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

Commit 716ef99

Browse files
committed
feat: support configuring title api for directories and frontmatters
1 parent 92fae58 commit 716ef99

File tree

7 files changed

+44
-23
lines changed

7 files changed

+44
-23
lines changed

docs/config/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ Entry page for current classifier, e.g. `/` or `/post/`.
4444
If you set `DirectoryClassifier.path` to `/`, it means that you want to access the matched pages list at `/`. set
4545
to `/post/` is the same.
4646

47+
### title
48+
49+
- Type: `string`
50+
- Default: `id`
51+
- Required: `false`
52+
53+
Entry and pagination page titles for current classifier.
54+
4755
### layout
4856

4957
- Type: `string`
@@ -146,6 +154,13 @@ module.exports = {
146154

147155
Entry page for current classifier, e.g. `/` or `/post/`.
148156

157+
### title
158+
159+
- Type: `string`
160+
- Default: `id`
161+
- Required: `false`
162+
163+
Entry, scope and pagination page titles for current classifier.
149164

150165
### layout
151166

docs/pagination/README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,16 @@ A function to get the title of pagination page dynamically:
6868

6969
```js
7070
// directories
71-
function getPaginationPageTitle (index, id) {
72-
return `Page ${index + 2} | ${id}`
71+
function getPaginationPageTitle (pageNumber) {
72+
return `Page ${pageNumber} | ${entryTitle}`
7373
}
7474

7575
// frontmatters
76-
function getPaginationPageTitle (index, id, scope) {
77-
return `Page ${index + 2} - ${id} | ${scope}`
76+
function getPaginationPageTitle (pageNumber, key) {
77+
return `Page ${pageNumber} - ${key} | ${entryTitle}`
7878
}
7979
```
8080

81-
There are three args to help you customize your title:
82-
- `index` is the index of pages.
83-
- `id` is the id in the [config](../config/#id).
84-
- `scope` is the [key](../config/#keys) while configuring frontmatters or same as `id` while configuring directories.
85-
86-
::: tip TIP
87-
`${index + 2}`: why `+2`?
88-
89-
Plus 1 since index starts at 0. <br>
90-
Plus another 1 since the index page won't show page number.
91-
:::
81+
There are two args to help you customize your title:
82+
- `pageNumber`
83+
- `key` : the [key](../config/#keys) while configuring frontmatters

src/node/handleOptions.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function handleOptions(
7171
pagination = {} as PaginationConfig,
7272
} = directory;
7373

74+
const { title = UpperFirstChar(id) } = directory;
7475
/**
7576
* 1.1 Required index path.
7677
*/
@@ -86,7 +87,7 @@ export function handleOptions(
8687
frontmatter: {
8788
// Set layout for index page.
8889
layout: ctx.getLayout(indexLayout),
89-
title: `${UpperFirstChar(id)}`,
90+
title,
9091
...frontmatter,
9192
},
9293
meta: {
@@ -125,8 +126,8 @@ export function handleOptions(
125126
*/
126127
paginations.push({
127128
classifierType: ClassifierTypeEnum.Directory,
128-
getPaginationPageTitle(index, id) {
129-
return `Page ${index + 2} | ${id}`;
129+
getPaginationPageTitle(pageNumber) {
130+
return `Page ${pageNumber} | ${title}`;
130131
},
131132
...resolvePaginationConfig(
132133
ClassifierTypeEnum.Directory,
@@ -153,6 +154,7 @@ export function handleOptions(
153154
frontmatter,
154155
pagination = {} as PaginationConfig,
155156
} = frontmatterPage;
157+
const { title = UpperFirstChar(id) } = frontmatterPage;
156158

157159
if (!indexPath) {
158160
continue;
@@ -163,7 +165,7 @@ export function handleOptions(
163165
frontmatter: {
164166
// Set layout for index page.
165167
layout: ctx.getLayout(indexLayout, 'FrontmatterKey'),
166-
title: `${UpperFirstChar(id)}`,
168+
title,
167169
...frontmatter,
168170
},
169171
meta: {
@@ -176,6 +178,7 @@ export function handleOptions(
176178

177179
frontmatterClassificationPages.push({
178180
id,
181+
entryTitle: title,
179182
pagination,
180183
keys,
181184
map,

src/node/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
151151
...frontmatterClassificationPages
152152
.map(frontmatterClassifiedPage => {
153153
const {
154+
entryTitle,
154155
map,
155156
pagination,
156157
keys,
@@ -164,8 +165,8 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
164165
*/
165166
paginations.push({
166167
classifierType: ClassifierTypeEnum.Frontmatter,
167-
getPaginationPageTitle(index, id, scope) {
168-
return `Page ${index + 2} - ${id} | ${scope}`;
168+
getPaginationPageTitle(pageNumber, key) {
169+
return `Page ${pageNumber} - ${key} | ${entryTitle}`;
169170
},
170171
...resolvePaginationConfig(
171172
ClassifierTypeEnum.Frontmatter,
@@ -192,7 +193,7 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
192193
scopeLayout,
193194
DefaultLayoutEnum.FrontmatterPagination
194195
),
195-
title: `${key} ${scope}`,
196+
title: `${key} ${entryTitle}`,
196197
},
197198
};
198199
});

src/node/interface/Frontmatter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PaginationConfig } from './Pagination';
33

44
export interface FrontmatterClassificationPage {
55
id: string;
6+
entryTitle: string;
67
pagination: PaginationConfig;
78
keys: string[];
89
scopeLayout?: string;

src/node/interface/Options.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export interface DirectoryClassifier {
1616
* Entry page for current classifier.
1717
*/
1818
path: string;
19+
/**
20+
* Entry and pagination page titles for current classifier.
21+
*/
22+
title?: string;
1923
/**
2024
* Layout component name for entry page.
2125
*/
@@ -57,6 +61,10 @@ export interface FrontmatterClassifier {
5761
* Index page for current classifier.
5862
*/
5963
path: string;
64+
/**
65+
* Entry, scope and pagination page titles for current classifier.
66+
*/
67+
title?: string;
6068
/**
6169
* Layout for index page.
6270
*/

src/node/pagination.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ export async function registerPaginations(
8383
const extraPages = pagination.pages
8484
.slice(1) // The index page has been generated.
8585
.map(({ path }, index) => {
86+
const pageNumber = index + 2;
8687
return {
8788
permalink: path,
8889
frontmatter: {
8990
layout,
9091
title: (getPaginationPageTitle as GetPaginationPageTitle)(
91-
index,
92+
pageNumber,
9293
id,
9394
pid
9495
),

0 commit comments

Comments
 (0)