-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
87 lines (82 loc) · 2.28 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
const path = require("path")
const slugify = require("slugify")
exports.createPages = async ({ actions, graphql }) => {
const { createPage } = actions
// Create Notes and Note category pages
const NoteTemplate = path.resolve("src/templates/NoteTemplate.js")
const NoteCategoryTemplate = path.resolve(
"src/templates/NoteCategoryTemplate.js"
)
const { data: noteData } = await graphql(`
{
allFile(filter: { sourceInstanceName: { eq: "notes" } }) {
edges {
node {
name
relativeDirectory
absolutePath
}
}
}
}
`)
noteData.allFile.edges.forEach(({ node }) => {
const { absolutePath, name, relativeDirectory } = node
const isCategory = name === "README"
const title = isCategory ? "" : `/${slugify(name, { lower: true })}`
const category = slugify(relativeDirectory, { lower: true })
const notePath = `/notes/${category}${title}`
return createPage({
path: notePath,
component: isCategory ? NoteCategoryTemplate : NoteTemplate,
context: {
absolutePath,
relativeDirectory,
},
})
})
// Create Blog posts and Project pages
const BlogPostTemplate = path.resolve("src/templates/BlogPostTemplate.js")
const ProjectTemplate = path.resolve("src/templates/ProjectTemplate.js")
const { data: media } = await graphql(`
{
allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/src.pages.(blog|projects)/" } }
sort: { order: DESC, fields: [frontmatter___date] }
) {
edges {
node {
frontmatter {
type
slug
}
}
}
}
}
`)
media.allMarkdownRemark.edges.forEach(({ node }) => {
const { frontmatter: fm } = node
let template = null
let mediaPath = null
switch (fm.type) {
case "blog":
template = BlogPostTemplate
mediaPath = `/blog/${fm.slug}`
break
case "projects":
template = ProjectTemplate
mediaPath = `/projects/${fm.slug}`
break
default:
throw new Error(`Unknown media type: ${fm.type}`)
}
return createPage({
path: mediaPath,
component: template,
context: {
slug: fm.slug,
},
})
})
}