Skip to content

Commit d954d39

Browse files
committed
Programatically create chapter pages
- Add chapter content folder for json file and create seattle.json - Add chapter schema to CMS - Update gatsby-node.js to query allChapters and create a page for each one
1 parent da6acbb commit d954d39

File tree

4 files changed

+80
-15
lines changed

4 files changed

+80
-15
lines changed

content/chapters/seattle.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"city": "Seattle"
3+
}

gatsby-node.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
const path = require('path')
77
const { createFilePath } = require('gatsby-source-filesystem')
88

9+
// Add slug field based on filepath to content nodes
10+
// We use this slug to create the path for the automatically generated pages
11+
exports.onCreateNode = ({ node, actions, getNode }) => {
12+
const { createNodeField } = actions
13+
14+
if (
15+
node.internal.type === `MarkdownRemark` ||
16+
node.internal.type === `ChaptersJson`
17+
) {
18+
const value = createFilePath({ node, getNode })
19+
createNodeField({
20+
name: `slug`,
21+
node,
22+
value,
23+
})
24+
}
25+
}
26+
27+
// Create subpages from markdown and chapter pages from json
928
exports.createPages = ({ actions, graphql }) => {
1029
const { createPage } = actions
1130

@@ -24,41 +43,50 @@ exports.createPages = ({ actions, graphql }) => {
2443
}
2544
}
2645
}
46+
allChaptersJson {
47+
edges {
48+
node {
49+
id
50+
city
51+
fields {
52+
slug
53+
}
54+
}
55+
}
56+
}
2757
}
2858
`).then(result => {
2959
if (result.errors) {
3060
result.errors.forEach(e => console.error(e.toString()))
3161
return Promise.reject(result.errors)
3262
}
3363

34-
const pages = result.data.allMarkdownRemark.edges
64+
const subPages = result.data.allMarkdownRemark.edges
65+
const chapterPages = result.data.allChaptersJson.edges
3566

36-
pages.forEach(edge => {
67+
subPages.forEach(edge => {
3768
const id = edge.node.id
3869
createPage({
3970
path: edge.node.fields.slug,
4071
tags: edge.node.frontmatter.tags,
4172
component: path.resolve(
4273
`src/templates/${String(edge.node.frontmatter.templateKey)}.js`
4374
),
44-
// additional data can be passed via context
4575
context: {
4676
id,
4777
},
4878
})
4979
})
50-
})
51-
}
52-
53-
exports.onCreateNode = ({ node, actions, getNode }) => {
54-
const { createNodeField } = actions
5580

56-
if (node.internal.type === `MarkdownRemark`) {
57-
const value = createFilePath({ node, getNode })
58-
createNodeField({
59-
name: `slug`,
60-
node,
61-
value,
81+
chapterPages.forEach(edge => {
82+
const id = edge.node.id
83+
createPage({
84+
path: edge.node.fields.slug,
85+
component: path.resolve(`src/templates/chapter.js`),
86+
context: {
87+
id,
88+
},
89+
})
6290
})
63-
}
91+
})
6492
}

src/templates/chapter.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react'
2+
import { graphql } from 'gatsby'
3+
import Layout from '../layouts/default'
4+
5+
const ChapterTemplate = ({ data }) => {
6+
const { chaptersJson: chapter } = data
7+
return (
8+
<Layout>
9+
<section>
10+
<h1>{chapter.city}</h1>
11+
</section>
12+
</Layout>
13+
)
14+
}
15+
16+
export default ChapterTemplate
17+
18+
export const chapterQuery = graphql`
19+
query Chapter($id: String!) {
20+
chaptersJson(id: { eq: $id }) {
21+
city
22+
}
23+
}
24+
`

static/admin/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ publish_mode: editorial_workflow
1313
show_preview_links: false
1414

1515
collections:
16+
- name: 'chapters'
17+
label: 'Chapters'
18+
folder: 'content/chapters'
19+
delete: false
20+
fields:
21+
- label: 'City'
22+
name: 'city'
23+
widget: 'string'
24+
required: true
25+
1626
- name: 'pages'
1727
label: 'Pages'
1828
description: 'Edit the copy of standalone pages on our site.'

0 commit comments

Comments
 (0)