-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.config.js
80 lines (77 loc) · 2.16 KB
/
static.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const fs = require('fs')
const klaw = require('klaw')
const path = require('path')
const matter = require('gray-matter')
function getPosts () {
const items = []
// Walk ("klaw") through posts directory and push file paths into items array //
const getFiles = () => new Promise(resolve => {
// Check if posts directory exists //
if (fs.existsSync('./src/posts')) {
klaw('./src/posts')
.on('data', item => {
// Filter function to retrieve .md files //
if (path.extname(item.path) === '.md') {
// If markdown file, read contents //
const data = fs.readFileSync(item.path, 'utf8')
// Convert to frontmatter object and markdown content //
const dataObj = matter(data)
// Create slug for URL //
dataObj.data.slug = dataObj.data.title.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, '')
// Remove unused key //
delete dataObj.orig
// Push object into items array //
items.push(dataObj)
}
})
.on('error', e => {
console.log(e)
})
.on('end', () => {
// Resolve promise for async getRoutes request //
// posts = items for below routes //
resolve(items)
})
} else {
// If src/posts directory doesn't exist, return items as empty array //
resolve(items)
}
})
return getFiles()
}
export default {
getSiteData: () => ({
title: 'React Static with Netlify CMS',
}),
getRoutes: async () => {
const posts = await getPosts()
return [
{
path: '/',
component: 'src/containers/Home',
},
{
path: '/about',
component: 'src/containers/About',
},
{
path: '/blog',
component: 'src/containers/Blog',
getData: () => ({
posts,
}),
children: posts.map(post => ({
path: `/post/${post.data.slug}`,
component: 'src/containers/Post',
getData: () => ({
post,
}),
})),
},
{
is404: true,
component: 'src/containers/404',
},
]
},
}