From cecf72762ed8c7b8745f217414437ae7d091e76c Mon Sep 17 00:00:00 2001 From: oott123 Date: Tue, 1 May 2018 14:18:30 +0800 Subject: [PATCH 1/3] feat: render theme-related pages --- docs/zh/guide/custom-themes.md | 18 ++++++++++++++++++ lib/build.js | 11 +++++++++++ lib/prepare.js | 5 +++++ 3 files changed, 34 insertions(+) diff --git a/docs/zh/guide/custom-themes.md b/docs/zh/guide/custom-themes.md index 474bca7aad..bf9dc44eb5 100644 --- a/docs/zh/guide/custom-themes.md +++ b/docs/zh/guide/custom-themes.md @@ -85,6 +85,24 @@ export default ({ } ``` +## 渲染主题页面 + +自定义主题可以通过主题根目录下的 `pages.js` 文件使 VuePress 渲染属于主题的页面。这个文件应当 `export default` 一个异步函数,并接受一个包含了站点元数据等属性的对象作为参数。该函数需要返回一个数组,数组内容为需要渲染的页面元数据。你可以在这个文件中定义主题所需渲染的页面,例如:为博客系统设计的归档页面、分类页面等。 + +另外,你需要结合[应用配置](#应用配置)一节中的说明,注册合适的应用路由。否则,渲染结果只会是一些 404 页面。 + +::: tip +该模块被 webpack 直接引用,因此你可能需要使用 CommonJS 语法来导出函数。 +::: + +```js +exports.default = async function ({ siteData }) { + return [ + { path: '/archive.html' } // path 定义渲染目标,它和其它元数据一起将被放在 $page 里。 + ] +} +``` + ## 使用来自 npm 的主题 主题可以以 Vue 单文件组件的格式,并以 `vuepress-theme-xxx` 的名称发布到 npm 上。 diff --git a/lib/build.js b/lib/build.js index a904aa7ba0..1da3e8bd2c 100644 --- a/lib/build.js +++ b/lib/build.js @@ -66,6 +66,16 @@ module.exports = async function build (sourceDir, cliOptions = {}) { await renderPage(page) } + // render theme pages + if (options.themePagesPath) { + const pages = await require(options.themePagesPath).default({ + siteData: options.siteData + }) + for (const page of pages) { + await renderPage(page) + } + } + // if the user does not have a custom 404.md, generate the theme's default if (!options.siteData.pages.some(p => p.path === '/404.html')) { await renderPage({ path: '/404.html' }) @@ -154,6 +164,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) { const filePath = path.resolve(outDir, filename) await fs.ensureDir(path.dirname(filePath)) await fs.writeFile(filePath, html) + return html } function renderPageMeta (meta) { diff --git a/lib/prepare.js b/lib/prepare.js index 2d0fc56db2..0e5d44dab0 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -178,6 +178,11 @@ async function resolveOptions (sourceDir) { if (fs.existsSync(themeEnhanceAppPath)) { options.themeEnhanceAppPath = themeEnhanceAppPath } + + const themePagesPath = path.resolve(themeDir, 'pages.js') + if (fs.existsSync(themePagesPath)) { + options.themePagesPath = themePagesPath + } } // resolve pages From 3be32abfec8f826bfeb167def1ed876d9aedbdd2 Mon Sep 17 00:00:00 2001 From: oott123 Date: Tue, 1 May 2018 14:25:41 +0800 Subject: [PATCH 2/3] fix: remove unnessary code added by accidentally --- lib/build.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/build.js b/lib/build.js index 1da3e8bd2c..beb15d37af 100644 --- a/lib/build.js +++ b/lib/build.js @@ -164,7 +164,6 @@ module.exports = async function build (sourceDir, cliOptions = {}) { const filePath = path.resolve(outDir, filename) await fs.ensureDir(path.dirname(filePath)) await fs.writeFile(filePath, html) - return html } function renderPageMeta (meta) { From 5d54d0927ea6a64fd629a5351a394249d22adc72 Mon Sep 17 00:00:00 2001 From: oott123 Date: Tue, 1 May 2018 15:30:33 +0800 Subject: [PATCH 3/3] fix: move themePages to prepare.js --- lib/build.js | 10 ---------- lib/prepare.js | 13 +++++++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/build.js b/lib/build.js index beb15d37af..a904aa7ba0 100644 --- a/lib/build.js +++ b/lib/build.js @@ -66,16 +66,6 @@ module.exports = async function build (sourceDir, cliOptions = {}) { await renderPage(page) } - // render theme pages - if (options.themePagesPath) { - const pages = await require(options.themePagesPath).default({ - siteData: options.siteData - }) - for (const page of pages) { - await renderPage(page) - } - } - // if the user does not have a custom 404.md, generate the theme's default if (!options.siteData.pages.some(p => p.path === '/404.html')) { await renderPage({ path: '/404.html' }) diff --git a/lib/prepare.js b/lib/prepare.js index 0e5d44dab0..185f3b25dc 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -33,6 +33,8 @@ module.exports = async function prepare (sourceDir) { ].join('\n\n')) // 3. generate siteData + const themePages = await resolveThemePages(options) + options.siteData.pages = options.siteData.pages.concat(themePages) const dataCode = `export const siteData = ${JSON.stringify(options.siteData, null, 2)}` await writeTemp('siteData.js', dataCode) @@ -229,6 +231,17 @@ async function resolveOptions (sourceDir) { return options } +async function resolveThemePages ({ themePagesPath, siteData }) { + // render theme pages + if (themePagesPath) { + const pages = await require(themePagesPath).default({ + siteData: siteData + }) + return pages + } + return [] +} + async function genComponentRegistrationFile ({ sourceDir }) { function genImport (file) { const name = fileToComponentName(file)