-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
146 lines (121 loc) · 2.9 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const path = require("path");
const fs = require("fs");
// start copypast from ./src/utils.tsx :(
const process = array => {
return array.map(item => {
const newItem = { ...item };
const { child } = newItem;
const parentPath = newItem.path;
if (Array.isArray(child)) {
newItem.child = child.map(({ path, ...other }) => {
const newPath = parentPath.concat(`/${path}`);
return {
path: newPath,
...other,
};
});
return {
...newItem,
child: process(newItem.child),
path: `/${newItem.path}`,
};
} else {
return {
...newItem,
path: `/${parentPath}`,
};
}
});
};
const getPagePaths = array => {
return array.flatMap(item => {
if (Array.isArray(item.child)) {
return getPagePaths(item.child);
} else {
return { ...item };
}
});
};
// end copypast from ./src/utils.tsx :(
const getFlatPagePaths = data =>
getPagePaths(process(data)).map(pathObj => pathObj.path);
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const newsTemplate = path.resolve("./src/template/newsTemplate.tsx");
const result = await graphql(`
query {
allMarkdownRemark(
sort: { fields: frontmatter___date, order: DESC }
filter: {
frontmatter: { path: { ne: null }, date: { ne: null } }
fileAbsolutePath: { regex: "/news/" }
}
) {
edges {
node {
html
frontmatter {
date(formatString: "D MMMM, YYYY", locale: "uk_UA")
path
title
}
}
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const { path } = node.frontmatter;
createPage({
path,
component: newsTemplate,
context: {},
});
});
const RawMarkdownPage = path.resolve(
"./src/components/RawMD/RawMarkdownPage.tsx",
);
const ComingSoonPage = path.resolve("./src/components/ComingSoonPage.tsx");
const menu = JSON.parse(
fs.readFileSync(path.resolve("./src/page_data/navbar.json")),
).menu;
const paths = getFlatPagePaths(menu);
console.log("\n\n");
paths.forEach(_path => {
if (fs.existsSync(path.resolve(`./src/pages${_path}.tsx`))) {
console.warn(`Pages "${_path}" exist`);
return;
}
if (fs.existsSync(path.resolve(`./src/page_data${_path}.md`))) {
const pageContext =
_path.charAt(0) + _path.slice(1).replace(/\//g, "/") + "/";
createPage({
path: _path,
component: RawMarkdownPage,
context: {
page: pageContext,
},
});
console.info(`Pages "${_path}" was creating.`);
return;
}
createPage({
path: _path,
component: ComingSoonPage,
context: {},
});
console.info(`${_path} => ComingSoonPage`);
});
console.log("\n\n");
};
exports.onCreateWebpackConfig = ({ getConfig, actions }) => {
if (getConfig().mode === "production") {
actions.setWebpackConfig({
devtool: false,
});
}
};