-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
110 lines (90 loc) · 2.71 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
const path = require('path')
const rawMarkdown2LogicalPost = (MDArray,option = {}) =>{
let blog_prefix = 'blog/';
return MDArray.map( md => {
let post = {};
post.id = md.id;
post.title = md.frontmatter.title;
post.categories = md.frontmatter.categories;
post.date = md.frontmatter.date;
post.slug = md.frontmatter.slug;
post.url = blog_prefix + md.frontmatter.slug;
post.description = md.excerpt;
post.heroimage = md.frontmatter.heroimage;
post.tags = md.frontmatter.tags;
return post;
}
)
}
const createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const tagindex = path.resolve('./src/templates/tag.js')
const categoryindex = path.resolve('./src/templates/category.js')
const result = await graphql(
`
{
allMarkdownRemark(sort: {frontmatter: {date: DESC}}) {
nodes {
id
frontmatter {
categories
date
slug
title
tags
}
}
}
}
`
)
if (result.errors) {
throw result.errors
}
const posts = rawMarkdown2LogicalPost(result.data.allMarkdownRemark.nodes)
//const posts = all;//.filter((post) => post.node.frontmatter.template === 'post')
const tagSet = new Set()
const categorySet = new Set()
// =====================================================================================
// Posts' tags and cates
// =====================================================================================
posts.forEach((post, i) => {
if (post.tags) {
post.tags.forEach((tag) => {
tagSet.add(tag)
})
}
if (post.categories) {
post.categories.forEach((category) => {
categorySet.add(category)
})
}
})
// =====================================================================================
// Tags
// =====================================================================================
const tagList = Array.from(tagSet)
tagList.forEach((tag) => {
createPage({
path: `/blog/tags/${tag}/`,
component: tagindex,
context: {
tag,
},
})
})
// =====================================================================================
// Categories
// =====================================================================================
const categoryList = Array.from(categorySet)
categoryList.forEach((category) => {
createPage({
path: `/blog/categories/${category}/`,
component: categoryindex,
context: {
category,
},
})
})
}
exports.createPages = createPages