2020-11-27 08:08:32 -08:00
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
exports.createPages = async ({ actions, graphql, reporter }) => {
|
|
|
|
const { createPage } = actions;
|
|
|
|
|
|
|
|
const noteTemplate = path.resolve(`src/templates/note.tsx`);
|
2020-11-27 11:07:44 -08:00
|
|
|
const result = await graphql(`
|
|
|
|
{
|
|
|
|
allMdx(
|
|
|
|
limit: 10
|
|
|
|
filter: { fileAbsolutePath: { glob: "**/src/notes/*" } }
|
|
|
|
) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
frontmatter {
|
|
|
|
path
|
2020-11-27 12:25:22 -08:00
|
|
|
hidden
|
2020-11-27 11:07:44 -08:00
|
|
|
}
|
|
|
|
id
|
2020-11-27 08:08:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 11:07:44 -08:00
|
|
|
`);
|
2020-11-27 08:08:32 -08:00
|
|
|
|
|
|
|
if (result.errors) {
|
|
|
|
reporter.panicOnBuild(`Error while running GraphQL query.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-27 12:25:22 -08:00
|
|
|
const posts = result.data.allMdx.edges.filter(({ node }) => !node.frontmatter.hidden);
|
2020-11-27 11:19:33 -08:00
|
|
|
const postsPerPage = 10;
|
2020-11-27 11:07:44 -08:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2020-11-27 12:25:22 -08:00
|
|
|
// Must decouple to ensure hidden pages are still rendered, just excluded from
|
|
|
|
// notes mapping
|
|
|
|
result.data.allMdx.edges.forEach(({ node }) => {
|
2020-11-27 08:08:32 -08:00
|
|
|
createPage({
|
2020-11-27 11:07:44 -08:00
|
|
|
path: `${rootPath}/${node.frontmatter.path}`,
|
2020-11-27 08:08:32 -08:00
|
|
|
component: noteTemplate,
|
|
|
|
context: {
|
|
|
|
id: node.id,
|
2020-11-27 11:07:44 -08:00
|
|
|
},
|
2020-11-27 08:08:32 -08:00
|
|
|
});
|
|
|
|
});
|
2020-11-27 11:07:44 -08:00
|
|
|
}
|
2020-11-27 12:25:22 -08:00
|
|
|
exports.createSchemaCustomization = ({ actions: { createTypes } }) => {
|
|
|
|
createTypes(`
|
|
|
|
type Mdx implements Node {
|
|
|
|
frontmatter: MdxFrontmatter
|
|
|
|
}
|
|
|
|
|
|
|
|
type MdxFrontmatter {
|
|
|
|
hidden: Boolean
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
};
|