website/gatsby-node.js

75 lines
1.7 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
hidden
}
id
}
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const posts = result.data.allMdx.edges.filter(
({ node }) => !node.frontmatter.hidden
);
const postsPerPage = 10;
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,
},
});
});
// Must decouple to ensure hidden pages are still rendered, just excluded from
// notes mapping
result.data.allMdx.edges.forEach(({ node }) => {
createPage({
path: `${rootPath}/${node.frontmatter.path}`,
component: noteTemplate,
context: {
id: node.id,
},
});
});
};
exports.createSchemaCustomization = ({ actions: { createTypes } }) => {
createTypes(`
type Mdx implements Node {
frontmatter: MdxFrontmatter
}
type MdxFrontmatter {
hidden: Boolean
}
`);
};