-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
75 lines (71 loc) · 2.27 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
// You can delete this file if you're not using it
// We're using it, thank you very much XD
const fetch = require('isomorphic-fetch');
const crypto = require('crypto');
const marked = require('marked');
const path = require('path');
exports.sourceNodes = ({ actions }) => {
const { createNode } = actions;
const apiURL = process.env.GATSBY_API_URL || 'http://friday-songbird-admin.herokuapp.com/canciones';
return fetch(apiURL)
.then(res => res.json())
.then(jsonResp => {
jsonResp.forEach((post) => createNode(Object.assign(
post,
{
id: post.id.toString(),
internal: {
type: 'post',
contentDigest: crypto.createHash('md5').update(JSON.stringify(post)).digest('hex')
},
parent: `__SOURCE__`,
children: [],
contenidoHTML: marked(post.texto),
slug: '/archive/'+post.fecha_publicacion+'-'+post.titulo.toLowerCase().replace(/(\s|\W)+/g, '-')
}
)));
});
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
graphql(`{
allPost (sort: {fields: [fecha_publicacion], order: DESC}){
edges {
node {
id
slug
}
next {
slug
titulo
fecha_publicacion(formatString:"DD/MM/YYYY")
}
previous {
slug
titulo
fecha_publicacion(formatString:"DD/MM/YYYY")
}
}
}
}`).then(result => {
result.data.allPost.edges.forEach(({ node, next, previous }) => {
createPage({
path: node.slug,
component: path.resolve('./src/templates/post.js'),
context: {
id: node.id,
nextPost: next,
prevPost: previous
}
});
});
resolve();
});
});
};