This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
192 lines (176 loc) · 4.44 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const path = require('path')
const parseFilepath = require(`parse-filepath`)
const _ = require('lodash')
const trimStart = require('lodash/trimStart')
const createPaginatedPages = require('gatsby-paginate')
exports.onCreateWebpackConfig = ({
stage,
rules,
loaders,
plugins,
actions,
}) => {
actions.setWebpackConfig({
module: {
rules: [],
},
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
},
})
}
exports.onCreateNode = ({ node, actions, getNode, reporter }) => {
const { createNodeField } = actions
const basePath = '/articles/'
if (
node.internal.type === `MarkdownRemark` &&
getNode(node.parent).internal.type === 'File'
) {
const fileNode = getNode(node.parent)
const parsedFilePath = parseFilepath(fileNode.relativePath)
let slug;
if (fileNode.sourceInstanceName === 'docs') {
if (parsedFilePath.name !== 'index') {
slug = `${basePath}${parsedFilePath.name}`
} else {
slug = `${basePath}${trimStart(parsedFilePath.dir, 'markdown/')}`
}
if(slug) {
createNodeField({ node, name: `slug`, value: slug })
}
}
}
}
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
const TAGS_ARCHIVE_TEMPLATE = path.resolve(
`src/templates/TagsArchiveTemplate/index.js`
)
const ARTICLE_OVERVIEW_TEMPLATE = path.resolve(
`src/templates/ArticleOverviewTemplate/index.js`
)
const ARTICLES_LIST_TEMPLATE = path.resolve(
`src/templates/ArticlesListTemplate/index.js`
)
const AUTHORS_LIST_TEMPLATE = path.resolve(
`src/templates/ContributorsTemplatePage/index.js`
)
return graphql(`
{
posts: allMarkdownRemark(sort: { fields: [frontmatter___latestUpdateDate] }) {
edges {
node {
id
fields {
slug
}
excerpt(pruneLength: 200)
frontmatter {
date
latestUpdateDate
title
tags
author {
id
firstName
lastName
avatar {
childImageSharp {
fixed(quality: 100, width: 55, height: 55) {
tracedSVG
width
height
src
srcSet
}
}
}
}
}
}
}
}
tags: allTagsYaml {
edges {
node {
id
description
web
}
}
}
authors: allAuthorYaml {
edges {
node {
id
firstName
lastName
bioFull
avatar {
childImageSharp {
fixed(quality: 100, width: 55, height: 55) {
tracedSVG
width
height
src
srcSet
}
}
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}
const listArticles = result.data.posts.edges.reverse()
const listTags = result.data.tags.edges
const listAuthors = result.data.authors.edges
createPaginatedPages({
edges: listArticles,
createPage: createPage,
pageTemplate: ARTICLES_LIST_TEMPLATE,
pageLength: 9,
pathPrefix: '/articles/',
buildPath: (index, pathPrefix) =>
index > 1 ? `${pathPrefix}${index}` : `${pathPrefix}`,
context: {
tags: listTags,
},
})
// Make pages detail article
listArticles.forEach(({ node }) => {
let slug = _.get(node, 'fields.slug')
createPage({
path: slug,
component: ARTICLE_OVERVIEW_TEMPLATE,
context: {
slug,
},
})
})
// Make tag pages
listTags.forEach(tag => {
createPage({
path: `/archive-tags/${_.kebabCase(tag.node.id)}/`,
component: TAGS_ARCHIVE_TEMPLATE,
context: {
tag: tag.node.id,
tagContend: tag.node,
},
})
})
// Make authors pages
listAuthors.forEach(author => {
createPage({
path: `/contributors/${_.kebabCase(author.node.id)}/`,
component: AUTHORS_LIST_TEMPLATE,
context: {
slug: author.node.id
},
})
})
})
}