forked from ethereum/nxbn-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
95 lines (79 loc) · 2.18 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
// https://www.gatsbyjs.org/docs/node-apis/
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const Parser = require('rss-parser');
const crypto = require('crypto');
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
// only edit markdown nodes
if (node.internal.type === `Mdx`) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
createNodeField({
node,
name: `slug`,
value: slug,
})
}
}
exports.sourceNodes = async ({ actions }) => {
const { createNode } = actions;
const parser = new Parser({
customFields: {
item: ['description'],
},
})
const feed = await parser.parseURL('https://blog.ethereum.org/en/next-billion/feed.xml')
feed.items.forEach(blogPost => {
const blogPostNode = {
// Gatsby wants this...
id: blogPost.title,
parent: `__SOURCE__`,
internal: {
type: `BlogPost`
},
// Actually useful props
title: blogPost.title,
description: blogPost.description,
date: blogPost.pubDate ? new Date(blogPost.pubDate).getTime() : 0,
body: blogPost['content:encoded'] || blogPost.description,
permaLink: blogPost.link,
imageUrl: blogPost.enclosure ? blogPost['enclosure'].url : '',
}
// Gatsby also wants this...
const contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(blogPostNode))
.digest(`hex`);
blogPostNode.internal.contentDigest = contentDigest;
createNode(blogPostNode);
})
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql(`
query {
allMdx {
edges {
node {
fields {
slug
}
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query')
}
result.data.allMdx.edges.forEach(({ node }) => {
const component = path.resolve(`./src/templates/static.js`)
createPage({
path: node.fields.slug,
component,
context: {
slug: node.fields.slug,
},
})
})
}