forked from jlengstorf/gatsby-theme-jam-example
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgatsby-node.js
141 lines (121 loc) · 3.79 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Implement Gatsby's Node APIs in this file.
* @2019/01/30
*
* Migrate to theme version
* @2019/07/15
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const fs = require('fs-extra')
const { createFilePath } = require('gatsby-source-filesystem')
const { fmImagesToRelative } = require('gatsby-remark-relative-images')
// 0. add public directory clean @2019/01/18, 05/20
exports.onPreInit = () => {
console.log('>>>> theme pre build...');
let folder = './public';
// REPLACEMENT: $ gatsby clean
// fs.emptyDirSync(folder);
}
// 1. create node first
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
fmImagesToRelative(node) // convert image paths for gatsby images
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
node,
name: `slug`,
value,
})
}
}
// 2. then create pages by template & .md content
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const categoryTplt = require.resolve(`./src/templates/category.js`)
const tutorialTplt = require.resolve(`./src/templates/tutorial.js`)
const quizTplt = require.resolve(`./src/templates/quiz.js`)
const markdowns = `{
pages: allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
edges {
node {
fields {
slug
}
frontmatter {
template
}
}
}
}
}`
try {
console.log('>>> start query markdowns...')
const result = await graphql(markdowns)
console.log('>>> start generate pages...')
// Create site pages by .md file
const pages = result.data.pages.edges;
pages.forEach((page, index) => {
const { slug } = page.node.fields
// add template reference in main page @2019/04/17
const { template } = page.node.frontmatter
// console.log(slug)
if(/\/category\/[\w-]+\/$/.test(slug)){// create `category` by index.md
// console.log('>>> create category index.md: ', slug)
createPage({
path: slug,
component: categoryTplt,
context: {slug}
})
return
}
// MUST: placed BEFORE the tutorial section creation
if(/\/category\/([\w-]+\/){2}test\//.test(slug)){// create `quiz` page
const tutpath = slug.match(/\/category\/([\w-]+\/){2}/g)[0]
const catpath = slug.match(/\/category\/([\w-]+\/){1}/g)[0]
// console.log('>>> create quiz page ...', slug)
createPage({
path: slug,
component: quizTplt,
context: {slug, tutpath, catpath}
})
return
}
if(/\/category\/([\w-]+\/){3}/.test(slug)){// create `tutorial` section
const tutpath = slug.match(/\/category\/([\w-]+\/){2}/g)[0]
const catpath = slug.match(/\/category\/([\w-]+\/){1}/g)[0]
const quizpath= tutpath+'test/'
// console.log('>>> create tutorial section: ', tutpath)
createPage({
path: slug,
component: tutorialTplt,
context: {slug, tutpath, catpath, quizpath}
})
return
}
// Last: to generate page of navi bar menu!
createPage({
path: slug,
component: require.resolve(
// one on one mapping!
// `src/templates${String(slug).slice(0, -1)}.js`
template ? template : './src/templates/post.js'
),
// Data passed to context is available
// in page queries as GraphQL variables.
// also available in props.pageContext of component
context: {
slug: slug,
},
})
// end of pages loop
});
} catch (error) {
console.error(error);
}
// end of createPages
}