-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
84 lines (80 loc) · 2.53 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
const { kebabCase } = require("lodash");
const createTagPages = require("./gatsby-actions/createTagPages");
const createCategoryPages = require("./gatsby-actions/createCategoryPages");
const createArchivePages = require("./gatsby-actions/createArchivePages");
const createPostPages = require("./gatsby-actions/createPostPages");
const createPaginatedPages = require("gatsby-paginate");
const filterStr =
process.env.NODE_ENV === "production"
? "filter: { frontmatter: { draft: { ne: true } } }"
: "";
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
resolve(
graphql(`
{
posts: allMarkdownRemark(
sort: { frontmatter: { date: DESC } }
${filterStr}
) {
totalCount
edges {
node {
wordCount{
words
}
timeToRead
fields {
slug
}
html
frontmatter {
slug
title
date
category
tags
cover
}
tableOfContents
excerpt(pruneLength: 150)
}
}
}
}
`).then((result) => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
const posts = result.data.posts.edges;
createPostPages(createPage, posts);
createArchivePages(createPage, createPaginatedPages, posts);
createTagPages(createPage, createPaginatedPages, posts);
createCategoryPages(createPage, createPaginatedPages, posts);
resolve();
})
);
});
};
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === "MarkdownRemark") {
let slug = "";
const fileName = node.fileAbsolutePath.split("/").pop();
const title = fileName.substring(0, fileName.length - 3);
if (
Object.prototype.hasOwnProperty.call(node, "frontmatter") &&
Object.prototype.hasOwnProperty.call(node.frontmatter, "slug")
) {
slug = `/posts/${kebabCase(node.frontmatter.slug)}`;
node.frontmatter.slug = slug;
}
node.frontmatter.title = title;
node.frontmatter.date = new Date(
node.frontmatter.date.replace(/-/g, "/")
).toISOString();
createNodeField({ node, name: "slug", value: slug });
}
};