forked from fikaproductions/fika-gatsby-source-cockpit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
99 lines (85 loc) · 2.78 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
const fs = require("fs");
const path = require("path");
const CockpitService = require("./src/CockpitService");
const CollectionItemNodeFactory = require("./src/CollectionItemNodeFactory");
const {
MARKDOWN_IMAGE_REGEXP_GLOBAL,
MARKDOWN_ASSET_REGEXP_GLOBAL
} = require("./src/constants");
const FileNodeFactory = require("./src/FileNodeFactory");
const MarkdownNodeFactory = require("./src/MarkdownNodeFactory");
exports.sourceNodes = async ({ actions, cache, store }, configOptions) => {
const { createNode } = actions;
const cockpit = new CockpitService(
configOptions.baseUrl,
configOptions.token,
configOptions.locales
);
const fileNodeFactory = new FileNodeFactory(createNode, store, cache);
const markdownNodeFactory = new MarkdownNodeFactory(createNode);
await cockpit.validateBaseUrl();
await cockpit.validateToken();
const collections = await cockpit.getCollections();
const images = await cockpit.normalizeCollectionsImages(collections);
const assets = await cockpit.normalizeCollectionsAssets(collections);
const markdowns = await cockpit.normalizeCollectionsMarkdowns(
collections,
images,
assets
);
for (let path in images) {
const imageNode = await fileNodeFactory.createImageNode(path);
images[path] = {
localPath: copyFileToStaticFolder(imageNode),
id: imageNode.id
};
}
for (let path in assets) {
const assetNode = await fileNodeFactory.createAssetNode(path);
assets[path] = {
localPath: copyFileToStaticFolder(assetNode),
id: assetNode.id
};
}
for (let markdown in markdowns) {
const localMarkdown = updateAssetPathsWithLocalPaths(
updateImagePathsWithLocalPaths(markdown, images),
assets
);
const id = markdownNodeFactory.create(localMarkdown);
markdowns[markdown] = { id };
}
collections.forEach(collection => {
const nodeFactory = new CollectionItemNodeFactory(
createNode,
collection.name,
images,
assets,
markdowns
);
collection.items.forEach(item => {
nodeFactory.create(item);
});
});
};
const copyFileToStaticFolder = ({ absolutePath, name, ext, internal }) => {
const localPath = path.join(
"/",
"static",
`${name}-${internal.contentDigest}${ext}`
);
fs.copyFileSync(absolutePath, path.join(process.cwd(), "public", localPath));
return localPath;
};
const updateImagePathsWithLocalPaths = (markdown, images) => {
return markdown.replace(MARKDOWN_IMAGE_REGEXP_GLOBAL, (...match) =>
match[0].replace(match[1], images[match[1]].localPath)
);
};
const updateAssetPathsWithLocalPaths = (markdown, assets) => {
return markdown.replace(MARKDOWN_ASSET_REGEXP_GLOBAL, (...match) =>
assets[match[1]]
? match[0].replace(match[1], assets[match[1]].localPath)
: match[0]
);
};