-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
619 lines (586 loc) · 17.2 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
const createLocation = require("./location");
const moment = require("moment");
const parsegedcom = require(`parse-gedcom`);
const path = require(`path`);
const fs = require(`fs`);
const dateFormats = ["D MMM YYYY", "MMM YYYY", "YYYY"];
const MAX_AGE = 105;
async function onCreateNode({
node,
actions,
loadNodeContent,
createNodeId,
createContentDigest,
}) {
const { createNode, createParentChildLink } = actions;
function convertNode(obj, i, parent) {
const { tag, tree, pointer, data } = obj;
const id = createNodeId(
pointer ? pointer : `${parent.id} [${i}] >>> gedcom`
);
const findTags = (tag, t = tree) => t.filter((node) => node.tag === tag);
const findTag = (tag, t = tree) => {
const [firstTag] = findTags(tag, t);
return firstTag;
};
const findTagData = (tag, t = tree) => {
if (!hasTag(tag, t)) return undefined;
const { data } = findTag(tag, t);
return data;
};
const hasTag = (tag, t = tree) => findTags(tag, t).length > 0;
const findSource = (t = tree) => {
const source = findTags("SOUR", t).map(({ data, tree }) => {
const apid = findTagData("_APID", tree);
return {
image: `${data}-${apid}.jpg`,
path: `${data}.jpg`,
page: findTagData("PAGE", tree),
_APID: apid,
};
});
return source.length === 0 ? null : source;
};
switch (tag) {
case "SOUR":
const title = findTagData("TITL");
const author = findTagData("AUTH");
const publisher = findTagData("PUBL");
return {
title,
author,
publisher,
id,
parent: null, //parent.id,
internal: {
contentDigest: createContentDigest(obj),
type: "Source",
},
};
break;
case "INDI":
let name = findTagData("NAME");
let nameTags = findTags("NAME");
let given, surname, nameSource;
if (nameTags !== undefined) {
let nameTree = nameTags && nameTags[0] ? nameTags[0].tree : undefined;
given = findTagData("GIVN", nameTree);
surname = findTagData("SURN", nameTree);
nameSource = findSource(nameTree);
}
if (
given === undefined &&
surname === undefined &&
name !== undefined
) {
let [g, s] = name.split("/");
given = g;
surname = s;
}
let death, birth, baptism, residence;
if (hasTag("DEAT")) {
let [{ tree: deathTree }] = findTags("DEAT");
let died =
(deathTree.length === 0 && findTagData("DEAT") === "Y") || true;
let place = findTagData("PLAC", deathTree);
let date = findTagData("DATE", deathTree);
let deathSource = findSource(deathTree);
death = {
died,
place: place ? { place } : null,
date: date ? moment(date, dateFormats).toISOString() : null,
source: deathSource,
};
}
if (hasTag("BIRT")) {
let [{ tree: birthTree }] = findTags("BIRT");
let place = findTagData("PLAC", birthTree);
let date = findTagData("DATE", birthTree);
let birthSource = findSource(birthTree);
let birthDate = date ? moment(date, dateFormats).toISOString() : null;
birth = {
born: true,
place: place ? { place } : null,
date: birthDate,
source: birthSource,
};
if (death === undefined && birthDate !== null) {
const yearsSinceBirth = moment().diff(birthDate, "years", true);
if (yearsSinceBirth > MAX_AGE) {
death = {
died: true, //assumed dead
place: null,
date: null,
source: null,
};
}
}
}
if (hasTag("BAPM")) {
let [{ tree: baptismTree }] = findTags("BAPM");
let place = findTagData("PLAC", baptismTree);
let date = findTagData("DATE", baptismTree);
let baptismSource = findSource(baptismTree);
baptism = {
place: place ? { place } : null,
date: date ? moment(date, dateFormats).toISOString() : null,
source: baptismSource,
};
}
if (hasTag("RESI")) {
residence = findTags("RESI").map(({ tree: residenceTree }) => {
let place = findTagData("PLAC", residenceTree);
let date = findTagData("DATE", residenceTree);
let source = findSource(residenceTree);
return {
place: place ? { place } : null,
date: date ? moment(date, dateFormats).toISOString() : null,
source: source,
};
});
}
const sex = findTagData("SEX");
const occupation = findTagData("OCCU");
const familyChild = findTagData("FAMC");
const familySpouse = findTags("FAMS").map(({ data }) =>
createNodeId(data)
);
return {
name: {
given: given ? given.trim() : undefined,
surname: surname ? surname.trim() : undefined,
source: nameSource,
},
sex,
occupation,
birth,
baptism,
residence,
death,
familyChild: familyChild ? createNodeId(familyChild) : undefined,
familySpouse,
id,
children: [],
parent: null, //parent.id,
internal: {
contentDigest: createContentDigest(obj),
type: "Individual",
},
};
break;
case "FAM":
const husband = findTagData("HUSB");
const wife = findTagData("WIFE");
const children = findTags("CHIL").map(({ data }) => createNodeId(data));
return {
husband: husband ? createNodeId(husband) : undefined,
wife: wife ? createNodeId(wife) : undefined,
children,
id,
parent: null, //parent.id,
internal: {
contentDigest: createContentDigest(obj),
type: "Family",
},
};
break;
}
return null;
}
if (node.extension !== `ged`) {
return;
}
const content = await loadNodeContent(node);
const parsedContent = parsegedcom.parse(content);
parsedContent.forEach((obj, i) => {
const convertedObj = convertNode(obj, i, node);
if (convertedObj !== null) {
createNode(convertedObj);
createParentChildLink({ parent: node, child: convertedObj });
}
});
}
exports.onCreateNode = onCreateNode;
exports.createResolvers = ({
actions,
cache,
createNodeId,
createResolvers,
store,
reporter,
}) => {
const { createNode } = actions;
const resolvers = {
Individual: {
relationships: {
type: ["Individual"],
args: {
relationship: "String!",
},
resolve(a, { relationship }, context, info) {
const { id, familyChild, familySpouse } = a;
const getFamC = () =>
context.nodeModel.getNodeById({ id: familyChild, type: "Family" });
const getFamSs = () =>
context.nodeModel.getNodesByIds({
ids: familySpouse,
type: "Family",
});
const getIndividual = (id) => {
if (id === null) return null;
return context.nodeModel.getNodeById({ id, type: "Individual" });
};
switch (relationship) {
case "father":
const famcA = getFamC();
return famc !== null && famc.hasOwnProperty("husband")
? [getIndividual(famc.husband)]
: null;
case "mother":
const famcB = getFamC();
return famcB !== null && famcB.hasOwnProperty("wife")
? [getIndividual(famcB.wife)]
: null;
case "parents":
const famcC = getFamC();
if (famcC === null) {
return [];
}
if (
famcC.hasOwnProperty("husband") &&
famcC.husband &&
famcC.hasOwnProperty("wife") &&
famcC.wife
) {
return [
getIndividual(famcC.husband),
getIndividual(famcC.wife),
];
}
if (famcC.hasOwnProperty("husband") && famcC.husband) {
return [getIndividual(famcC.husband)];
}
if (famcC.hasOwnProperty("wife") && famcC.wife) {
return [getIndividual(famcC.wife)];
}
return [];
case "children":
return getFamSs().flatMap(({ children }) =>
children.map((c) => getIndividual(c))
);
case "spouse":
return getFamSs()
.flatMap(({ husband, wife }) => [
getIndividual(husband),
getIndividual(wife),
]) // this includes the current individual
.filter((s) => {
if (s === null) return false;
return s.id !== id;
}); // so here we exclude them
}
},
},
},
Place: {
location: {
type: "Location",
resolve(source, args, context, info) {
return createLocation({
location: source.place,
store,
cache,
createNode,
createNodeId,
reporter,
});
},
},
},
};
createResolvers(resolvers);
};
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes, createFieldExtension } = actions;
createFieldExtension({
name: "fullName",
extend(options, prevFieldConfig) {
return {
resolve(source) {
return `${source.given || "(missing)"} ${source.surname || ""}`;
},
};
},
});
createFieldExtension({
name: "allEvent",
extend(options, prevFieldConfig) {
return {
resolve(source) {
const events = source.event ? source.event : [];
const birth = source.birth
? {
title: "Birth",
place: source.birth.place,
date: source.birth.date,
source: source.birth.source,
}
: null;
const residence = source.residence
? source.residence.map((r) => ({
title: "Residence",
place: r.place,
date: r.date,
source: r.source,
}))
: [];
const death = source.death
? {
title: "Death",
place: source.death.place,
date: source.death.date,
source: source.death.source,
}
: null;
const baptism = source.baptism
? {
title: "Baptism",
place: source.baptism.place,
date: source.baptism.date,
source: source.baptism.source,
}
: null;
const sortedEvents = [...residence, ...events].sort((a, b) => {
let x = a ? a : 0;
let y = b ? b : 0;
return x - y;
});
return [birth, baptism, ...sortedEvents, death].filter((e) => e);
},
};
},
});
createFieldExtension({
name: "age",
extend(options, prevFieldConfig) {
return {
resolve({
birth: { date: bDate } = { date: undefined },
death: { date: dDate } = { date: undefined },
}) {
if (bDate === undefined) {
return null;
}
if (dDate === undefined) {
const years = moment().diff(moment(bDate), "years");
if (isNaN(years)) return null;
if (years > MAX_AGE) return null;
return years;
}
const years = moment(dDate).diff(bDate, "years");
if (isNaN(years)) return null;
return years;
},
};
},
});
createFieldExtension({
name: "alive",
extend(options, prevFieldConfig) {
return {
resolve({
birth: { date: bDate } = { date: undefined },
death: { date: dDate } = { date: undefined },
}) {
if (bDate === undefined) {
return false;
}
if (dDate === undefined) {
return true;
}
return false;
},
};
},
});
createFieldExtension({
name: "birthCentury",
extend(options, prevFieldConfig) {
return {
resolve({ birth: { date: bDate } = { date: undefined } }) {
if (bDate === undefined) {
return null;
}
return moment(bDate).century();
},
};
},
});
const typeDefs = [
`type Individual implements Node @dontInfer {
name: Name
sex: String
occupation: String
birth: Birth
death: Death
baptism: Baptism
residence: [Residence]
alive: Boolean @alive
familyChild: Family @link
familySpouse: [Family] @link
age: Int @age
event: [ Event ]
allEvent: [Event] @allEvent
}`,
`type Family implements Node @dontInfer {
children: [Individual] @link
husband: Individual @link
wife: Individual @link
}`,
`type Name @dontInfer {
fullName: String! @fullName
given: String
surname: String
source: [Source]
}`,
`type Birth @dontInfer {
born:Boolean
place: Place
date:Date @dateformat
source:[Source]
}`,
`type Residence @dontInfer {
place: Place
date:Date @dateformat
source:[Source]
}`,
`type Baptism @dontInfer {
place: Place
date:Date @dateformat
source:[Source]
}`,
`type Death @dontInfer {
died: Boolean
place: Place
date:Date @dateformat
source:[Source]
}`,
`type Event @dontInfer {
title : String
place: Place
date:Date @dateformat
source:[Source]
}`,
schema.buildObjectType({
name: "Source",
interfaces: ["Node"],
extensions: {
// While in SDL you have two different directives, @infer and @dontInfer to
// control inference behavior, Gatsby Type Builders take a single `infer`
// extension which accepts a Boolean
infer: false,
},
fields: {
title: { type: "String" },
author: { type: "String" },
publisher: { type: "String" },
page: { type: "String" },
_APID: { type: "String" },
image: {
type: "File",
resolve: (source, args, context, info) =>
context.nodeModel.getAllNodes({ type: "File" }).find((file) => {
if (
file.relativePath === undefined ||
file.relativePath === null
) {
throw Error("missing file path: " + file.toString());
}
if (source.image === undefined || source.image === null) {
throw Error("missing source image: " + source.toString());
}
return file.relativePath === source.image;
}),
},
},
}),
`type Place @dontInfer {
place: String
}`,
`type Location @dontInfer {
lat: String
lng: String
}`,
];
createTypes(typeDefs);
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(`
query {
allIndividual {
nodes {
id
}
}
allFamily {
nodes {
id
}
}
}
`);
result.data.allIndividual.nodes.forEach((node) => {
createPage({
path: `individual/${node.id}`,
component: path.resolve(`./src/templates/individual.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
id: node.id,
},
});
});
result.data.allFamily.nodes.forEach((node) => {
createPage({
path: `family/${node.id}`,
component: path.resolve(`./src/templates/family.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
id: node.id,
},
});
});
};
const publicPath = `./public`;
exports.onPostBuild = async ({ graphql }) => {
const result = await graphql(`
query {
allIndividual {
nodes {
id
name {
fullName
}
birth {
born
date(formatString: "DD MMMM YYYY")
place {
place
}
}
death {
died
date(formatString: "DD MMMM YYYY")
place {
place
}
}
}
}
}
`);
const outputPath = path.join(publicPath, "individualsAPI.json");
fs.writeFileSync(outputPath, JSON.stringify(result));
};