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

fix: extend page data on wrong pages (fix #32) #53

Merged
merged 1 commit into from
Jan 6, 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
18 changes: 14 additions & 4 deletions src/node/handleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/node/interface/PageEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface PageEnhancer {
/**
* Conditions for enhancer execution
*/
when($page: VuePressPage): boolean;
filter($page: VuePressPage): boolean;

/**
* frontmatter injected to matched pages
Expand Down