Repository navigation
Expand file tree
/
Copy pathgatsby-node.js
More file actions
96 lines (91 loc) · 2.61 KB
/
Copy pathgatsby-node.js
File metadata and controls
96 lines (91 loc) · 2.61 KB
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
const path = require("path");
const fmImagesToRelative = require("./fmImagesToRelative");
exports.onCreateWebpackConfig = ({ actions, stage, plugins, loaders }) => {
const additionalPlugins = [plugins.provide({ Buffer: ["buffer", "Buffer"] })];
if (stage === "build-javascript" || stage === "develop") {
additionalPlugins.push(plugins.provide({ process: "process/browser" }));
}
actions.setWebpackConfig({
resolve: {
alias: {
path: require.resolve("path-browserify")
},
fallback: {
fs: false,
stream: false
}
},
plugins: additionalPlugins
});
// Fix self is not defined webpack error
// if (stage === "develop-html" || stage === "build-html") {
// actions.setWebpackConfig({
// module: {
// rules: [
// {
// test: /react-notifications-component/,
// use: loaders.null()
// }
// ]
// }
// });
// }
};
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
// Get all nodes from MDX plugin
const result = await graphql(`
{
allMdx(limit: 1000) {
edges {
node {
id
slug
frontmatter {
templateKey
}
}
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
// Create pages for each node
result.data.allMdx.edges.forEach(({ node }) => {
const id = node.id;
let pagePath = node.slug.replace(/-$/);
const len = pagePath.length;
if (pagePath[len - 1] !== "/") {
pagePath = pagePath + "/";
}
createPage({
path: pagePath,
component: path.resolve(`src/templates/${String(node.frontmatter.templateKey)}.tsx`),
context: {
id
}
});
});
};
exports.onCreateNode = ({ node, getNodes, actions }) => {
// Change image paths in markdown files to relative paths to cms static folder
const transformedFm = fmImagesToRelative(node, getNodes, {
staticFolderName: "/static",
include: ["featuredImage"]
});
// Replace new line and non-breaking space characters for markdown files
if (node.internal.type === `MarkdownRemark` || node.internal.type === `Mdx`) {
if (transformedFm) {
Object.keys(transformedFm).forEach((key) => {
const value = transformedFm[key];
if (typeof value === "string") {
transformedFm[key] = value.replace(/ /g, "\u00a0").replace(/\\n/g, "\n");
}
});
}
}
actions.createNodeField({ node, name: "frontmatter", value: transformedFm });
};