diff --git a/gatsby/create-pages.js b/gatsby/create-pages.js index 6d9025c..3cd2ae6 100644 --- a/gatsby/create-pages.js +++ b/gatsby/create-pages.js @@ -7,10 +7,16 @@ module.exports = async ({ graphql, actions, reporter }) => { query { allChaptersJson { edges { + previous { + id + } node { slug id } + next { + id + } } } } @@ -21,15 +27,22 @@ module.exports = async ({ graphql, actions, reporter }) => { } // Create a page for each chapter - const pages = result.data.allChaptersJson.edges; + const chapters = result.data.allChaptersJson.edges; - pages.forEach(({ node }) => { - createPage({ - path: `/${node.slug}/`, - component: path.resolve(`./src/layouts/ChapterLayout.js`), - context: { - id: node.id, - }, + if (chapters.length > 0) { + chapters.forEach(({ previous, node, next }) => { + const previousId = previous === null ? null : previous.id; + const nextId = next === null ? null : next.id; + + createPage({ + path: `/${node.slug}/`, + component: path.resolve(`./src/layouts/ChapterLayout.js`), + context: { + id: node.id, + previousId, + nextId, + }, + }); }); - }); + } }; diff --git a/src/components/PrevNextButtons.js b/src/components/PrevNextButtons.js new file mode 100644 index 0000000..aa6ccf0 --- /dev/null +++ b/src/components/PrevNextButtons.js @@ -0,0 +1,31 @@ +import React from 'react'; +import { Link } from 'gatsby'; + +const PrevNextButtons = ({ previous, next }) => { + return ( +
+
+ {previous && ( + +

Previous Chapter

+ + ← {previous.title} + + + )} +
+
+ {next && ( + +

Next Chapter

+ + {next.title} → + + + )} +
+
+ ); +}; + +export default PrevNextButtons; diff --git a/src/layouts/ChapterLayout.js b/src/layouts/ChapterLayout.js index 5f3a3fa..58b5768 100644 --- a/src/layouts/ChapterLayout.js +++ b/src/layouts/ChapterLayout.js @@ -8,6 +8,7 @@ import { parseContent } from '../utils/parseContent'; import Layout from '../components/Layout'; import ChapterNav from '../components/ChapterNav'; import TableOfContents from '../components/TableOfContents'; +import PrevNextButtons from '../components/PrevNextButtons'; import Image from '../components/Image'; import Example from '../components/Example'; @@ -25,7 +26,7 @@ const renderAst = (ast) => { }; export default function ChapterLayout({ data }) { - const { chaptersJson: chapter } = data; + const { chapter, previous, next } = data; const { ast, toc } = parseContent({ html: chapter.src.fields.html, @@ -42,6 +43,10 @@ export default function ChapterLayout({ data }) {
{renderAst(ast)} + +
+ +