-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
66 lines (57 loc) · 1.68 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
const { documentToPlainTextString } = require("@contentful/rich-text-plain-text-renderer")
const words = require('lodash/words');
exports.createPages = async ({ graphql, actions }) => {
const blogTemplate = require.resolve("./src/blogTemplate.js");
const tagsTemplate = require.resolve("./src/tagsTemplate.js");
const { createPage } = actions;
const blogData = await graphql(`
{
allContentfulBlogPost {
nodes {
slug
id
}
}
}`)
const allTags = await graphql(`
{
allContentfulTags {
nodes {
tags
}
}
}`)
if (blogData.error) throw blogData.error;
const blogs = blogData.data.allContentfulBlogPost.nodes;
blogs.forEach((blog) => {
createPage({
path: `/${blog.slug}`,
component: blogTemplate,
context: { id: blog.id }
})
})
if (allTags.error) throw allTags.error;
const tags = allTags.data.allContentfulTags.nodes;
console.log(tags);
tags.forEach((item) => {
createPage({
path: `/tags/${item.tags.substring(1)}`,
component: tagsTemplate,
context: { tag: item.tags }
})
})
}
exports.onCreateNode = async ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type !== "ContentfulBlogPost") {
return ;
}
// Match the "bodyContent" below to match the name of your rich text field
const data = JSON.parse(node.blogContent.raw);
const allText = documentToPlainTextString(data);
const countPerMinute = 200;
const totalWords = words(allText).length;
var time = Math.round(totalWords / countPerMinute);
if (time === 0) time=1;
createNodeField({ node, name: 'timeToRead', value: time })
}