This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgatsby-node.js
72 lines (60 loc) · 1.96 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
const _camelCase = require('lodash/camelCase');
const path = require('path');
const crypto = require('crypto');
const digest = str => crypto
.createHash('md5')
.update(str)
.digest('hex');
exports.onCreateNode = ({
node,
getNodes,
actions
}, pluginOptions) => {
if (node.internal.owner !== 'gatsby-source-drupal') {
return
}
if (!pluginOptions.hasOwnProperty("nodes")) {
pluginOptions.nodes = [`article`,`page`];
}
const node_types = pluginOptions.nodes.map((type) => {
return `node__${type}`;
});
if (node_types.includes(node.internal.type)) {
const {
createNode,
createNodeField
} = actions;
let content = node.body.value;
const inlineImageRegExp = /\(\/sites[^)]+\)/gi;
const nodeImages = content.match(inlineImageRegExp);
if (nodeImages) {
const nodes = getNodes();
nodeImages.forEach((element) => {
const nodeImage = element.slice(1, -1);
const nodeImageCached = nodes.find(contentNode => (contentNode.internal.type === 'File' && contentNode.internal.description.includes(nodeImage)));
if (nodeImageCached) {
console.log(`replace ${nodeImage}`);
content = content.replace(new RegExp(nodeImage, 'g'), nodeImageCached.relativePath);
}
});
}
const textNode = {
id: `${node.id}-MarkdownBody`,
parent: node.id,
dir: path.resolve('./'),
internal: {
type: _camelCase(`${node.internal.type}MarkdownBody`),
mediaType: 'text/markdown',
content: content,
contentDigest: digest(content)
}
};
createNode(textNode);
createNodeField({
node,
name: 'markdownBody___NODE',
value: textNode.id
});
}
return;
}