convert website from hugo to gatsby (#1369)
This commit is contained in:
committed by
Shawn Erquhart
parent
d6c03707d8
commit
04c44518b2
91
website/gatsby-node.js
Normal file
91
website/gatsby-node.js
Normal file
@ -0,0 +1,91 @@
|
||||
const path = require('path');
|
||||
const { createFilePath } = require('gatsby-source-filesystem');
|
||||
|
||||
exports.createPages = async ({ graphql, boundActionCreators }) => {
|
||||
const { createPage } = boundActionCreators;
|
||||
|
||||
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) {
|
||||
console.error(allMarkdown.errors);
|
||||
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: {
|
||||
slug
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const pad = n => (n >= 10 ? n : `0${n}`);
|
||||
|
||||
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
|
||||
const { createNodeField } = boundActionCreators;
|
||||
|
||||
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',
|
||||
value: date.toJSON()
|
||||
});
|
||||
}
|
||||
|
||||
// used for doc posts
|
||||
createNodeField({
|
||||
node,
|
||||
name: 'slug',
|
||||
value: slug
|
||||
});
|
||||
|
||||
// used to create GitHub edit link
|
||||
createNodeField({
|
||||
node,
|
||||
name: 'path',
|
||||
value: relativePath
|
||||
});
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user