From b6325cd06a8e66e4fb6939e82c23107f45c33742 Mon Sep 17 00:00:00 2001 From: Billyyyyy3320 Date: Thu, 9 Jan 2020 23:26:04 +0800 Subject: [PATCH] feat: support configuring title for directories and frontmatters (close #52)(#55) --- docs/config/README.md | 15 +++++++++++++++ docs/pagination/README.md | 22 +++++++--------------- src/node/handleOptions.ts | 11 +++++++---- src/node/index.ts | 7 ++++--- src/node/interface/Frontmatter.ts | 1 + src/node/interface/Options.ts | 8 ++++++++ src/node/pagination.ts | 3 ++- 7 files changed, 44 insertions(+), 23 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index b1487ad..65457da 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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` @@ -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 diff --git a/docs/pagination/README.md b/docs/pagination/README.md index ed3dbb1..707ccb0 100644 --- a/docs/pagination/README.md +++ b/docs/pagination/README.md @@ -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.
-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 diff --git a/src/node/handleOptions.ts b/src/node/handleOptions.ts index c6ded23..be664f4 100644 --- a/src/node/handleOptions.ts +++ b/src/node/handleOptions.ts @@ -71,6 +71,7 @@ export function handleOptions( pagination = {} as PaginationConfig, } = directory; + const { title = UpperFirstChar(id) } = directory; /** * 1.1 Required index path. */ @@ -86,7 +87,7 @@ export function handleOptions( frontmatter: { // Set layout for index page. layout: ctx.getLayout(indexLayout), - title: `${UpperFirstChar(id)}`, + title, ...frontmatter, }, meta: { @@ -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, @@ -153,6 +154,7 @@ export function handleOptions( frontmatter, pagination = {} as PaginationConfig, } = frontmatterPage; + const { title = UpperFirstChar(id) } = frontmatterPage; if (!indexPath) { continue; @@ -163,7 +165,7 @@ export function handleOptions( frontmatter: { // Set layout for index page. layout: ctx.getLayout(indexLayout, 'FrontmatterKey'), - title: `${UpperFirstChar(id)}`, + title, ...frontmatter, }, meta: { @@ -176,6 +178,7 @@ export function handleOptions( frontmatterClassificationPages.push({ id, + entryTitle: title, pagination, keys, map, diff --git a/src/node/index.ts b/src/node/index.ts index c205e9a..a0476e4 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -151,6 +151,7 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => { ...frontmatterClassificationPages .map(frontmatterClassifiedPage => { const { + entryTitle, map, pagination, keys, @@ -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, @@ -192,7 +193,7 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => { scopeLayout, DefaultLayoutEnum.FrontmatterPagination ), - title: `${key} ${scope}`, + title: `${key} ${entryTitle}`, }, }; }); diff --git a/src/node/interface/Frontmatter.ts b/src/node/interface/Frontmatter.ts index e524a05..19ca081 100644 --- a/src/node/interface/Frontmatter.ts +++ b/src/node/interface/Frontmatter.ts @@ -3,6 +3,7 @@ import { PaginationConfig } from './Pagination'; export interface FrontmatterClassificationPage { id: string; + entryTitle: string; pagination: PaginationConfig; keys: string[]; scopeLayout?: string; diff --git a/src/node/interface/Options.ts b/src/node/interface/Options.ts index e40a439..c63ebf6 100644 --- a/src/node/interface/Options.ts +++ b/src/node/interface/Options.ts @@ -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. */ @@ -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. */ diff --git a/src/node/pagination.ts b/src/node/pagination.ts index fadeba0..3b0395b 100644 --- a/src/node/pagination.ts +++ b/src/node/pagination.ts @@ -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 ),