-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
162 lines (138 loc) · 4.14 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const path = require('path')
const createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const blogPage = path.resolve('./src/templates/post.js')
const pagePage = path.resolve('./src/templates/page.js')
const tagPage = path.resolve('./src/templates/tag.js')
const result = await graphql(
`
{
allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }) {
edges {
node {
id
frontmatter {
title
tags
template
}
fields {
slug
}
}
}
}
}
`
)
if (result.errors) {
throw result.errors
}
const all = result.data.allMarkdownRemark.edges
const posts = all.filter((post) => post.node.frontmatter.template === 'post')
const pages = all.filter((post) => post.node.frontmatter.template === 'page')
const presentations = all.filter((post) => post.node.frontmatter.template === 'presentations')
const tagSet = new Set()
// =====================================================================================
// Posts
// =====================================================================================
posts.forEach((post, i) => {
const previous = i === posts.length - 1 ? null : posts[i + 1].node
const next = i === 0 ? null : posts[i - 1].node
if (post.node.frontmatter.tags) {
post.node.frontmatter.tags.forEach((tag) => {
tagSet.add(tag)
})
}
createPage({
path: post.node.fields.slug,
component: blogPage,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
})
// =====================================================================================
// Presentations
// =====================================================================================
presentations.forEach((post, i) => {
const previous = i === presentations.length - 1 ? null : presentations[i + 1].node
const next = i === 0 ? null : presentations[i - 1].node
if (post.node.frontmatter.tags) {
post.node.frontmatter.tags.forEach((tag) => {
tagSet.add(tag)
})
}
createPage({
path: post.node.fields.slug,
component: blogPage,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
})
// =====================================================================================
// Pages
// =====================================================================================
pages.forEach((page) => {
createPage({
path: page.node.fields.slug,
component: pagePage,
context: {
slug: page.node.fields.slug,
},
})
})
// =====================================================================================
// Tags
// =====================================================================================
const tagList = Array.from(tagSet)
tagList.forEach((tag) => {
createPage({
path: `/tags/${slugify(tag)}/`,
component: tagPage,
context: {
tag,
},
})
})
}
const createNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
// =====================================================================================
// Slugs
// =====================================================================================
let slug
if (node.internal.type === 'MarkdownRemark') {
const fileNode = getNode(node.parent)
const parsedFilePath = path.parse(fileNode.relativePath)
if (Object.prototype.hasOwnProperty.call(node.frontmatter, 'slug')) {
slug = `/${node.frontmatter.slug}/`
} else {
slug = `/${parsedFilePath.dir}/`
}
createNodeField({
name: 'slug',
node,
value: slug,
})
}
}
exports.createPages = createPages
exports.onCreateNode = createNode
// Helpers
function slugify(str) {
return (
str &&
str
.match(
/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g
)
.map((x) => x.toLowerCase())
.join('-')
)
}