-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
169 lines (160 loc) · 6.25 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const { lowerFirst, result, forEach } = require('lodash')
const path = require('path')
const slash = require('slash')
const axios = require('axios')
// const marked = require('marked')
// const IG_CLIENT_ID = 'de8e2627b5ef4a1cba8847faee64535c'
// const IG_CLIENT_SECRET = '213548d4c97647ceaed49d21134d5323'
const IG_ACCESS_TOKEN = '403340749.de8e262.ed321a7e075b4152b2663b39f5b6be61'
// get https://api.instagram.com/v1/users/self/media/recent/?access_token=403340749.de8e262.ed321a7e075b4152b2663b39f5b6be61
let instagram = require('./src/data/instagram.json')
exports.onPreBootstrap = () => new Promise((resolve, reject) => {
if ( process.env.NODE_ENV === 'development' ) {
return resolve()
}
return axios.get(`https://api.instagram.com/v1/users/self/media/recent/?access_token=${ IG_ACCESS_TOKEN }`)
.then(response => {
if ( process.env.NODE_ENV === 'development' ) {
console.log('Instagram >>', JSON.stringify(response.data, null, '\t'));
} else {
console.log(`Instagram >> received data from ${ response.data.data.length } posts.`);
}
instagram = response.data
resolve();
})
.catch(error => {
console.log('err',error)
reject(error)
})
})
const ARTICLES_QUERY = `{
allContentfulTag {
edges {
node {
name
article {
id
}
}
}
}
allContentfulCategory {
edges {
node {
name
article {
id
}
}
}
}
allContentfulArticle(sort: { order: ASC, fields: [publishedOn] }, limit: 1000) {
edges {
node {
id
slug
category {
name
}
}
}
}
}`
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect, deletePage } = actions
return new Promise((resolve, reject) => {
createPage({
path: '/',
component: slash(path.resolve(`./src/containers/PageHome.js`)),
context: { instagram }
})
createRedirect({
fromPath: '/home',
toPath: '/',
isPermanent: true,
redirectInBrowser: true,
})
resolve(
graphql(ARTICLES_QUERY).then(({errors, data}) => {
if (errors) {
reject(errors)
}
if ( process.env.NODE_ENV === 'development' ) {
console.log('Contentful >>',JSON.stringify(data, null, '\t'))
} else {
console.log(`Contentful >> received data from ${ data.allContentfulArticle.edges.length } articles.`)
}
// ARTICLE PAGES
forEach(data.allContentfulArticle.edges, (edge, index) => {
const { id, slug, category } = edge.node
if (!id || !slug || !category) {
return;
}
const categoryName = category.name
const prevId = result(data.allContentfulArticle.edges, `[${index-1}].node.id`) || ''
const nextId = result(data.allContentfulArticle.edges, `[${index+1}].node.id`) || ''
const articlePath = `/${lowerFirst(categoryName)}/${slug}/`
console.log(`>> Creating Article Page: ${articlePath}`)
createPage({
path: `/${lowerFirst(categoryName)}/${slug}/`,
component: slash(path.resolve(`./src/containers/PageArticle.js`)),
context: { id, slug, prevId, nextId },
})
})
// CATEGORY PAGES
forEach(data.allContentfulCategory.edges, ({node: {name: categoryName, article}}) => {
if (!article) return;
console.log(`>> Creating Category Page: /${lowerFirst(categoryName)}/`)
createPage({
path: `/${lowerFirst(categoryName)}/`,
component: slash(path.resolve(`./src/containers/PageCategory.js`)),
context: { categoryName },
})
})
// TAG PAGES
forEach(data.allContentfulTag.edges, ({node: {name: tagName, article}}) => {
if (!article) return;
console.log(`>> Creating Tag Page: /tag/${tagName}/`)
createPage({
path: `/tag/${tagName}/`,
component: slash(path.resolve(`./src/containers/PageTag.js`)),
context: { tagName },
})
})
// REDIRECTS
forEach(data.allContentfulArticle.edges, (edge, index) => {
const { id, slug, category } = edge.node
if (!id || !slug || !category) {
return;
}
const categoryName = category.name
const articlePath = `/${lowerFirst(categoryName)}/${slug}/`
console.log(`>> Creating Redirect: /article/${id}/ -> ${articlePath}`)
createRedirect({
fromPath: `/article/${id}/`,
toPath: articlePath,
isPermanent: true,
redirectInBrowser: true,
})
console.log(`>> Create Redirect: /articles/${slug}/ -> ${articlePath}`)
createRedirect({
fromPath: `/articles/${slug}/`,
toPath: articlePath,
isPermanent: true,
redirectInBrowser: true,
})
})
})
)
})
}
// exports.onPostBootstrap = ({ actions }) => {
// const { deletePage } = actions
// return new Promise((resolve, reject) => {
// deletePage({
// path: '/home/',
// component: slash(path.resolve('./src/pages/home')),
// })
// resolve()
// })
// }