-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgastby-template-selector.js
53 lines (52 loc) · 1.38 KB
/
gastby-template-selector.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
const path = require('path')
const _ = require('lodash')
function postsIndexPrevious(posts, slug) {
const index = _.findIndex(posts, function(p) {
return p.node.fields.slug == slug
})
const previous = index === posts.length - 1 ? null : posts[index + 1].node
return previous
}
function postsIndexNext(posts, slug) {
const index = _.findIndex(posts, function(p) {
return p.node.fields.slug == slug
})
const next = index === 0 ? null : posts[index - 1].node
return next
}
function filterByPost(posts, posttype) {
return _.filter(posts, p => {
return p.node.frontmatter.posttype === posttype
})
}
exports.templateSelector = (post, posts) => {
const template = path.resolve(
`./src/templates/${post.node.frontmatter.posttype}.tsx`
)
switch (post.node.frontmatter.posttype) {
case 'post':
return {
path: post.node.fields.slug,
component: template,
context: {
slug: post.node.fields.slug,
previous: postsIndexPrevious(
filterByPost(posts, 'post'),
post.node.fields.slug
),
next: postsIndexNext(
filterByPost(posts, 'post'),
post.node.fields.slug
),
},
}
default:
return {
path: post.node.fields.slug,
component: template,
context: {
slug: post.node.fields.slug,
},
}
}
}