gatsby-starter-deck/gatsby-node.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-02-10 15:56:56 +03:00
const path = require('path');
2017-11-24 02:22:47 +03:00
// Implement the Gatsby API “onCreatePage”. This is
// called after every page is created.
exports.onCreatePage = ({ page, boundActionCreators }) => {
const { createPage, deletePage } = boundActionCreators;
return new Promise((resolve, reject) => {
// Remove trailing slash
const newPage = Object.assign({}, page, {
2018-02-10 15:56:56 +03:00
path: page.path === `/` ? page.path : page.path.replace(/\/$/, ``)
2017-11-24 02:22:47 +03:00
});
if (newPage.path !== page.path) {
// Remove the old page
deletePage(page);
// Add the new page
createPage(newPage);
}
resolve();
});
};
2018-02-10 15:56:56 +03:00
// Create slides from Markdown.
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage } = boundActionCreators;
const blogPostTemplate = path.resolve(`src/templates/slide.js`);
return graphql(`
{
allMarkdownRemark {
edges {
node {
html
fileAbsolutePath
id
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors);
}
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const absolutePath = node.fileAbsolutePath;
const fileName = path.basename(absolutePath, path.extname(absolutePath));
createPage({
path: fileName,
component: blogPostTemplate,
context: { absolutePath }
});
});
});
};