-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathgatsby-node.js
100 lines (89 loc) · 2.89 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
const XLSX = require(`xlsx`)
const fs = require(`fs-extra`)
const _ = require(`lodash`)
const crypto = require(`crypto`)
// read files as `binary` from file system
function _loadNodeContent(fileNode, fallback) {
return fileNode.absolutePath
? fs.readFile(fileNode.absolutePath, `binary`)
: fallback(fileNode)
}
async function onCreateNode(
{ node, actions, loadNodeContent, createNodeId },
options = {}
) {
const { createNode, createParentChildLink } = actions
const extensions = `xls|xlsx|xlsm|xlsb|xml|xlw|xlc|csv|txt|dif|sylk|slk|prn|ods|fods|uos|dbf|wks|123|wq1|qpw|htm|html`.split(
`|`
)
if (extensions.indexOf((node.extension || ``).toLowerCase()) == -1) {
return
}
// Load binary string
const content = await _loadNodeContent(node, loadNodeContent)
// accept *all* options to pass to the sheet_to_json function
const xlsxOptions = options
// alias legacy `rawOutput` to correct `raw` attribute if raw isn't already defined
if (!_.has(xlsxOptions, `raw`) && _.has(xlsxOptions, `rawOutput`)) {
xlsxOptions.raw = xlsxOptions.rawOutput
}
if (!_.has(xlsxOptions, `defval`) && _.has(xlsxOptions, `defaultValue`)) {
xlsxOptions.defval = xlsxOptions.defaultValue
}
delete xlsxOptions.rawOutput
delete xlsxOptions.defaultValue
delete xlsxOptions.plugins
// Parse
let wb = XLSX.read(content, { type: `binary`, cellDates: true })
wb.SheetNames.forEach((n, idx) => {
let ws = wb.Sheets[n]
let parsedContent = XLSX.utils.sheet_to_json(ws, xlsxOptions)
if (_.isArray(parsedContent)) {
const csvArray = parsedContent.map((obj, i) => {
const objStr = JSON.stringify(obj)
const contentDigest = crypto
.createHash(`md5`)
.update(objStr)
.digest(`hex`)
return {
...obj,
id: obj.id
? obj.id
: createNodeId(`${node.id} [${n} ${i}] >>> ${node.extension}`),
children: [],
parent: node.id,
internal: {
contentDigest,
type:
_.upperFirst(_.camelCase(`${node.name} ${node.extension}`)) +
`__` +
_.upperFirst(_.camelCase(`${n}`)),
},
}
})
_.each(csvArray, y => {
createNode(y)
createParentChildLink({ parent: node, child: y })
})
const shObj = { name: n, idx: idx }
const shStr = JSON.stringify(shObj)
const contentDigest = crypto
.createHash(`md5`)
.update(shStr)
.digest(`hex`)
const z = {
id: createNodeId(`${node.id} [${idx}] >>> ${node.extension}`),
children: [],
parent: node.id,
internal: {
contentDigest,
type: _.upperFirst(_.camelCase(`${node.name} ${node.extension}`)),
},
}
createNode(z)
createParentChildLink({ parent: node, child: z })
}
})
return
}
exports.onCreateNode = onCreateNode