-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
60 lines (56 loc) · 1.55 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
const remark = require('remark');
const strip = require('strip-markdown');
const { createFilePath } = require('gatsby-source-filesystem');
exports.onCreateNode = async ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'Mdx') {
const slug = createFilePath({ node, getNode, basePath: 'content/blog' });
createNodeField({
name: 'slug',
node,
value: slug,
});
}
};
exports.onCreatePage = async ({ page, actions, getNodes }) => {
const { createPage, deletePage } = actions;
if (page.path.match(/^\/$/)) {
const postsRemark = await getNodes().filter((value) => value.internal.type === 'Mdx');
const allPosts = [];
postsRemark.forEach((element) => {
let body;
const { rawBody } = element;
const bodyStartIndex = rawBody.indexOf('---\n', 4);
const markdownBody = rawBody.slice(bodyStartIndex);
remark()
.use(strip)
.process(markdownBody, (err, file) => {
if (err) throw err;
body = String(file);
});
const { categories, featuredImageAlt, postTitle, seoMetaDescription, tags } =
element.frontmatter;
const { slug } = element.fields;
allPosts.push({
id: element.id,
body,
categories,
featuredImageAlt,
postTitle,
seoMetaDescription,
slug,
tags,
});
});
deletePage(page);
createPage({
...page,
context: {
...page.context,
postData: {
allPosts,
},
},
});
}
};