-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgatsby-node.js
96 lines (89 loc) · 2.11 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
/* eslint-env node */
const DEFAULT_LANG_KEY = 'en'
function getLangKey(node) {
const matches = node.fileAbsolutePath.match(/\.([a-z]{2})\.md/)
if (!matches) return DEFAULT_LANG_KEY
return matches[1]
}
function getPath(node) {
if (node.fields.langKey === DEFAULT_LANG_KEY)
return `/blog/${node.frontmatter.slug}`
return `/${node.fields.langKey}/blog/${node.frontmatter.slug}`
}
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'Mdx') {
const langKey = getLangKey(node)
createNodeField({
node,
name: `langKey`,
value: langKey,
})
createNodeField({
node,
name: `slug`,
value: node.frontmatter.slug,
})
createNodeField({
node,
name: `link`,
value: getPath({ ...node, fields: { langKey } }),
})
createNodeField({
node,
name: `editLink`,
value: `https://github.com/gregberge/gregberge.com/edit/master${node.fileAbsolutePath.replace(
__dirname,
'',
)}`,
})
}
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(
`
{
allMdx {
edges {
node {
id
fileAbsolutePath
fields {
langKey
}
frontmatter {
slug
}
}
}
}
}
`,
)
if (result.errors) {
console.log(result.errors) // eslint-disable-line no-console
throw new Error('Error during page creation')
}
// Create blog posts pages.
result.data.allMdx.edges.forEach(({ node }) => {
createPage({
path: getPath(node),
component: require.resolve('./src/templates/post.js'),
context: {
id: node.id,
slug: node.frontmatter.slug,
langKey: node.fields.langKey,
},
})
})
}
// exports.sourceNodes = ({ actions }) => {
// const { createTypes } = actions
// const typeDefs = `
// type MdxFrontmatter {
// banner: File
// }
// `
// createTypes(typeDefs)
// }