From 463625c78334a22a0d353f82a34219e9fd2da2f0 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Sun, 9 Sep 2018 03:03:56 +0800 Subject: [PATCH] feat($theme-blog): add 'type' field to page data and more reasonable layout fallback --- packages/@vuepress/plugin-blog/index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/@vuepress/plugin-blog/index.js b/packages/@vuepress/plugin-blog/index.js index 5a6365f9..03d67d41 100644 --- a/packages/@vuepress/plugin-blog/index.js +++ b/packages/@vuepress/plugin-blog/index.js @@ -16,21 +16,22 @@ module.exports = (options, ctx) => ({ const { layoutComponentMap } = ctx const { pageEnhancers = [] } = options const isLayoutExists = name => layoutComponentMap[name] !== undefined - const getLayout = name => isLayoutExists(name) ? name : 'Layout' + const getLayout = (name, fallback) => isLayoutExists(name) ? name : fallback const isDirectChild = regularPath => path.parse(regularPath).dir === '/' const enhancers = [ { when: ({ regularPath }) => isDirectChild(regularPath), - frontmatter: { layout: getLayout('Page') } + frontmatter: { layout: getLayout('Page', 'Layout') }, + data: { type: 'page' } }, { when: ({ regularPath }) => regularPath === '/category.html', - frontmatter: { layout: getLayout('Category') } + frontmatter: { layout: getLayout('Category', 'Page') } }, { when: ({ regularPath }) => regularPath === '/tags.html', - frontmatter: { layout: getLayout('Tag') } + frontmatter: { layout: getLayout('Tag', 'Page') } }, { when: ({ regularPath }) => regularPath === '/', @@ -39,19 +40,22 @@ module.exports = (options, ctx) => ({ { when: ({ regularPath }) => regularPath.startsWith('/_posts/'), frontmatter: { - layout: getLayout('Post'), + layout: getLayout('Post', 'Page'), permalink: '/:year/:month/:day/:slug' - } + }, + data: { type: 'post' } }, ...pageEnhancers ] enhancers.forEach(({ when, + data = {}, frontmatter = {} }) => { if (when(pageCtx)) { Object.assign(rawFrontmatter, frontmatter) + Object.assign(pageCtx, data) } }) } -- GitLab