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($core): better layout check #1455

Merged
merged 7 commits into from
Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
74 changes: 39 additions & 35 deletions packages/@vuepress/core/lib/node/theme-api/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const { logger, fs, path: { resolve }} = require('@vuepress/shared-utils')
const readdirSync = dir => fs.existsSync(dir) && fs.readdirSync(dir) || []
const {
logger,
fs,
path: { resolve }
} = require('@vuepress/shared-utils')
const readdirSync = dir => (fs.existsSync(dir) && fs.readdirSync(dir)) || []

module.exports = class ThemeAPI {
constructor (theme, parentTheme) {
constructor (theme, parentTheme, context) {
this.theme = theme
this.context = context
this.parentTheme = parentTheme || {}
this.existsParentTheme = !!this.parentTheme.path
this.vuepressPlugin = {
Expand All @@ -30,12 +35,12 @@ module.exports = class ThemeAPI {
this.componentMap = this.getComponents()
this.layoutComponentMap = this.getLayoutComponentMap()

Object.keys(this.componentMap).forEach((name) => {
Object.keys(this.componentMap).forEach(name => {
const { filename, path } = this.componentMap[name]
alias[`@theme/components/${filename}`] = path
})

Object.keys(this.layoutComponentMap).forEach((name) => {
Object.keys(this.layoutComponentMap).forEach(name => {
const { filename, path } = this.layoutComponentMap[name]
alias[`@theme/layouts/${filename}`] = path
})
Expand All @@ -44,13 +49,9 @@ module.exports = class ThemeAPI {
}

getComponents () {
const componentDirs = [
resolve(this.theme.path, 'components')
]
const componentDirs = [resolve(this.theme.path, 'components')]
if (this.existsParentTheme) {
componentDirs.unshift(
resolve(this.parentTheme.path, 'components'),
)
componentDirs.unshift(resolve(this.parentTheme.path, 'components'))
}
return resolveSFCs(componentDirs)
}
Expand All @@ -63,15 +64,15 @@ module.exports = class ThemeAPI {
if (this.existsParentTheme) {
layoutDirs.unshift(
resolve(this.parentTheme.path, '.'),
resolve(this.parentTheme.path, 'layouts'),
resolve(this.parentTheme.path, 'layouts')
)
}
// built-in named layout or not.
const layoutComponentMap = resolveSFCs(layoutDirs)

const { Layout = {}, NotFound = {}} = layoutComponentMap
const { Layout, NotFound } = layoutComponentMap
// layout component does not exist.
if (!Layout || !fs.existsSync(Layout.path)) {
if (!Layout) {
const fallbackLayoutPath = resolve(__dirname, 'Layout.fallback.vue')
layoutComponentMap.Layout = {
filename: 'Layout.vue',
Expand All @@ -81,10 +82,10 @@ module.exports = class ThemeAPI {
}
logger.warn(
`[vuepress] Cannot resolve Layout.vue file in \n ${Layout.path}, `
+ `fallback to default layout: ${fallbackLayoutPath}`
+ `fallback to default layout: ${fallbackLayoutPath}`
)
}
if (!NotFound || !fs.existsSync(NotFound.path)) {
if (!NotFound) {
layoutComponentMap.NotFound = {
filename: 'NotFound.vue',
componentName: 'NotFound',
Expand All @@ -104,25 +105,28 @@ module.exports = class ThemeAPI {
*/

function resolveSFCs (dirs) {
return dirs.map(
layoutDir => readdirSync(layoutDir)
.filter(filename => filename.endsWith('.vue'))
.map(filename => {
const componentName = getComponentName(filename)
return {
filename,
componentName,
isInternal: isInternal(componentName),
path: resolve(layoutDir, filename)
}
})
).reduce((arr, next) => {
arr.push(...next)
return arr
}, []).reduce((map, component) => {
map[component.componentName] = component
return map
}, {})
return dirs
.map(layoutDir =>
readdirSync(layoutDir)
.filter(filename => filename.endsWith('.vue'))
.map(filename => {
const componentName = getComponentName(filename)
return {
filename,
componentName,
isInternal: isInternal(componentName),
path: resolve(layoutDir, filename)
}
})
)
.reduce((arr, next) => {
arr.push(...next)
return arr
}, [])
.reduce((map, component) => {
map[component.componentName] = component
return map
}, {})
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/@vuepress/theme-default/components/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<main class="page">
<slot name="top" />

<Content />
<Content class="theme-default-content" />
<PageEdit />

<PageNav v-bind="{ sidebarItems }" />
Expand All @@ -22,6 +22,8 @@ export default {
</script>

<style lang="stylus">
@require '../styles/wrapper.styl';

.page {
padding-bottom: 2rem;
display: block;
Expand Down