-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
103 lines (94 loc) · 3.64 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
// This config file hooks into Gatsby's build pipeline
// for friendly urls we need the createFilePath component
// note we use the CommonJS import syntax here because node
// doesn't play nicely with ES6 imports
const { createFilePath } = require('gatsby-source-filesystem')
// for resolving paths to our template components
const path = require('path')
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type BlogPost implements Node {
title: String,
subtitle: String!,
date: Date,
period: String!,
languages: String!,
tools: String!,
demo: String!,
class: String,
static: Boolean,
link: String
}
`
createTypes(typeDefs)
}
// onCreateNode is called once per node, in Gatsby a node is a data source
// e.g. File, MarkdownRemark, SitePage etc.
exports.onCreateNode = ({node, getNode, actions}) => {
// Create friendly URLs for our markdown blog posts
if(node.internal.type === 'Mdx') {
// console.log(`*** I am processing a node with type: ${node.internal.type}`)
// to add the friendly url to the markdown node we use
// createNodeField, which we extract from the actions
// array of functions via onCreateNode
// Note this syntax is the same as:
// const createNodeField = actions.createNodeField
const { createNodeField } = actions
// Distinguish between blog posts, which have a slug, and
// static pages which have a predefined link set in the frontmatter
// NOTE! gatsby-develop doesn't serve static html pages so static links
// won't work in develop, but do a gatsby build/serve to see how it
// looks in production and they will work
//
// basePath is the folder where our markdown files live
const slug = node.frontmatter.static ? node.frontmatter.link : createFilePath({node, getNode, basePath: 'markdown'})
// const slug = createFilePath({node, getNode, basePath: 'markdown'})
// Assign the field, now we have a 'slug' prop available in our markdown
// nodes and can retrieve this using GraphQL queries, all node fields are
// inside a 'fields' object in GraphQL
createNodeField({
node,
name: 'slug',
value: slug
})
}
}
// Create our post pages programmatically
exports.createPages = ({graphql, actions}) => {
const { createPage } = actions
// createPages requires a promise
return new Promise(resolve => {
graphql(`
{
allMdx {
edges {
node {
frontmatter {
static
}
fields {
slug
}
}
}
}
}
`).then(result => {
result.data.allMdx.edges.forEach(({node}) => {
// createPage from each node if not a "static" page
// i.e. one with a link set in the frontmatter
if(!node.frontmatter.static) {
createPage({
path: node.fields.slug,
component: path.resolve('./src/templates/post.js'),
context: {
slug: node.fields.slug
},
})
}
})
resolve()
})
})
}