-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
90 lines (79 loc) · 2.3 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
const csv2json = require('csvtojson');
const fetch = require('node-fetch');
const config = require('./gatsby-config');
const isDebug = process.env.NODE_ENV !== 'production';
const createPublishedGoogleSpreadsheetNode = async (
{ actions: { createNode }, createNodeId, createContentDigest },
publishedURL,
type,
{ skipFirstLine = false, alwaysEnabled = false, subtype = null }
) => {
// All table has first row reserved
const result = await fetch(
`${publishedURL}&single=true&output=csv&headers=0${
skipFirstLine ? "&range=A2:ZZ" : ""
}`
)
const data = await result.text()
const records = await csv2json().fromString(data)
records
.filter(
r => alwaysEnabled || (isDebug && r.enabled === "N") || r.enabled === "Y"
)
.forEach((p, i) => {
// create node for build time data example in the docs
const meta = {
// required fields
id: createNodeId(
`${type.toLowerCase()}${subtype ? `-${subtype}` : ""}-${i}`
),
parent: null,
children: [],
internal: {
type,
contentDigest: createContentDigest(p),
},
}
const node = { ...p, subtype, ...meta }
createNode(node)
})
}
exports.sourceNodes = async props => {
await Promise.all([
createPublishedGoogleSpreadsheetNode(
props,
'https://docs.google.com/spreadsheets/d/e/2PACX-1vSp3oltt5RkGHzhSBQ24J9cNZXL8YWF4gvVdJFe5tXRgJ9lM9ZNxDeKWgPmYebAeIFuuts4ktiwh78f/pub?gid=0&single=true&output=csv',
"KeyValue",
{ skipFirstLine: true }
),
]);
};
exports.onCreatePage = async ({ page, actions: { createPage, deletePage } }) => {
const originalPath = page.path;
// Delete the original page (since we are gonna create localized versions of it)
await deletePage(page);
// create the alias for '/' using zh-hk
await createPage({
...page,
path: originalPath,
context: {
...page.context,
originalPath,
lang: 'zh-hk',
},
});
await Promise.all(
config.siteMetadata.supportedLanguages.map(async ({ locale }) => {
const localizedPath = `/${locale}${page.path}`;
await createPage({
...page,
path: localizedPath,
context: {
...page.context,
originalPath,
lang: locale,
},
});
})
);
};