-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
46 lines (36 loc) · 1.17 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
const axios = require('axios');
const crypto = require("crypto");
HASHNODE_API_URL = 'https://api.hashnode.com/';
async function getAllPosts(username) {
let query = `query { user(username: "` + username + `") { publication { posts { cuid slug title type dateUpdated dateAdded contentMarkdown brief coverImage tags { name }}}}}`;
let { data } = await axios.post(HASHNODE_API_URL, { query: query });
let publication = data.data.user.publication;
if (!publication) {
throw new Error('No publications found for this user.');
}
return publication.posts;
}
exports.sourceNodes = async ({ actions }, options) => {
if (!options.username) {
throw new Error('Missing username option.')
}
const { createNode } = actions;
const posts = await getAllPosts(options.username);
for (let post of posts) {
const jsonString = JSON.stringify(post);
const gatsbyNode = {
...post,
id: `${post.cuid}`,
parent: "__SOURCE__",
children: [],
internal: {
type: 'devblogPost',
contentDigest: crypto
.createHash("md5")
.update(jsonString)
.digest("hex")
}
};
createNode(gatsbyNode);
}
}