-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
109 lines (99 loc) · 3.01 KB
/
gatsby-node.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const _ = require('lodash')
const Promise = require('bluebird')
const path = require('path')
const slash = require('slash')
const moment = require('moment')
const menuLinks = require('./src/consts/menuLinks.jsx')
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect } = actions
const { CategoryLinks } = menuLinks
createRedirect({
fromPath: '/blog',
toPath: '/blog/software',
redirectInBrowser: true,
})
return new Promise((resolve, reject) => {
const postTemplate = path.resolve('./src/templates/PostTemplate/index.jsx')
const pageTemplate = path.resolve('./src/templates/PageTemplate/index.jsx')
const postListTemplate = path.resolve('./src/templates/PostListTemplate/index.jsx')
_.each(CategoryLinks, categoryLink => {
createPage({
path: `/blog/${_.kebabCase(categoryLink.id)}`,
component: postListTemplate,
context: {
categoryId: categoryLink.id,
categoryLabel: categoryLink.label
},
})
})
graphql(`
{
allMarkdownRemark(
limit: 1000
filter: { frontmatter: { draft: { ne: true } } }
) {
edges {
node {
fields {
slug
}
frontmatter {
layout
}
}
}
}
}
`).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
_.each(result.data.allMarkdownRemark.edges, edge => {
if (_.get(edge, 'node.frontmatter.layout') === 'page') {
createPage({
path: edge.node.fields.slug,
component: slash(pageTemplate),
context: { slug: edge.node.fields.slug },
})
} else if (_.get(edge, 'node.frontmatter.layout') === 'post') {
createPage({
path: edge.node.fields.slug,
component: slash(postTemplate),
context: { slug: edge.node.fields.slug },
})
}
})
resolve()
})
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === 'File') {
const parsedFilePath = path.parse(node.absolutePath)
const slug = `/${parsedFilePath.dir.split('---')[1]}/`
createNodeField({ node, name: 'slug', value: slug })
} else if (
node.internal.type === 'MarkdownRemark' &&
typeof node.slug === 'undefined'
) {
const fileNode = getNode(node.parent)
let slug = fileNode.fields.slug
if (typeof node.frontmatter.path !== 'undefined') {
if (node.frontmatter.layout == 'post') {
const postDate = moment(node.frontmatter.date)
const postYear = postDate.format('YYYY')
const postMonth = postDate.format('MM')
slug = `/blog/${postYear}/${postMonth}/${node.frontmatter.path}/`
} else {
slug = node.frontmatter.path
}
}
createNodeField({
node,
name: 'slug',
value: slug,
})
}
}