Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit aac8088

Browse files
committed
seperated route path configuration from generate CLOSES #39
1 parent ac69d49 commit aac8088

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

lib/content/build.js

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,48 +36,52 @@ export default function buildContent ({ nuxt, options }) {
3636
const { page, generate, permalink } = content[dirName]
3737
const contentPath = join(sitePath, srcDir)
3838

39-
if (!generate) return
40-
41-
const db = createDatabase(contentPath, dirName, options)
42-
43-
generate.forEach(reqType => {
44-
const req = {}
45-
if (typeof reqType === 'string') {
46-
req['method'] = reqType
47-
} else if (Array.isArray(reqType)) {
48-
const [reqMethod, reqOptions] = reqType
49-
req['method'] = reqMethod
50-
req['query'] = reqOptions.query ? reqOptions.query : {}
51-
req['args'] = reqOptions.args ? reqOptions.args : []
52-
}
39+
let name
40+
if (page) {
41+
name = routeName(page)
42+
routePaths.set(name, permalink.replace(/^\//, ''))
43+
}
5344

54-
switch (req['method']) {
55-
case 'get':
56-
if (!page) throw new Error('You must specify a page path')
57-
const name = routeName(page)
58-
const pathPrefix = getPrefix(name)
59-
routePaths.set(name, permalink.replace(/^\//, ''))
60-
db.findAll(req['query']).forEach((page) => {
61-
routePages.push(join(pathPrefix, page.permalink))
62-
assetMap.set(buildPath(page.permalink, dirName, options), page)
63-
})
64-
break
65-
case 'getAll':
66-
assetMap.set(
67-
buildPath('_all', dirName, options),
68-
db.findAll(req['query'])
69-
)
70-
break
71-
case 'getOnly':
72-
assetMap.set(
73-
buildPath('_only', dirName, options),
74-
db.findOnly(req['args'], req['query'])
75-
)
76-
break
77-
default:
78-
throw new Error(`The ${req['method']} is not supported for static builds.`)
79-
}
80-
})
45+
if (generate) {
46+
const db = createDatabase(contentPath, dirName, options)
47+
48+
generate.forEach(reqType => {
49+
const req = {}
50+
if (typeof reqType === 'string') {
51+
req['method'] = reqType
52+
} else if (Array.isArray(reqType)) {
53+
const [reqMethod, reqOptions] = reqType
54+
req['method'] = reqMethod
55+
req['query'] = reqOptions.query ? reqOptions.query : {}
56+
req['args'] = reqOptions.args ? reqOptions.args : []
57+
}
58+
59+
switch (req['method']) {
60+
case 'get':
61+
if (!page) throw new Error('You must specify a page path')
62+
const pathPrefix = getPrefix(name)
63+
db.findAll(req['query']).forEach((page) => {
64+
routePages.push(join(pathPrefix, page.permalink))
65+
assetMap.set(buildPath(page.permalink, dirName, options), page)
66+
})
67+
break
68+
case 'getAll':
69+
assetMap.set(
70+
buildPath('_all', dirName, options),
71+
db.findAll(req['query'])
72+
)
73+
break
74+
case 'getOnly':
75+
assetMap.set(
76+
buildPath('_only', dirName, options),
77+
db.findOnly(req['args'], req['query'])
78+
)
79+
break
80+
default:
81+
throw new Error(`The ${req['method']} is not supported for static builds.`)
82+
}
83+
})
84+
}
8185
})
8286

8387
interceptRoutes(nuxt, routePaths)

lib/content/database.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ export default function createDatabase (contentPath, dirName, options) {
3333

3434
const [startIndex, endIndex] = onlyArg
3535
let currIndex = max(0, parseInt(startIndex))
36-
let finalIndex = min(parseInt(endIndex), pagesArr.length - 1)
36+
let finalIndex = endIndex !== undefined
37+
? min(parseInt(endIndex), pagesArr.length - 1) : null
38+
39+
if (!finalIndex) return pagesArr[currIndex].create(query)
3740

3841
const pages = []
3942
while(currIndex <= finalIndex && finalIndex !== 0) {

0 commit comments

Comments
 (0)