This repository was archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
84 lines (79 loc) · 2.16 KB
/
gatsby-node.js
File metadata and controls
84 lines (79 loc) · 2.16 KB
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
const path = require('path')
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const marbleResult = await graphql(`
{
allMarbleItem {
nodes {
id
slug
sourceType
sourceSystem
}
}
}
`)
const marbleItems = marbleResult.data && marbleResult.data.allMarbleItem ? marbleResult.data.allMarbleItem.nodes : []
marbleItems.forEach(node => {
if (node.id) {
createPage({
path: node.slug,
component: require.resolve('./src/templates/marble-item.js'),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.slug,
id: node.id,
iiifUri: node.iiifUri,
},
})
}
})
// Get all markdown blog posts sorted by date
const result = await graphql(
`
{
allMarkdownRemark(sort: {frontmatter: {date: ASC}}, limit: 1000) {
nodes {
id
frontmatter {
template
slug
menu
}
}
}
}
`,
)
if (result.errors) {
reporter.panicOnBuild(
'There was an error loading your blog posts',
result.errors,
)
return
}
const pages = result.data.allMarkdownRemark.nodes
// Create pages
// But only if there's at least one markdown file found at "content/blog" (defined in gatsby-config.js)
// `context` is available in the template as a prop and as a variable in GraphQL
if (pages.length > 0) {
pages.forEach((page, index) => {
const previousPostId = index === 0 ? null : pages[index - 1].id
const nextPostId = index === pages.length - 1 ? null : pages[index + 1].id
// Define a template for blog post
const pageTemplate = path.resolve(`./src/templates/${page.frontmatter.template}`)
createPage({
path: page.frontmatter.slug,
component: pageTemplate,
context: {
id: page.id,
slug: page.frontmatter.slug,
menu: page.frontmatter.menu,
previousPostId,
nextPostId,
},
})
})
}
}