-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathgatsby-node.js
More file actions
121 lines (112 loc) · 4.05 KB
/
gatsby-node.js
File metadata and controls
121 lines (112 loc) · 4.05 KB
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
const path = require(`path`);
const { slash } = require(`gatsby-core-utils`);
const { createFilePath } = require(`gatsby-source-filesystem`);
// Implement the Gatsby API “createPages”. This is
// called after the Gatsby bootstrap is finished, so you have
// access to any information necessary to programmatically
// create pages.
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
// The “graphql” function allows us to run arbitrary
// queries against the local Drupal graphql schema. Think of
// it like the site has a built-in database constructed
// from the fetched data that you can run queries against.
return graphql(`
{
allAsciidoc(
limit: 1000
sort: { fields: { publicationDate: DESC } }
) {
edges {
node {
id
html
document {
title
main
}
fields {
slug
}
pageAttributes {
datepublished
name
pronouns
location
firstcommit
linkedin
twitter
github
email
image
featured
intro
}
}
}
}
}
`).then((result) => {
if (result.errors) {
throw result.errors;
}
// Create Asciidoc pages.
const articleTemplate = path.resolve(
`./src/templates/contributor-details.jsx`
);
const contributors = result.data.allAsciidoc.edges;
contributors.forEach((edge, index) => {
const previous = index === 0 ? null : contributors[index - 1].node;
const next =
index === contributors.length - 1
? null
: contributors[index + 1].node;
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
// can query data specific to each page.
path: edge.node.fields.slug,
component: slash(articleTemplate),
context: {
id: edge.node.id,
previous: previous
? {
slug: previous.fields.slug,
title: previous.document.title,
image: previous.pageAttributes.image,
}
: null,
next: next
? {
slug: next.fields.slug,
title: next.document.title,
image: next.pageAttributes.image,
}
: null,
},
});
});
});
};
exports.onCreateNode = async ({ node, actions, getNode, loadNodeContent }) => {
const { createNodeField } = actions;
if (node.internal.type === `Asciidoc`) {
const value = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
});
}
if (node.pageAttributes?.datepublished) {
createNodeField({
name: `publicationDate`,
node,
value: new Date(node.pageAttributes.datepublished).toISOString(),
});
}
};