From c8ff2081961ce99a12c6c0acded8b11bc5513cd6 Mon Sep 17 00:00:00 2001 From: Xinyu Liu Date: Tue, 1 May 2018 18:28:24 +0800 Subject: [PATCH 1/3] feat: support extra pages rendering --- lib/build.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/build.js b/lib/build.js index a904aa7ba0..5f5ea90d48 100644 --- a/lib/build.js +++ b/lib/build.js @@ -60,12 +60,22 @@ module.exports = async function build (sourceDir, cliOptions = {}) { .map(renderHeadTag) .join('\n ') - // render pages console.log('Rendering static HTML...') + + // render pages from md files for (const page of options.siteData.pages) { await renderPage(page) } + // render extra pages + const extraPages = [ + ...(options.siteConfig.themeConfig.extraPages || []), + ...(options.siteConfig.extraPages || []) + ] + for (const path of extraPages) { + await renderPage({ path }) + } + // 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' }) @@ -150,7 +160,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) { console.error(chalk.red(`Error rendering ${pagePath}:`)) throw e } - const filename = pagePath.replace(/\/$/, '/index.html').replace(/^\//, '') + const filename = pagePath.replace(/(\/$)|((? Date: Thu, 3 May 2018 23:18:02 +0800 Subject: [PATCH 2/3] feat: support theme default config --- lib/prepare.js | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/prepare.js b/lib/prepare.js index e892f1cbcc..a8e9bd1b84 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -71,21 +71,7 @@ if (!Object.assign) Object.assign = require('object-assign')` async function resolveOptions (sourceDir) { const vuepressDir = path.resolve(sourceDir, '.vuepress') - const configPath = path.resolve(vuepressDir, 'config.js') - const configYmlPath = path.resolve(vuepressDir, 'config.yml') - const configTomlPath = path.resolve(vuepressDir, 'config.toml') - - delete require.cache[configPath] - - // resolve siteConfig - let siteConfig = {} - if (fs.existsSync(configYmlPath)) { - siteConfig = await parseConfig(configYmlPath) - } else if (fs.existsSync(configTomlPath)) { - siteConfig = await parseConfig(configTomlPath) - } else if (fs.existsSync(configPath)) { - siteConfig = require(configPath) - } + const siteConfig = await resolveConfig(vuepressDir) // normalize head tag urls for base const base = siteConfig.base || '/' @@ -159,9 +145,12 @@ async function resolveOptions (sourceDir) { themeEnhanceAppPath = null } } + // resolve theme default config + const themeDefaultConfig = await resolveConfig(themePath) // resolve theme config - const themeConfig = siteConfig.themeConfig || {} + const themeConfig = Object.assign({}, themeDefaultConfig, siteConfig.themeConfig || {}) + siteConfig.themeConfig = themeConfig // resolve algolia const isAlgoliaSearch = ( @@ -335,6 +324,26 @@ function sort (arr) { }) } +async function resolveConfig (dir) { + const configPath = path.resolve(dir, 'config.js') + const configYmlPath = path.resolve(dir, 'config.yml') + const configTomlPath = path.resolve(dir, 'config.toml') + + delete require.cache[configPath] + + // resolve siteConfig + let config = {} + if (fs.existsSync(configYmlPath)) { + config = await parseConfig(configYmlPath) + } else if (fs.existsSync(configTomlPath)) { + config = await parseConfig(configTomlPath) + } else if (fs.existsSync(configPath)) { + config = require(configPath) + } + + return config +} + async function parseConfig (file) { const content = await fs.readFile(file, 'utf-8') const [extension] = /.\w+$/.exec(file) From 940560ee31f1afd4bbd7569e28e73b6e44c23be7 Mon Sep 17 00:00:00 2001 From: Xinyu Liu Date: Sat, 5 May 2018 16:51:46 +0800 Subject: [PATCH 3/3] tweak: modify the resolveConfig function --- lib/prepare.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/prepare.js b/lib/prepare.js index a8e9bd1b84..1ed3d57c4f 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -332,16 +332,19 @@ async function resolveConfig (dir) { delete require.cache[configPath] // resolve siteConfig - let config = {} + if (fs.existsSync(configPath)) { + return require(configPath) + } + if (fs.existsSync(configYmlPath)) { - config = await parseConfig(configYmlPath) - } else if (fs.existsSync(configTomlPath)) { - config = await parseConfig(configTomlPath) - } else if (fs.existsSync(configPath)) { - config = require(configPath) + return await parseConfig(configYmlPath) + } + + if (fs.existsSync(configTomlPath)) { + return await parseConfig(configTomlPath) } - return config + return {} } async function parseConfig (file) {