提交 936577df 编写于 作者: E Evan You

small refactor

上级 72e56661
......@@ -2,40 +2,27 @@ import Vue from 'vue'
import Router from 'vue-router'
import Content from './Content'
import NotFound from '~notFound'
import metadataMixin from './metadataMixin'
// .temp/siteData.js is a dynamically generated file that:
// 1. registers all *.md pages and *.vue files found in _components as global
// async components;
// 2. exports siteData
// 3. exports routes
import { siteData, routes } from './.temp/siteData'
routes.push({
path: '*',
component: NotFound
})
import { routes } from './.temp/siteData'
Vue.use(Router)
// mixin for exposing $site and $page
Vue.mixin(metadataMixin)
// component for rendering markdown content and setting title etc.
Vue.component('Content', Content)
Vue.mixin({
computed: {
$site () {
return siteData
},
$page () {
const pages = siteData.pages
for (let i = 0; i < pages.length; i++) {
const page = pages[i]
if (page.path === this.$route.path) {
page.frontmatter = page.frontmatter || {}
return page
}
}
}
}
// add now found route
routes.push({
path: '*',
component: NotFound
})
export function createApp () {
......
import { siteData } from './.temp/siteData'
export default {
computed: {
$site () {
return siteData
},
$page () {
const pages = siteData.pages
for (let i = 0; i < pages.length; i++) {
const page = pages[i]
if (page.path === this.$route.path) {
page.frontmatter = page.frontmatter || {}
return page
}
}
}
}
}
......@@ -73,8 +73,9 @@ async function resolveOptions (sourceDir) {
}
const pagesData = options.pages.map(file => {
const name = file.replace(/\.md$/, '').replace(/\\/g, '/')
const urlPath = '/' + (name === 'index' ? '' : (name + '.html'))
const urlPath = isIndexFile(file)
? '/'
: `/${file.replace(/\.md$/, '').replace(/\\/g, '/')}.html`
const content = fs.readFileSync(path.resolve(sourceDir, file), 'utf-8')
const data = {
path: urlPath
......@@ -119,12 +120,15 @@ async function genComponentRegistrationFile ({ sourceDir, pages }) {
function toComponentName (file) {
const isPage = /\.md$/.test(file)
return (
(isPage ? `page-` : ``) +
file
.replace(/\.(vue|md)$/, '')
.replace(/\/|\\/g, '-')
)
const isIndex = isIndexFile(file)
const normalizedName = isIndex
? 'index'
: file.replace(/\.(vue|md)$/, '').replace(/\/|\\/g, '-')
return isPage ? `page-${normalizedName}` : normalizedName
}
function isIndexFile (file) {
return /^(index|readme)\.md$/i.test(file)
}
async function resolveComponents (sourceDir) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册