From aec267e586de776df30bfbb42e3fc9e15fe95a17 Mon Sep 17 00:00:00 2001 From: Billyyyyy3320 Date: Mon, 6 Jan 2020 00:29:27 +0800 Subject: [PATCH] fix: extend page data on wrong pages --- src/node/handleOptions.ts | 18 ++++++++++++++---- src/node/index.ts | 6 +++--- src/node/interface/PageEnhancer.ts | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/node/handleOptions.ts b/src/node/handleOptions.ts index 993ba9c..c6ded23 100644 --- a/src/node/handleOptions.ts +++ b/src/node/handleOptions.ts @@ -99,10 +99,20 @@ export function handleOptions( * 1.3 Set layout for pages that match current directory classifier. */ pageEnhancers.push({ - when: ({ regularPath }) => - Boolean(regularPath) && - regularPath !== indexPath && - regularPath.startsWith(`/${dirname}/`), + /** + * Exclude index pages + * Exclude pagination pages + * Pick pages matched directory name + */ + filter({ regularPath }) { + const regex = new RegExp(`^/${dirname}/page/\\d+/`); + return ( + Boolean(regularPath) && + regularPath !== indexPath && + !regex.test(regularPath) && + regularPath.startsWith(`/${dirname}/`) + ); + }, frontmatter: { layout: ctx.getLayout(itemLayout, 'Post'), permalink: itemPermalink, diff --git a/src/node/index.ts b/src/node/index.ts index 5b5e14b..c205e9a 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -90,10 +90,10 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => { * 1. Execute `pageEnhancers` generated in handleOptions */ extendPageData(pageCtx: VuePressPage) { - const { frontmatter: rawFrontmatter } = pageCtx; + pageEnhancers.forEach(({ filter, data = {}, frontmatter = {} }) => { + const { frontmatter: rawFrontmatter } = pageCtx; - pageEnhancers.forEach(({ when, data = {}, frontmatter = {} }) => { - if (when(pageCtx)) { + if (filter(pageCtx)) { Object.keys(frontmatter).forEach(key => { /** * Respect the original frontmatter in markdown diff --git a/src/node/interface/PageEnhancer.ts b/src/node/interface/PageEnhancer.ts index 0b6fd9d..a3ab8e4 100644 --- a/src/node/interface/PageEnhancer.ts +++ b/src/node/interface/PageEnhancer.ts @@ -4,7 +4,7 @@ export interface PageEnhancer { /** * Conditions for enhancer execution */ - when($page: VuePressPage): boolean; + filter($page: VuePressPage): boolean; /** * frontmatter injected to matched pages