2018-07-25 07:47:26 -04:00
|
|
|
const path = require('path');
|
|
|
|
const { createFilePath } = require('gatsby-source-filesystem');
|
|
|
|
|
2020-01-23 21:55:18 -05:00
|
|
|
exports.createPages = async ({ graphql, actions }) => {
|
|
|
|
const { createPage } = actions;
|
2018-07-25 07:47:26 -04:00
|
|
|
|
|
|
|
const docPage = path.resolve('./src/templates/doc-page.js');
|
|
|
|
const blogPost = path.resolve('./src/templates/blog-post.js');
|
|
|
|
|
|
|
|
// get all markdown with a frontmatter path field and title
|
|
|
|
const allMarkdown = await graphql(`
|
|
|
|
{
|
|
|
|
allMarkdownRemark(filter: { frontmatter: { title: { ne: null } } }) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
fields {
|
|
|
|
slug
|
|
|
|
}
|
|
|
|
frontmatter {
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
if (allMarkdown.errors) {
|
2018-11-02 09:52:01 -06:00
|
|
|
console.error(allMarkdown.errors); // eslint-disable-line no-console
|
2018-07-25 07:47:26 -04:00
|
|
|
throw Error(allMarkdown.errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
allMarkdown.data.allMarkdownRemark.edges.forEach(({ node }) => {
|
|
|
|
const { slug } = node.fields;
|
|
|
|
|
|
|
|
let template = docPage;
|
|
|
|
|
|
|
|
if (slug.includes('blog/')) {
|
|
|
|
template = blogPost;
|
|
|
|
}
|
|
|
|
|
|
|
|
createPage({
|
|
|
|
path: slug,
|
|
|
|
component: template,
|
|
|
|
context: {
|
2018-08-07 14:46:54 -06:00
|
|
|
slug,
|
|
|
|
},
|
2018-07-25 07:47:26 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function pad(n) {
|
|
|
|
return n >= 10 ? n : `0${n}`;
|
|
|
|
}
|
2018-07-25 07:47:26 -04:00
|
|
|
|
2020-01-23 21:55:18 -05:00
|
|
|
exports.onCreateNode = ({ node, actions, getNode }) => {
|
|
|
|
const { createNodeField } = actions;
|
2018-07-25 07:47:26 -04:00
|
|
|
|
|
|
|
if (node.internal.type === 'MarkdownRemark') {
|
|
|
|
const value = createFilePath({ node, getNode });
|
|
|
|
const { relativePath } = getNode(node.parent);
|
|
|
|
|
|
|
|
let slug = value;
|
|
|
|
|
|
|
|
if (relativePath.includes('blog/')) {
|
|
|
|
const date = new Date(node.frontmatter.date);
|
|
|
|
const year = date.getFullYear();
|
|
|
|
const month = date.getMonth() + 1;
|
|
|
|
const filename = path.basename(relativePath, '.md');
|
|
|
|
slug = `/blog/${year}/${pad(month)}/${filename}`;
|
|
|
|
|
|
|
|
createNodeField({
|
|
|
|
node,
|
|
|
|
name: 'date',
|
2018-08-07 14:46:54 -06:00
|
|
|
value: date.toJSON(),
|
2018-07-25 07:47:26 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// used for doc posts
|
|
|
|
createNodeField({
|
|
|
|
node,
|
|
|
|
name: 'slug',
|
2018-08-07 14:46:54 -06:00
|
|
|
value: slug,
|
2018-07-25 07:47:26 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// used to create GitHub edit link
|
|
|
|
createNodeField({
|
|
|
|
node,
|
|
|
|
name: 'path',
|
2018-08-07 14:46:54 -06:00
|
|
|
value: relativePath,
|
2018-07-25 07:47:26 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2020-05-04 14:47:55 +03:00
|
|
|
|
|
|
|
exports.onCreateWebpackConfig = ({ actions }) => {
|
|
|
|
actions.setWebpackConfig({
|
|
|
|
resolve: {
|
|
|
|
extensions: ['.ts', '.tsx', '.js', '.json'],
|
|
|
|
alias: {
|
|
|
|
moment$: 'moment/moment.js',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|