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

feat: support configuring title api for directories and frontmatters (close #52) #55

Merged
merged 1 commit into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ Entry page for current classifier, e.g. `/` or `/post/`.
If you set `DirectoryClassifier.path` to `/`, it means that you want to access the matched pages list at `/`. set
to `/post/` is the same.

### title

- Type: `string`
- Default: `id`
- Required: `false`

Entry and pagination page titles for current classifier.

### layout

- Type: `string`
Expand Down Expand Up @@ -146,6 +154,13 @@ module.exports = {

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

### title

- Type: `string`
- Default: `id`
- Required: `false`

Entry, scope and pagination page titles for current classifier.

### layout

Expand Down
22 changes: 7 additions & 15 deletions docs/pagination/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,16 @@ A function to get the title of pagination page dynamically:

```js
// directories
function getPaginationPageTitle (index, id) {
return `Page ${index + 2} | ${id}`
function getPaginationPageTitle (pageNumber) {
return `Page ${pageNumber} | ${entryTitle}`
}

// frontmatters
function getPaginationPageTitle (index, id, scope) {
return `Page ${index + 2} - ${id} | ${scope}`
function getPaginationPageTitle (pageNumber, key) {
return `Page ${pageNumber} - ${key} | ${entryTitle}`
}
```

There are three args to help you customize your title:
- `index` is the index of pages.
- `id` is the id in the [config](../config/#id).
- `scope` is the [key](../config/#keys) while configuring frontmatters or same as `id` while configuring directories.

::: warning Note
`${index + 2}`: why `+2`?

Plus 1 since index starts at 0. <br>
Plus another 1 since the index page won't show page number.
:::
There are two args to help you customize your title:
- `pageNumber`
- `key` : the [key](../config/#keys) while configuring frontmatters
11 changes: 7 additions & 4 deletions src/node/handleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function handleOptions(
pagination = {} as PaginationConfig,
} = directory;

const { title = UpperFirstChar(id) } = directory;
/**
* 1.1 Required index path.
*/
Expand All @@ -86,7 +87,7 @@ export function handleOptions(
frontmatter: {
// Set layout for index page.
layout: ctx.getLayout(indexLayout),
title: `${UpperFirstChar(id)}`,
title,
...frontmatter,
},
meta: {
Expand Down Expand Up @@ -125,8 +126,8 @@ export function handleOptions(
*/
paginations.push({
classifierType: ClassifierTypeEnum.Directory,
getPaginationPageTitle(index, id) {
return `Page ${index + 2} | ${id}`;
getPaginationPageTitle(pageNumber) {
return `Page ${pageNumber} | ${title}`;
},
...resolvePaginationConfig(
ClassifierTypeEnum.Directory,
Expand All @@ -153,6 +154,7 @@ export function handleOptions(
frontmatter,
pagination = {} as PaginationConfig,
} = frontmatterPage;
const { title = UpperFirstChar(id) } = frontmatterPage;

if (!indexPath) {
continue;
Expand All @@ -163,7 +165,7 @@ export function handleOptions(
frontmatter: {
// Set layout for index page.
layout: ctx.getLayout(indexLayout, 'FrontmatterKey'),
title: `${UpperFirstChar(id)}`,
title,
...frontmatter,
},
meta: {
Expand All @@ -176,6 +178,7 @@ export function handleOptions(

frontmatterClassificationPages.push({
id,
entryTitle: title,
pagination,
keys,
map,
Expand Down
7 changes: 4 additions & 3 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
...frontmatterClassificationPages
.map(frontmatterClassifiedPage => {
const {
entryTitle,
map,
pagination,
keys,
Expand All @@ -164,8 +165,8 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
*/
paginations.push({
classifierType: ClassifierTypeEnum.Frontmatter,
getPaginationPageTitle(index, id, scope) {
return `Page ${index + 2} - ${id} | ${scope}`;
getPaginationPageTitle(pageNumber, key) {
return `Page ${pageNumber} - ${key} | ${entryTitle}`;
},
...resolvePaginationConfig(
ClassifierTypeEnum.Frontmatter,
Expand All @@ -192,7 +193,7 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
scopeLayout,
DefaultLayoutEnum.FrontmatterPagination
),
title: `${key} ${scope}`,
title: `${key} ${entryTitle}`,
},
};
});
Expand Down
1 change: 1 addition & 0 deletions src/node/interface/Frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PaginationConfig } from './Pagination';

export interface FrontmatterClassificationPage {
id: string;
entryTitle: string;
pagination: PaginationConfig;
keys: string[];
scopeLayout?: string;
Expand Down
8 changes: 8 additions & 0 deletions src/node/interface/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface DirectoryClassifier {
* Entry page for current classifier.
*/
path: string;
/**
* Entry and pagination page titles for current classifier.
*/
title?: string;
/**
* Layout component name for entry page.
*/
Expand Down Expand Up @@ -57,6 +61,10 @@ export interface FrontmatterClassifier {
* Index page for current classifier.
*/
path: string;
/**
* Entry, scope and pagination page titles for current classifier.
*/
title?: string;
/**
* Layout for index page.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/node/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ export async function registerPaginations(
const extraPages = pagination.pages
.slice(1) // The index page has been generated.
.map(({ path }, index) => {
const pageNumber = index + 2;
return {
permalink: path,
frontmatter: {
layout,
title: (getPaginationPageTitle as GetPaginationPageTitle)(
index,
pageNumber,
id,
pid
),
Expand Down