Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support theme default config and extra pages rendering #315

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
14 changes: 12 additions & 2 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,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' })
Expand Down Expand Up @@ -153,7 +163,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(/(\/$)|((?<!(.html))$)/, '/index.html').replace(/^\//, '')
const filePath = path.resolve(outDir, filename)
await fs.ensureDir(path.dirname(filePath))
await fs.writeFile(filePath, html)
Expand Down
41 changes: 25 additions & 16 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '/'
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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 = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my honest opinion 😊❤️🙈, I think 335-344 would be cleaner with the following:

if (fs.existsSync(configYmlPath)) {
  return await parseConfig(configYmlPath)
}

if (fs.existsSync(configTomlPath)) {
  return await parseConfig(configTomlPath)
}

if (fs.existsSync(configPath)) {
  return require(configPath)
}

return {}

Copy link
Member Author

@meteorlxy meteorlxy May 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh looks better. I just moved things down into a function without much refactor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea yea! Just spot that when I see the changes, might be worth it to make the change.

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)
Expand Down