website/gatsby-node.js

58 lines
1.3 KiB
JavaScript

const path = require("path");
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const noteTemplate = path.resolve(`src/templates/note.tsx`);
const result = await graphql(`
{
allMdx(
limit: 10
filter: { fileAbsolutePath: { glob: "**/src/notes/*" } }
) {
edges {
node {
frontmatter {
path
}
id
}
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const posts = result.data.allMdx.edges;
const postsPerPage = 2;
const numPages = Math.ceil(posts.length / postsPerPage);
const rootPath = "/notes";
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: rootPath + (i === 0 ? '' : `/${i + 1}`),
component: path.resolve("./src/templates/notes.tsx"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
rootPath,
},
})
});
posts.forEach(({ node }) => {
createPage({
path: `${rootPath}/${node.frontmatter.path}`,
component: noteTemplate,
context: {
id: node.id,
},
});
});
}