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

Commit b6325cd

Browse files
feat: support configuring title for directories and frontmatters (close #52)(#55)
1 parent 3fdc6a3 commit b6325cd

File tree

7 files changed

+44
-23
lines changed

7 files changed

+44
-23
lines changed

docs/config/README.md

+15
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

+7-15
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,16 @@ A function to get the title of pagination page dynamically:
7272

7373
```js
7474
// directories
75-
function getPaginationPageTitle (index, id) {
76-
return `Page ${index + 2} | ${id}`
75+
function getPaginationPageTitle (pageNumber) {
76+
return `Page ${pageNumber} | ${entryTitle}`
7777
}
7878

7979
// frontmatters
80-
function getPaginationPageTitle (index, id, scope) {
81-
return `Page ${index + 2} - ${id} | ${scope}`
80+
function getPaginationPageTitle (pageNumber, key) {
81+
return `Page ${pageNumber} - ${key} | ${entryTitle}`
8282
}
8383
```
8484

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

src/node/handleOptions.ts

+7-4
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

+4-3
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

+1
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

+8
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

+2-1
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)