9
9
inferTitle,
10
10
extractHeaders,
11
11
parseFrontmatter,
12
- getGitLastUpdatedTimeStamp
12
+ getGitLastUpdatedTimeStamp,
13
+ getUrlSafePath,
14
+ getPageComponentName
13
15
} = require ( './util' )
14
16
15
17
fs . ensureDirSync ( tempPath )
@@ -71,6 +73,16 @@ if (!Object.assign) Object.assign = require('object-assign')`
71
73
// 7. handle the theme enhanceApp.js
72
74
await writeEnhanceTemp ( 'themeEnhanceApp.js' , options . themeEnhanceAppPath )
73
75
76
+ // 8. generate page content
77
+ const pageContent = options . siteData . pages . reduce (
78
+ ( pageContent , { name : componentName , filepath, content } ) => {
79
+ pageContent [ pageFilePathToVirtualPath ( filepath , componentName ) ] = content
80
+ return pageContent
81
+ } ,
82
+ { }
83
+ )
84
+ writeTemp ( 'pageContent.js' , `module.exports = ${ JSON . stringify ( pageContent ) } ` )
85
+
74
86
return options
75
87
}
76
88
@@ -204,8 +216,14 @@ async function resolveOptions (sourceDir) {
204
216
data . lastUpdated = getGitLastUpdatedTimeStamp ( filepath )
205
217
}
206
218
207
- // extract yaml frontmatter
208
219
const content = await fs . readFile ( filepath , 'utf-8' )
220
+ Object . defineProperty ( data , 'filepath' , { value : filepath } )
221
+ Object . defineProperty ( data , 'content' , { value : content } )
222
+
223
+ // get page component name
224
+ data . name = getPageComponentName ( data . path )
225
+
226
+ // extract yaml frontmatter
209
227
const frontmatter = parseFrontmatter ( content )
210
228
// infer title
211
229
const title = inferTitle ( frontmatter )
@@ -297,6 +315,11 @@ function fileToComponentName (file) {
297
315
return `${ pagePrefix } ${ normalizedName } `
298
316
}
299
317
318
+ function pageFilePathToVirtualPath ( filepath , componentName ) {
319
+ const { dir, ext } = path . parse ( filepath )
320
+ return path . join ( dir , `__${ componentName } __${ ext } ` )
321
+ }
322
+
300
323
function isIndexFile ( file ) {
301
324
return indexRE . test ( file )
302
325
}
@@ -310,16 +333,17 @@ async function resolveComponents (sourceDir) {
310
333
}
311
334
312
335
async function genRoutesFile ( { siteData : { pages } , sourceDir, pageFiles } ) {
313
- function genRoute ( { path : pagePath } , index ) {
336
+ function genRoute ( { path : pagePath , content , name : componentName } , index ) {
314
337
const file = pageFiles [ index ]
315
338
const filePath = path . resolve ( sourceDir , file )
316
339
let code = `
317
340
{
318
- path: ${ JSON . stringify ( pagePath ) } ,
341
+ name: ${ JSON . stringify ( componentName ) } ,
342
+ path: ${ JSON . stringify ( getUrlSafePath ( pagePath ) ) } ,
319
343
component: ThemeLayout,
320
344
beforeEnter: (to, from, next) => {
321
- import(${ JSON . stringify ( filePath ) } ).then(comp => {
322
- Vue.component(${ JSON . stringify ( fileToComponentName ( file ) ) } , comp.default)
345
+ import(${ JSON . stringify ( pageFilePathToVirtualPath ( filePath , componentName ) ) } ).then(comp => {
346
+ Vue.component(${ JSON . stringify ( componentName ) } , comp.default)
323
347
next()
324
348
})
325
349
}
@@ -328,8 +352,8 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
328
352
if ( / \/ $ / . test ( pagePath ) ) {
329
353
code += `,
330
354
{
331
- path: ${ JSON . stringify ( pagePath + 'index.html' ) } ,
332
- redirect: ${ JSON . stringify ( pagePath ) }
355
+ path: ${ JSON . stringify ( getUrlSafePath ( pagePath + 'index.html' ) ) } ,
356
+ redirect: ${ JSON . stringify ( getUrlSafePath ( pagePath ) ) }
333
357
}`
334
358
}
335
359
0 commit comments