forked from greymass/retired-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
291 lines (251 loc) · 8.64 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const FilterWarningsPlugin = require('webpack-filter-warnings-plugin');
const jsonConcat = require('json-concat');
const path = require(`path`)
const fs = require('fs-extra')
const { createFilePath } = require(`gatsby-source-filesystem`);
const { siteMetadata: { defaultLanguage } } = require('./gatsby-config');
const {ApiClient, EosjsDataProvider, render} = require('decentium')
const {JsonRpc} = require('eosjs')
const fetch = require('node-fetch')
const rpc = new JsonRpc('http://eos.greymass.com', { fetch })
const dataProvider = new EosjsDataProvider(rpc, {whitelist: ['decentiumorg']});
const client = new ApiClient({dataProvider})
const slugify = require('slugify')
const showdown = require('showdown')
const converter = new showdown.Converter()
const jsdom = require('jsdom');
const dom = new jsdom.JSDOM();
const exec = require('await-exec');
const BP_JSON_REPO = 'https://github.com/greymass/bp.json';
exports.onPreInit = async () => {
console.log('Combining Localization Files')
const p = `${__dirname}/src/intl`;
const dirs = fs.readdirSync(p).filter((f) =>
fs.statSync(`${p}/${f}`).isDirectory());
const excludes = ['build'];
dirs
.filter((d) => !excludes.includes(d))
.forEach((dir) => {
const sourceDirectory = `${p}/${dir}/`;
const destinationFile = `${p}/build/${dir}.json`;
console.log(`${sourceDirectory} -> ${destinationFile}`);
// validateLocaleDir(sourceDirectory);
jsonConcat({
src: sourceDirectory,
dest: destinationFile
}, (e) => {
if (e) {
throw(`Invalid locale JSON. Error: ${JSON.stringify(e)}`);
}
});
});
console.log('Syncing Decentium Blog Posts')
const response = await client.getPosts('teamgreymass')
response.posts.forEach((post) => {
try {
client.resolvePost(post)
.then((resolvedPost) => {
const html = render(resolvedPost.doc)
const summary = (resolvedPost.metadata && resolvedPost.metadata.summary) ? resolvedPost.metadata.summary.replace(/"/g, '\\"') : ''
const frontmatter = `---
title: "${resolvedPost.title.replace(/"/g, '\\"')}"
date: ${post.timestamp.split("T")[0]}
author: ${(resolvedPost.author === 'teamgreymass') ? 'Greymass Team' : resolvedPost.author}
description: "${summary}"
image: "${resolvedPost.metadata.image}"
featured: true
---
`
const content = frontmatter + converter.makeMd(html, dom.window.document)
fs.writeFile(`src/pages/blog/${slugify(resolvedPost.title, { strict: true }).toLowerCase()}.en.md`, content)
})
.catch((e) => {
console.log(post)
console.log("decentium post render error", e)
})
} catch (e) {
console.log("decentium post resolve error", e)
}
})
if (process.env.NODE_ENV === 'production') {
console.log(`Downloading the latest bp json files from "${BP_JSON_REPO}".`);
await exec(`git clone ${BP_JSON_REPO} tmp/bp.json`);
await exec(`cp -r ./tmp/bp.json/* ./static`);
await exec(`rm -rf tmp/bp.json`);
}
}
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
plugins: [
// Silence mini-css-extract-plugin generating lots of warnings for CSS ordering.
// We use CSS modules that should not care for the order of CSS imports, so we
// should be safe to ignore these.
//
// See: https://github.com/webpack-contrib/mini-css-extract-plugin/issues/250
new FilterWarningsPlugin({
exclude: /mini-css-extract-plugin/
})
]
});
};
exports.onCreateNode = ({ node, actions, getNode, getNodes, reporter }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const pageData = updateNodeData(node, createNodeField, getNode, getNodes, reporter);
const defaultLanguageNode = getNodes().find(node => {
return
node.internal.type === `MarkdownRemark` &&
node.fileAbsolutePath.includes(pageData.slug) &&
node.fields.page.locale === defaultLanguage;
});
defaultLanguageNode &&
updateNodeData(defaultLanguageNode, createNodeField, getNode, getNodes, reporter);
}
return node;
}
function updateNodeData(node, createNodeField, getNode, getNodes, reporter) {
const {
sluggishTitle,
locale,
pageType,
} = getDataFromNode(node, getNode, getNodes, reporter);
const pageData = {
pageId: node.id,
slug: sluggishTitle,
path: (pageType === 'website') ? sluggishTitle : `${pageType}/${sluggishTitle}/`,
locale,
};
createNodeField({
node,
name: 'page',
value: pageData,
});
return pageData;
}
function getDataFromNode(node, getNode) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
const partsOfSlug = slug.split('.');
const locale = partsOfSlug[1].split('/').join('');
const sluggishTitle = partsOfSlug[0].split('/')[1];
const pageType =
node.fileAbsolutePath.split('/src/pages/')[1].split('/')[0];
return { sluggishTitle, pageType, locale };
}
exports.onCreatePage = ({ page, actions: { createPage, deletePage } }) => {
const pageLanguage = page.context.intl && page.context.intl.language;
deletePage(page);
createPage({
...page,
context: {
...page.context,
language: pageLanguage || page.context.locale || defaultLanguage
},
});
};
exports.createPages = async ({ actions, graphql, reporter }) => {
await createWebsitePages(actions, graphql, reporter);
await createBlogPages(actions, graphql, reporter);
await createBlogIndexPages(actions, graphql, reporter);
}
async function createWebsitePages(actions, graphql, reporter) {
const { createPage } = actions
const resourceTemplate = path.resolve('src/templates/resource.js');
const result = await fetchMarkdownPagesByFolder('website', graphql, reporter);
const resourcePages = result.data.results.edges;
// Create post detail pages
resourcePages.forEach(({ node }) => {
createPage({
path: node.fields.page.slug,
context: { slug: node.fields.page.slug, locale: node.fields.page.locale || defaultLanguage },
component: resourceTemplate,
})
})
}
async function createBlogPages(actions, graphql, reporter) {
const { createPage } = actions;
const blogPostTemplate = path.resolve('src/templates/blog-post.js');
const result = await fetchMarkdownPagesByFolder('blog', graphql, reporter);
const blogPages = result.data.results.edges;
// Create post detail pages
blogPages.forEach(({ node }) => {
createPage({
path: node.fields.page.path,
context: { slug: node.fields.page.slug, locale: node.fields.page.locale || defaultLanguage },
component: blogPostTemplate,
})
})
}
async function createBlogIndexPages(actions, graphql, reporter) {
const { createPage } = actions;
const result = await fetchMarkdownPagesByFolder('blog', graphql, reporter);
const blogPosts = result.data.results.edges;
const locales = [];
blogPosts.forEach(blogPost => {
const blogPostLocale = blogPost.node.fields.page.locale;
if (!locales.includes(blogPostLocale)) {
locales.push(blogPostLocale);
}
});
const blogPostsComponent = path.resolve('src/templates/blog.js');
locales.forEach(locale => {
const blogPostForLocale = blogPosts.filter(blogPost => {
return blogPost.node.fields.page.locale === locale;
});
const numberOfPagesForLocale = Math.ceil(blogPostForLocale.length / 10);
for (var pageNumber = 1; pageNumber <= numberOfPagesForLocale; pageNumber++) {
createPage({
path: `/blog/${pageNumber}`,
context: {
pageNumber,
resultsToSkip: (pageNumber - 1) * 10,
locale,
totalNumberOfPages: numberOfPagesForLocale,
},
component: blogPostsComponent,
})
}
});
}
async function fetchMarkdownPagesByFolder(folder, graphql, reporter) {
const result = await graphql(`
{
results: allMarkdownRemark(filter: {
fileAbsolutePath: {regex: "/(${folder})/.*.md$/"}
})
{
edges {
node {
fields {
page {
slug
path
locale
}
}
}
}
}
}
`)
// handle errors
if (result.errors) {
return reporter.panicOnBuild(
`Error while running GraphQL query to build resource pages. ${JSON.stringify(result.errors)}`
);
}
return result;
}
function validateLocaleDir(dir) {
fs.readdir(dir, (err, fileNames) => {
fileNames.forEach(fileName => {
const filePath = `${dir}${fileName}`;
try {
require(filePath);
} catch (e) {
if (e) {
throw(`Invalid locale JSON at "${filePath}". Error: ${e.message}`);
}
}
});
});
}