-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
160 lines (148 loc) · 4.09 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
const path = require('path')
const { defaultLanguage } = require('./src/common/Languages')
const ContentCategory = {
page: 'page',
post: 'post',
}
const slugify = (str) => {
return (
str &&
str
.replace(/[ ]*-[ ]*/g, ' ') // replace '-', starts and ends with 0 - n blank
.split(' ')
.map((x) => x.toLowerCase())
.join('-')
)
}
const getMatchStr = (strContent, regExpression) => {
let matchStr
if (strContent) {
let match
while ((match = regExpression.exec(strContent)) !== null) {
matchStr = match[0]
}
}
return matchStr || ''
}
const getMDFileCategory = (path) => {
// starts with 'content/', ends with '/', but the match doesn't contain 'content/' and '/'
// design: this is because the names of the first-level directories under content represent their respective categories
const regex = /(?<=content\/)(?!content\/).*?(?=\/)/g
return getMatchStr(path, regex)
}
const getMDFileSlug = (category, path) => {
let slug
if (path) {
switch (category) {
case ContentCategory.page: {
// starts with 'page, ends with 'index.', but the match doesn't contain 'page' and 'index.'
// design: there is no directory under the page directory, the `/${fileName}` is the slug of the page files
const regex = /(?<=page)(?!page).*?(?=\/index.)/g
slug = getMatchStr(path, regex)
break
}
case ContentCategory.post: {
// starts with '/post', ends with 'index.', but the match doesn't contain 'index.'
// design: a leaf directory's name is the post name, there are multiple index.{lang}.md files under the directory
const regex = /\/post.*?(?=\/index.)/g
slug = getMatchStr(path, regex)
break
}
default: {
slug = ''
break
}
}
}
return slugify(slug || '')
}
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'MarkdownRemark') {
const category = getMDFileCategory(node.fileAbsolutePath)
const slug = getMDFileSlug(category, node.fileAbsolutePath)
const name = path.basename(node.fileAbsolutePath, '.md')
const isDefault = name === `index.${defaultLanguage}`
const lang = isDefault ? defaultLanguage : name.split('.')[1]
createNodeField({
node,
name: 'category',
value: category,
})
createNodeField({
node,
name: 'slug',
value: slug,
})
createNodeField({
node,
name: 'isDefault',
value: isDefault,
})
createNodeField({
node,
name: 'locale',
value: lang,
})
if (category === ContentCategory.post) {
const previousSlug = node.frontmatter.previous
? slugify(`/${category}/${node.frontmatter.previous}`)
: ''
const nextSlug = node.frontmatter.next
? slugify(`/${category}/${node.frontmatter.next}`)
: ''
createNodeField({
node,
name: 'previousSlug',
value: previousSlug,
})
createNodeField({
node,
name: 'nextSlug',
value: nextSlug,
})
}
}
}
exports.createPages = async (props) => {
const { graphql, actions, reporter } = props
const { createPage } = actions
const pageTemplate = require.resolve('./src/templates/page.js')
const blogTemplate = require.resolve('./src/templates/post.js')
const result = await graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
category
slug
isDefault
locale
}
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild('Error loading MD result', result.errors)
}
// create pages
const all = result.data.allMarkdownRemark.edges
all.forEach((page) => {
const { category, slug, isDefault } = page.node.fields
const pageData = {
path: `${slug}`,
component:
category === ContentCategory.page ? pageTemplate : blogTemplate,
context: {
category,
slug,
},
}
if (isDefault) {
createPage(pageData)
}
})
}