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 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
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
44 changes: 28 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,29 @@ 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
if (fs.existsSync(configPath)) {
return require(configPath)
}

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

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

return {}
}

async function parseConfig (file) {
const content = await fs.readFile(file, 'utf-8')
const [extension] = /.\w+$/.exec(file)
Expand Down