-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
232 lines (209 loc) · 7.7 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
// ----------------------------------------------------------------------- Imports
const twix = require("twix");
const moment = require("moment");
const path = require("path");
const _ = require("lodash");
const unified = require("unified");
const markdown = require("remark-parse");
// const webpackLodashPlugin = require("lodash-webpack-plugin");
// console.log(unified().use(markdown).parse(testMd));
// ----------------------------------------------------------------------------
// ------------------------------------------------------------------ Functions
// ----------------------------------------------------------------------------
/** gets last page */
const getPrev = (index, edges, thisEdge) => {
const sliced = _.slice(edges, 0, index);
let returnNode = null;
_.map(_.reverse(sliced), edge => {
if (_.isNull(returnNode)) {
const type = _.split(_.trim(edge.node.fields.route), "/", 1)[0];
const thisType = _.split(_.trim(thisEdge.node.fields.route), "/", 1)[0];
if (type === thisType) {
returnNode = edge.node;
}
}
});
return returnNode;
};
/** gets next page */
const getNext = (index, edges, thisEdge) => {
const sliced = _.slice(edges, index + 1);
let returnNode = null;
_.map(sliced, edge => {
if (_.isNull(returnNode)) {
const type = _.split(_.trim(edge.node.fields.route), "/", 1)[0];
const thisType = _.split(_.trim(thisEdge.node.fields.route), "/", 1)[0];
if (type === thisType) {
returnNode = edge.node;
}
}
});
return returnNode;
};
// ----------------------------------------------------------------------- Create Nodes
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators;
let route;
// Where will createPages attach the component? ...
// Get raw markdown ...
if (node.internal.type === "MarkdownRemark") {
const fileNode = getNode(node.parent);
const parsedFilePath = path.parse(fileNode.relativePath);
route = `${parsedFilePath.dir === "" ? "" : parsedFilePath.dir}/${
parsedFilePath.name
}`;
const { date, startDate, finishDate } = node.frontmatter;
const begins = moment(!_.isUndefined(startDate) ? startDate : date);
const beginDateInt = parseInt(begins.format("YYYYMMDD"), 10);
const ends = moment(
!_.isUndefined(finishDate) ? finishDate : begins.clone().add(1, "hour"),
);
const endDateInt = parseInt(begins.format("YYYYMMDD"), 10);
const diff = !_.isNull(finishDate)
? moment.duration(ends.diff(begins)).asDays()
: 0;
const sameDay = _.isUndefined(finishDate);
const elapsed = begins.fromNow();
let humanDate = begins.format("ddd, MMMM D, YYYY");
if (sameDay === false) {
const range = begins.twix(ends, { allDay: false });
const rangeX = range.simpleFormat("ddd, MMMM D");
const beginsYear = begins.format("YYYY");
const endsYear = ends.format("YYYY");
if (beginsYear === endsYear) {
humanDate = `${rangeX}, ${beginsYear}`;
} else {
humanDate = rangeX;
}
}
const isoDate = begins.format();
const beginHumanDate = begins.format("ddd, MMMM D, YYYY");
const beginIsoDate = begins.format();
const endHumanDate = ends.format("ddd, MMMM D, YYYY");
const endIsoDate = ends.format();
createNodeField({ node, name: "elapsed", value: elapsed });
createNodeField({ node, name: "isoDate", value: isoDate });
createNodeField({ node, name: "beginHumanDate", value: beginHumanDate });
createNodeField({ node, name: "beginIsoDate", value: beginIsoDate });
createNodeField({ node, name: "endHumanDate", value: endHumanDate });
createNodeField({ node, name: "endIsoDate", value: endIsoDate });
createNodeField({ node, name: "humanDate", value: humanDate });
createNodeField({ node, name: "route", value: route });
createNodeField({ node, name: "rawContent", value: node.internal.content });
createNodeField({ node, name: "beginDateInt", value: beginDateInt });
createNodeField({ node, name: "diff", value: diff });
createNodeField({ node, name: "startDate", value: beginDateInt });
createNodeField({ node, name: "finishDate", value: endDateInt });
// console.log(node.internal.content);
}
};
// ----------------------------------------------------------------------- Create Pages
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators;
return new Promise((resolve, reject) => {
// const doc = path.resolve("src/templates/doc.jsx");
const event = path.resolve("src/templates/event.jsx");
// const faq = path.resolve("src/templates/faq.jsx");
const page = path.resolve("src/templates/page.jsx");
const post = path.resolve("src/templates/post.jsx");
resolve(
graphql(
`
{
allMarkdownRemark {
edges {
node {
frontmatter {
category
title
subTitle
cover
date
fromTime
toTime
category
tags
cost
abstract
}
headings {
depth
value
}
fields {
route
rawContent
elapsed
humanDate
isoDate
beginHumanDate
beginIsoDate
endHumanDate
endIsoDate
beginDateInt
diff
startDate
finishDate
}
}
}
}
}
`,
).then(result => {
if (result.errors) {
console.log(result.errors); // eslint-disable-line no-console
reject(result.errors);
}
const { edges } = result.data.allMarkdownRemark;
edges.sort((a, b) => {
const aNode = a.node.frontmatter;
const bNode = b.node.frontmatter;
const A = !_.isNull(aNode.startDate) ? aNode.startDate : aNode.date;
const B = !_.isNull(bNode.startDate) ? bNode.startDate : bNode.date;
const dateA = new Date(A);
const dateB = new Date(B);
return dateA - dateB;
});
// Loop through markdown nodes
edges.forEach((edge, i) => {
const trimmedRoute = _.trim(edge.node.fields.route);
const prev = i === 0 ? null : getPrev(i, edges, edge);
const next = i === edges.length - 1 ? null : getNext(i, edges, edge);
const context = {
frontmatter: edge.node.frontmatter,
headings: edge.node.headings,
route: edge.node.fields.route,
elapsed: edge.node.fields.elapsed,
humanDate: edge.node.fields.humanDate,
markdownAst: unified()
.use(markdown)
.parse(edge.node.fields.rawContent),
prev,
next,
};
const pathX = edge.node.fields.route;
if (_.startsWith(trimmedRoute, "events")) {
createPage({
path: pathX,
component: event,
context,
});
} else if (_.startsWith(trimmedRoute, "writings")) {
createPage({
path: pathX,
component: post,
context,
});
} else {
createPage({
path: pathX,
component: page,
context,
});
}
});
}),
);
});
};