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

Commit 876ca8d

Browse files
fix: extend page data on wrong pages (fix #32)(#53)
1 parent 10d8f65 commit 876ca8d

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

Diff for: src/node/handleOptions.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,20 @@ export function handleOptions(
9999
* 1.3 Set layout for pages that match current directory classifier.
100100
*/
101101
pageEnhancers.push({
102-
when: ({ regularPath }) =>
103-
Boolean(regularPath) &&
104-
regularPath !== indexPath &&
105-
regularPath.startsWith(`/${dirname}/`),
102+
/**
103+
* Exclude index pages
104+
* Exclude pagination pages
105+
* Pick pages matched directory name
106+
*/
107+
filter({ regularPath }) {
108+
const regex = new RegExp(`^/${dirname}/page/\\d+/`);
109+
return (
110+
Boolean(regularPath) &&
111+
regularPath !== indexPath &&
112+
!regex.test(regularPath) &&
113+
regularPath.startsWith(`/${dirname}/`)
114+
);
115+
},
106116
frontmatter: {
107117
layout: ctx.getLayout(itemLayout, 'Post'),
108118
permalink: itemPermalink,

Diff for: src/node/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
9090
* 1. Execute `pageEnhancers` generated in handleOptions
9191
*/
9292
extendPageData(pageCtx: VuePressPage) {
93-
const { frontmatter: rawFrontmatter } = pageCtx;
93+
pageEnhancers.forEach(({ filter, data = {}, frontmatter = {} }) => {
94+
const { frontmatter: rawFrontmatter } = pageCtx;
9495

95-
pageEnhancers.forEach(({ when, data = {}, frontmatter = {} }) => {
96-
if (when(pageCtx)) {
96+
if (filter(pageCtx)) {
9797
Object.keys(frontmatter).forEach(key => {
9898
/**
9999
* Respect the original frontmatter in markdown

Diff for: src/node/interface/PageEnhancer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface PageEnhancer {
44
/**
55
* Conditions for enhancer execution
66
*/
7-
when($page: VuePressPage): boolean;
7+
filter($page: VuePressPage): boolean;
88

99
/**
1010
* frontmatter injected to matched pages

0 commit comments

Comments
 (0)